//main public override void InAMain(AMain node) { // build def for allowed types BasicTypeDefinition intType; intType = new BasicTypeDefinition(); intType.name = "int"; BasicTypeDefinition fltType; fltType = new BasicTypeDefinition(); fltType.name = "float"; BasicTypeDefinition boolType; boolType = new BasicTypeDefinition(); boolType.name = "boolean"; StringTypeDefinition stringType = new StringTypeDefinition(); stringType.name = "string"; // create and seed symbol tables _currentSymbolTable = new Dictionary <string, Definition>(); _currentSymbolTable.Add("int", intType); _currentSymbolTable.Add("float", fltType); _currentSymbolTable.Add("boolean", boolType); _currentSymbolTable.Add("string", stringType); }
//or expr1 public override void OutAOrExpr1(AOrExpr1 node) { Definition lhs, rhs; // check if both sides are decorated if (!_decoratedParseTree.TryGetValue(node.GetExpr1(), out lhs)) { Console.WriteLine("[" + node.GetOr().Line + "] : left hand side of 'or' was not decorated."); // Ensure rhs of the plus is decorated } else if (!_decoratedParseTree.TryGetValue(node.GetExpr2(), out rhs)) { Console.WriteLine("[" + node.GetOr().Line + "] : right hand side of 'or' was not decorated."); // check if both sides are bool types } else if (!lhs.name.Equals("boolean") && !rhs.name.Equals("boolean")) { Console.WriteLine("[" + node.GetOr().Line + "] : Type mismatch. Cannot or " + lhs.name + " to " + rhs.name + " because one isnt a boolean."); // decorate } else { TypeDefinition currNodeType = new BasicTypeDefinition(); currNodeType.name = lhs.name; _decoratedParseTree.Add(node, currNodeType); } }
//in method public override void InAMethod(AMethod node) { // save current symbol table _previousSymbolTables.AddFirst(_currentSymbolTable); // build new param list _currentParamList = new List <VariableDefinition>(); // build def allowed by types according to grammar BasicTypeDefinition intType; intType = new BasicTypeDefinition(); intType.name = "int"; BasicTypeDefinition fltType; fltType = new BasicTypeDefinition(); fltType.name = "float"; BasicTypeDefinition boolType; boolType = new BasicTypeDefinition(); boolType.name = "boolean"; StringTypeDefinition stringType = new StringTypeDefinition(); stringType.name = "string"; // create and seed symbol table _currentSymbolTable = new Dictionary <string, Definition>(); _currentSymbolTable.Add("int", intType); _currentSymbolTable.Add("float", fltType); _currentSymbolTable.Add("boolean", boolType); _currentSymbolTable.Add("string", stringType); }
//subtract expr6 public override void OutASubtractExpr6(ASubtractExpr6 node) { Definition lhs, rhs; // check if both sides are decorated // Ensure lhs of the plus is decorated if (!_decoratedParseTree.TryGetValue(node.GetExpr6(), out lhs)) { Console.WriteLine("[" + node.GetNegative().Line + "] : left hand side of '-' was not decorated."); } // Ensure rhs of the plus is decorated else if (!_decoratedParseTree.TryGetValue(node.GetExpr7(), out rhs)) { Console.WriteLine("[" + node.GetNegative().Line + "] : right hand side of '-' was not decorated."); } // check if both sides are equal types else if (!lhs.name.Equals(rhs.name)) { Console.WriteLine("[" + node.GetNegative().Line + "] : Type mismatch. Cannot subtract " + lhs.name + " to " + rhs.name + "."); } // check if left hand type is valid else if (!(lhs is BasicTypeDefinition)) { Console.WriteLine("[" + node.GetNegative().Line + "] : Invalid Type. Cannot subtract " + lhs.name + "s."); } // decorate else { TypeDefinition currNodeType = new BasicTypeDefinition(); currNodeType.name = lhs.name; _decoratedParseTree.Add(node, currNodeType); } }
public override void InAFunctionFunctionDeclaration(AFunctionFunctionDeclaration node) { // Save current symbol table. _previousSymbolTables.AddFirst(_currentSymbolTable); // Build definitions for allowed types according to grammar. CS426 only allows int and string and float BasicTypeDefinition intType; intType = new BasicTypeDefinition(); intType.name = "int"; StringTypeDefinition stringType = new StringTypeDefinition(); stringType.name = "string"; BasicTypeDefinition floatType; floatType = new BasicTypeDefinition(); floatType.name = "float"; // Create and seed the symbol table. if (!_functionSymbolTable.ContainsKey("int")) { _functionSymbolTable.Add("int", intType); _functionSymbolTable.Add("string", stringType); _functionSymbolTable.Add("float", floatType); } }
public override void OutAFltOperand(AFltOperand node) { // decorate this node BasicTypeDefinition intDef = new BasicTypeDefinition(); intDef.name = "flt"; _decoratedParseTree.Add(node, intDef); }
public override void OutAStringExpression(AStringExpression node) { // Decorate this node BasicTypeDefinition stringDef = new BasicTypeDefinition(); stringDef.name = "string"; _decoratedParseTree.Add(node, stringDef); }
public override void OutAFloatExpression(AFloatExpression node) { // Decorate this node BasicTypeDefinition floatDef = new BasicTypeDefinition(); floatDef.name = "float"; _decoratedParseTree.Add(node, floatDef); }
// Override functions for constant variables - global scope public override void OutAIntExpression(AIntExpression node) { // Decorate this node BasicTypeDefinition intDef = new BasicTypeDefinition(); intDef.name = "int"; _decoratedParseTree.Add(node, intDef); }
public override void InAMainProgram(AMainProgram node) { // Build definitions for allowed types according to grammar. CS246 grammar only allows int, string, and float BasicTypeDefinition intType; intType = new BasicTypeDefinition(); intType.name = "int"; StringTypeDefinition stringType = new StringTypeDefinition(); stringType.name = "string"; BasicTypeDefinition floatType = new BasicTypeDefinition(); floatType.name = "float"; // Create and seed the symbolTable _currentSymbolTable = new Dictionary <string, Definition>(); _currentSymbolTable.Add("int", intType); _currentSymbolTable.Add("string", stringType); _currentSymbolTable.Add("float", floatType); }
public override void OutAMultiMultiExpr(AMultiMultiExpr node) { Definition lhs, rhs; // Ensure lhs of the plus is decorated if (!_decoratedParseTree.TryGetValue(node.GetMultiExpr(), out lhs)) { Console.WriteLine("[" + node.GetMult().Line + "] : left hand side of '*' was not decorated."); // Ensure rhs of the plus is decorated } else if (!_decoratedParseTree.TryGetValue(node.GetMultiExpr(), out rhs)) { Console.WriteLine("[" + node.GetMult().Line + "] : right hand side of '*' was not decorated."); // Ensure sides are the same type } else if (!lhs.name.Equals(rhs.name)) { Console.WriteLine("[" + node.GetMult().Line + "] : Type mismatch. Cannot multiply " + lhs.name + " to " + rhs.name + "."); // Ensure that lhs and rhs are basic types } else if (!(lhs is BasicTypeDefinition)) { Console.WriteLine("[" + node.GetMult().Line + "] : Invalid Type. Cannot multiply " + lhs.name + "s."); // Decorate this node } else { TypeDefinition currNodeType = new BasicTypeDefinition(); currNodeType.name = lhs.name; _decoratedParseTree.Add(node, currNodeType); } }
// Condition check public override void OutACondCondition(ACondCondition node) { Definition lhs, rhs; // Ensure lhs of the plus is decorated if (!_decoratedParseTree.TryGetValue(node.GetFirst(), out lhs)) { Console.WriteLine("[" + node.GetOperator() + "] : left hand side of operator was not decorated."); // Ensure rhs of the plus is decorated } else if (!_decoratedParseTree.TryGetValue(node.GetAdditionExpr(), out rhs)) { Console.WriteLine("[" + node.GetOperator() + "] : right hand side of operator was not decorated."); // Ensure sides are the same type } else if (!lhs.name.Equals(rhs.name)) { Console.WriteLine("[" + node.GetOperator() + "] : Type mismatch. Cannot compare " + lhs.name + " with " + rhs.name + "."); // Ensure that lhs and rhs are basic types } else if (!(lhs is BasicTypeDefinition)) { Console.WriteLine("[" + node.GetOperator() + "] : Invalid Type. Cannot add " + lhs.name + "s."); // Decorate this node } else { TypeDefinition currNodeType = new BasicTypeDefinition(); currNodeType.name = lhs.name; _decoratedParseTree.Add(node, currNodeType); } }
//not equal expr4 public override void OutANotequalExpr4(ANotequalExpr4 node) { Definition leftExpr4Def, rightExpr5Def; if (!_decoratedParseTree.TryGetValue(node.GetExpr4(), out leftExpr4Def)) { Console.WriteLine("[" + node.GetNot().Line + node.GetEqualityoperator().Line + "] : left side was not decorated."); // Ensure lhs of the plus is decorated } else if (!_decoratedParseTree.TryGetValue(node.GetExpr5(), out rightExpr5Def)) { Console.WriteLine("[" + node.GetNot().Line + node.GetEqualityoperator().Line + "] : right side was not decorated."); // Ensure rhs of the plus is decorated } else if (!(leftExpr4Def is BasicTypeDefinition)) { Console.WriteLine("[" + node.GetNot().Line + node.GetEqualityoperator().Line + "] : Invalid Type. Cannot compare " + leftExpr4Def.name + "s."); // Check if the left hand side is a valid type } else if (!leftExpr4Def.name.Equals(rightExpr5Def.name)) { Console.WriteLine("[" + node.GetNot().Line + node.GetEqualityoperator().Line + "] : Type mismatch. Cannot compare " + leftExpr4Def.name + " to " + rightExpr5Def.name + "."); // Check if types are equal } else { TypeDefinition currNodeType = new BasicTypeDefinition(); currNodeType.name = "boolean"; _decoratedParseTree.Add(node, currNodeType); } }