/// <summary> /// /// </summary> /// <param name="FunctionData"></param> public void ProcessFunctionCall(CFunction FunctionData) { int ParamCounter = 0; String CurrentParameter = ""; TTokens DummyData = TTokens.NONE; CVariable Parameter = null; m_LexScanner.Match(TTokens.IDENTIFIER); if (m_LexScanner.Match(TTokens.LPAREN) == false) throw new CCompilerException("( Expected for function calls.", m_LexScanner.LineNumber, TErrorCodes.PARSER_ERROR); /** process parameters **/ while (ParamCounter < FunctionData.ParameterCount) { // generate code similar to an assigment because // that is what is happening effectivly; AssignmentExpression(ref m_CurrentOperator); // assignment code. CurrentParameter = FunctionData.GetParameter(ParamCounter); Parameter = (CVariable) m_SymbolTable[CurrentParameter]; m_CodeGen.AddInstruction(m_MachineCodeOps.ByteOperations.MovWF((Byte)Parameter.MemoryAddress)); ++ParamCounter; if(ParamCounter < FunctionData.ParameterCount) if (m_LexScanner.Match(TTokens.COMMA) == false) throw new CCompilerException("Hold on a second, I am expecting a ,", m_LexScanner.LineNumber, TErrorCodes.PARSER_ERROR); } if (m_LexScanner.NextToken() == TTokens.RPAREN) { m_LexScanner.Match(TTokens.RPAREN); m_CodeGen.AddInstruction(m_MachineCodeOps.ControlOperations.Call((Byte)FunctionData.MemoryAddress)); } }
/// <summary> /// /// </summary> public void ParameterList(CFunction FunctionDefinition) { ParameterDeclaration(FunctionDefinition); while (m_LexScanner.NextToken() == TTokens.COMMA) { m_LexScanner.Match(TTokens.COMMA); ParameterDeclaration(FunctionDefinition); } }
/// <summary> /// FunctionDefinition = <TypeSpecifier> <Declarator> <BlockStatement> /// </summary> public void FunctionDefinition() { CFunction NewFunction = null; TDataType ReturnType = TypeSpecifier(); // the next valid token should be the name of function. if (m_LexScanner.NextToken() == TTokens.IDENTIFIER) m_CurrentFunction = m_LexScanner.TokenValue; m_CodeGen.AddInstruction(m_MachineCodeOps.ByteOperations.Nop()); NewFunction = new CFunction(m_CurrentFunction, (Byte)(m_CodeGen.CurrentCodeMemoryLocation)); NewFunction.DataType = ReturnType; Declarator(NewFunction); m_SymbolTable.Add(m_CurrentFunction, NewFunction); BlockStatement(); }
/// <summary> /// /// </summary> public void ParameterDeclaration(CFunction FunctionDefinition) { String Argument = ""; CVariable NewIdentifier = null; TypeSpecifier(); Argument = Declarator(FunctionDefinition); NewIdentifier = new CVariable(Argument, TDataType.INTEGER, m_LexScanner.ScopeLevelCount, 0); FunctionDefinition.AddParameter(Argument); m_SymbolTable.Add(Argument, NewIdentifier); }
/// <summary> /// /// </summary> public String Declarator(CFunction FunctionDeclaration) { String Identifer = m_LexScanner.TokenValue; if (m_LexScanner.Match(TTokens.IDENTIFIER) == false) throw new CCompilerException("Identifier Expected.", m_LexScanner.LineNumber, TErrorCodes.PARSER_ERROR); if (m_LexScanner.NextToken() == TTokens.LPAREN) { m_LexScanner.Match(TTokens.LPAREN); // the parameter list maybe empty. If the parameter list is // empty then the next valid token should be ) if(m_LexScanner.NextToken() != TTokens.RPAREN) ParameterList(FunctionDeclaration); if (m_LexScanner.Match(TTokens.RPAREN) == false) throw new CCompilerException(") Expected.", m_LexScanner.LineNumber, TErrorCodes.PARSER_ERROR); } return Identifer; }