leftop and rightop

                                         {my %variables}

start:          statement  /^\Z/         {$variables {'.'} = $item [1]}

statement:      <rightop: variable '=' expression>
                         {my $value = pop @{$item [1]};
                          while (@{$item [1]}) {
                              $variables {shift @{$item [1]}} = $value;
                          }
                          $value
                         }

expression:     <leftop: term ('+' | '-') term>
                         {my $s = shift @{$item [1]};
                          while (@{$item [1]}) {
                              my ($op, $t) = splice @{$item [1]}, 0, 2;
                              if ($op eq '+') {$s += $t}
                              else            {$s -= $t}
                          }
                          $s
                         }

term:           <leftop: factor m{([*/])} factor>
                         {my $t = shift @{$item [1]};
                          while (@{$item [1]}) {
                              my ($op, $f) = splice @{$item [1]}, 0, 2;
                              if ($op eq '/') {$t /= $f}
                              else            {$t *= $f}
                          }
                          $t
                         }


factor:         number
              | variable                 {$variables {$item [1]} ||=
                                                Math::BigInt -> new (0)}
              | '+' factor               {$item [2]}
              | '-' factor               {$item [2] * -1}
              | '(' statement ')'        {$item [2]}

number:         /\d+/                    {Math::BigInt -> new ($item [1])}

variable:       /[a-z]+/i
              | '.'

[Prev] [Next] [Index]