public MemorySpace(string name, AST root, Scope scope, MemorySpaceNodeListCache cache) { Debug.Assert(name != null); Debug.Assert(root != null); Debug.Assert(scope != null); Debug.Assert(cache != null); m_name = name; m_scope = scope; m_cache = cache; //Console.WriteLine("Creating list of nodes from tree: " + root.getTreeAsString()); if(m_cache.hasCachedFunction(root)) { //Console.WriteLine("Found cached list for " + m_name); m_nodes = m_cache.getList(root).ToArray(); } else { List<AST> list = new List<AST>(); addToList(list, root); m_cache.addMemorySpaceList(list, root); m_nodes = list.ToArray(); //Console.WriteLine("Created new list for " + m_name); } m_currentNode = -1; //Console.WriteLine("New memory space " + name + " has got " + list.Count + " AST nodes in its list."); }
public Interpreter(AST ast, Scope globalScope, ErrorHandler errorHandler, ExternalFunctionCreator externalFunctionCreator) { m_ast = ast; m_errorHandler = errorHandler; m_globalScope = globalScope; m_currentScope = m_globalScope; m_externalFunctionCreator = externalFunctionCreator; }
public void setScope(Scope scope) { #if DEBUG if (scope == null) { throw new Exception ("can't set m_scope to null for IfNode at line " + getToken ().LineNr); } #endif m_scope = scope; }
public Scope(ScopeType scopeType, string name, Scope enclosingScope) { Debug.Assert(name != null && name != ""); Debug.Assert(enclosingScope != null); m_scopeType = scopeType; m_name = name; m_enclosingScope = enclosingScope; }
public FunctionSymbol(Scope enclosingScope, string name, ReturnValueType type, AST functionDefinitionNode) : base(Scope.ScopeType.FUNCTION_SCOPE, name, enclosingScope) { Debug.Assert(enclosingScope != null); Debug.Assert(functionDefinitionNode != null); m_enclosingScope = enclosingScope; m_functionDefinitionNode = functionDefinitionNode; m_returnValueType = type; }
public void process() { m_globalScope = new Scope(Scope.ScopeType.MAIN_SCOPE, "global scope"); m_currentScope = m_globalScope; #if WRITE_DEBUG_INFO Console.WriteLine("Evaluate scope declarations:"); #endif evaluateScopeDeclarations(m_ast); #if WRITE_DEBUG_INFO Console.WriteLine("\nEvaluate references:"); #endif evaluateReferences(m_ast); }
private void evaluateFunctionScope(AST tree) { // Define function name ReturnValueType returnType = ReturnValue.getReturnValueTypeFromString(tree.getChild(0).getTokenString()); string functionName = tree.getChild(1).getTokenString(); Symbol functionScope = new FunctionSymbol(m_currentScope, functionName, returnType, tree); m_globalScope.define(functionScope); // all functions are saved in the global scope m_currentScope = (Scope)functionScope; AST_FunctionDefinitionNode functionCallNode = (AST_FunctionDefinitionNode)(tree); functionCallNode.setScope((Scope)functionScope); #if WRITE_DEBUG_INFO Console.WriteLine("\nDefined function with name " + functionName + " and return type " + returnType); #endif // Process the body of the function evaluateScopeDeclarations(tree.getChild(3)); m_currentScope = m_currentScope.getEnclosingScope(); // pop scope }
public void Delete() { m_name = ""; m_valuesForStrings = null; m_nodes = null; m_currentNode = -1; m_scope = null; m_cache = null; }
private void ifThenElse(AST tree) { // Push scope AST_IfNode ifNode = (AST_IfNode)(tree); Assert.IsNotNull(ifNode); m_currentScope = (Scope)ifNode.getScope(); Assert.IsNotNull(m_currentScope); // Evaluate conditional ReturnValue conditionalExpression = execute(tree.getChild(0)); if (conditionalExpression.FloatValue != 0) { Assert.IsNotNull(tree.getChild(1)); execute(tree.getChild(1)); } else { if (tree.getChildren().Count == 3) { Assert.IsNotNull(tree.getChild(2)); execute(tree.getChild(2)); } } // Pop scope m_currentScope = (Scope)ifNode.getScope().getEnclosingScope(); Assert.IsNotNull(m_currentScope); }
private void evaluateReferencesForFUNC_DECLARATION(AST tree) { string functionName = tree.getChild(1).getTokenString(); m_currentScope = (Scope)m_currentScope.resolve(functionName); // push the scope with the function #if WRITE_DEBUG_INFO Console.WriteLine("\n Trying to resolve function parameters and body of " + functionName); #endif evaluateReferencesInAllChildren(tree.getChild(2)); // parameters evaluateReferencesInAllChildren(tree.getChild(3)); // function body m_currentScope = m_currentScope.getEnclosingScope(); // pop scope }
private ReturnValue function(AST tree, List <ReturnValue> parameterValues) { // Push scope Scope m_previousScope = m_currentScope; AST_FunctionDefinitionNode functionDefinitionNode = (AST_FunctionDefinitionNode)(tree); Assert.IsNotNull(functionDefinitionNode); m_currentScope = (Scope)functionDefinitionNode.getScope(); Assert.IsNotNull(m_currentScope); // Push memory space MemorySpace m_previousMemorySpace = m_currentMemorySpace; MemorySpace functionMemorySpace = new MemorySpace("<FUNCTION_SPACE " + tree.getChild(1).getTokenString() + ">"); m_memoryStack.Push(functionMemorySpace); m_currentMemorySpace = functionMemorySpace; // Add parameters to memory space List <AST> parameterDeclarations = tree.getChild(2).getChildren(); if (parameterDeclarations != null) { if (parameterDeclarations.Count != parameterValues.Count) { m_errorHandler.errorOccured( "The number of arguments in function " + tree.getChild(1).getTokenString() + " does not match!", Error.ErrorType.SYNTAX); } foreach (AST parameter in parameterDeclarations) { varDeclaration(parameter); } } // Assign values to parameters if (parameterValues != null) { int i = 0; foreach (ReturnValue parameterValue in parameterValues) { string parameterName = parameterDeclarations[i].getChild(1).getTokenString(); assignValue(parameterName, parameterValue); i++; } } // Execute function ReturnValue returnValue = null; try { executeAllChildNodes(tree.getChild(3)); // child 3 is the function body } catch (ReturnValue functionReturnValue) { returnValue = functionReturnValue; } // Pop memory space m_memoryStack.Pop(); m_currentMemorySpace = m_previousMemorySpace; // Pop scope m_currentScope = m_previousScope; Assert.IsNotNull(m_currentScope); return(returnValue); }
private void evaluateIfScope(AST tree) { Scope subscope = new Scope(Scope.ScopeType.IF_SCOPE,"<IF-SUBSCOPE>", m_currentScope); #if WRITE_DEBUG_INFO Console.WriteLine("\nDefined IF-subscope"); #endif m_currentScope = subscope; AST_IfNode ifNode = (tree as AST_IfNode); Debug.Assert(ifNode != null); ifNode.setScope(subscope); // save the new scope in the IF-token tree node // Evaluate expression evaluateScopeDeclarationsInAllChildren(tree.getChild(0)); AST trueNode = ifNode.getChild(1); AST falseNode = null; if (ifNode.getChildren().Count == 3) { falseNode = ifNode.getChild(2); } evaluateScopeDeclarationsInAllChildren(trueNode); if (falseNode != null) { evaluateScopeDeclarationsInAllChildren(falseNode); } m_currentScope = m_currentScope.getEnclosingScope(); // pop scope }
private void evaluateReferencesForLOOP_BLOCK(AST tree) { AST_LoopBlockNode loopBlockNode = tree as AST_LoopBlockNode; m_currentScope = loopBlockNode.getScope(); #if WRITE_DEBUG_INFO Console.WriteLine("\n Trying to resolve body of loop block"); #endif evaluateReferencesInAllChildren(tree); m_currentScope = m_currentScope.getEnclosingScope(); // pop scope }
private void evaluateReferencesForIF(AST tree) { AST_IfNode ifNode = (AST_IfNode)(tree); m_currentScope = (Scope)ifNode.getScope(); // push IF-subscope evaluateReferencesInAllChildren(tree); m_currentScope = m_currentScope.getEnclosingScope(); // pop scope }
public void setScope(Scope scope) { m_scope = scope; }
private ReturnValue function(AST tree, List<ReturnValue> parameterValues) { // Push scope Scope m_previousScope = m_currentScope; AST_FunctionDefinitionNode functionDefinitionNode = (AST_FunctionDefinitionNode)(tree); Assert.IsNotNull(functionDefinitionNode); m_currentScope = (Scope)functionDefinitionNode.getScope(); Assert.IsNotNull(m_currentScope); // Push memory space MemorySpace m_previousMemorySpace = m_currentMemorySpace; MemorySpace functionMemorySpace = new MemorySpace("<FUNCTION_SPACE " + tree.getChild(1).getTokenString() + ">"); m_memoryStack.Push(functionMemorySpace); m_currentMemorySpace = functionMemorySpace; // Add parameters to memory space List<AST> parameterDeclarations = tree.getChild(2).getChildren(); if(parameterDeclarations != null) { if(parameterDeclarations.Count != parameterValues.Count) { m_errorHandler.errorOccured( "The number of arguments in function " + tree.getChild(1).getTokenString() + " does not match!", Error.ErrorType.SYNTAX); } foreach(AST parameter in parameterDeclarations) { varDeclaration(parameter); } } // Assign values to parameters if(parameterValues != null) { int i = 0; foreach(ReturnValue parameterValue in parameterValues) { string parameterName = parameterDeclarations[i].getChild(1).getTokenString(); assignValue(parameterName, parameterValue); i++; } } // Execute function ReturnValue returnValue = null; try { executeAllChildNodes(tree.getChild(3)); // child 3 is the function body } catch(ReturnValue functionReturnValue) { returnValue = functionReturnValue; } // Pop memory space m_memoryStack.Pop(); m_currentMemorySpace = m_previousMemorySpace; // Pop scope m_currentScope = m_previousScope; Assert.IsNotNull(m_currentScope); return returnValue; }
private void evaluateLoopScope(AST tree) { Scope subscope = new Scope(Scope.ScopeType.LOOP_SCOPE, "<LOOP-SUBSCOPE>", m_currentScope); m_currentScope = subscope; #if WRITE_DEBUG_INFO Console.WriteLine("\nDefined LOOP-subscope"); #endif AST_LoopNode loopNode = (tree as AST_LoopNode); Debug.Assert(loopNode != null); evaluateScopeDeclarationsInAllChildren(loopNode); loopNode.setScope(m_currentScope); m_currentScope = m_currentScope.getEnclosingScope(); // pop scope }