I am writing a parser for a metalanguage to be run in the browser. The metalanguage is blocking, but needs to be converted to nonblocking in the interpreter due to the limitations of javascript.
For example, the metalanguage code might look something like
1. dosomething(4)
2. dosomethingelse(1)
3. dosomething(7)
4. goto 2
with the functions implemented in javascript as
function dosomething(n, callback) {
... // do something with n
setTimeout(callback, 1000);
}
function dosomethingelse(n, callback) {
... // do something else with n
setTimeout(callback, 1000);
}
Without the goto
statements, this would be easy to compile to javascript and then eval
. However, I have no clue how to implement the goto
s. Any help is appreciated.
As others have said, check out Promises. This tutorial really helped me out for getting started with them, hopefully it helps you too. https://spring.io/understanding/javascript-promises