Bariq (barīq) is a Golang interpreter for a simple programming language. The language incorporates immutable data structures and includes essential ones such as arrays and hash tables all guided by the awesome book by Thorstem ball WaIGo.
The interpreter supports advanced features such as closures and first-class functions, Adding Async-Await, Generators support for now.
- Async-Await
- Generators
- Modules
- Macros
- Static Type Checker
Ex:
let s = async fn(x) {sleep(5); 5};
let task = s(5);
puts("4");
await(task)
output
4
5
- when parser read
async
keyword, it marks that function as Async. - async function returns a variable of type
object.Task
which hasSpawned
of type*sched.Task
, - the schedular is the responsible for spawning tasks given by the evaluator.
await
accepts an epxression, if the evaluated expression is of typeTask
it calls the schedular spwaned task attach to that task in order toawait
it
generators in bariq is inspired by how Javascript handles generators,
EX:
let s = fn gen () { yield 2;yield 0;yield 6;yield 1; };
let genr = s();
next(genr);
next(genr);
next(genr);
// s: object.Iteration{ Val : 6, Done: False}
a fancy Ex:
let w = fn (){1}
let s = fn gen () {
let q = w();
yield q;
};
let genr = s();
next(genr);
// s: object.Iteration{ Val : 1, Done: False}
- when parser read
gen
keyword, it marks that function as generator. Generator
type has a reference to the function in addition to the env and index to indicate the position where it left the function.- when
next()
is called, it checks if the object is of typeGenerator
and starts evaluting the function body starting from theIndex
passing theEnv
that passed first at creating the generator. - if
yield
keyword is found, it sets theIndex
and theValue
of the generator (you can call it a frame) and reutrn anIteration
Object having the state of beingDone
or not and the current value of the generator (the current frame)
run:
go test ./... -v
Thnx .. Anthony GG for his Async Await wrapper over channels (give a look)