Compiled and interpreted languages

The compilation process for compiled languages:

  • For the code lexical analysis, syntax analysis, generate AST tree, and then code optimization, regeneration into the processor can process machine code. If the compilation succeeds, a binary file is generated, otherwise an error is reported.

The interpretive process of interpretive language:

  • Conducts lexical analysis and grammar analysis on code, generates AST tree, generates bytecode according to AST tree, and executes program according to bytecode.

Second, the AST tree

Var myName = "geektime" function foo(){return 23; } myName = "geektime" foo()Copy the code

The AST tree generated for the above code is as follows:

The principle of Babel is to convert ES6 code into ES6 AST tree, and then generate ES5 AST tree according to ES6 AST tree, and generate ES5 JS code according to ES5 AST tree.

There are two steps to converting an AST tree:

  • Lexical analysis: Convert code into tokens.

  • Parsing: Generates an AST tree based on the generated tokens.

Three, execute,

After the bytecode is generated, the interpreter executes the bytecode one bytecode at a time, and if a piece of code is executed multiple times (hot code), the compiler converts that code into efficient machine code and stores it in memory for later use.

What the interpreter does:

  • Convert the AST tree to bytecode.

  • Execute bytecode.

Bytecode and machine code

Bytecode takes up less memory than machine code. So why does hot code go to machine code, because machine code is more efficient.