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.

(thanks Shaun)

Posted by Michael Mahemoff at 7:37 am
3 Comments

++++-
4 rating from 26 votes

3 Comments »

Comments feed TrackBack URI

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 :>

Comment by Aimos — January 11, 2010

goto is evil

Comment by sym3tri — January 11, 2010

compared to the javascript c64 emulator, which emulates an entire machine, this isn’t that interesting.

Comment by bunnyhero — January 15, 2010

Leave a comment

You must be logged in to post a comment.