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 Gruverusing System.Collections.Generic; 34324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruverusing Antlr.Runtime.Tree; 35324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver 36324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruverusing BigInteger = java.math.BigInteger; 37324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruverusing Console = System.Console; 38324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver 39324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruverpartial class ProfileTreeGrammar 40324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver{ 41324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver /** Points to functions tracked by tree builder. */ 42324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver private List<CommonTree> functionDefinitions; 43324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver 44324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver /** Remember local variables. Currently, this is only the function parameter. 45324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver */ 46324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver private readonly IDictionary<string, BigInteger> localMemory = new Dictionary<string, BigInteger>(); 47324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver 48324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver /** Remember global variables set by =. */ 49324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver private IDictionary<string, BigInteger> globalMemory = new Dictionary<string, BigInteger>(); 50324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver 51324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver /** Set up an evaluator with a node stream; and a set of function definition ASTs. */ 52324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver public ProfileTreeGrammar( CommonTreeNodeStream nodes, List<CommonTree> functionDefinitions ) 53324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver : this( nodes ) 54324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver { 55324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver this.functionDefinitions = functionDefinitions; 56324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver } 57324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver 58324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver /** Set up a local evaluator for a nested function call. The evaluator gets the definition 59324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * tree of the function; the set of all defined functions (to find locally called ones); a 60324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * pointer to the global variable memory; and the value of the function parameter to be 61324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * added to the local memory. 62324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver */ 63324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver private ProfileTreeGrammar( CommonTree function, 64324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver List<CommonTree> functionDefinitions, 65324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver IDictionary<string, BigInteger> globalMemory, 66324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver BigInteger paramValue ) 67324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver // Expected tree for function: ^(FUNC ID ( INT | ID ) expr) 68324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver : this( new CommonTreeNodeStream( function.GetChild( 2 ) ), functionDefinitions ) 69324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver { 70324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver this.globalMemory = globalMemory; 71324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver localMemory[function.GetChild( 1 ).Text] = paramValue; 72324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver } 73324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver 74324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver /** Find matching function definition for a function name and parameter 75324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * value. The first definition is returned where (a) the name matches 76324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * and (b) the formal parameter agrees if it is defined as constant. 77324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver */ 78324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver private CommonTree findFunction( string name, BigInteger paramValue ) 79324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver { 80324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver foreach ( CommonTree f in functionDefinitions ) 81324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver { 82324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver // Expected tree for f: ^(FUNC ID (ID | INT) expr) 83324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver if ( f.GetChild( 0 ).Text.Equals( name ) ) 84324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver { 85324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver // Check whether parameter matches 86324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver CommonTree formalPar = (CommonTree)f.GetChild( 1 ); 87324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver if ( formalPar.Token.Type == INT 88324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver && !new BigInteger( formalPar.Token.Text ).Equals( paramValue ) ) 89324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver { 90324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver // Constant in formalPar list does not match actual value -> no match. 91324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver continue; 92324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver } 93324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver // Parameter (value for INT formal arg) as well as fct name agrees! 94324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver return f; 95324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver } 96324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver } 97324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver return null; 98324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver } 99324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver 100324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver /** Get value of name up call stack. */ 101324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver public BigInteger getValue( string name ) 102324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver { 103324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver BigInteger value; 104324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver if ( localMemory.TryGetValue( name, out value ) && value != null ) 105324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver { 106324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver return value; 107324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver } 108324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver if ( globalMemory.TryGetValue( name, out value ) && value != null ) 109324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver { 110324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver return value; 111324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver } 112324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver // not found in local memory or global memory 113324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver Console.Error.WriteLine( "undefined variable " + name ); 114324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver return new BigInteger( "0" ); 115324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver } 116324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver} 117