ThinkGeek - Cool Stuff for Geeks and Technophiles

Wednesday, March 4, 2009

programming language music

I don't know what's more impressive: This parody of Julia Ecklar's God Lives on Terra, or the fact that Ecklar herself was recruited to sing it.



Of course, God doesn't really create the universe with Lisp code. It's common knowledge that God used Perl.

Labels: , , ,

Saturday, December 13, 2008

SPTL grammar: names

A Simple Parrot Test Language (SPTL) program consists of an elementblock, which in turn consists of one or more simpleelements. This seeming redundancy results from having elementblocks as elements of some definitions. I don't want to say, for example, that every for, if, or repeat statement contains a program.

A simpleelement is a statement, a vardec, or a functiondef. Today we're going to look at vardecs.


vardec = 'new' ( scalardec | arraydec ) ';'

varname = scalarname | arrayname

scalardec = scalarname [ '=' expr ]

scalarname = '$' { '_' | letter | digit }+ { '[' expr ']' }

arraydec = arrayname [ '=' '(' exprarray ')' ]

arrayname = '@' { '_' | letter | digit }+ { '[' expr ']' }


SPTL, being a simple language, has only two kinds of variables: scalars and arrays. These use the same sigils as in Perl, $ and @ respectively.

A variable name is any combination of letters, digits, and underscores. Because all variable names are prepended with a sigil, there is no need to forbid names from beginning with digits, or indeed being entirely digits. The scanner can immediately see that $123 is a variable and not a number.

A variable declaration begins with the keyword new. A scalar or array can be declared with a null value by simply stating the name, or can be assigned a value (or multiple values if it's an array) by using the equals sign.

SPTL actually has other types of names. The next one we're going to look at is the filehandle:


filehandle = '^' { '_' | letter | digit }+


Filehandles are prepended with the sigil ^. This differs from Perl, where a filehandle can be either a bareword or a normal scalar. I've chosen to use a unique sigil for filehandles to simplify the work of the scanner. By requiring unique sigils for all types of names, the scanner knows from the beginning not only that this is a name, but just what sort of name it is.

The tradeoff is that this makes more work for the programmer. The ^ sigil is an extra thing to remember, and it's non-intuitive for a programmer coming from any other language. For a personal project like SPTL usability is beside the point; if this were a language that people were actually going to use, I'd do some things differently.

Finally we have functions:


functiondef = 'function' funcname '{' elementblock 'return' expr '}'

funcname = '&' { '_' | letter | digit }+


A function name is prepended with a & sigil. Superficially this resembles a Perl function call, which begins with an optional &. The difference can be seen, though, in the function definition. SPTL requires the & sigil here too. In Perl, because the & is not a sigil, the function definition has no & in the function name. In a milder but more noticeable departure from Perl syntax, function definitions use the keyword function rather than sub.

Inside the functiondef you can see the elementblock that I mentioned at the beginning of this post. Again, the definition of elementblock is identical to that of program, but the English meaning of the word program makes it a poor choice to describe an element of a program.

Finally, SPTL requires all functions to return a value, whether or not it will be used.

(Incidentally, I initially had defined program thus:


program = '{' elementblock '}'


but found it too laborious to wrap every test program in an extra set of curly braces for no other benefit than to give program and elementblock different definitions.)

So, by reviewing the types of names, we've covered two of the three types of simpleelements. We're almost done with the grammar. Ha!

Labels: , ,