aboutsummaryrefslogtreecommitdiff
path: root/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Composition/Reduce.g3
blob: db61e42e7f5f6bf4b8fcd09470878c85cfac7c64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
tree grammar Reduce;

options
{
	tokenVocab=VecMath;
	ASTLabelType=CommonTree;
	output=AST;
	filter=true;
	language=CSharp3;
}

@namespace{Antlr3.Runtime.Test.Composition}

/** Rewrite: x+x to be 2*x, 2*x to be x<<1, x<<n<<m to be x<<(n+m) */
bottomup
    :  ^(PLUS i=INT j=INT {$i.int==$j.int}?) -> ^(MULT["*"] INT["2"] $j)
    |  ^(MULT x=INT {$x.int==2}? y=.)        -> ^(SHIFT["<<"] $y INT["1"])
    |  ^(SHIFT ^(SHIFT e=. n=INT) m=INT)
       -> ^(SHIFT["<<"] $e INT[($n.int+$m.int).ToString()])
    ;