How saulvaldelvira/compiler works
Main Components
• Compiler Driver (bin/compiler): This is the main executable that initializes the compilation process.
• Lexical Analysis: Represented by lib/lexer – handles tokenizing the source input.
• Parsing: Represented by lib/parser – converts the stream of tokens into a concrete AST (abstract syntax tree).
• AST Representation: Found in lib/ast – defines the syntax structures with declarations, expressions, statements, types, etc.
• AST Lowering: In lib/ast_lowering – performs transformations or lowering from the high-level AST into a simpler or more canonical form.
• AST Validation: In lib/ast_validate – ensures the correctness of the AST.
• Semantic Analysis: In lib/semantic – applies semantic rules, type checking, and other analyses on the AST.
• Intermediate Representations:
• High-Level IR (HIR): Implemented in lib/hir and complemented by lib/hir_passes and lib/hir_typecheck.
• (There might also be interactions with lib/session and lib/span for session management and source code spans.)
• Code Generation: In lib/mapl_codegen – translates the IR or lowered structures into target code or MIR.
• Supporting Libraries:
• lib/arena for memory allocation patterns (likely used by various phases).
• lib/error_manager to centralize error reporting.
• lib/compiler_driver, which might wrap and coordinate the entire compilation process.
Relationships and Data Flow
• The Compiler Driver receives the source file and initiates the process.
• The Lexical Analyzer (lexer) processes the raw source into a stream of tokens.
• The Parser uses the token stream to build the AST.
• AST Lowering and AST Validation work on the AST to prepare and check it before semantic analysis.
• Semantic Analysis then validates type rules and semantics, possibly producing errors that are coordinated by the Error Manager.
• The semantic phase passes results to the HIR phase, where the qualified representations are built (in lib/hir), then processed further by HIR passes and type checking.
• Finally, the Code Generator (mapl_codegen) translates the IR into code, likely using helper modules for addressing, metadata, and evaluation (indicated by the structure inside the code generation library).
Architectural Patterns and Design Principles
• Modularity: Each phase of the compiler is separated as a different crate, which clearly models the pipeline.
• Layered Architecture: Data flows from the lexer to the parser, then to the AST, followed by semantic analysis, IR/HIR, and finally code generation.
• Error Handling and Cross-Cutting Concerns: Components such as the Error Manager, Session, and Span libraries add cross-phase support.
• The use of a driver library shows that the orchestration is separated from the concerns of each compilation phase.