// Call this method to begin the semantic checking process public void CheckSemantics(AbstractNode node) { symTable = new SymTbl_Stack(); if (node == null) { return; } node.Accept(this); }
public void VisitNode(Assign node) { AbstractNode lhs = node.Child; AbstractNode rhs = lhs.Sib; rhs.Accept(this); LHSVisitor lhsVisitor = new LHSVisitor(); lhs.Accept(lhsVisitor); }
// *** You will need the following if you implement classes. //protected static ClassAttributes currentClass = null; //protected void SetCurrentClass(ClassAttributes c) //{ // currentClass = c; //} //protected ClassAttributes GetCurrentClass() //{ // return currentClass; //} // Call this method to begin the semantic checking process public void CheckSemantics(AbstractNode node) { if (node == null) { return; } TopDeclVisitor visitor = new TopDeclVisitor(); node.Accept(visitor); }
public virtual void VisitNode(Parameter node) { var typeVisitor = new TypeVisitor(); AbstractNode child = node.Child; child.Accept(typeVisitor); // to type of type specifier child.Sib.Accept(this); child.Sib.NodeType = CreateTypeDiscriptor(typeVisitor.type); table.enter(this.name, new TypeAttributes(child.Sib.NodeType)); }
// Call this method to begin the tree printing process public void PrintTree(AbstractNode node, string prefix = "") { if (node == null) { return; } Console.Write(prefix); node.Accept(this); VisitChildren(node, prefix + " "); }
public void VisitChildren(AbstractNode node) { AbstractNode child = node.Child; while (child != null) { child.Accept(this); child = child.Sib; } ; }
//check if allnames are in symbol table and add identifier if not public void VisitNode(QualifiedName node) { AbstractNode name = node.Child; while (name != null) { name.Accept(this); node.TypeRef = name.TypeRef; name = name.Sib; } }
public virtual void VisitChildren(AbstractNode node) { AbstractNode child = node.Child; while (child != null) { child.Accept(this); node.TypeRef = child.TypeRef; child = child.Sib; } ; }
// Call this method to begin the semantic checking process public void CheckSemantics(AbstractNode node) { if (node == null) { return; } TopDclVisitor tdv = new TopDclVisitor(); // Do Top-Declaration pass node.Accept(tdv); ///More here }
public virtual void VisitNode(LocalVariableDeclaration node) { var typeVisitor = new TypeVisitor(); var typeSpecifier = node.Child; typeSpecifier.Accept(typeVisitor); this.datatype = typeVisitor.type; AbstractNode child = node.Child.Sib; child.Accept(this); //table.PrintTable(); }
//define method attribute, increase scope, and check body public void VisitNode(MethodDeclaration node) { AbstractNode mods = node.Child; AbstractNode type = mods.Sib; AbstractNode name = type.Sib.Child; AbstractNode body = type.Sib.Sib; AbstractNode param = name.Sib; TypeVisitor typeVisitor = new TypeVisitor(); type.Accept(typeVisitor); MethodAttributes attr = new MethodAttributes(); attr.ReturnType = type.TypeRef; attr.TypeRef = type.TypeRef; attr.Mods = ((ModifierList)mods).list; attr.IsDefinedIn = GetCurrentClass(); table.enter(((Identifier)name).Name, attr); table.incrNestLevel(); attr.Signature = new SignatureTypeDescriptor(); if (param != null) { param.Accept(typeVisitor); ((SignatureTypeDescriptor)attr.Signature).Parameters = param; } name.AttributesRef = attr; node.AttributesRef = attr; MethodAttributes oldCurrentMethod = GetCurrentMethod(); SetCurrentMethod(attr); if (param != null) { param.Accept(this); } body.Accept(this); SetCurrentMethod(oldCurrentMethod); Console.WriteLine("Symbol table after Method " + ((Identifier)name).Name); table.PrintTable(); attr.Locals = table.decrNestLevel(); }
public virtual void VisitNode(AbstractNode node) { AbstractNode child = node.Child; TopDeclVisitor visitor = new TopDeclVisitor(); while (child != null) { child.Accept(visitor); node.TypeRef = child.TypeRef; child = child.Sib; } ; }
public virtual void VisitNode(LocalVariableNames node) { AbstractNode child = node.Child; while (child != null) { child.Accept(this); child.NodeType = CreateTypeDiscriptor(this.datatype); Console.WriteLine($"< In TopDeclVisitor.VisitNode for {node.ClassName()} and {this.name}>"); table.enter(this.name, new TypeAttributes(child.NodeType)); child = child.Sib; } ; }
// Call this method to begin the tree printing process public void PrintTree(AbstractNode node, string prefix = "") { if (node == null) { return; } bool isLastChild = (node.Sib == null); Console.ForegroundColor = ConsoleColor.Magenta; Console.Write(prefix); Console.Write(isLastChild ? "└─ " : "├─ "); Console.ResetColor(); node.Accept(this); VisitChildren(node, prefix + (isLastChild ? " " : "│ ")); }
public virtual void VisitChildren(AbstractNode node) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("< In SemanticsVisitor.VistChildren for " + node.ClassName() + " >"); Console.ResetColor(); AbstractNode child = node.Child; while (child != null) { child.Accept(this); child = child.Sib; } ; }
//Starting Node of an AST public void VisitNode(CompilationUnit node) { table.incrNestLevel(); table.BuildTree(); AbstractNode child = node.Child; TopDeclVisitor visitor = new TopDeclVisitor(); while (child != null) { child.Accept(visitor); node.TypeRef = child.TypeRef; child = child.Sib; } ; }
//Starting Node of an AST public void VisitNode(CompilationUnit node) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("< In SemanticsVisitor.VisitNode for " + node.ClassName() + " >"); Console.ResetColor(); TopDeclVisitor visitor = new TopDeclVisitor(); AbstractNode child = node.Child; while (child != null) { child.Accept(visitor); child = child.Sib; } ; }
//check lhs and rhs are the same and assign node type based on lhs or rhs type public void VisitNode(ArithmeticExpression node) { AbstractNode expr1 = node.Child; AbstractNode expr2 = expr1.Sib; expr1.Accept(this); expr2.Accept(this); if (Assignable(expr1.TypeRef, expr2.TypeRef)) { node.TypeRef = expr1.TypeRef; } else { Console.WriteLine("Right hand side expression is not assignable to left hand side"); node.TypeRef = new ErrorTypeDescriptor(); } }
public virtual void VisitNode(Expression node) { Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("<" + node.ClassName() + ", " + node.exprKind.ToString() + ">: "); Console.ResetColor(); var visitor = new TypeVisitor(); AbstractNode child = node.Child; while (child != null) { child.Accept(visitor); child = child.Sib; } ; if (node.Child.Sib == null) { node.NodeType = node.Child.NodeType; } else { if (node.Child.NodeType?.type != node.Child.Sib.NodeType?.type) { //CallError($"type mismatch {node.exprKind.ToString()} cannot perform on different datatype"); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"type mismatch {node.exprKind.ToString()} cannot perform on different datatype LHS: {node.Child.NodeType?.type} and RHS : {node.Child.Sib.NodeType?.type}"); Console.ResetColor(); } else { var boolExprList = new List <ExprKind>() { ExprKind.OP_GE, ExprKind.OP_GT, ExprKind.OP_LE, ExprKind.OP_LT, ExprKind.OP_EQ, ExprKind.OP_NE, ExprKind.OP_LAND, ExprKind.OP_LOR }; if (boolExprList.Contains(node.exprKind)) { node.NodeType = new BooleanTypeDescriptor(); } else { node.NodeType = node.Child.NodeType; } } } }
// Call this method to begin the semantic checking process public void GenCode(AbstractNode node, string filename) { string writeTo = pathName; writeTo += filename; writeTo = writeTo.Substring(0, writeTo.Length - 4); //remove the .txt this.asmbName = filename; writeTo += ".il"; this.outFile = new FileStream(writeTo, FileMode.Create); using (this.write = new StreamWriter(outFile)) { if (node == null) { return; } node.Accept(this); } }
//Visit Assign and type check to make sure rhs can be assigned to lhs public virtual void VisitNode(Assign node) { LHSSemanticVisitor lhsVisitor = new LHSSemanticVisitor(); AbstractNode targetName = node.Child; AbstractNode valueExpr = targetName.Sib; targetName.Accept(lhsVisitor); valueExpr.Accept(this); if (Assignable(targetName.TypeRef, valueExpr.TypeRef)) { node.TypeRef = targetName.TypeRef; } else { Console.WriteLine("Right hand side expression is not assignable to left hand side"); node.TypeRef = new ErrorTypeDescriptor(); } }
public void VisitNode(Block node) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("< In TopDeclVisitor.VisitNode for " + node.ClassName() + " >"); Console.ResetColor(); TopDeclVisitor visitor = new TopDeclVisitor(); table.incrNestLevel(); AbstractNode child = node.Child; while (child != null) { child.Accept(visitor); child = child.Sib; } ; table.decrNestLevel(); }
private bool allChildrenAreSame(Expr node) { { Utilities.TypeVisitAllChildren(node); AbstractNode child = node.Child; AbstractNode lastChild = node.Child; Boolean isOkay = true; // Child while (child != null) { if (child.attrRef != null) { // If no typeref, visit this node first if (child.attrRef.typeInfo == null) { TypeVisitor t = new TypeVisitor(); child.Accept(t); } // If we have a typeref, evaluate the node else { if (Utilities.attrToType(lastChild.attrRef) != Utilities.attrToType(child.attrRef)) { isOkay = false; lastChild = child; } // Do the same thing to child's child isOkay &= allChildrenAreInts(child); // Now that we're done, move on to sibling child = child.Sib; } } } return(isOkay); } }
// Call this method to begin the tree printing process public void PrintTree(AbstractNode node, string prefix = "") { if (node == null) { return; } Console.Write(prefix); node.Accept(this); if (node.TypeRef != null && node.TypeRef.GetType() != typeof(ASTBuilder.ErrorTypeDescriptor)) { node.TypeRef.PrintType(); } else if (node.AttributesRef != null && node.AttributesRef.TypeRef.GetType() != typeof(ASTBuilder.ErrorTypeDescriptor)) { node.AttributesRef.TypeRef.PrintType(); } else { Console.WriteLine(); } VisitChildren(node, prefix + " "); }
//check parameter and declare in Symbol table public void VisitNode(Parameter node) { AbstractNode typeName = node.Child; AbstractNode idName = typeName.Sib; TypeVisitor typeVisitor = new TypeVisitor(); typeName.Accept(typeVisitor); string name = ((Identifier)idName).Name; try { VariableAttributes attr = new VariableAttributes(null, typeName.TypeRef); idName.TypeRef = typeName.TypeRef; table.enter(name, attr); idName.AttributesRef = attr; } catch (FoundKeyException e) { Console.WriteLine(e.Message); node.TypeRef = new ErrorTypeDescriptor(); node.AttributesRef = null; } idName = idName.Sib; }
public void VisitNode(MethodCall node) { Identifier functionCall = (Identifier)node.Child.Child; AbstractNode list = node.Child.Sib; string s = ""; if (functionCall.TypeRef.GetType() == typeof(ASTBuilder.MSCorLibTypeDescriptor) && node.TypeRef.GetType() == typeof(ASTBuilder.StringTypeDescriptor)) { list.Accept(this); using (FileStream fs = File.Open(GlobalVar.PATH, FileMode.Append)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine("call " + functionCall.TypeRef.type + functionCall.Name + "(string)"); } } } else if (functionCall.TypeRef.GetType() == typeof(ASTBuilder.MSCorLibTypeDescriptor) && node.TypeRef.GetType() == typeof(ASTBuilder.IntegerTypeDescriptor)) { using (FileStream fs = File.Open(GlobalVar.PATH, FileMode.Append)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine("ldstr \"{0}\""); } } list.Accept(this); using (FileStream fs = File.Open(GlobalVar.PATH, FileMode.Append)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine("box int32"); sw.WriteLine("call void [mscorlib]System.Console::WriteLine(string, object) "); } } } else { while (list != null) { list.Accept(this); list = list.Sib; } s += "call " + functionCall.TypeRef.type + " " + functionCall.Name + "("; list = node.Child.Sib; while (list != null) { s += list.TypeRef.type; list = list.Sib; if (list != null) { s += ", "; } } s += ")"; using (FileStream fs = File.Open(GlobalVar.PATH, FileMode.Append)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine(s); } } } }
public void VisitNode(MethodDeclaration node) { stlocCount = 0; Identifier name = (Identifier)node.Child.Sib.Sib.Child; AbstractNode body = node.Child.Sib.Sib.Sib; MethodAttributes attr = (MethodAttributes)node.AttributesRef; List <string> mods = attr.Mods; AbstractNode param = null; if (((SignatureTypeDescriptor)attr.Signature).Parameters != null) { param = ((SignatureTypeDescriptor)attr.Signature).Parameters.Child; } using (FileStream fs = File.Open(GlobalVar.PATH, FileMode.Append)) { using (StreamWriter sw = new StreamWriter(fs)) { string s = ".method "; if (mods.Contains("PUBLIC")) { s += "public "; } if (mods.Contains("PRIVATE")) { s += "private "; } if (mods.Contains("STATIC")) { s += "static "; } s += attr.ReturnType.type + " "; s += name.Name; s += " ("; if (param != null) { while (param != null) { AbstractNode type = param.Child; Identifier paramName = (Identifier)type.Sib; s += type.TypeRef.type; param = param.Sib; if (param != null) { s += ", "; } } } s += ")"; sw.WriteLine(s + "\n{"); if (name.Name == "main") { sw.WriteLine(".entrypoint"); } sw.WriteLine(".maxstack 100"); if (((SignatureTypeDescriptor)attr.Signature).Parameters != null) { param = ((SignatureTypeDescriptor)attr.Signature).Parameters.Child; while (param != null) { param.Accept(this); param = param.Sib; } } } } MethodBodyVisitor visitor = new MethodBodyVisitor(); body.Accept(visitor); using (FileStream fs = File.Open(GlobalVar.PATH, FileMode.Append)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine("\nret\n}"); } } }