jaba is a programming language built using golang. The language source code is transformed into tokens through lexical analysis and then tokens are transformed to an Abstract Syntax Tree using a parser. Evaluation (Interpretation) is done to give the AST meaning and returns the respective result of the source code entered.
Source Code -> Tokens -> Abstract Syntax tree (AST) -> Output
The first transformation, from source code to tokens, is called “lexical analysis”, or “lexing” for short. It’s done by a lexer (also called tokenizer or scanner – some use one word or the other to denote subtle differences in behaviour).
Tokens themselves are small, easily categorizable data structures that are then fed to the parser, which does the second transformation and turns the tokens into an “Abstract Syntax Tree”. The final step is evaluation where the AST is given meaning by an interpreter which gives the respective result. The interpreter used here is called a tree walking interpreter
. An object is used for the interpreter; it is not very performant, but it's easy to get started with.
- C-like syntax
- variable bindings
- integers and booleans
- arithmetic expressions
- built-in functions
- first-class and higher-order functions
- closures
- Clone the repository
- Run
go run main.go
- Enter the jaba program on the command line
let age = 1;
let name = "Trent";
let result = 10 * (20 / 2);
let myArray = [1, 2, 3, 4, 5];
myArray[0] // => 1
let thorsten = {"name": "Thorsten", "age": 28};
thorsten["name"] // => "Thorsten"
let add = fn(a, b) { return a + b; };
let add = fn(a, b) { a + b; };
add(1, 2);
let fibonacci = fn(x) {
if (x == 0) {
0
} else {
if (x == 1) {
1
} else {
fibonacci(x - 1) + fibonacci(x - 2);
}
}
};
let twice = fn(f, x) { return f(f(x)); };
let addTwo = fn(x) { return x + 2; };
twice(addTwo, 2); // => 6
// newGreeter returns a new function, that greets a `name` with the given
// `greeting`.
let newGreeter = fn(greeting) {
// `puts` is a built-in function we add to the interpreter
return fn(name) { puts(greeting + " " + name); }
};
// `hello` is a greeter function that says "Hello"
let hello = newGreeter("Hello");
// Calling it outputs the greeting:
hello("dear, future Reader!"); // => Hello dear, future Reader!
The host language for jaba is Golang, which does the garbage collection and allows the memory not to leak when we run code like this
let counter = fn(x) {
if (x > 100) {
return true;
} else {
let foobar = 9999;
counter(x + 1);
}
};
counter(0);
If C was used to write the interpreter, we would have to implement our own garbage collector to avoid memory leaks. An example of a garbage collector implementation is Mark and Sweep.
- Evolve the REPL to launch as a cmd application
- Support reading of
.jaba
files for good programming experience - support syntax highlighting
- Language documentation
- Ball, Thorsten. Writing An Interpreter In Go (p. 8). Kindle Edition.
- Ball, Thorsten. Writing An Interpreter In Go (pp. 8-9). Kindle Edition.
- Ball, Thorsten. Writing An Interpreter In Go (p. 9). Kindle Edition.
- Ball, Thorsten. Writing An Interpreter In Go (p. 13). Kindle Edition.
- Ball, Thorsten. Writing An Interpreter In Go (pp. 189-190). Kindle Edition.