grammar Benchmark;

token program { <.statement>+ }

rule statement { 
    | print <expression> ';'            {{ $I0 = match['expression']
                                           print $I0
					   print "\n" }}
    | <var> '=' <expression> ';'        {{ $P0 = match['expression']
                                           $S0 = match['var']
                                           set_global $S0, $P0 }}
}

rule expression { <valor> [ <add> | <sub> ]* {{ $I0 = match['valor']

                                                $I9 = defined match['add']
                                                unless $I9 goto sub_part 
                                                $P0 = match['add']
                                                .local pmc iter
                                                iter = new Iterator, $P0
                                                iter = 0

                                              iter_loop:
                                                unless iter, iter_end
                                                shift $P1, iter
                                                $I1 = $P1['valor']
                                                $I0 += $I1
                                                goto iter_loop
                                              iter_end:

                                              sub_part:
                                                $I9 = defined match['sub']
                                                unless $I9 goto end_part
                                                $P0 = match['sub']
                                                iter = new Iterator, $P0
                                                iter = 0

                                              iter_loop1:
                                                unless iter, iter_end1
                                                shift $P1, iter
                                                $I1 = $P1['valor']
                                                $I0 -= $I1
                                                goto iter_loop1
                                              iter_end1:

                                              end_part:
                                                match.'result_object'($I0)
                                              }}
}

rule add { '+' <valor> }
rule sub { '-' <valor> }

rule valor { <number>   {{ $I0 = match['number']
                           match.'result_object'($I0) }}
           | <var>      {{ $S0 = match['var']
                           $P0 = get_global $S0
                           $I0 = $P0
                           match.'result_object'($I0) }} 
}

token number { \d+ }
token var    { <[a..z]>+ }
