paincompile is a minimal compiler written in Rust that translates a small subset of a C-like language (TinyC) into assembly (targeting ARM64 on macOS or optionally x86). This project is designed to help you understand the basics of compiler design—including lexing, parsing, AST construction, and code generation—while working with low-level architecture concepts.
The TinyC Compiler processes TinyC source code in several stages:
- Lexical Analysis: Convert raw source code into tokens.
- Parsing: Build an Abstract Syntax Tree (AST) from tokens.
- Code Generation: Traverse the AST and produce assembly code.
- Assembly & Linking: Use system tools to assemble and link the generated assembly into an executable.
The project serves as a hands-on demonstration of how high-level constructs are translated into low-level instructions.
- Implement a lexer to convert TinyC source files into a stream of tokens.
- Build a recursive descent parser that constructs an AST.
- Create a code generator that outputs assembly code (target ARM64 on macOS, with potential x86 support).
- Develop a build script to assemble, link, and run the compiled code.
- Provide thorough documentation and tests for the compiler components.
-
Project Initialization
- Set up the repository, file structure, and basic module files.
- Write initial skeletons for lexer, parser, AST, and code generator.
-
Lexer Development
- Define token types (e.g., keywords, identifiers, literals, operators, punctuation).
- Implement functions to scan and tokenize a TinyC source file.
-
Parser Development
- Create a grammar for TinyC (supporting declarations, assignments, expressions, and print statements).
- Implement a recursive descent parser that builds the AST.
-
AST and Code Generation
- Define data structures for expressions and statements.
- Write a code generation module that produces ARM64 assembly (Darwin ABI) from the AST.
-
Tooling and Testing
- Develop unit tests for each module.
- Write a shell script (
build.sh) to streamline assembling, linking, and running the generated assembly. - Document usage and implementation details in this README.
-
Enhancements (Optional)
- Extend language features (e.g., conditionals, loops, functions).
- Consider adding basic optimizations in code generation.
- Implement a simple REPL for TinyC.