70 points · by beagle3 · 6y ago · 19 comments
At first I couldn't believe that's all there was. The heart of the interpreter is "almost nothing":
const think = (l,r,x) => (iter(l, (f,l) => (r.push(x = call(f,l,r,x)), args[f])), {r,x})
const wrap = x => /[a-z]+/.test(x) ? '\''+x+'\'' : x
const parse = x => eval('['+x.replace(/([a-z]+)|(-?\d+)|(\])/g, x => wrap(x)+',')+']')
const run = (text, x) => [x].concat(think(parse(text), [], x).r)
And the LOGO commands are simply global variables.It's a fun project, and I appreciated the hand-crafted code. It reminded me of how much is possible with so little, small is beautiful indeed.
I recently submitted a small post here about implementing a simple FORTH-like language, in golang. I figured I should go a step further and document how you'd embed that inside a host-application. So I came up with a trivial turtle-implementation.
To draw a square:
50 forward
90 turn
50 forward
90 turn
50 forward
90 turn
50 forward
Of course with FORTH you'd define a word to draw a square of the given size: : square ( top of the stack is length of side)
dup dup dup
4 0 do
forward \ move forward, drawing a line
90 turn \ turn 90 degrees. we're a square!
loop
;
The result was definitely fun to play with, especially once I made it output both the final result and an animated gif of each draw-step:This reminds me strongly of that post. DSLs are fun, and something like FORTH maps well to a lot of simple use-cases.
With Logo you would do exactly the same. In fact, growing the language’s base vocabulary with your own words is the whole point of Logo and Forth. Why waste your entire lifetime speaking the machine’s primitive, low-level language when you can teach it how to speak your own powerful, high-level ones?
+1 for using the amazing horseshoe crab† as cursor, but -1,000,000 for so utterly missing the point.
--
† Not actual crabs, they’re more closely related to spiders and have lived on Earth virtually unchanged for almost half a billion years. https://oceantoday.noaa.gov/fullmoon-remarkablehorseshoecrab...
Whilst we were in lockdown the first time, I wrote a small Logo interpreter for my son with almost exactly the same commands: https://freememory.github.io/azlogo/ (https://github.com/freememory/azlogo)
My code isn't nearly as elegant, not at all animated, and my parsing is awful but I had a lot of fun doing it.
There's something magical about Logo, and even now as an adult of almost 40 I continue to enjoy playing with it and making pretty line graphics.
Port it to WebAseembly/WebGL already! No downloads, no warnings, also works on phones, etc.
EDIT: just wanted to add that you have something really incredible in your hands, try to make it more accessible, more tutorials would be good
They do make for good languages for toy robots, though - for instance, you can use Scratch with Lego Mindstorms in a way that's not much more advanced than Logo.