A compiler that translates a Java-like language into SaM (a JVM simulator) for educational purposes.
- Java
- SaM Assembly
- Multi-pass compilation process
- Comprehensive symbol table management
- Type checking and error handling
- Support for:
- Basic arithmetic and logical operations
- Control flow statements (if-else, while loops)
- Method declarations and invocations
- String operations and manipulation
- Object-oriented programming constructs (no inheritance yet)
- Java JDK 8 or higher
- SaM Assembler and Runtime
-
Clone the repository
git clone https://github.com/lebinary/compiler.git
-
Enter Directory
cd compiler
This starter code package includes a gradle build configuration and JUnit test cases to help you in completing the assignment. You should follow the gradle installation instructions for your system at https://gradle.org/install/ and make sure you have Java version 11 or later installed as the build file targets Java 11.
You can build the JAR file with
gradle build
which will leave the jar file at build/libs/compiler.jar
If you have some tests failing and want to build a jar anyway, you can skip tests using
gradle build -x test
On some platforms you may need to use the included gradlew
or gradlew.bat
scripts instead of calling gradle
directly.
Any IDE with gradle support can run the test cases, but they can also be run manually from the commandline by
gradle test
-
Prepare a source code file (e.g.,
program.lo
) Example source code:int main() { String message; message = "Hello, World!"; return 0; }
-
Build jar file of the compiler, a
compiler.jar
file should be created in/build/libs
gradle build --rerun-tasks
-
Run the jar file to generate a SAM file
java -jar compiler.jar program.lo output.sam
-
Start the Stack Abstract Machine simulator
java -Dfile.encoding=UTF8 -jar SaM-2.6.3.jar
-
Load the generated SAM file
output.sam
and run the simulator!
The compiler implements a two-pass compilation strategy:
-
First Pass: Symbol Table Population
- Collects variable and method declarations
- Performs scope analysis
- Validates declarations and signatures
-
Second Pass: Code Generation
- Generates SaM assembly code
- Performs type checking
- Manages memory allocation
- Handles control flow