/// <summary> /// Sets the program to execute function. /// Returns true if the program had the function. /// </summary> public ProgramFunctionCallStatus SetProgramToExecuteFunction(string functionName, object[] args) { //Console.WriteLine ("Will execute '" + functionName + "' in global scope '" + m_globalScope + "'"); FunctionSymbol functionSymbol = (FunctionSymbol)m_globalScope.resolve(functionName); //Console.WriteLine("Found function symbol: " + functionSymbol.ToString()); if (functionSymbol == null) { return(ProgramFunctionCallStatus.NO_FUNCTION); } if (IsFunctionExternal(functionName)) { CallExternalFunction(functionName, args); return(ProgramFunctionCallStatus.EXTERNAL_FUNCTION); } else { AST_FunctionDefinitionNode functionDefinitionNode = (AST_FunctionDefinitionNode)functionSymbol.getFunctionDefinitionNode(); if (functionDefinitionNode != null) { var parameters = functionDefinitionNode.getChild(2).getChildren(); int nrOfParameters = parameters.Count; if (nrOfParameters != args.Length) { throw new Error("The function " + functionName + " takes " + nrOfParameters + " arguments, not " + args.Length); } Reset(); m_topLevelDepth = 1; m_currentScope = functionDefinitionNode.getScope(); m_currentScope.ClearMemorySpaces(); string nameOfNewMemorySpace = functionName + "_memorySpace" + functionCounter++; PushNewScope(functionDefinitionNode.getScope(), nameOfNewMemorySpace, functionDefinitionNode); for (int i = args.Length - 1; i >= 0; i--) { var declaration = parameters[i].getChild(0) as AST_VariableDeclaration; object convertedValue = ReturnValueConversions.ChangeTypeBasedOnReturnValueType(args[i], declaration.Type); PushValue(convertedValue); // reverse order } //Console.WriteLine ("Ready to start running function '" + functionName + "' with memory space '" + nameOfNewMemorySpace + "'"); } else { throw new Error(functionName + " has got no function definition node!"); } } return(ProgramFunctionCallStatus.NORMAL_FUNCTION); // all went well (starting the function) }
private AST getFunctionTreeNode(string functionName) { FunctionSymbol funcSym = (FunctionSymbol)m_globalScope.resolve(functionName); if (funcSym == null) { throw new InvalidOperationException("Can't find function with name " + functionName); } return(funcSym.getFunctionDefinitionNode()); }
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 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 }
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 }