GithubHelp home page GithubHelp logo

alexp11223 / minic Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 1.0 611 KB

A simple compiler for a C-like programming language to JVM bytecode. Kotlin, ANTLR

License: MIT License

ANTLR 2.50% Kotlin 97.50%
jvm-bytecode antlr kotlin compiler compiler-design parsing ast

minic's Introduction

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

minic's People

Contributors

alexp11223 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

geethya1995

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.