The KnightCode Compiler compiles programs written in KnightCode into Java bytecode. This bytecode is executable on any Java Virtual Machine (JVM). The compiler is built using Java and utilizes the ANTLR tool for parsing the KnightCode syntax, transforming it into a format that can be converted into bytecode.
- Lexer and Parser: Automatically generated by ANTLR based on the grammar definitions provided for KnightCode. These components break down the input code into tokens and construct a parse tree.
- SymbolTable: Manages variable declarations and scope to ensure correct bytecode generation concerning variable usage.
- BytecodeGenerator: Utilizes the ASM library to generate Java bytecode directly from the parsed KnightCode.
- KccVisitor: Traverses the parse tree, utilizing the SymbolTable and BytecodeGenerator to produce the final bytecode output based on the logic of the KnightCode program.
- Java Development Kit (JDK) must be installed.
- Clone the repository to you local machine.
- ANTLR and ASM libraries need to be included in your project or build path.
- Run the build-grammar and compile-grammar ANT commands to generate and compile the Java files produced by ANTLR. These commands can be found in the build.xml file.
- Write Your KnightCode File: Write your program in a file with the
.kc
extension. - Compile Your Program:
- Open your command line interface (CLI).
- Navigate to the directory containing your
.kc
file and thekcc
compiler. - Run the following command:
java compiler/kcc tests/yourKnightCodeFile.kc output/yourKnightCodeFile
- Replace
"yourKnightCodeFile"
with the name of the KnightCode file that you wrote. - Run your KnightCode class file using this command:
java output/yourKnightCodeFile