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.
- Building & running ferrous
- Variables
- Flow Control
- Functions
- Scopes
yarn install && yarn build.
chmod a+x ./ferrous
./ferrous
You can bind a value to a variable using =
operator.ex.
var color = '#FF0000';
var a = 10;
var b = 20;
if ( a+b > 60) {
print "yes !";
} else {
print "No !";
}
- while with block of statements
var a = 0;
while (a < 10){
print a;
a = a + 1;
}
// prints from 0...9
- while with single expression body
var c = 0;
while (c < 3) print c = c + 1;
// prints 1...3
- Using single expression body.
for (var i = 0; i < 10; i = i + 1) print i;
// 0..9
- Using block statements
for (var i = 0; i < 10; i = i + 1){
print i;
}
fun sayHi(msg){
print msg;
}
sayHi("hello world");
fun sayHi() { return "Hi"; }
var msg = demo();
print msg; // Hi
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
fun makeCounter() {
var i = 0;
fun count() {
i = i + 1;
print i;
}
return count;
}
var counter = makeCounter();
counter(); // "1".
counter(); // "2".
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
*/