Wednesday, December 3, 2008

wForth - A JavaScript Forth Interpreter

‘wForth’ is a JavaScript based Forth interpreter. It is a simplified version of Forth and doesn’t contain all the functionality of a ANSI Forth system. The main goal of it is to provide a basic web based platform for people to try out programming with Forth. At the same time though, I wanted it flexible enough to allow programmers to write their own JavaScript Forth dictionaries to customize and extend the system. For example, allow wForth to interact with others aspects of the browser or to hook into Ajax calls to interact with servers.

Why wForth? jForth, jsForth, webForth were all taken. So wForth is for web based Forth.

What is supported:

1. New Forth word creation
2. Standard math operators on integer types only
3. Parameter stack manipulation
4. Infinite loops
5. Do loops
6. Pass data to and from the parameter stack to the return stack
7. Conditionals (if. then.else)

Try it out:

Future Items:
Storing user defined Forth word dictionaries in cookies or serializing them to and from JSON format to mimic loading them from a disk.

   

9 comments:

Anonymous said...

This is possibly the coolest thing I have ever seen. Would love to see info on how you embedded on page with street view.

Em Aich said...

very cool!

I was thinking today about writing a variant of Joy using JavaScript as the host language. Currently trying to think how to warp the syntax a little so that quoted programs are legal JSON ...

Anonymous said...

Appears to be rather incomplete, after comparing it with http://forthfreak.net/jsforth80x25.html, which claims to be a quite complete DPANS94 Forth, written in JavaScript too. At least I couldn't get this to respond to words, WORDS, vlist, VLIST, create, etc. Is this still under construction, or merely a feature-starved "demo"? Lackinf elementary facilities, I wonder whether calling this "Forth" isn't somewhat misleading.

K Jacobson said...

I agree that it is incomplete and readily admit so in the comments. Its intent is to provide the basics of a Forth system to allow interested users to try some basic commands.

How about trying to run this simple program below in http://forthfreak.net/jsforth80x25.html using IE or Chrome, and then try running it in this Javascript Forth implementation. You might have a second opinion on what should be called Forth.

10 0 DO CR ." Hello " LOOP ;

Anonymous said...

Cool indeed
." Happy " 2 dup over dup over * * + dup over * over * rot * + . ." to all"

But it seems rot is not implemented right
1 2 3 rot -> 2 1 3
but should give 2 3 1 as far as I know

K Jacobson said...

I took the definition for 'rot' from here. http://www.forth.com/starting-forth/sf2/sf2.html

It says to rotate the third item to the top of the stack, but some of the documentation doesn't show it that way.

K Jacobson said...

rot

should be fixed now. Thanks for catching the error!

Mark Hagstrom said...

Try this:
: test1 1024 0 do 1 drop loop;

test1 7 .

It takes way too long. A good deal of forth is based on it's ability to loop. This simple test shows a major looping problem. Theoretically this should translate/encode to a simple structure that completes almost immediately yet it is taking a very long time.

K Jacobson said...

It does take awhile to complete the program you submitted. It is a looping problem. The bottleneck is with browsers prompting users if they want to continue executing long running running JavaScript routines; which couls happen here. To overcome that issue this interpreter calls the next Forth word using JavaScript's setTimeout function. This is what is slowing down the program. Try this same program in my Forth as a Web Service post where I am posting backing a Forth program to a Python interpreter and you can the performace gain you expect. And that interpreter is based upon this one here.

Forth as a web service: http://pybasicws.appspot.com/apps/ForthConsole.html