A direct-to-machine-code compiler for GDScript, the scripting language used in the Godot game engine. This compiler translates GDScript source code directly into native machine code without transpiling to another high-level language.
- Lexical Analysis: Tokenizes GDScript source code with support for all GDScript syntax
- Syntax Analysis: Builds Abstract Syntax Tree (AST) using recursive descent parsing
- Semantic Analysis: Performs type checking, symbol resolution, and semantic validation
- Code Generation: Generates native machine code with register allocation and optimization
- Data Types:
int,float,String,bool,Array,Dictionary,Variant - Control Flow:
if/else,while,for,matchstatements - Functions: Function declarations with parameters and return types
- Classes: Class definitions with inheritance, methods, and properties
- Signals: GDScript signal declarations and handling
- Built-in Functions:
print(),len(),range(),str(),int(),float()
- Type Inference: Automatic type deduction where possible
- Register Allocation: Efficient register usage with virtual register support
- Code Optimization: Dead code elimination and constant folding
- Error Reporting: Comprehensive error messages with line numbers
- Cross-Platform: Supports x86_64 and ARM64 architectures
GDScript Compiler/
├── main.cpp # Main compiler entry point
├── lexer.h/.cpp # Lexical analyzer
├── parser.h/.cpp # Syntax analyzer and AST builder
├── semantic_analyzer.h/.cpp # Semantic analysis and type checking
├── code_generator.h/.cpp # Machine code generation
├── examples/ # Example GDScript files
├── Makefile # Build system
├── clean.sh # Cleanup script
└── README.md # This file
- C++17 compatible compiler (GCC 7+ or Clang 5+)
- Make build system
- POSIX-compliant operating system (Linux, macOS, BSD)
# Build the compiler
make
# Clean build artifacts
make clean
# Debug build with symbols
make debug
# Optimized release build
make release
# Run basic tests
make test
# Install to system path
make install# Compile GDScript file to machine code
./bin/gdscript-compiler input.gd output
# This generates:
# - output.s (assembly code)
# - output.o (object file)
# - output (executable)See the examples/ directory for sample GDScript files that can be compiled with this compiler.
Usage: gdscript-compiler [options] <input.gd> <output>
Options:
-h, --help Show help message
-v, --version Show compiler version
-O, --optimize Enable optimizations
-g, --debug Include debug information
-S, --asm-only Generate assembly only
-c, --compile Generate object file only-
Lexical Analysis (
lexer.cpp)- Tokenizes source code into meaningful symbols
- Handles keywords, operators, literals, and identifiers
- Manages indentation-based syntax
-
Syntax Analysis (
parser.cpp)- Builds Abstract Syntax Tree (AST)
- Implements recursive descent parser
- Handles operator precedence and associativity
-
Semantic Analysis (
semantic_analyzer.cpp)- Type checking and inference
- Symbol table management
- Scope resolution
- Error detection
-
Code Generation (
code_generator.cpp)- Intermediate representation (IR) generation
- Register allocation
- Machine code emission
- Optimization passes
- x86_64: Intel/AMD 64-bit processors
- ARM64: Apple Silicon and ARM64 processors
- Linux: All major distributions
- macOS: Intel and Apple Silicon Macs
- Windows: PE executable format support
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
System calls are not fully implemented yet..
This project is open source. See the LICENSE file for details.