ThinkGeek - Cool Stuff for Geeks and Technophiles

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: , ,

Wednesday, December 3, 2008

introducing SPTL

There seems to be a long period of initial obscurity for any new language. Then after that comes a long period of semi-obscurity, followed by total obscurity.

Paul Bissex, quoted by Steve Yegge in The Next Big Language

So I've been working on the grammar for my language to target the Parrot Virtual Machine. As it turns out, creating a consistent context-free grammar is a lot harder than it may seem. Or maybe I've got a warped sense of what seems easy and what seems hard.

At any rate, the Simple Parrot Test Language (SPTL) is a small Perl-like language. In fact, at a glance it might appear to be a subset of Perl. However, some elements have different meanings in SPTL than in Perl. I'm going to spend the next few posts going over the details and explaining my design decisions. For now, the full BNF grammar can be found here (text file).

Labels: ,