- The grammar file for the Go (subset) language is located in the
grammar
directory. - The necessary runtime library for ANTLR4 is in the
lib
directory - All the source files are contained in
src
ast
: AST visitor & AST printercommandlineparser
: Simple command line parsercommon
: Cross-package classeserrorlisteners
: Custom ANTLR error listeners to generate nicer error messagesfunctionresolver
: First tree walk pass to collect function informationgenerated
: Contains generated ANTLR classesscopes
: Environment class to facilitate scope-based symbol lookuptypechecking
: Typechecking visitor
- Some example test cases for the compiler are in
tests
- All test cases can be run using the
run_tests.py
script
- All test cases can be run using the
- The
build.py
script can be used to build the project - The
cleanup.py
script is used to delete all.class
files generated after compilation usingjavac
This project is built and executed using OpenJDK 16.0.2
λ java -version
openjdk version "16.0.2" 2021-07-20
OpenJDK Runtime Environment Temurin-16.0.2+7 (build 16.0.2+7)
OpenJDK 64-Bit Server VM Temurin-16.0.2+7 (build 16.0.2+7, mixed mode, sharing)
λ javac -version
javac 16.0.2
Also, ANTLR version 4.11.1 was used.
NOTE:
If you do not want to execute all these steps, there is a simple build.py
script that automates everything.
-
It is first needed to generate the ANTLR base classes from the grammar file
SimpleGo.g4
SimpleGoCompiler
being the current working directory, execute the following command:antlr4 -o src/generated -no-listener -visitor -package generated -Xexact-output-dir grammar/SimpleGo.g4
As a result, a
generated
directory should be created inside of thesrc
directory, containing all required ANTLR API base classes.NOTE: In case this command yields an error, make sure you have the ANTLR toolchain installed:
pip install antlr4-tools
-
To build the actual project, execute the following commands:
# Windows cd src javac -cp .;../lib/antlr-4.11.1-complete.jar SimpleGoCompiler.java -d ../build # Alternatively cd src set classpath=.;../lib/antlr-4.11.1-complete.jar javac SimpleGoCompiler.java -d ../build
This will place all class files into the
build
directory. -
Running the program is then simply done using:
cd ../build java -cp .;../lib/antlr-4.11.1-complete.jar SimpleGoCompiler -compile <Test.go>
Or if the classpath has been set temporarily using
set
:cd ../build java SimpleGoCompiler -compile <Test.go>
Example:
java SimpleGoCompiler -compile ../tests/test_fibonacci.go
java SimpleGoCompiler [-compile | -liveness | -printast <true | false>] <Filename.go>
Options:
-compile: Typechecks and compiles an input .go file into Jasmin bytecode.
-liveness: Prints out the required number of registers to run program.
-printast: Prints the AST. When given 'true', all node attributes are also printed.
The included cleanup.py
script can be used to quickly get rid of .class
files and the generated ANTLR files:
python cleanup.py [--clean-generated]