internal static List <AbstractNode> GetMethodBodyChildren(AbstractNode node, CodeGenVisitor c) { // return children that are Assignment Expressions and also ones that are Method Calls List <AbstractNode> retVal = new List <AbstractNode>(); AbstractNode an; an = node.Child; while (an != null) { if (an.GetType() == typeof(AssignmentExpr) || an.GetType() == typeof(MethodCall) || an.GetType() == typeof(SelectionStmt) || an.GetType() == typeof(Stmt)) { retVal.Add(an); } if (an.GetType() == node.GetType()) { retVal.AddRange(GetMethodBodyChildren(an, c)); } if (an.Sib != an) { an = an.Sib; } } return(retVal); }
public static void CodeGenVisitAllChildren(AbstractNode node, CodeGenVisitor c) { AbstractNode an; an = node.Child; while (an != null) { an.Accept(c); if (an.Sib != an) { an = an.Sib; } } }
private void CreateIlFile() { Console.WriteLine("\nCreating .il file\n"); if (File.Exists(_filename + ".il")) { File.Delete(_filename + ".il"); } using (StreamWriter sw = File.CreateText(_filename + ".il")) { //CodeGenVisitor codeGenVisitor = new CodeGenVisitor(System.Console.Out); CodeGenVisitor codeGenVisitor = new CodeGenVisitor(sw); codeGenVisitor.CreateIlFileContent(CurrentSemanticValue); } }
internal static void VisitLoopBodyChildren(Stmt node, CodeGenVisitor codeGenVisitor) { // Get local var decls // Find the localvardeclsandstmts child Utilities.VisitSpecificChild(node, typeof(LocalVarDeclsAndStmts), codeGenVisitor); // And visit all of its children // Get list of children that are not variable declarations (only assignment expr and method calls currently supported) List <AbstractNode> loopBodyChidlren = Utilities.GetMethodBodyChildren(node, codeGenVisitor); // Process them in reverse order for (int i = loopBodyChidlren.Count - 1; i >= 0; i--) { // filter out statement for us if (loopBodyChidlren[i].Name != null && !loopBodyChidlren[i].Name.Contains(("Iteration"))) { loopBodyChidlren[i].Accept(codeGenVisitor); } } // }
internal static void VisitThenChildren(SelectionStmt node, CodeGenVisitor codeGenVisitor) { AbstractNode an; an = node.Child; while (an != null) { if (an.Name == null || !an.Name.Contains("Else")) { if (an.GetType() != typeof(Expr)) { an.Accept(codeGenVisitor); } } if (an.Sib != an) { an = an.Sib; } } }
// Visits first occurence of child internal static void VisitSpecificChild(AbstractNode node, Type type, CodeGenVisitor codeGenVisitor) { AbstractNode an; an = node.Child; while (an != null) { if (an.NodeType == type) { an.Accept(codeGenVisitor); break; } else { VisitSpecificChild(an, type, codeGenVisitor); } if (an.Sib != an) { an = an.Sib; } } }
public void GenCode() { // Create file string ilFileName = filename.Split('.')[0] + ".il"; if (File.Exists(ilFileName)) { File.Delete(ilFileName); } using (FileStream fs = File.Create(ilFileName)) { CodeGenVisitor visitor = new CodeGenVisitor(); Console.WriteLine("Starting code generation"); // Write file header StringBuilder sb = new StringBuilder(); Byte[] info; sb.AppendLine(String.Format("//-----------------------------------------------------------------", ilFileName, filename)); sb.AppendLine(String.Format("//IL Header - Lenny Bogdanov\n//File: {0} \n//Source File: {1} \n//Date: {2} {3}", ilFileName, filename, System.DateTime.Now.ToLongDateString(), System.DateTime.Now.ToLongTimeString())); sb.AppendLine(String.Format("//-----------------------------------------------------------------", ilFileName, filename)); sb.AppendLine(String.Format("\n", ilFileName, filename)); sb.AppendLine(String.Format(".assembly extern mscorlib {{}}")); sb.AppendLine(String.Format(".assembly a{{}} //Not sure why this was necessary for me to do", ilFileName, filename)); sb.AppendLine(String.Format("\n", ilFileName, filename)); info = new UTF8Encoding(true).GetBytes(sb.ToString()); fs.Write(info, 0, info.Length); // Visit AST // This is how we'll give the code gen reader access to the file we've opened currentFileStream = fs; CurrentSemanticValue.Accept(visitor); fs.Close(); } Console.WriteLine(filename + " -> " + filename.Split('.')[0] + ".il"); }
public void GenerateCode(string filename) { CodeGenVisitor visitor = new CodeGenVisitor(); Console.WriteLine("Starting code generation"); visitor.GenCode(CurrentSemanticValue, filename); }