Skip to content
/ jcc Public

A collection of toy compilers built using ANTLR4 and flat assembler

License

Notifications You must be signed in to change notification settings

dykstrom/jcc

Repository files navigation

JCC

Build Status Open Issues Latest Release Downloads License Top Language JDK compatibility: 17+

JCC, the Johan Compiler Collection, is a collection of toy compilers built using ANTLR4 and flat assembler. The current version of JCC compiles three programming languages: Tiny, Assembunny, and a subset of BASIC.

System Requirements

  • Windows
  • Java 17 or later

You can download the Java runtime from Adoptium.

Executables created with JCC depend on the library msvcrt.dll, which is a part of Windows. BASIC executables also depend on the BASIC standard library, jccbasic.dll, that is distributed together with JCC.

Installation

Download the latest zip file from the GitHub releases page, and unzip it somewhere on your hard drive. Add the bin directory of the JCC distribution to the Path environment variable for your account.

Please note that while JCC itself is licensed under GPLv3, the included version of flat assembler is licensed under a specific license. A copy of this license can be found in the fasm subdirectory of the project.

Usage

With JCC in your Path, you can run it like this:

jcc <source file>

To get help, type:

jcc --help

This will print a message similar to this:

Usage: jcc [options] <source file>
  Options:
    --help
      Show this help text
    --version
      Show compiler version
    -O, -O1
      Optimize output
      Default: false
    -S
      Compile only; do not assemble
      Default: false
    -assembler
      Use <assembler> to assemble intermediate files
      Default: fasm
    -assembler-include
      Set the assembler's include directory to <directory>
    -initial-gc-threshold
      Set the number of allocations before first garbage collection
      Default: 100
    -o
      Place output in <file>
    -print-gc
      Print messages at garbage collection
      Default: false
    -save-temps
      Save temporary intermediate files permanently
      Default: false
    -v
      Verbose mode
      Default: false

Supported Languages

Assembunny

Assembunny is a made up programming language from the programming challenge Advent of Code 2016. It is a small assembly language with only four instructions: inc, dec, cpy, and jnz. To make the language more interesting I have also added support for the outn instruction from the Assembunny extension Assembunny-Plus.

This is an example of Assembunny code:

cpy 3 a
inc a
outn a

Assembunny files end with the file extension ".asmb".

BASIC

BASIC was invented in the sixties, and became very popular on home computers in the eighties. JCC BASIC is inspired by Microsoft QuickBASIC 4.5 from 1988. The current version of JCC implements a subset of BASIC. It does, however, come with a mark-and-sweep garbage collector to keep track of dynamic strings.

The example below is a short program to compute prime numbers:

' Calculate all primes less than a number N

CONST N = 100

DIM index AS INTEGER
DIM isPrime AS INTEGER
DIM maxIndex as INTEGER
DIM number AS INTEGER
DIM primes(N) AS INTEGER

number = 2
WHILE number < N

    ' Check if number is prime
    isPrime = 1
    index = 0
    WHILE isPrime AND index < maxIndex
        ' If number is dividable by any prime found so far, it is not prime
        isPrime = number MOD primes(index)
        index = index + 1
    WEND

    ' Print number if prime
    IF isPrime THEN
        PRINT number
        primes(maxIndex) = number
        maxIndex = maxIndex + 1
    END IF

    number = number + 1
WEND

This table specifies the BASIC constructs that have been implemented so far:

Data Types DOUBLE (64-bit)
INTEGER (64-bit)
STRING
Static arrays of the types above. Dynamic arrays are not supported.
Arithmetic Operators + - * / \ MOD
Relational Operators = <> > >= < <=
Bitwise Operators AND, NOT, OR, XOR
Control Structures GOSUB-RETURN
GOTO
IF-GOTO
IF-THEN-ELSE (including ELSEIF)
ON-GOSUB-RETURN
ON-GOTO
WHILE-WEND
Statements CLS
CONST
DEFDBL
DEFINT
DEFSTR
DIM
END
LET
LINE INPUT
OPTION BASE
PRINT
RANDOMIZE
REM
SWAP
Functions abs, asc, atn, cdbl, chr$, cint, cos, cvd, cvi, date$, exp, fix, hex$, instr, int, lbound, lcase$, left$, len, log, ltrim$, mid$, mkd$, mki$, oct$, right$, rnd, rtrim$, sgn, sin, space$, sqr, str$, string$, tan, time$, timer, ubound, ucase$, val
User-defined Functions DEF FN expression functions

BASIC files end with the file extension ".bas". BASIC executables require the BASIC standard library to run. This library is distributed together with JCC in the form of a DLL file: jccbasic.dll.

Tiny

Tiny is a small programming language, designed for educational purposes.

A typical Tiny program looks like this:

BEGIN
    READ a, b
    c := a + b
    WRITE c
END

Tiny files end with the file extension ".tiny".