Skip to content

Latest commit

 

History

History
77 lines (49 loc) · 2.94 KB

README.md

File metadata and controls

77 lines (49 loc) · 2.94 KB

Mini-C compiler

Build Status Maven Central

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: 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 <parameters> (via shell script or Windows .exe) or java -jar minic.jar <parameters>.

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