Xerl: expression separator

01 Mar

As promised we are adding an expression separator this time. This will be short and easy.

In the tokenizer we only need to add a line recognizing the comma as a valid token.

, : {token, {',', TokenLine}}.

Then we need to change the following lines in the parser:

exprs -> expr : ['$1'].
exprs -> expr exprs : ['$1' | '$2'].

And add a comma between the expressions on the second line:

exprs -> expr : ['$1'].
exprs -> expr ',' exprs : ['$1' | '$3'].

That takes care of everything except the optional trailing comma at the end of our lists of expressions. We just need an additional rule to take care of this.

exprs -> expr ',' : ['$1'].

That’s it.

Wondering why we don’t have this optional trailing comma in Erlang considering how easy it was and the number of people complaining about it? Yeah, me too. But that’s for someone else to answer.

Another change I want to talk about is a simple modification of the compiler code to use an #env{} record for tracking state instead of passing around individual variables. This will be required later on when we make modules into proper expressions so I thought it was a good idea to anticipate.