1tree grammar Reduce;
2
3options
4{
5	tokenVocab=VecMath;
6	ASTLabelType=CommonTree;
7	output=AST;
8	filter=true;
9	language=CSharp3;
10}
11
12@namespace{Antlr3.Runtime.Test.Composition}
13
14/** Rewrite: x+x to be 2*x, 2*x to be x<<1, x<<n<<m to be x<<(n+m) */
15bottomup
16    :  ^(PLUS i=INT j=INT {$i.int==$j.int}?) -> ^(MULT["*"] INT["2"] $j)
17    |  ^(MULT x=INT {$x.int==2}? y=.)        -> ^(SHIFT["<<"] $y INT["1"])
18    |  ^(SHIFT ^(SHIFT e=. n=INT) m=INT)
19       -> ^(SHIFT["<<"] $e INT[($n.int+$m.int).ToString()])
20    ;
21