I recently had occasion to write a Forth interpreter in Linden Scripting Language (in Second Life). I've done it before (more than twice) and it only took me an hour or two. I've written the odd Scheme interpreter (and compiler) too. Forth and Scheme are both elegant little things, and I've always wanted to think of a way of combining them, but
(
Read more... )
Comments 10
The Church stuff is cute but useless. Until you got onto that, I was wondering whether there's something useful in here.
There's more than one "place": operand stack/return stack/dictionary. Sometimes (in "immediate mode"), when I read a token I apply it to the operand stack (which might have the effect of pushing the value onto the stack). Sometimes (in "compilation mode") I push it somewhere else. Is it interesting to abstract this idea?
Reply
The language XY has something like the three places you mention: operand stack, queue of things to be done (which incorporates the PC and return stack), and dictionary. However, I didn't like the notion of "valence", which seemed to make it more complicated than necessary.
Reply
The current Forth interpreter is just 180 lines of Linden Scripting Language (excluding comments, blank lines, and debugging) and occupies well under 8K of run-time memory. About 20 lines of that are inter-process communication for a registration mechanism for external libraries. Even the simplest GC would be a significant overhead, but the increase in programming power would be enormous.
Reply
Reply
I'd like to say a bit about in-memory representation. Again, there are two ways here: Forth-like and Scheme-like. In a Scheme-like system we have a heap containing (mostly) pairs, with a tagging scheme that lets us store other stuff. In Forth we have a dictionary which we mostly append threaded-executable code to. Each "object" in the dictionary is a list of other objects which is traversed depth-first in order to execute the code, except for some special primitives. There's a striking similarity between these two things, which is why I am particularly interested in things like Church encodings, which are precisely about making data structures out of executable raw materials.
Another way of looking at it is that each Forth word is a like a CDR coded list of other words, terminated by a return instruction. (In fact, "return" can just be a word like "R> drop R> jump".) The Fix "cat" operator builds a three-word object with the top two stack items and a "return". In this sense "cat" is almost like Scheme "cons". I think this ( ... )
Reply
Can you say something about the reason you did this?
Reply
Linden Scripting Language (LSL) is the language in which you write scripts in Second Life. When a script is placed in a primitive (such as a cube) that is rezzed (exists) in the virtual world, then it is run and becomes a process. Each simulator in Second Life schedules and runs all the processes in all the primitives that are rezzed. These processes provide behaviour for Second Life objects. (However, basic physics, drawing, particle systems, hovertext, purchasing, permissions, and a bunch of other stuff are stored properties of a primitive or object, and don't need scripting.)
However, LSL is an inherently event-driven language. You write a set of "states" and each state has a set of "event handlers". In order to, say, rez an object, you have to send a request and return from your event handler, then wait for your "object_rez" event handler to be poked. This means you can't write a subroutine like "make_a_cube_containing([list of objects])". Instead, you end up programming everything as a state machine with a load of ( ... )
Reply
Leave a comment