I read an interesting article. In it the writer is trying to send "code" for remote drawing over the websocket
and execute it on the receiving end. He is doing it in JavaScript, but to demo the useful nature of lisp code structure (brackets).
He can't just send Javascript code and eval() it to do this, as you can't seem to really limit and control the the eval's context. So he makes a simple interpreter of a lisp like structures.
In Rye contexts/scopes are first class values. While making them I created a function isolate, that creates an totally isolated context. Let's try to use it to create something similar without the need to make a separate interpreter.
The receiver in the article draws graphical primitives to the screen, our will behave like some sort of old school printer.
First instruction
?word is a get-word. It returns the value behind the word but doesn't evaluate it.
Instruction composition
The writer then tried to compose two instructions (functions together). Since we evaluate normal rye language we can compose them as we would normally.
Let's add one user function to the printer.
Definitions
The writer then supported definitions, and custom function definition. Set-words already work inside our context, fn is just another function so we can add it.
Control structures
The article hints at also using if. All Rye control structures are just functions: if, either, loop ... So let's add loop for finish.
Final thoughts
Basically in one line we created our own little "language" with it's own little space (context).
Our isolated context can also be linked to other contexts and those to other. Since last blog-post Rye also has a concept of pure functions and an isolated context of just pure functions, so you could link your context to it and gain all the functionality without any side effects immediately.
Imagine that you have a server, or a worker and instead of translating desired functionality to individual REST calls you could just send it sequences of specific composable Rye code to execute and return the result.
Rye (as Rebol) give you the ability to create custom interpreters (dialects), but sometimes you don't even need that.
Komentarji
Objavite komentar