Sunday, January 10th, 2010
QBasic on Javascript
Steve Hanov has been busy. Following from his hand-drawn canvas app, he’s now built a working QBasic implementation powered by Canvas and Javascript and is blogging about how he did it.
The most straightforward way of creating a basic compiler in javascript is to directly translate the basic into javascript functions. But this approach will not work for two reasons. First, there is “goto” which, although it is a reserved word, is not yet in Javascript. (Obviously, ECMAScript community finds “with”, prototype inheritance, and the rules of the ‘this’ keyword to be far less confusing than allowing “goto”). It is possible to automatically move statements around to eliminate GOTO, but you don’t want to go there.
The other problem is that browsers tend to freeze until javascript programs finish running. To avoid freezing the browser until the program ends, we break the program into small chunks, and execute a few of those chunks every so often using a javascript timer. This gives the appearance of a running program and doesn’t freeze the browser.
Bytecode solves both of those problems. By breaking the program into bytecode instructions, we can implement goto by just changing which instruction we are going to execute next. We can also suspend execution any time to allow the user to interact with the browser.








I *understand* that it is funny, but why investing so much work into something which is mostly useless. Some guys have to much free time :>
goto is evil
compared to the javascript c64 emulator, which emulates an entire machine, this isn’t that interesting.