C- - Language
This project aims to design a compiler for a new language: C--. This language is primarily composed of C99 constructs, and hints of C++, Python, Swift, and GNU built ins absorbed into the language. The language is currently in the process of incremental development and is expecting complex data types in the near future.
C- - Compiler
This compiler utilizes Antlr4 to autogenerate the front end parser and scanner. The intermediate and backend of the compiler is written in C++. The target assembly is Jasmin, a JVM assembly language that can be assembled into Java *.class files. From there, the JVM can be invoked to execute the program.
Directory Structure
.
├── antlr4-runtime # ANLTR runtime header and source files, usually untouched
├── build # Build files (bin)
├── generated # Autogenerated ANTLR files
├── grammars # All the grammar files
│ └── Cmm.g4 # The only grammar file under development
├── libs # ANTLR program specific files
│ ├── antlr-4.7-complete.jar # Java ANTLR program
│ ├── libantlr-runtime.a # Static library built from ANTLR runtime source files
│ └── PascalRTL.jar # Java helper runtime libraries
├── outputs # Where the output *.j and *.class files are created
├── samples # Sample C-- programs to test ANTLR with
├── scripts # Bash and python scripts for build automation
│ ├── assemble.sh # Assembles the *.j file into a *.class file
│ ├── build.sh # Builds the lexer / parser for C++ target
│ ├── compile.sh # Compiles the *.c file into a *.j file
│ └── recompile.sh # Recompiles the ANTLR library into a static library
├── src # Non-ANTLR source files
├── tools # Various tools
│ ├── jasmin-assembler # Used for assembling *.j files
│ └── Krakatau # Used for disassembling *.class files into *.j
├─ SConsript # SCons build script
└─ SConstruct # SCons root environment
How To Build
SCons is responsible for the build process and automation.
- Install SCons from their website https://scons.org/pages/download.html
- Install JAVA JDK from their website http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html
- Add the JDK bin path to your environment path
- Set up the paths in the build scripts :
$JAVA_JDK_PATH
- Run
scons.py
from the directory that containsSConstruct
Use scons.py run=SAMPLE_PROGRAM
to build and run.
Use bash assemble.sh SAMPLE_ASSEMBLY
to assemble a jasmin source file
Use bash compile.sh SAMPLE_PROGRAM_NO_EXTENSIONS_UNDER_SAMPLES_DIR
to compile a source file
How To Generate The Parse Tree
- Run the
build.sh
script - Use the optional command line arguments for a different grammar file or a different sample program
ANTLR Runtime
The runtime library has already been compiled into a static library for easy linking, no need to worry about the runtime source files
However, it will not run on all machines. Use the recompile.sh
to recompile a static library on your machine which will appear under
antlr4-runtime/temp
.
TODO
# | Task |
---|---|
JasminEmitter module |
|
4 | Integrate JasminEmitter into the visitors |
5 | Add all the operators into the language |
6 | Add for loops |
7 | Add switch case |
8 | Add tests |
9 | Add arrays |
10 | Add structs |
11 | Add enums |