
This past week I’ve been in Brighton working on the MIMIC Project at the EMUTE Lab with the Sema development team, Thor and Francisco and Chris. Part of the project is about creating livecoding languages for machine learning, and we’ve spent a few days getting deep into their hackable livecoding language and software, called SEMA.
Machine learning aside, I’ve used the time to learn the ins and outs of NearleyJS for creating grammars and I’m actually liking it. This week, I wrote a minilanguage grammar for my LivePrinter project, something I’ve been trying to do for quite awhile.
This is my 3rd attempt to write a grammar over the past 2 years. (Check out the failed PEGjs version or the unfinished GCode grammar). It’s been on my to do list for awhile because livecoding is all about typing, brevity and contextual information and general programming languages are more about structured syntax for a wide variety of programmers and situations.
For example, JavaScript is a powerful language but there is a lot of punctuation in the form of { } [ ] ( ) ; etc. and these are confusing for beginners, physically hard to type (you have to hit shift keys, hunt on the keyboard for them), and prone to typing errors when they don’t come in pairs. It can be very verbose, and it has all sorts of constructs that are very useful for writing functions and creating more complex structures but which aren’t usually needed when programming live.
When I’m sitting in front of an audience and trying to think of things to type, I need short snippets or routines and not larger, more complex code structures. One-liners are great. Also, sticking in some contextual information helps thinking. With 3D printing, we’re thinking about physical objects and real-world movements. Units shouldn’t just be numbers, they should be dimensions like millimeters, inches, feet, etc.
With a grammar, I write a very short sentence in code like:
# lp.move x:22mm | turn 90deg | extrude x:10mm y:10mm
…which translates to, in English: “Tell the printer to move 22mm in the x direction (perpendicular to the person sitting in front of it), then turn 90 degrees and extrude a line whilst moving 10mm across and 10mm away”. With a grammar, I could even make that more explicit so Tell the printer compiled to lp (which is the LivePrinter object, in code terms) and in the x direction compiled to the function argument x: . It’s a way of making code more like language, more readable.
Unfortunately, more verbose code takes longer to type. It also has more words to mess up when you’re typing under pressure in front of people. Hopefully this minilanguage will find the balance between the two. There’s an example in the system that you can read through.
Also – the interesting thing about using Nearley and it’s inline text manipulating functions is that I was able to make a transpiler (translator from one language to another) that directly compiles my minilanguage into JavaScript, which is then compiled in the web browser. That means I don’t need to parse this language into an AST (abstract syntax tree), which is a very verbose representation of the code, and then interpret that as JavaScript. There are definitely benefits to have an AST, but for an experimental language developed on the cheap, having a quick method for fast prototyping it and trying out new things is much preferred over a more abstract sense of completeness. Things can always be refactored in the future…