SpecterCC is a functioning C++ compiler implementation that follows a classic multi-stage compilation pipeline. The compiler is built with clean architecture principles, professional code organization, and modern C++17 standards.
Source Code (.spc)
↓
[ Lexical Analysis ]
↓
Token Stream
↓
[ Syntax Analysis ]
↓
Abstract Syntax Tree (AST)
↓
[ Semantic Analysis ]
↓
Annotated AST
↓
[ Code Generation ]
↓
x86-64 Assembly (.s)
↓
[ Assembler & Linker ]
↓
Executable Binary
Location: include/lexer/, src/lexer/
Responsibilities:
- Tokenize source code into a stream of tokens
- Handle keywords, identifiers, literals, operators, and delimiters
- Track source locations for error reporting
- Skip whitespace and comments
Key Classes:
Lexer: Main lexer class that processes source codeToken: Represents a single token with type, lexeme, and locationTokenType: Enumeration of all token types
Location: include/parser/, src/parser/
Responsibilities:
- Parse token stream into an Abstract Syntax Tree (AST)
- Implement recursive descent parsing with operator precedence
- Handle grammar rules and syntax errors
- Error recovery and synchronization
Key Classes:
Parser: Implements recursive descent parser- Grammar rules for expressions, statements, and declarations
Operator Precedence (highest to lowest):
- Primary (literals, identifiers, parentheses)
- Unary (-, !)
- Multiplicative (*, /, %)
- Additive (+, -)
- Relational (<, <=, >, >=)
- Equality (==, !=)
- Logical AND (&&)
- Logical OR (||)
- Assignment (=)
Location: include/ast/, src/ast/
Responsibilities:
- Represent program structure as a tree
- Provide base classes for all AST nodes
- Support type information attachment
Key Classes:
ASTNode: Base class for all nodesExpression: Base for expressions (literals, binary ops, calls, etc.)Statement: Base for statements (if, while, return, etc.)FunctionDecl: Function declarations with parameters and bodyProgram: Root node containing all functions
Location: include/semantic/, src/semantic/
Responsibilities:
- Type checking and inference
- Symbol table management with scoping
- Undeclared identifier detection
- Function signature verification
Key Classes:
SemanticAnalyzer: Main analyzer with scope managementSymbol: Represents variables and functions in symbol table
Features:
- Nested scope support
- Type checking for expressions and assignments
- Function parameter count validation
Location: include/codegen/, src/codegen/
Responsibilities:
- Generate x86-64 assembly code
- Manage stack frames and calling conventions
- Implement control flow
- Handle variable storage and parameter passing
Key Classes:
CodeGenerator: Generates assembly from AST
Code Generation Details:
- Calling Convention: Stack-based parameter passing
- Register Usage:
rax: Return values, expression resultsrcx: Temporary for binary operationsrbp: Frame pointerrsp: Stack pointerrdi: Exit code for syscall
- Stack Layout:
rbp+N: Parameters (N = 16, 24, 32, ...) rbp+8: Return address rbp+0: Saved frame pointer rbp-8: First local variable rbp-16: Second local variable ...
Location: src/main.cpp
Responsibilities:
- Command-line argument parsing
- File I/O
- Pipeline orchestration
- Error reporting
Tool: CMake 3.15+
Structure:
- C++17 standard
- Warning flags enabled for GCC/Clang
- Modular compilation units
- Single executable target:
spectercc
int: 32-bit signed integerfloat: Single precision floating pointdouble: Double precision floating pointchar: Character typebool: Boolean typevoid: Void type (for functions)
Arithmetic: +, -, *, /, %
Comparison: ==, !=, <, <=, >, >=
Logical: &&, ||, !
Assignment: =
If Statement:
if (condition) {
statements
} else {
statements
}While Loop:
while (condition) {
statements
}Declaration:
return_type function_name(type param1, type param2) {
statements
}Features:
- Parameters passed by value
- Return statement required for non-void functions
- Function calls with arguments
Declaration:
type variable_name;
type variable_name = initializer;Assignment:
variable = expression;The compiler provides detailed error messages with:
- Source file name
- Line number
- Column number
- Descriptive error message
Errors are reported at multiple stages:
- Lexer: Invalid tokens, unterminated strings
- Parser: Syntax errors, unexpected tokens
- Semantic: Type mismatches, undeclared identifiers
All test programs are in examples/:
simple.spc: Basic variable assignmentarithmetic.spc: Arithmetic operations and function callsconditional.spc: If/else statementsloop.spc: While loops and factorialtest_param.spc: Parameter passing verification
Run ./demo.sh to test all examples.