/// <summary> /// inserts symbols into the symboltable /// </summary> /// <param name="idRecordList"></param> /// <param name="typeRecord"></param> public void SymbolTableInsert(List<string> idRecordList, TypeRecord typeRecord) { Symbol symbol = null; int size = 0; foreach(string lexeme in idRecordList) { switch(typeRecord.symbolType) { case SymbolType.VariableSymbol: size = 1; //choose size based on variabletype symbol = new VariableSymbol(lexeme, typeRecord.symbolType, typeRecord.variableType, size, symbolTableStack.Peek().activationRecordSize); break; default: break; } symbolTableStack.Peek().activationRecordSize += size; symbolTableStack.Peek().Insert(symbol); } }
/// <summary> /// Parse FunctionHeading /// </summary> /// <param name="functionRecord"></param> private void FunctionHeading(MethodRecord functionRecord, ref IdentifierRecord identifierRecord) { TypeRecord typeRecord = new TypeRecord(SymbolType.FunctionSymbol, VariableType.Void); functionRecord.parameterList = new List<Parameter>(); switch (lookAheadToken.tag) { case Tags.MP_FUNCTION: UsedRules.WriteLine("18"); Match((int)Tags.MP_FUNCTION); Identifier(ref functionRecord); identifierRecord.lexeme = functionRecord.name; functionRecord.parameterList = OptionalFormalParameterList( functionRecord.parameterList); Type(ref typeRecord); functionRecord.returnType = typeRecord.variableType; analyzer.SymbolTableInsert(functionRecord); analyzer.ProcessId(ref identifierRecord); analyzer.CreateSymbolTable(functionRecord.name); //analyzer.ProcessMethod(identifierRecord, ref functionRecord); analyzer.SymbolTableInsert(functionRecord.parameterList); break; default: Error("Expecting FunctionHeading but found " + lookAheadToken.lexeme); break; } }
private List<Parameter> BuildParameterList(List<string> identifierList, TypeRecord variableType, IOMode ioMode,List<Parameter> parameterList) { foreach (string name in identifierList) { parameterList.Add(new Parameter(name,ioMode, variableType.variableType,1)); } return parameterList; }
/// <summary> /// Parse VariableParameterSection /// </summary> /// <param name="parameters"></param> private void VariableParameterSection(ref List<Parameter> parameters) { TypeRecord typeRecord = new TypeRecord(SymbolType.ParameterSymbol, VariableType.Void); List<string> identifierList = new List<string>(); switch (lookAheadToken.tag) { case Tags.MP_VAR: UsedRules.WriteLine("26"); Match((int)Tags.MP_VAR); IdentifierList(ref identifierList); Match(':'); Type(ref typeRecord); parameters = BuildParameterList(identifierList, typeRecord, IOMode.InOut,parameters); break; default: Error("Expecting VariableParameterSection but found " + lookAheadToken.lexeme); break; } }
/// <summary> /// Parse VariableDeclaration /// </summary> private void VariableDeclaration() { List<string> identifierRecordList = new List<string>(); TypeRecord typeRecord = new TypeRecord(SymbolType.VariableSymbol, VariableType.Void); switch(lookAheadToken.tag) { case Tags.MP_IDENTIFIER: UsedRules.WriteLine("9"); IdentifierList(ref identifierRecordList); Match(':'); Type(ref typeRecord); analyzer.SymbolTableInsert(identifierRecordList, typeRecord); break; default: Error("Expecting VariableDeclaration but found " + lookAheadToken.lexeme); break; } }
/// <summary> /// Parse Type /// </summary> /// <param name="typeRecord"></param> private void Type(ref TypeRecord typeRecord) { switch(lookAheadToken.tag) { case Tags.MP_INTEGER: // Integer UsedRules.WriteLine("10"); Match((int)Tags.MP_INTEGER); typeRecord.variableType = VariableType.Integer; break; case Tags.MP_FLOAT: // Float UsedRules.WriteLine("11"); Match((int)Tags.MP_FLOAT); typeRecord.variableType = VariableType.Float; break; default: Error("Expecting Type but found " + lookAheadToken.lexeme); break; } }