Skip to content

Tervicke/QuarkCompiler

Repository files navigation

QuarkCompiler

This is a simple little fun project I am working on to learn more about parsers and the JVM. The idea is to make a tiny but usable language that compiles and emits JVM bytecode (.class file).

How it works ?

The parser is generated using ANTLR . u can find the grammer in the Quark.g4 file . The tree walking is done in java classes while using the ASM library to generate the jvm bytecode .

Usage

Open the project in IntelliJ and add all the JARs from the jars folder to the classpath. Then run the code while passing the filepath to the .quark file . Here we use test.quark.
Remember IntelliJ runs the code from the root path so make sure the file path is correct . It will generate a filename.class file in the root folder itself , run it with the JVM. ($ java filename)
See the test.quark file for examples


using makefile

add the std library as class path in your .bashrc or .zashrc

For eg

export CLASSPATH=".:/home/tervicke/IdeaProjects/Quark/std:$CLASSPATH"

compile and create the build directory

make all

run the file

make run input=examples/structs.quark

run the binary

cd examples
java structs 

see more examples

Tests

The tests are stored in the json file , u can run the tests by running the python file file.

python3 run_tests.py

features


🔹 1. Literals & Basic Types

  • Integer literals (INT)
  • Double literals (DOUBLE)
  • Boolean literals (true, false)
  • String literals ("...")
  • Parenthesized expressions (expr)

🔹 2. Types & Variable Declarations

  • Basic types: int, string, bool, double
  • Variable declaration with type (e.g., int x = 5)
  • Variable declaration without type (type inference)
  • Variable reassignment (x = expr)
  • Typed function parameters
  • Optional return type for functions (-> TYPE)
  • Const or immutable variables (e.g., const x = 10)

🔹 3. Expressions

  • Arithmetic operators: +, -, *, /, %
  • Comparison operators: <, >, <=, >=
  • Equality operators: ==, !=
  • Field access: obj.field
  • Logical operators: &&, ||, !
  • Unary minus / plus (-a, +a)
  • Ternary expressions (cond ? a : b)

🔹 4. Control Flow

  • If statements (if (cond) { ... })
  • If-else blocks
  • While loops (while (cond) { ... })
  • Nested blocks { ... }
  • For loops
  • Break & continue statements
  • Switch / match expression

🔹 5. Functions

  • Function definition (fn name(args) { ... })
  • Function call (foo(1, 2))
  • Return statement (return expr)
  • Optional return type (-> int)
  • Function call from module (mod.func())
  • Recursive functions
  • Lambdas or anonymous functions

🔹 6. Structs

  • Struct definition (struct Name { ... })
  • Struct fields with types
  • Struct instantiation (Point(1, 2))
  • Field access (p.x)
  • Nested structs
  • Struct methods

🔹 7. I/O and Built-ins

  • Print statement (print(...))
  • Input from user
  • String formatting
  • Basic standard library (e.g., len(string), math functions)

🔹 8. Modules and Imports

  • Import statement (import module)
  • Importing specific functions or structs
  • Export keywords for public APIs

About

Quark is a statically typed compiled language targeting the JVM

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published