Skip to content
ZaeemahmedPublic

About

A Java based compiler for Languages & Translators course

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Custom Language Compiler

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.

Features

Core Language Features

  • 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

Advanced Features

  • 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

Project Structure

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

Building & Running

Prerequisites

  • Java 21 or higher
  • Gradle (included via wrapper)

Build the Compiler

./gradlew build

Run Tests

./gradlew test

Compile a Source File

./gradlew run --args="source_file.lang -o output.class"

Execute Generated Bytecode

java -cp output_directory class_name

Language Syntax

Variable Declaration

INT x = 5;
FLOAT pi = 3.14;
STRING name = "Hello";
BOOLEAN flag = true;
final INT constant = 42;

Functions

def INT add(INT a, INT b) {
    return a + b;
}

def main() {
    INT result = add(5, 3);
    println(result);
}

Collections (Structs)

coll Person {
    STRING name;
    INT age;
}

def main() {
    Person p = Person("Alice", 30);
    println(p.name);
}

Arrays

def main() {
    INT[] numbers = INT ARRAY[5];
    numbers[0] = 10;
    println(numbers.length());
}

Compound Operators

INT x = 5;
x += 3;  // x = x + 3
x *= 2;  // x = x * 2
x /= 4;  // x = x / 4

Higher-Order Functions

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
}

Implementation Details

Architecture

  • 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

Key Technologies

  • ASM Library: For bytecode generation
  • Java: Implementation language
  • Gradle: Build system
  • JUnit: Testing framework

Testing

Run the full test suite:

./gradlew test

Run specific tests:

./gradlew test --tests TestLexer
./gradlew test --tests TestParser
./gradlew test --tests TestSemanticCore

Test code generation examples:

./gradlew run --args="tests/codegen/01_print.lang -o test.class"
java -cp tests/codegen test

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new features
  5. Ensure all tests pass
  6. Submit a pull request

License

This project is open source. See individual files for license information.

About

A Java based compiler for Languages & Translators course

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages