A complete compiler for a custom imperative programming language that compiles to JVM bytecode. The compiler implements all phases of compilation: lexical analysis, parsing, semantic analysis, and code generation.
- Variables & Types: INT, FLOAT, STRING, BOOLEAN, user-defined collections
- Control Flow: if/else, while loops, for loops
- Functions: User-defined functions with parameters and return values
- Arrays: Dynamic arrays with length() function
- Collections: User-defined struct-like types with automatic constructors
- I/O Operations: Built-in print functions and input reading
- Compound Assignment Operators:
+=,-=,*=,/=,%= - Higher-Order Functions: FUNC type for function pointers and callbacks
- Function Composition: Pass functions as parameters, return functions from functions
- Type Safety: Full semantic analysis with type checking
- Block Scoping: Proper variable scoping rules
- Final Variables: Immutable variable declarations
compiler/
├── src/main/java/compiler/
│ ├── Compiler.java # Main entry point
│ ├── Lexer/
│ │ ├── Lexer.java # Lexical analyzer
│ │ └── Symbol.java # Token definitions
│ └── Parser/
│ ├── AST.java # Abstract syntax tree nodes
│ ├── Parser.java # Syntax analyzer
│ ├── SemanticAnalyzer.java # Type checking & validation
│ ├── CodeGenerator.java # JVM bytecode generation
│ └── [other AST nodes]
├── test/
│ └── Test*.java # Unit tests
└── tests/codegen/
├── 01_print.lang # Basic I/O
├── 02_ops_scope_collections.lang # Operations & collections
├── 03_loops_conditions.lang # Control flow
├── 04_extra_boolean_array.lang # Arrays & booleans
├── 05_test_compound.lang # Compound operators
├── 06_test_func_composition.lang # Function composition
└── RUN_CODEGEN_TESTS.md # Test runner guide
- Java 21 or higher
- Gradle (included via wrapper)
./gradlew build./gradlew test./gradlew run --args="source_file.lang -o output.class"java -cp output_directory class_nameINT x = 5;
FLOAT pi = 3.14;
STRING name = "Hello";
BOOLEAN flag = true;
final INT constant = 42;
def INT add(INT a, INT b) {
return a + b;
}
def main() {
INT result = add(5, 3);
println(result);
}
coll Person {
STRING name;
INT age;
}
def main() {
Person p = Person("Alice", 30);
println(p.name);
}
def main() {
INT[] numbers = INT ARRAY[5];
numbers[0] = 10;
println(numbers.length());
}
INT x = 5;
x += 3; // x = x + 3
x *= 2; // x = x * 2
x /= 4; // x = x / 4
def INT add(INT a, INT b) {
return a + b;
}
def INT apply(FUNC f, INT x, INT y) {
return f(x, y); // Dynamic function call
}
def main() {
INT result = apply(add, 5, 3);
println(result); // Outputs: 8
}
- Lexer: Tokenizes source code with support for multi-character operators and comments
- Parser: Recursive descent parser with 2-token lookahead
- Semantic Analyzer: Two-pass analysis (declaration collection, then validation)
- Code Generator: ASM library for JVM bytecode generation
- ASM Library: For bytecode generation
- Java: Implementation language
- Gradle: Build system
- JUnit: Testing framework
Run the full test suite:
./gradlew testRun specific tests:
./gradlew test --tests TestLexer
./gradlew test --tests TestParser
./gradlew test --tests TestSemanticCoreTest code generation examples:
./gradlew run --args="tests/codegen/01_print.lang -o test.class"
java -cp tests/codegen test- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new features
- Ensure all tests pass
- Submit a pull request
This project is open source. See individual files for license information.