• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * [The "BSD licence"]
3  * Copyright (c) 2005-2008 Terence Parr
4  * All rights reserved.
5  *
6  * Conversion to C#:
7  * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 using System.Collections.Generic;
34 using Antlr.Runtime.Tree;
35 
36 using BigInteger = java.math.BigInteger;
37 using Console = System.Console;
38 
39 partial class DebugTreeGrammar
40 {
41     /** Points to functions tracked by tree builder. */
42     private List<CommonTree> functionDefinitions;
43 
44     /** Remember local variables. Currently, this is only the function parameter.
45      */
46     private readonly IDictionary<string, BigInteger> localMemory = new Dictionary<string, BigInteger>();
47 
48     /** Remember global variables set by =. */
49     private IDictionary<string, BigInteger> globalMemory = new Dictionary<string, BigInteger>();
50 
51     /** Set up an evaluator with a node stream; and a set of function definition ASTs. */
DebugTreeGrammar( CommonTreeNodeStream nodes, List<CommonTree> functionDefinitions )52     public DebugTreeGrammar( CommonTreeNodeStream nodes, List<CommonTree> functionDefinitions )
53         : this( nodes )
54     {
55         this.functionDefinitions = functionDefinitions;
56     }
57 
58     /** Set up a local evaluator for a nested function call. The evaluator gets the definition
59      *  tree of the function; the set of all defined functions (to find locally called ones); a
60      *  pointer to the global variable memory; and the value of the function parameter to be
61      *  added to the local memory.
62      */
DebugTreeGrammar( CommonTree function, List<CommonTree> functionDefinitions, IDictionary<string, BigInteger> globalMemory, BigInteger paramValue )63     private DebugTreeGrammar( CommonTree function,
64                  List<CommonTree> functionDefinitions,
65                  IDictionary<string, BigInteger> globalMemory,
66                  BigInteger paramValue )
67         // Expected tree for function: ^(FUNC ID ( INT | ID ) expr)
68         : this( new CommonTreeNodeStream( function.GetChild( 2 ) ), functionDefinitions )
69     {
70         this.globalMemory = globalMemory;
71         localMemory[function.GetChild( 1 ).Text] = paramValue;
72     }
73 
74     /** Find matching function definition for a function name and parameter
75      *  value. The first definition is returned where (a) the name matches
76      *  and (b) the formal parameter agrees if it is defined as constant.
77      */
findFunction( string name, BigInteger paramValue )78     private CommonTree findFunction( string name, BigInteger paramValue )
79     {
80         foreach ( CommonTree f in functionDefinitions )
81         {
82             // Expected tree for f: ^(FUNC ID (ID | INT) expr)
83             if ( f.GetChild( 0 ).Text.Equals( name ) )
84             {
85                 // Check whether parameter matches
86                 CommonTree formalPar = (CommonTree)f.GetChild( 1 );
87                 if ( formalPar.Token.Type == INT
88                     && !new BigInteger( formalPar.Token.Text ).Equals( paramValue ) )
89                 {
90                     // Constant in formalPar list does not match actual value -> no match.
91                     continue;
92                 }
93                 // Parameter (value for INT formal arg) as well as fct name agrees!
94                 return f;
95             }
96         }
97         return null;
98     }
99 
100     /** Get value of name up call stack. */
getValue( string name )101     internal BigInteger getValue( string name )
102     {
103         BigInteger value;
104         if ( localMemory.TryGetValue( name, out value ) && value != null )
105         {
106             return value;
107         }
108         if ( globalMemory.TryGetValue( name, out value ) && value != null )
109         {
110             return value;
111         }
112         // not found in local memory or global memory
113         Console.Error.WriteLine( "undefined variable " + name );
114         return new BigInteger( "0" );
115     }
116 }
117