public override List <string> ExecuteSemanticAction(Stack <SemanticRecord> semanticRecordTable, Stack <SymbolTable> symbolTable, IToken lastToken) { Variable variable = new Variable(); List <string> errors = new List <string>(); while (semanticRecordTable.Peek().recordType == RecordTypes.IdName || semanticRecordTable.Peek().recordType == RecordTypes.TypeName || semanticRecordTable.Peek().recordType == RecordTypes.Size) { SemanticRecord topRecord = semanticRecordTable.Pop(); switch (topRecord.recordType) { case RecordTypes.Size: variable.AddDimension(int.Parse(topRecord.getValue())); break; case RecordTypes.IdName: variable.SetName(topRecord.getValue()); break; case RecordTypes.TypeName: variable.SetType(topRecord.getType()); SemanticRecord variableRecord = new SemanticRecord(variable); semanticRecordTable.Push(variableRecord); break; default: // This should only occur if the grammar is not valid errors.Add("Grammar error, parsed rule that placed unexpected character on semantic stack"); break; } } return(errors); }
public override List <string> ExecuteSemanticAction(Stack <SemanticRecord> semanticRecordTable, Stack <SymbolTable> symbolTable, IToken lastToken, MoonCodeResult moonCode) { SymbolTable currentTable = symbolTable.Peek(); // Get the last created variable SemanticRecord variableRecord = semanticRecordTable.Pop(); Entry variableEntry = new VarParamEntry(currentTable, variableRecord.getVariable(), EntryKinds.variable); // Only declare variables for functions (not class member variables) //if(currentTable.getParent() is FunctionEntry) { int size = variableRecord.getVariable().GetSize(); string address = variableEntry.getAddress(); if (size <= 0) { moonCode.AddGlobal(string.Format("% {0} res 0", address)); } else if (size == 4) { moonCode.AddGlobal(string.Format("{0} dw 0", address)); } else { moonCode.AddGlobal(string.Format("{0} res {1}{2}align", address, size, Environment.NewLine)); } } return(new List <string>()); }
public override List <string> ExecuteSemanticAction(Stack <SemanticRecord> semanticRecordTable, Stack <SymbolTable> symbolTable, IToken lastToken, MoonCodeResult moonCode) { SemanticRecord top = semanticRecordTable.Pop(); List <string> errors = new List <string>(); LinkedList <ExpressionRecord> expressionParameters = new LinkedList <ExpressionRecord>(); // Collect parameter expressions until we hit the function's name while (top.recordType != RecordTypes.IdNameReference) { if (top.recordType != RecordTypes.ExpressionType) { errors.Add(string.Format("Grammar error at {0}. Migrating a function call should only count parameters, found {1}", lastToken.getLine(), top.recordType.ToString())); } else { expressionParameters.AddFirst((ExpressionRecord)top); } top = semanticRecordTable.Pop(); } // Create the new semantic record semanticRecordTable.Push(new FunctionCallRecord(top.getValue(), expressionParameters)); return(errors); }
public override List <string> ExecuteSemanticAction(Stack <SemanticRecord> semanticRecordTable, Stack <SymbolTable> symbolTable, IToken lastToken) { SymbolTable currentTable = symbolTable.Peek(); // Get the last created variable SemanticRecord variableRecord = semanticRecordTable.Pop(); Entry variableEntry = new VarParamEntry(currentTable, variableRecord.getVariable(), EntryKinds.variable); return(new List <string>()); }
public override List <string> ExecuteSemanticAction(Stack <SemanticRecord> semanticRecordTable, Stack <SymbolTable> symbolTable, IToken lastToken, MoonCodeResult moonCode) { LinkedList <ExpressionRecord> expressions = new LinkedList <ExpressionRecord>(); List <string> errors = new List <string>(); // Accumulate the last two expressions on the semantic stack while (expressions.Count < 2) { if (!semanticRecordTable.Any()) { errors.Add(string.Format("Grammar error: Not enough expressions to validate assignment at line {0}", lastToken.getLine())); return(errors); } SemanticRecord lastRecord = semanticRecordTable.Pop(); if (lastRecord.recordType != RecordTypes.ExpressionType) { errors.Add(string.Format("Grammar error, expression validation at line {0} encountered an illegal record: \"{1}\"", lastToken.getLine(), lastRecord.recordType.ToString())); continue; } expressions.AddLast((ExpressionRecord)lastRecord); } // The first expression is the value we are reading // while the second one is the expression we are writing to ExpressionRecord type1 = expressions.First.Value; ExpressionRecord type2 = expressions.Last.Value; if (type1.GetExpressionType() != type2.GetExpressionType()) { errors.Add(string.Format("Cannot equate at line {0} a value of type {1} to {2}", lastToken.getLine(), type1.GetExpressionType().getName(), type2.GetExpressionType().getName())); } SymbolTable currentScope = symbolTable.Peek(); string outAddress = string.Empty; if (currentScope.getParent() == null) { errors.Add(string.Format("Cannot perform an assignment operation outside of a function")); } else { // Generate the code to read and store the value moonCode.AddLine(currentScope.getParent().getAddress(), string.Format(@" lw r2, {0}(r0) sw {1}(r0), r2 ", type1.GetAddress(), type2.GetAddress())); } return(errors); }
public override List <string> ExecuteSemanticAction(Stack <SemanticRecord> semanticRecordTable, Stack <SymbolTable> symbolTable, IToken lastToken, MoonCodeResult moonCode) { SymbolTable currentTable = symbolTable.Peek(); LinkedList <Variable> foundParameters = new LinkedList <Variable>(); bool entryCreated = false; string funcName = string.Empty; List <string> errors = new List <string>(); // Iterate over the semantic stack and consume records relevant to this function while (!entryCreated) { SemanticRecord topRecord = semanticRecordTable.Pop(); // Handle the record depending on its type switch (topRecord.recordType) { case RecordTypes.Variable: // Encountered paramenters foundParameters.AddFirst(topRecord.getVariable()); break; case RecordTypes.IdName: // The name of this function funcName = topRecord.getValue(); break; case RecordTypes.TypeName: // If we encounter a type we are done collecting and can create the entry FunctionEntry funcEntry = new FunctionEntry(currentTable, funcName, topRecord.getType()); funcEntry.AddParameters(foundParameters); // Push the function's scope to the stack of symbol tables symbolTable.Push(funcEntry.getChild()); entryCreated = true; break; default: // This should only fail if there is an error in the grammar. errors.Add("Grammar error, parsed rule that placed unexpected character on semantic stack"); break; } } return(errors); }
public override List <string> ExecuteSemanticAction(Stack <SemanticRecord> semanticRecordTable, Stack <SymbolTable> symbolTable, IToken lastToken, MoonCodeResult moonCode) { int sizeCount = 0; List <string> errors = new List <string>(); if (!semanticRecordTable.Any()) { errors.Add(string.Format("Grammar error at line {0}: could not migrate variable for emtpy stack", lastToken.getLine())); return(errors); } SemanticRecord top = semanticRecordTable.Pop(); // Accumulate indices until we hit the variable's name while (top.recordType != RecordTypes.IdNameReference) { // Count the number of indices sizeCount++; if (top.recordType != RecordTypes.IndiceCount) { errors.Add(string.Format("Grammar error at {0}. Migrating a variable reference should only count indecies, found {1}", lastToken.getLine(), top.recordType.ToString())); sizeCount--; } if (!semanticRecordTable.Any()) { errors.Add(string.Format("Grammar error at {0}. Could not find Id name for variable", lastToken.getLine())); break; } top = semanticRecordTable.Pop(); } // Create the new semantic record semanticRecordTable.Push(new VariableReferenceRecord(top.getValue(), sizeCount)); return(errors); }
public override List <string> ExecuteSemanticAction(Stack <SemanticRecord> semanticRecordTable, Stack <SymbolTable> symbolTable, IToken lastToken, MoonCodeResult moonCode) { Stack <SemanticRecord> callChain = new Stack <SemanticRecord>(); List <string> errors = new List <string>(); if (!semanticRecordTable.Any()) { errors.Add(string.Format("Grammar error at line {0}: could not verify factor for emtpy stack", lastToken.getLine())); return(errors); } SemanticRecord lastRecord = semanticRecordTable.Pop(); // Accumulate semantic records until we reach the start of the factor while (lastRecord.recordType != RecordTypes.FactorStart) { callChain.Push(lastRecord); if (!semanticRecordTable.Any()) { errors.Add(string.Format("Grammar error at {0}. Could not find start for a factor", lastToken.getLine())); break; } lastRecord = semanticRecordTable.Pop(); } SemanticRecord currentLink = callChain.Pop(); Entry linkedVariable = null; // The first element in the chain is a reference to a locally accessible variable or function // Go through symbol tables of increasing scope until we find the referenced identifier foreach (SymbolTable table in symbolTable) { linkedVariable = table.GetEntries().FirstOrDefault(x => (x is VarParamEntry || x is FunctionEntry) && x.getName() == currentLink.getValue()); if (linkedVariable != null) { break; } } // Verify the validity of this initial reference bool success = VerifyLink(currentLink, linkedVariable, lastToken, errors); // Go through the call chain and verify each link while (callChain.Any() && success) { currentLink = callChain.Pop(); ClassEntry referredClass = ((VarParamEntry)linkedVariable).getVariable().getClass(); if (referredClass.getChild() == null) { errors.Add(string.Format("Identifier {0} cannot be reached at line {1}", currentLink.getValue(), lastToken.getLine())); return(errors); } // Look in the referred class for the variable or function that is being referred to linkedVariable = referredClass.getChild().GetEntries().FirstOrDefault(x => (x is VarParamEntry || x is FunctionEntry) && x.getName() == currentLink.getValue()); success = VerifyLink(currentLink, linkedVariable, lastToken, errors); } ClassEntry expressionType = new ClassEntry("undefined", 0); // Find the linked variable and create a new expression for it if (linkedVariable is VarParamEntry) { expressionType = ((VarParamEntry)linkedVariable).getVariable().getClass(); } else if (linkedVariable is FunctionEntry) { expressionType = ((FunctionEntry)linkedVariable).GetReturnType(); } if (linkedVariable == null) { errors.Add(string.Format("Grammar error: Could not link variable at line {0}", lastToken.getLine())); } else { semanticRecordTable.Push(new ExpressionRecord(expressionType, linkedVariable.getAddress())); } return(errors); }
// A helper function to verify that a reference to a variable or function // as the correct indicies or parameters private bool VerifyLink(SemanticRecord currentLink, Entry linkedVariable, IToken lastToken, List <string> errors) { if (linkedVariable == null) { errors.Add(string.Format("Undefined identifier {0} at line {1}", currentLink.getValue(), lastToken.getLine())); return(false); } if (linkedVariable is VarParamEntry && currentLink is FunctionCallRecord) { errors.Add(string.Format("Identifier {0} at line {1} is referring to a variable like a function", currentLink.getValue(), lastToken.getLine())); return(false); } else if (linkedVariable is FunctionEntry && currentLink is VariableReferenceRecord) { errors.Add(string.Format("Identifier {0} at line {1} is referring to a function like a variable", currentLink.getValue(), lastToken.getLine())); return(false); } // Verify that a variable is being accessed with the correct number of indices if (currentLink is VariableReferenceRecord) { if (((VarParamEntry)linkedVariable).getVariable().GetDimensions().Count != ((VariableReferenceRecord)currentLink).getDimensions()) { errors.Add(string.Format("Identifier {0} at line {1} does not have the correct number of indices. Counted {2} expected {3}" , currentLink.getValue(), lastToken.getLine(), ((VariableReferenceRecord)currentLink).getDimensions(), ((VarParamEntry)linkedVariable).getVariable().GetDimensions().Count)); return(false); } return(true); } else if (currentLink is FunctionCallRecord) { var parameters = GetParameters(linkedVariable as FunctionEntry); // Verify the number of paramters if (parameters.Count() != ((FunctionCallRecord)currentLink).GetParameterCount()) { errors.Add(string.Format("Function call {0} at line {1} does not have the correct number of parameters. Counted {2} expected {3}" , currentLink.getValue(), lastToken.getLine(), ((FunctionCallRecord)currentLink).GetParameterCount(), parameters.Count())); return(false); } else { var parameterComparers = parameters.Zip(((FunctionCallRecord)currentLink).GetParameters(), (f, a) => new { aparam = a, fparam = (VarParamEntry)f }); // Verify that the parameters are of the correct type foreach (var parameterCompare in parameterComparers) { if (parameterCompare.aparam.GetExpressionType() != parameterCompare.fparam.getVariable().getClass()) { errors.Add(string.Format("Cannot convert {0} to {1} in parameter at line {2}" , parameterCompare.aparam.GetExpressionType().getName(), parameterCompare.fparam.getVariable().getClass().getName(), lastToken.getLine())); } } } return(true); } else { errors.Add("Grammar error: Illegal record on semantic stack"); return(false); } }
public override List <string> ExecuteSemanticAction(Stack <SemanticRecord> semanticRecordTable, Stack <SymbolTable> symbolTable, IToken lastToken, MoonCodeResult moonCode) { Token op = null; LinkedList <ExpressionRecord> expressions = new LinkedList <ExpressionRecord>(); List <string> errors = new List <string>(); // Accumulate the two last expressions on the semantic stack while (expressions.Count < 2) { if (!semanticRecordTable.Any()) { errors.Add(string.Format("Grammar error: Not enough expressions to validate arithmetic operation at line {0}", lastToken.getLine())); return(errors); } SemanticRecord record = semanticRecordTable.Pop(); BasicTokenRecord tokenRec = record as BasicTokenRecord; if (tokenRec != null) { // Save the token representing the operation op = tokenRec.getToken(); continue; } if (record is ExpressionRecord) { expressions.AddLast(record as ExpressionRecord); } else { errors.Add(string.Format("Grammar error: record {0} at line {1} is not allowed during an arithmetic expression validation", record.recordType.ToString(), lastToken.getLine())); } } ExpressionRecord type1 = expressions.Last.Value; ExpressionRecord type2 = expressions.First.Value; // Ensure that both expressions are integers if (type1.GetExpressionType() != AddTypeToList.intClass || type2.GetExpressionType() != AddTypeToList.intClass) { errors.Add(string.Format("Cannot perform arithmetic operation at line {0} between factors of type {1} and {2}" , lastToken.getLine(), type1.GetExpressionType().getName(), type2.GetExpressionType().getName())); } SymbolTable currentScope = symbolTable.Peek(); string outAddress = string.Empty; if (currentScope.getParent() == null) { errors.Add(string.Format("Cannot perform an arithemetic operation outside of a function")); } else if (op == TokenList.And || op == TokenList.Or) { // Generate an address for the result of this sub-expression outAddress = Entry.MakeAddressForEntry(currentScope.getParent(), "arithmExpr"); // Get a unique id for the jump label string jumpId = IDGenerator.GetNext(); // Generate the code to perform a logic "and" or "or" instead of bitwise moonCode.AddGlobal(string.Format("{0} dw 0", outAddress)); moonCode.AddLine(currentScope.getParent().getAddress(), string.Format(@" lw r3, {0}(r0) lw r4, {1}(r0) {2} r2, r3, r4 bz r2, zero_{4} addi r2, r0, 1 sw {3}(r0), r2 j endop_{4} zero_{4} sw {3}(r0), r0 endop_{4} ", type1.GetAddress(), type2.GetAddress(), opLists[op], outAddress, jumpId)); } else { // Generate an address for the result of this sub-expression outAddress = Entry.MakeAddressForEntry(currentScope.getParent(), "arithmExpr"); // Generate code for a mathematical or relational expression moonCode.AddGlobal(string.Format("{0} dw 0", outAddress)); moonCode.AddLine(currentScope.getParent().getAddress(), string.Format(@" lw r3, {0}(r0) lw r4, {1}(r0) {2} r2, r3, r4 sw {3}(r0), r2 ", type1.GetAddress(), type2.GetAddress(), opLists[op], outAddress)); } // Place the resulting expression on the semantic stack semanticRecordTable.Push(new ExpressionRecord(AddTypeToList.intClass, outAddress)); return(errors); }