February 1, 2019 · Project

Sumatra - a Java-like Language, and a Compiler

Part of Compiling Methods course in my bachelor studies, I created a Java-like programming language. I called it Sumatra.

SumatraLang is a static, strongly typed programming language. It supports all basic programming languages features, as well as some more advanced ones, such as arrays. The source is compiled to machine code, which later gets executed on a VM. The compiler supports simple graphic manipulation instructions as well, such that it's possible to write to the video memory to e.g. develop games like Tetris.

The compiler is modular. It composed of a separate lexer, parser, code generator and a virtual machine. Due to this, it is easy to add new features, such as compiling to a new architecture (e.g. x86) or automatically building the entire abstract syntax tree from the BNF description. I wrote the compiler in Ruby. I love Ruby, there's no faster way to develop anything other than with Ruby.

Here's an interactive code editor, which POSTs the source code to compiler's API on submit. I've written a sample program, which outputs nth Fibonacci number. Try to run the code below, or even change the source yourself to see how it responds:

class B { digitsCount(num: int) -> int { if (num == 0) { return 1; }
    int count = 0;

    while (num != 0)
    {
        num = num / 10;
        count = count + 1;
    }

    return count;
}

fib(num: int) -> int {
    if (num == 0) {
        return 0;
    }

    if (num == 1) {
        return 1;
    }

    return B.fib(num - 1) + B.fib(num - 2);
}

main() -> int {
    print B.fib(15);

    return 0;
}

}

Result:

Visit the repository to see the full source code! This was never intented for production use though, and I have no plans to do so :).

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket