1324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver/*
2324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * [The "BSD licence"]
3324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * Copyright (c) 2005-2008 Terence Parr
4324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * All rights reserved.
5324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver *
6324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * Conversion to C#:
7324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc.
8324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * All rights reserved.
9324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver *
10324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * Redistribution and use in source and binary forms, with or without
11324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * modification, are permitted provided that the following conditions
12324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * are met:
13324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * 1. Redistributions of source code must retain the above copyright
14324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver *    notice, this list of conditions and the following disclaimer.
15324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * 2. Redistributions in binary form must reproduce the above copyright
16324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver *    notice, this list of conditions and the following disclaimer in the
17324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver *    documentation and/or other materials provided with the distribution.
18324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * 3. The name of the author may not be used to endorse or promote products
19324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver *    derived from this software without specific prior written permission.
20324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver *
21324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver */
32324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
33324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruvertree grammar DebugTreeGrammar;
34324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
35324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruveroptions
36324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver{
37324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver	language=CSharp3;
38324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver	tokenVocab=DebugGrammar;
39324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver	ASTLabelType=CommonTree;
40324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver}
41324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
42324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// START:members
43324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver@header
44324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver{
45324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//import java.util.Map;
46324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//import java.util.HashMap;
47324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruverusing BigInteger = java.math.BigInteger;
48324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruverusing Console = System.Console;
49324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver}
50324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// END:members
51324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
52324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// START:rules
53324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruverprog:   stat*
54324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    ;
55324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
56324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruverstat:   expr                       { string result = $expr.value.ToString();
57324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                                     Console.Out.WriteLine($expr.value + " (about " + result[0] + "*10^" + (result.Length-1) + ")");
58324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                                   }
59324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    |   ^('=' ID expr)             { globalMemory[$ID.text] = $expr.value; }
60324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    |   ^(FUNC .+)	               // ignore FUNCs - we added them to functionDefinitions already in parser.
61324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    ;
62324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
63324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruverexpr returns [BigInteger value]
64324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    :   ^('+' a=expr b=expr)       { $value = $a.value.add($b.value); }
65324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    |   ^('-' a=expr b=expr)       { $value = $a.value.subtract($b.value); }
66324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    |   ^('*' a=expr b=expr)       { $value = $a.value.multiply($b.value); }
67324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    |   ^('/' a=expr b=expr)       { $value = $a.value.divide($b.value); }
68324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    |   ^('%' a=expr b=expr)       { $value = $a.value.remainder($b.value); }
69324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    |   ID                         { $value = getValue($ID.text); }
70324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    |   INT                        { $value = new BigInteger($INT.text); }
71324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    |   call                       { $value = $call.value; }
72324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    ;
73324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
74324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruvercall returns [BigInteger value]
75324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    :   ^(CALL ID expr)            { BigInteger p = $expr.value;
76324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                                     CommonTree funcRoot = findFunction($ID.text, p);
77324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                                     if (funcRoot == null) {
78324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                                         Console.Error.WriteLine("No match found for " + $ID.text + "(" + p + ")");
79324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                                     } else {
80324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                                         // Here we set up the local evaluator to run over the
81324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                                         // function definition with the parameter value.
82324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                                         // This re-reads a sub-AST of our input AST!
83324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                                         DebugTreeGrammar e = new DebugTreeGrammar(funcRoot, functionDefinitions, globalMemory, p);
84324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                                         $value = e.expr();
85324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                                     }
86324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                                   }
87324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    ;
88324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// END:rules
89