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... )
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 global variables keeping track of what you're doing.
If LSL had coroutines you could co-call the event handlers and make it work. But it doesn't. You can't even use another script and inter-process communication because that too uses event handlers.
I had a simple object-construction script to write, and it was getting very complicated because I had to keep all my state around. I realized that I needed to abstract away from the event driven nature of LSL. I would've liked to have implemented Scheme, but each LSL process has only 16K of memory, and it's pretty slow too, so I wanted the smallest layer I could manage. Hence the Forth-like language. I can now write things like:
"product-foo" : make-box dup "item A" add "item B" add ;
in order to create a box containing a copy of item A and item B. The clever bit here, I suppose, it that the "make-box" and "add" words are supplied by a separate script from the interpreter, which registers dictionary entries at start-up. These entries do IPC, passing the stack back and forth. The interpreter itself only has the words ":" and ";" in its starting dictionary at the moment, but some flow control will be needed at some point. This is why I started thinking about how little I'd have to have to get full power.
Reply
Leave a comment