public void ChangeGlobalVariableInitValue(string pName, object pobject) { bool foundVariable = false; AST statementListTree = m_ast.getChild(0); AST globalVarDefs = statementListTree.getChild(0); if (globalVarDefs.getTokenString() != "<GLOBAL_VARIABLE_DEFINITIONS_LIST>") { throw new Exception("Wrong node, " + globalVarDefs.getTokenString()); } if (globalVarDefs.getChildren() != null && globalVarDefs.getChildren().Count > 0) { foreach (AST defAndAssignmentNode in globalVarDefs.getChildren()) { AST_Assignment assigmentNode = (AST_Assignment)defAndAssignmentNode.getChild(1); if (assigmentNode.VariableName == pName) { defAndAssignmentNode.removeChild(1); defAndAssignmentNode.addChild(CreateAssignmentTreeFromInitValue(pName, pobject)); foundVariable = true; break; } } } if (!foundVariable) { throw new Exception("Couldn't find and change the variable " + pName); } }
private void evaluateReferencesInAllChildren(AST tree) { if (tree.getChildren() != null) { // Go through all other subtrees foreach (AST subtree in tree.getChildren()) { evaluateReferences(subtree); } } }
private void executeAllChildNodes(AST tree) { if (tree.getChildren() == null) { return; } foreach (AST childTree in tree.getChildren()) { execute(childTree); } }
private bool IsLastChild(AST pNode, AST pParent) { if (pParent == null) { return(true); } if (pParent.getChildren().IndexOf(pNode) == pParent.getChildren().Count - 1) { return(true); } return(false); }
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 evaluateScopeDeclarations(AST tree) { Debug.Assert(tree != null); if (tree.getTokenType() == Token.TokenType.FUNC_DECLARATION) { evaluateFunctionScope(tree); } else if (tree.getTokenType() == Token.TokenType.IF) { evaluateIfScope(tree); } else if (tree.getTokenType() == Token.TokenType.LOOP) { evaluateLoopScope(tree); } else if (tree.getTokenType() == Token.TokenType.LOOP_BLOCK) { evaluateLoopBlockScope(tree); } else if (tree.getChildren() != null) { evaluateScopeDeclarationsInAllChildren(tree); } }
private void evaluateScopeDeclarationsInAllChildren(AST tree) { foreach (AST subtree in tree.getChildren()) { evaluateScopeDeclarations(subtree); } }
private string BuildRow(AST pLeaf, AST pRoot) { currentNode = pLeaf; StringBuilder resultString = new StringBuilder(); if (currentNode == _root) { PrependString(resultString, GetNodeString(currentNode)); } else if (currentNode.getChildren().Count <= 0) { #if CANT_HANDLE_FANCY_CHARS PrependString(resultString, "-------- " + GetNodeString(currentNode)); #else PrependString(resultString, "──────── " + GetNodeString(currentNode)); #endif } else #if CANT_HANDLE_FANCY_CHARS { PrependString(resultString, "-------- " + GetNodeString(currentNode)); } #else { PrependString(resultString, "──────┬─ " + GetNodeString(currentNode)); } #endif if (currentNodeIsLastChild) { #if CANT_HANDLE_FANCY_CHARS PrependString(resultString, " |"); #else PrependString(resultString, " └"); #endif } else { #if CANT_HANDLE_FANCY_CHARS PrependString(resultString, " |"); #else PrependString(resultString, " ├"); #endif } currentNode = currentParentNode; while (currentParentNode != null) { if (currentNodeIsLastChild) { PrependString(resultString, " "); } else { #if CANT_HANDLE_FANCY_CHARS PrependString(resultString, " |"); #else PrependString(resultString, " │"); #endif } currentNode = currentParentNode; } return(resultString.ToString()); }
private ReturnValue functionCall(AST tree) { ReturnValue returnValue = null; if (m_externalFunctionCreator.externalFunctions.ContainsKey(tree.getTokenString())) { ExternalFunctionCreator.OnFunctionCall functionCall = m_externalFunctionCreator.externalFunctions[tree.getTokenString()]; if (functionCall != null) { ReturnValue[] parameters = new ReturnValue[tree.getChildren().Count]; int i = 0; foreach (AST parameter in tree.getChildren()) { parameters[i] = execute(parameter); i++; } returnValue = functionCall(parameters); } else { throw new Error("Can't find external function " + tree.getTokenString(), Error.ErrorType.UNDEFINED, tree.getToken().LineNr, tree.getToken().LinePosition); } } else { // Call user defined function string functionName = tree.getTokenString(); AST functionTree = getFunctionTreeNode(functionName); Assert.IsNotNull(functionTree); // Create list of parameter values List <ReturnValue> parameterValues = new List <ReturnValue>(); List <AST> functionCallChildNodes = tree.getChildren(); if (functionCallChildNodes != null) { foreach (AST parameter in tree.getChildren()) { ReturnValue val = execute(parameter); parameterValues.Add(val); } } returnValue = function(functionTree, parameterValues); } return(returnValue); }
private void BuildNodeList(AST pCurrentList, List <AST> pList) { pList.Add(pCurrentList); foreach (AST a in pCurrentList.getChildren()) { BuildNodeList(a, pList); } }
private void addChildren(List <AST> list, AST ast) { List <AST> children = ast.getChildren(); if (children != null) { foreach (AST child in children) { addToList(list, child); } } }
private void evaluateReferencesForFUNCTION_CALL(AST tree) { // Function name: string functionName = tree.getTokenString(); var sym = m_currentScope.resolve(functionName); FunctionSymbol function = sym as FunctionSymbol; if (function == null) { m_errorHandler.errorOccured("Can't find function with name " + functionName, Error.ErrorType.SCOPE, tree.getToken().LineNr, tree.getToken().LinePosition ); } else { #if WRITE_DEBUG_INFO Console.WriteLine("Resolved function call with name " + functionName + " (on line " + tree.getToken().LineNr + ")"); #endif // Parameters evaluateReferencesInAllChildren(tree); AST node = function.getFunctionDefinitionNode(); AST_FunctionDefinitionNode functionDefinitionTree = (AST_FunctionDefinitionNode)(node); /*if(functionDefinitionTree.getTokenString() != "<EXTERNAL_FUNC_DECLARATION>") { * evaluateReferencesForFUNC_DECLARATION(functionDefinitionTree); * }*/ // Setup reference to Function Definition AST node AST_FunctionCall functionCallAst = tree as AST_FunctionCall; Debug.Assert(functionCallAst != null); functionCallAst.FunctionDefinitionRef = functionDefinitionTree; List <AST> calleeParameterList = functionDefinitionTree.getChild(2).getChildren(); // Check that the number of arguments is right AST callerParameterList = tree.getChild(0); List <AST> arguments = callerParameterList.getChildren(); if (arguments.Count != calleeParameterList.Count) { m_errorHandler.errorOccured( "Wrong nr of arguments to '" + functionDefinitionTree.getChild(1).getTokenString() + "' , expected " + calleeParameterList.Count + " but got " + arguments.Count , Error.ErrorType.SYNTAX, tree.getToken().LineNr, tree.getToken().LinePosition); } } }
private void BuildNodeList(AST pCurrentList, List <AST> pList) { if (pCurrentList == null) { Console.WriteLine("Can't paint node, will return"); return; } pList.Add(pCurrentList); foreach (AST a in pCurrentList.getChildren()) { BuildNodeList(a, pList); } }
private void GetLeafNodes(AST pCurrentNode, List<AST> pList) { List<AST> tmpList = pCurrentNode.getChildren(); if (tmpList == null || tmpList.Count <= 0) { pList.Add(pCurrentNode); } else { foreach (AST child in tmpList) { GetLeafNodes(child, pList); } } }
private void GetLeafNodes(AST pCurrentNode, List <AST> pList) { List <AST> tmpList = pCurrentNode.getChildren(); if (tmpList == null || tmpList.Count <= 0) { pList.Add(pCurrentNode); } else { foreach (AST child in tmpList) { GetLeafNodes(child, pList); } } }
private void returnStatement(AST tree) { ReturnValue returnValue = new ReturnValue(); if (tree.getChildren().Count > 0) { returnValue = execute(tree.getChild(0)); } if (returnValue != null) { #if WRITE_DEBUG_INFO Console.Write("Return value was: "); printReturnValue(returnValue); #endif throw returnValue; } }
private bool IsLastChild(AST pNode, AST pParent) { if (pParent == null) return true; if (pParent.getChildren().IndexOf(pNode) == pParent.getChildren().Count - 1) return true; return false; }
private void BuildNodeList(AST pCurrentList, List<AST> pList) { pList.Add(pCurrentList); foreach(AST a in pCurrentList.getChildren()) { BuildNodeList(a, pList); } }
private void executeAllChildNodes(AST tree) { if(tree.getChildren() == null) { return; } foreach (AST childTree in tree.getChildren()) { execute(childTree); } }
private ReturnValue functionCall(AST tree) { ReturnValue returnValue = null; if (m_externalFunctionCreator.externalFunctions.ContainsKey(tree.getTokenString())) { ExternalFunctionCreator.OnFunctionCall functionCall = m_externalFunctionCreator.externalFunctions[tree.getTokenString()]; if (functionCall != null) { ReturnValue[] parameters = new ReturnValue[tree.getChildren().Count]; int i = 0; foreach (AST parameter in tree.getChildren()) { parameters[i] = execute(parameter); i++; } returnValue = functionCall(parameters); } else { throw new Error("Can't find external function " + tree.getTokenString(), Error.ErrorType.UNDEFINED, tree.getToken().LineNr, tree.getToken().LinePosition); } } else { // Call user defined function string functionName = tree.getTokenString(); AST functionTree = getFunctionTreeNode(functionName); Assert.IsNotNull(functionTree); // Create list of parameter values List<ReturnValue> parameterValues = new List<ReturnValue>(); List<AST> functionCallChildNodes = tree.getChildren(); if (functionCallChildNodes != null) { foreach(AST parameter in tree.getChildren()) { ReturnValue val = execute(parameter); parameterValues.Add(val); } } returnValue = function(functionTree, parameterValues); } return returnValue; }
private void addChildren(List<AST> list, AST ast) { List<AST> children = ast.getChildren(); if (children != null) { foreach (AST child in children) { addToList(list, child); } } }
private void returnStatement(AST tree) { ReturnValue returnValue = new ReturnValue(); if (tree.getChildren().Count > 0) { returnValue = execute(tree.getChild(0)); } if(returnValue != null) { #if WRITE_DEBUG_INFO Console.Write("Return value was: "); printReturnValue(returnValue); #endif throw returnValue; } }
private string BuildRow(AST pLeaf, AST pRoot) { currentNode = pLeaf; StringBuilder resultString = new StringBuilder(); if (currentNode == _root) { PrependString(resultString, GetNodeString(currentNode) ); } else if (currentNode.getChildren().Count <= 0) { #if CANT_HANDLE_FANCY_CHARS PrependString(resultString, "-------- " + GetNodeString(currentNode) ); #else PrependString(resultString, "──────── " + GetNodeString(currentNode) ); #endif } else #if CANT_HANDLE_FANCY_CHARS PrependString(resultString, "-------- " + GetNodeString(currentNode) ); #else PrependString(resultString, "──────┬─ " + GetNodeString(currentNode) ); #endif if (currentNodeIsLastChild) { #if CANT_HANDLE_FANCY_CHARS PrependString(resultString, " |"); #else PrependString(resultString, " └"); #endif } else { #if CANT_HANDLE_FANCY_CHARS PrependString(resultString, " |"); #else PrependString(resultString, " ├"); #endif } currentNode = currentParentNode; while (currentParentNode != null) { if (currentNodeIsLastChild) { PrependString(resultString, " "); } else { #if CANT_HANDLE_FANCY_CHARS PrependString(resultString, " |"); #else PrependString(resultString, " │"); #endif } currentNode = currentParentNode; } return resultString.ToString(); }