erg/doc/EN/compiler/overview.md
2022-08-23 08:24:15 +09:00

1.4 KiB

Overview of `erg

This section introduces the function of each layer and especially important functions and methods.

1. Lexical analysis

  • Lexer performs lexical analysis. Lexer::next (Lexer is implemented as an iterator) handles the main logic of lexical analysis. Token is output as a result of parsing.

2. Parsing

  • Parser performs the parsing. Especially important is Parser::parse_expr. The result of parsing is AST, a collection of ast::Expr.

3. Desugaring

  • Desugarer performs desugaring. An AST is output.

4. Type checking/type inference

  • ASTLowerer performs typing. Type checking is mainly done by Context. Of particular importance are Context::supertype_of (to determine subtype relationships), Context::unify/sub_unify (to unify/semi-unify type variables) and Context::init_builtin_* (to define built-in API). The HIR is output as a result of the analysis.

5. Side Effect Check

  • SideEffectChecker does this.

6. Ownership check

  • Performed by OwnershipChecker.

7. Bytecode generation

  • CodeGenerator converts HIR to CodeObj. The CodeObj holds the bytecode and execution settings. Especially important is CodeGenerator::compile_expr.

  • All the above processes are put together by Compiler as a facade.
  • Execution of the generated bytecode is of course done by Python, which is called by DummyVM.