A minilanguage for 3D printing ver 1

A new short-hand grammar, containing a general rule, for writing any language, whether English, Latin, French, etc Fleuron T112099-17
A new short-hand grammar, containing a general rule, for writing any language, whether English, Latin, French, &c. Also, particular shortening and joining rules, fitted to the English tongue, for Joining in every-sentence Five, Six, or more Words together; without taking off the Pen in an Instant of Time Never before invented. Done according to all the Improvements, which the Author hath made since he published his Former Short Hand Grammar about Twenty Years ago: Which Improvements render this New Method a great deal more Easy, Speedy, and Legible, as is evident by Comparing the Large Specimen of the Writing in the Former Grammar with the Same Large Specimen, in this New Grammar. And yet these Improvements may be learn’d with Ease, in the space of Three or Four Days, by any who have learned the Former Method. This New Method is so Compleat, That any Gentleman, or Lady may Learn the Art perfectly by this Book alone, and Take down from a Moderate Speakers Mouth, any Speech, Lecture, Sermon, Trial, Play, &c. Word for Word, and also Read distinctly one anothers Writing. Authoriz’d by his Majesty, And Attested by many Gentlemen, at the Beginning hereof; compos’d by James Weston, the only Author of this New Method.

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…