/ferrous

Ferrous is an interpreter for lox language implemented in Typescript

Primary LanguageTypeScript

Ferrous 🧪

Ferrous is a Typescript port of Lox, a dynamic programming language.

Once I am done with adding features into this language , I will re implement ferrous with a byte code compiler, until then this port will remain a tree walk interpreter.

Currently it tries to be a one to one port of jlox but I plan on adding additional features making this lox's superset, wherein all valid lox programs will be valid ferrous programs.

Intoduction to ferrous

Build and run

yarn install && yarn build.   
chmod a+x ./ferrous
./ferrous

Variables.

You can bind a value to a variable using = operator.ex.

var color = '#FF0000';

Flow control using conditionals.

var a = 10;
var b = 20;
if ( a+b  > 60) {
  print "yes !";
} else {
  print "No !";
}

While

  1. while with block of statements
var a = 0;
while (a < 10){
    print a;
    a = a + 1;
}
// prints from 0...9
  1. while with single expression body
var c = 0;
while (c < 3) print c = c + 1;
// prints 1...3

For

  1. Using single expression body.
for (var i = 0; i < 10; i = i + 1) print i;
// 0..9
  1. Using block statements
for (var i = 0; i < 10; i = i + 1){
  print i;
}

Function

  1. Function syntax
fun sayHi(msg){
    print msg;
}
sayHi("hello world");
  1. Function return values
fun sayHi() { return "Hi"; }
var msg = demo();
print msg; // Hi
  1. Function implicit return value.

If a return statement is missing in the function declaration null is returned by default.

fun calculate() {
    print 10+20;
    // No return statement here
}
var value = demo();
print value; // null
  1. Closures.
fun makeCounter() {
  var i = 0;
  fun count() {
    i = i + 1;
    print i;
  }
  return count;
}

var counter = makeCounter();
counter(); // "1".
counter(); // "2".

Scopes

var a = "global a";
var b = "global b";
var c = "global c";
{
  var a = "outer a";
  var b = "outer b";
  {
    var a = "inner a";
    print a;
    print b;
    print c;
  }
  print a;
  print b;
  print c;
}
print a;
print b;
print c;

/*
inner a
outer b
global c
outer a
outer b
global c
global a
global b
global c
*/