September 2017
Beginner
402 pages
9h 52m
English
In many grammars that aim to parse source code in different programming languages, one of the central parts is handling expressions. We have already introduced the expression rule, and it can understand simple expressions such as 100 or $x.
Let us continue with expressions and teach the compiler to parse and calculate expressions with the + operator. We will start with a simple case when both operands are integer literals:
rule expression { | <value> '+' <value> { $/.make($<value>[0].ast + $<value>[1].ast) } | <value> { $/.make(~$<value>) } | <variable> { $/.make(%var{$<variable><sigil>}{$<variable><identifier>}) }}
The rule got a new branch <value> '+' <value>, which uses two rules with the same name. In the action, ...
Read now
Unlock full access