• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1tree grammar Reduce;
2
3options
4{
5    tokenVocab=VecMath;
6    ASTLabelType=CommonTree;
7    output=AST;
8    filter=true;
9    language=CSharp3;
10}
11
12
13@members
14{
15   //public override IAstRuleReturnScope Topdown() { return topdown(); }
16   public override IAstRuleReturnScope Bottomup() { return bottomup(); }
17}
18
19
20/** Rewrite: x+x to be 2*x, 2*x to be x<<1, x<<n<<m to be x<<(n+m) */
21bottomup
22    :  ^(PLUS i=INT j=INT {$i.int==$j.int}?) -> ^(MULT["*"] INT["2"] $j)
23    |  ^(MULT x=INT {$x.int==2}? y=.)        -> ^(SHIFT["<<"] $y INT["1"])
24    |  ^(SHIFT ^(SHIFT e=. n=INT) m=INT)
25       -> ^(SHIFT["<<"] $e INT[($n.int+$m.int).ToString()])
26    ;
27