Compler Thory in JavaScript

5448 ワード

Source:https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20&%20closures/ch1.md
Common Comppliation Step.s
  • Tokenizing/Lexing: breaking up a string of characters into meaningful(to the langage)chunks、caled tokens.For instance、consider the program:  var a = 2;.This program would likely be brooken up into the follwing tokens:  var、  a、  =、  2、and  ;.Whitoespace may or may not be persisted a s a token、depending on whether it's meaningful or not.Note: The difference between tokenizing and lexing is subtle and academic、but it centeron whether or not thers tokens are e e dentified in a stateless or stateful way.Put simply,if the tokenizer were to invoke stateful parsing rules to figur out whether  a Shuld be consided a distinct token or just part of another token、 that would be lexing.
  • Parsing: Tang a stream(array)of tokens and turning it into a tree of nested elemens,which collective represent the grammatication of the program.This tree is caled an「AST」(Abstractact) Syntax Tree. The tree for  var a = 2; might start with a top-level node caled  VariableDeclaration、with a child node caled  Identifier (whose value is  a)、and another child caled  AssignmentExpression which itself has a child caled  NumericLiteral (whose value is  2).Code-Generation: the process of taring an AST and turning it into executable code.This part varies greatly depending on the langage,the plotform it's targing,etc.Coplier Model in JS
  • Engine:reponsible for start-to-finish complation and execution of our JavaScript program.Compler:one of Engine's friends;handles all the dirty work of parsing and code-generation.
  • Scrope:another friend of Engine;collecs and mantains a look-up list of all the declead identifers、and enforces a strit set of rules these are accessible to currently executing code.LHS&RHS
  • An RHS look-up is indistinguishable,for our purposes,from simply a look-up of the value of some variable.(get the value of)
  • An LHS look-up is trying to find the variable container itself,so that it can assign.
  • In this way,RHS doesn't really mean"right-hand side of an assigment"per se,it just,more accurately,means"not left-hand side"
  • function foo(a) {
    
        console.log( a ); // RHS of console Object, LHS of parameter in log
    
    }
    
    foo( 2 ); // RHS of foo
    
    // inplied a = 2 // LHS of a
    function foo(a) {
    
        var b = a; // LHS of b, RHS of a
    
        return a + b; // RHS of a, RHS of b
    
    }
    
    var c = foo( 2 ); // LHS of c, RHS of foo
    
    // implied a = 2 // LHS of a
    Nested Scope
    function foo(a) {
    
        console.log( a + b );
    
    }
    
    var b = 2;
    
    foo( 2 ); // 4
    The RHS reference for  b cannot be reolved inside the function  foo、but it can be resoved in the Scrope surrounding it(in this case,the global)
    エロエロ
    function foo(a) {
    
        console.log( a + b ); // RHS can not find b in the scopes, results in a ReferenceError being thrown by the Engine
    
        b = a;
    
    }
    
    foo( 2 );
    If the Egine is performing an LHS look-up、and it arrives at the top flooor(global) Scrope)without finding it,if the program is not running in“Strict Mode”、then the global Scrope will create a new variable of that name in the global scope,and hand it back to Engine.
    Strict mode disallows the atomatic/implicit global variable creation.In that case,there would beのglobal Scope'd variable to hand back from an LHS look-up.
    If a variable is found for an RHS look-up、but you try to something with its value that is impossible、such as trying to execute-as-function a non-function value、or reference a property on a  null or  undefined value、then Egine throws a different kind of error、caled a  TypeError.