Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
75 lines (59 sloc) 2.28 KB
using Nitra;
using Nitra.Runtime;
using Nemerle;
using Nemerle.Collections;
using System.Collections.Generic;
language Calc
{
syntax module CalcGrammar start rule start2;
}
syntax module CalcGrammar
{
using Nitra.Core;
using IncGrammar;
using NumParser;
[StartRule]
syntax start = (expr; ','; ?)+ !Any { [Cached] Value() : list[double] = Exprs.Item1.MapToList(_.Value()); }
[StartRule]
syntax start2 = expr !Any { [Cached] Value() : double = Expr.Value(); }
// We can redefine names for fields in Any time.
literal Operator = "+", "-", "??";
[StartRule]
syntax expr
{
Value() : double;
missing Value = double.NaN;
| [SpanClass(Number)]
num = number { override Value = Number.Value(); }
| rounds = '(' expr ')' { override Value = Expr.Value(); }
precedence Additive:
| add = expr sm '+' sm expr { override Value = Expr1.Value() + Expr2.Value(); }
| sub = expr sm '-' sm expr { override Value = Expr1.Value() - Expr2.Value(); }
precedence Multiplicative:
| mul = expr sm '*' sm expr { override Value = Expr1.Value() * Expr2.Value(); }
| div = expr sm '/' sm expr { override Value = Expr1.Value() / Expr2.Value(); }
| mod = expr sm '%' sm expr { override Value = Expr1.Value() % Expr2.Value(); }
precedence Power:
| pow = expr sm '^' sm expr right-associative
{ override Value = System.Math.Pow(Expr1.Value(), Expr2.Value()); }
precedence Unary:
| neg = '-' expr { override Value = -Expr.Value(); }
precedence UnaryChange:
| prefixDec = "--" expr { override Value = Expr.Value() - 1.0; }
| postfixDec = expr "--" { override Value = Expr.Value() - 1.0; }
}
}
syntax module IncGrammar
{
using Nitra.Core;
using cp = CalcGrammar;
using CalcGrammar;
extend syntax cp.expr
{
precedence Unary:
| plus = '+' cp.expr ^ Unary { override Value = Expr.Value(); }
precedence UnaryChange:
| prefixInc = "++" cp.expr ^ UnaryChange { override Value = Expr.Value() + 1; }
| postfixInc = cp.expr ^ UnaryChange "++" { override Value = Expr.Value() + 1; }
}
}