How bbayazit16/cart works
Overall Components
• Compiler (located in the "cartc" directory): Responsible for the entire compilation pipeline from reading source files to generating LLVM IR (using Inkwell) and linking the output with the system’s C compiler.
• Language Server (in "cart-lsp"): Provides tooling support such as code-completion, error linting, etc., that integrate with editors.
• Standard Library (in "cart_std"): Implements core functionalities for the Cart language, which the compiled programs use.
• Grammar Definitions and Installation Scripts: The "grammar" directory hints at the formal language definitions, and the "install.sh" script sets up the environment.
Detailed Compiler Architecture
• Lexical Analysis: In "cartc/src/lexer", files like alpha.rs, common.rs, etc. indicate that lexical analysis is performed early in the pipeline.
• Parsing & AST Generation: The "cartc/src/parser" directory contains modules for declarations, expressions, statements, and patterns. Additionally, the "ast" directory suggests a transformation phase which might include lowering from raw AST into a more structured form.
• Semantic Analysis / HIR: In "cartc/src/hir" you see components like type checking (type_check.rs) and error analysis (errors.rs), indicating a subsequent semantic phase where the AST is transformed into a higher-level representation (HIR).
• Code Generation: Under "cartc/src/codegen", you see files responsible for generating LLVM IR components, such as declarations, expressions, and symbol table management. This module interfaces with the LLVM IR generation library (Inkwell).
• Context and Error Reporting: The "context" and "errors" modules provide supporting functionality for tracking file positions and making the error messages user-friendly.
• CLI/Driver: The "cartc/src/cli" and "cartc/src/main.rs" files indicate the command-line interface which drives the overall compilation process.
Key Interactions and Data Flow
• Source Code → Lexical Analysis (Tokenizer): The CLI accepts a Cart source file, which is then tokenized.
• Tokens → Parser: The parser module processes tokens into an Abstract Syntax Tree (AST).
• AST → Lowering and HIR: The AST is subsequently transformed (using lowering techniques) into a high-level intermediate representation.
• HIR → Code Generation: The code generation module then translates this intermediate representation into LLVM IR.
• LLVM IR → Native Code: External tools (Inkwell via LLVM and cc for linking) are used to generate machine code.
• Error Reporting: At each stage there is integration with error reporting modules, ensuring that lexical, syntactical, and semantic issues are clearly communicated.
The language server (cart-lsp) likely communicates with the compiler backend to provide real-time feedback, thus forming an external connection that supports interactive development.
Architectural Patterns and Technologies
• Modular Design: The project is modularized into clearly separated concerns (lexer, parser, AST/HIR, codegen, context, errors).
• Pipeline Architecture: The compiler flows from one stage to the next (tokenization to parsing to semantic analysis to code generation).
• External Dependencies: LLVM IR generation is performed via the Inkwell library, and linking is done via cc.
• Integration Points: The standard library (cart_std) is compiled separately and then linked during execution; the language server (cart-lsp) enhances the developer experience by integration with editors.