Skip to content

A bytecode compiler and interpreter for a simple language

Notifications You must be signed in to change notification settings

gAbelli/simplelang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simplelang

A bytecode compiler and interpreter for a simple language (technically instructions are two bytes long but you get the idea).

Example

function fib(n) {
  let a = 0;
  let b = 1;
  while (n > 0) {
    let tmp = b;
    b = a + b;
    a = tmp;
    n = n - 1;
  }
  return a;
}

function fibSlow(n) {
  if (n == 0) {
    return 0;
  }
  if (n == 1) {
    return 1;
  }
  return fibSlow(n - 1) + fibSlow(n - 2);
}

function main() {
  let n = 20;
  let _ = print(fibSlow(n));
  let _ = print(fib(n));
}

Usage

Generate the parser using antlr4:

alias antlr4='java -jar /path/to/antlr-4.XX-complete.jar'
cd modules/parser/src/main/antlr4/
antlr4 -package parser -o ../java/parser/ SimpleLang.g4
cd -

Compile and run a program:

sbt 'vm/run <file>'

About

A bytecode compiler and interpreter for a simple language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published