How plc-lang/rusty works
Main components
• Compiler Frontend: Represent the tokenization and parsing phases. Components include the Lexer (src/lexer, tests), Parser (src/parser and its tests), and the AST construction in the various “plc_ast” modules.
• Semantic Analysis & Indexing: Show the role of symbol resolution and type checking. This includes the indexing crates (compiler/plc_index, src/index) and resolver (src/resolver, including const evaluator and generics) which work together to validate the structured text and build a semantic model.
• Lowering and Validation: Indicate the “lowering” phase (compiler/plc_lowering and src/lowering) that converts the high-level AST into an intermediate representation with proper type conversions and checks. In parallel, the “validation” parts (src/validation) ensure that language and domain‐specific rules are enforced.
• Code Generation: Include the CodeGen modules (src/codegen and its generators) that interact with LLVM (evident via files like src/codegen/llvm_index.rs and src/codegen/llvm_typesystem.rs) to create native IR/code.
• Compiler Driver / CLI: The overall entry-point is managed by components such as “compiler/plc_driver” where the command line interface (cli.rs) dispatches to the various compiler pipelines.
• Auxiliary Components: There are integration components like the “xtask” directory which handles building, reporting, and utility tasks, plus the “libs/stdlib” which provides the standard library functions for Structured Text programs.
Relationships and Data Flows
• Flow begins at the Compiler Driver/CLI: Input Structured Text source files (from examples or user inputs) are fed into the Lexer.
• The Lexer tokenizes the input, passing tokens to the Parser.
• The Parser constructs an AST that is enriched by derive macros (compiler/plc_derive) and later annotated by the Diagnostics (compiler/plc_diagnostics).
• The AST is processed by the Indexer to build symbol tables and resolve dependencies.
• Next, the Lowering phase converts the high-level AST into a lower-level intermediate representation while validating types, performing optimizations, and flagging errors.
• The Code Generation phase then takes over, using LLVM-based generators to produce the native code.
• Finally, the Linker (src/linker.rs, scripts/linker) ties the output together into an executable.