1 /* 2 * [The "BSD license"] 3 * Copyright (c) 2011 Terence Parr 4 * All rights reserved. 5 * 6 * Conversion to C#: 7 * Copyright (c) 2011 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 namespace Antlr3.Runtime.Test.BuildOptions 34 { 35 using System.Collections.Generic; 36 using Antlr.Runtime.Tree; 37 38 using BigInteger = java.math.BigInteger; 39 using Console = System.Console; 40 41 partial class ProfileTreeGrammar 42 { 43 /** Points to functions tracked by tree builder. */ 44 private List<CommonTree> functionDefinitions; 45 46 /** Remember local variables. Currently, this is only the function parameter. 47 */ 48 private readonly IDictionary<string, BigInteger> localMemory = new Dictionary<string, BigInteger>(); 49 50 /** Remember global variables set by =. */ 51 private IDictionary<string, BigInteger> globalMemory = new Dictionary<string, BigInteger>(); 52 53 /** Set up an evaluator with a node stream; and a set of function definition ASTs. */ ProfileTreeGrammar(CommonTreeNodeStream nodes, List<CommonTree> functionDefinitions)54 public ProfileTreeGrammar(CommonTreeNodeStream nodes, List<CommonTree> functionDefinitions) 55 : this(nodes) 56 { 57 this.functionDefinitions = functionDefinitions; 58 } 59 60 /** Set up a local evaluator for a nested function call. The evaluator gets the definition 61 * tree of the function; the set of all defined functions (to find locally called ones); a 62 * pointer to the global variable memory; and the value of the function parameter to be 63 * added to the local memory. 64 */ ProfileTreeGrammar(CommonTree function, List<CommonTree> functionDefinitions, IDictionary<string, BigInteger> globalMemory, BigInteger paramValue)65 private ProfileTreeGrammar(CommonTree function, 66 List<CommonTree> functionDefinitions, 67 IDictionary<string, BigInteger> globalMemory, 68 BigInteger paramValue) 69 // Expected tree for function: ^(FUNC ID ( INT | ID ) expr) 70 : this(new CommonTreeNodeStream(function.GetChild(2)), functionDefinitions) 71 { 72 this.globalMemory = globalMemory; 73 localMemory[function.GetChild(1).Text] = paramValue; 74 } 75 76 /** Find matching function definition for a function name and parameter 77 * value. The first definition is returned where (a) the name matches 78 * and (b) the formal parameter agrees if it is defined as constant. 79 */ findFunction(string name, BigInteger paramValue)80 private CommonTree findFunction(string name, BigInteger paramValue) 81 { 82 foreach (CommonTree f in functionDefinitions) 83 { 84 // Expected tree for f: ^(FUNC ID (ID | INT) expr) 85 if (f.GetChild(0).Text.Equals(name)) 86 { 87 // Check whether parameter matches 88 CommonTree formalPar = (CommonTree)f.GetChild(1); 89 if (formalPar.Token.Type == INT 90 && !new BigInteger(formalPar.Token.Text).Equals(paramValue)) 91 { 92 // Constant in formalPar list does not match actual value -> no match. 93 continue; 94 } 95 // Parameter (value for INT formal arg) as well as fct name agrees! 96 return f; 97 } 98 } 99 return null; 100 } 101 102 /** Get value of name up call stack. */ getValue(string name)103 public BigInteger getValue(string name) 104 { 105 BigInteger value; 106 if (localMemory.TryGetValue(name, out value) && value != null) 107 { 108 return value; 109 } 110 if (globalMemory.TryGetValue(name, out value) && value != null) 111 { 112 return value; 113 } 114 // not found in local memory or global memory 115 Console.Error.WriteLine("undefined variable " + name); 116 return new BigInteger("0"); 117 } 118 } 119 } 120