private AST_VariableDeclaration declaration() { #if WRITE_DEBUG_INFO Console.WriteLine("declaration"); #endif Token typeName = match(Token.TokenType.BUILT_IN_TYPE_NAME); Token variableName = match(Token.TokenType.NAME); AST_VariableDeclaration declarationTree = new AST_VariableDeclaration( new Token(Token.TokenType.VAR_DECLARATION, "<VAR_DECL>", lookAhead(1).LineNr, lookAhead(1).LinePosition), ReturnValue.getReturnValueTypeFromString(typeName.getTokenString()), variableName.getTokenString()); return(declarationTree); }
private void varDeclaration(AST tree) { string typeName = tree.getChild(0).getTokenString(); ReturnValueType variableType = ReturnValue.getReturnValueTypeFromString(typeName); string variableName = tree.getChild(1).getTokenString(); switch (variableType) { case ReturnValueType.FLOAT: m_currentMemorySpace.setValue(variableName, new ReturnValue(0.0f)); break; case ReturnValueType.STRING: m_currentMemorySpace.setValue(variableName, new ReturnValue(0.0f)); break; default: throw new InvalidOperationException("Can't declare a variable of type " + typeName); } }
private AST parameter() { #if WRITE_DEBUG_INFO Console.WriteLine("parameter"); #endif AST parameterTree = new AST(new Token(Token.TokenType.PARAMETER, "<PARAMETER>", lookAhead(1).LineNr, lookAhead(1).LinePosition)); AST type = new AST(match(Token.TokenType.BUILT_IN_TYPE_NAME)); AST name = new AST(match(Token.TokenType.NAME)); AST declaration = new AST_VariableDeclaration(new Token(Token.TokenType.VAR_DECLARATION, "<PARAMETER_DECLARATION>"), ReturnValue.getReturnValueTypeFromString(type.getTokenString()), name.getTokenString()); AST assigment = new AST_Assignment(new Token(Token.TokenType.ASSIGNMENT, "="), name.getTokenString()); parameterTree.addChild(declaration); parameterTree.addChild(assigment); return(parameterTree); }
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 AST createParameterDefinition(string typeName, string variableName) { AST parameter = new AST(new Token(Token.TokenType.PARAMETER, "<PARAMETER>")); AST declaration = new AST_VariableDeclaration(new Token(Token.TokenType.VAR_DECLARATION, "<PARAMETER_DECLARATION>"), ReturnValue.getReturnValueTypeFromString(typeName), variableName); AST assigment = new AST_Assignment(new Token(Token.TokenType.ASSIGNMENT, "="), variableName); parameter.addChild(declaration); parameter.addChild(assigment); return(parameter); }