A robust and advanced compiler for the SAM programming language that translates high-level code into x86-64 assembly.
- Variables:
x = 10; - Arithmetic Operations:
+, -, *, / - Control Flow:
if,else,while - Functions:
function name(params) { ... } - Function Calls:
result = function(arg1, arg2); - Print Statements:
print("message"); - Return Statements:
return value; - Expressions: Complex arithmetic with proper precedence
- Comments:
// Single line comments
- Proper Error Handling: Detailed error messages with line and column numbers
- Expression Parsing: Full arithmetic expression support with operator precedence
- Variable Scope: Local variable management
- Control Flow: Complete if/else and while loop support
- Function Support: Function definitions and calls
- Assembly Generation: Clean, optimized x86-64 assembly output
makeThis will create the sam compiler executable in the build/ directory.
./build/sam <source_file.sam>The compiler will:
- Parse the SAM source file
- Generate x86-64 assembly
- Assemble using NASM
- Link using the system linker
- Create an executable
x = 10;
y = 20;
z = x + y;
result = (a + b) * c;
sum = x + y * z;
if(x > y) {
print("x is greater");
result = x + y;
} else {
print("y is greater");
result = y - x;
}
counter = 0;
while(counter < 10) {
sum = sum + counter;
counter = counter + 1;
}
function add(a, b) {
return a + b;
}
result = add(5, 3);
print("Hello, World!");
The examples/ directory contains comprehensive examples:
- 01_basic_variables.sam - Basic variable assignments and arithmetic
- 02_control_flow.sam - If statements and control flow
- 03_loops.sam - While loops and iteration
- 04_functions.sam - Function definitions and calls
- 05_complex_arithmetic.sam - Complex arithmetic expressions
- 06_factorial.sam - Factorial calculation using loops
- 07_fibonacci.sam - Fibonacci sequence calculation
- 08_prime_check.sam - Prime number checking algorithm
# Compile an example
./build/sam examples/01_basic_variables.sam
# Run the resulting executable
./01_basic_variables- Tokenizes source code into tokens
- Supports keywords, identifiers, literals, operators
- Provides detailed error reporting with line/column information
- Handles comments and whitespace
- Builds Abstract Syntax Tree (AST) from tokens
- Implements recursive descent parsing
- Supports expressions with proper operator precedence
- Handles control flow constructs
- Converts AST to x86-64 assembly
- Manages variable scope and memory layout
- Generates optimized assembly code
- Handles function calls and control flow
The compiler provides detailed error messages:
SYNTAX ERROR! Unexpected token: + at 5:12
Lexer error: Undefined symbol '#' at 3:8
Error: Undefined variable 'x' at 7:3
The compiler generates clean, readable x86-64 assembly:
section .data
msg_0 db "Hello, World!", 0
msg_0_len equ $ - msg_0
section .text
global _start
_start:
push rbp
mov rbp, rsp
mov rax, 10
mov [rbp - 8], rax
mov rax, 1
mov rdi, 1
mov rsi, msg_0
mov rdx, msg_0_len
syscall
mov rax, 60
mov rdi, 0
syscall- C++17 compatible compiler (GCC/Clang)
- NASM (Netwide Assembler)
- System linker (ld)
# Install NASM (macOS)
brew install nasm
# Install NASM (Ubuntu/Debian)
sudo apt-get install nasm
# Build the compiler
makeThis is an educational compiler project. Feel free to extend it with:
- More language features (arrays, strings, etc.)
- Better optimization
- More target architectures
- Enhanced error recovery
- Standard library functions
Compiler development for sam language schema