https://github.com/alexp11223/minic
A simple compiler for a C-like programming language to JVM bytecode. Kotlin, ANTLR
https://github.com/alexp11223/minic
antlr ast compiler compiler-design jvm-bytecode kotlin parsing
Last synced: about 2 months ago
JSON representation
A simple compiler for a C-like programming language to JVM bytecode. Kotlin, ANTLR
- Host: GitHub
- URL: https://github.com/alexp11223/minic
- Owner: AlexP11223
- License: mit
- Created: 2016-12-30T18:52:51.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-09-12T11:32:59.000Z (almost 8 years ago)
- Last Synced: 2025-03-31T09:37:41.433Z (3 months ago)
- Topics: antlr, ast, compiler, compiler-design, jvm-bytecode, kotlin, parsing
- Language: Kotlin
- Homepage:
- Size: 597 KB
- Stars: 12
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: license.txt
Awesome Lists containing this project
README
# Mini-C compiler
[](https://travis-ci.org/AlexP11223/minic)
[](https://mvnrepository.com/artifact/com.github.alexp11223/minic)A simple compiler for a C-like programming language.
It has if-else statements, loops, variables (of int, double, bool or string types), arithmetic, comparison, logical operations, “functions” for user communications via stdin/stdout.
Implemented in Kotlin, using ANTLR for parsing and ASM library for JVM bytecode output.
# How to build
Requirements:
- JDK 8+.
- Maven 3+.Run Maven **package** phase. This will download all dependencies, run JUnit tests and build JAR file + .exe and shell script. Check Maven output to see if all tests and build steps are completed successfully.
(Maven is included in popular Java IDEs such as IntelliJ Idea or Eclipse. You can run it either via your IDE Maven plugin or from command line in separate [Maven installation](https://maven.apache.org/install.html): `mvn package`.)
`dist/` folder will contain JAR file, .exe for Windows and shell script for Linux/MacOS (it simply executes the JAR file via `java`), as well as a sample source code file.
Some of the tests launch `java`, using path from `System.getProperty("java.home")`. Fallbacks to `java` (from PATH environment variable) if it is not found.
# Usage
1. `cd dist/minic-dist`
2. Run `minic ` (via shell script or Windows .exe) or `java -jar minic.jar `.
If launched without parameters, it reads input from stdin until EOF (Ctrl+D, or Ctrl+Z for Windows), compiles and runs the program.
Also it is possible to specify input and output files:```
minic [input_file [output_file]] [options]
```**input_file** is path (or name) of file with Mini-C source code.
**output_file** is path (or name) of output file with JVM bytecode. Optional. If not specified, _input_file_ without extension will be used. **.class** extension is appended if not present (otherwise `java` will not run it), such as MyProgram.class.
Options:
**--ast** [png_output_file]: draw AST and save as PNG image (default ast.png).
**--tokens**: output lexer tokens.
**--bytecode**: output bytecode as text. Only the main code, not the whole generated class. Also not includes frames map.
**--debug**, **-d**: adds additional information, such as source code line numbers in bytecode.
**--decompiled_bytecode**: The same as *--bytecode* but extracts bytecode from generated result instead of writing it during codegen, and includes frames map.
Example:
```
minic MyProgram.mc
```
or
```
minic MyProgram.mc MyProgram
```
and
```
java MyProgram
```Additional output:
```
minic MyProgram.mc --ast my_ast.png --tokens --bytecode --decompiled_bytecode
``````
minic MyProgram.mc --bytecode --debug
```