public static void Gen(string input, string output) { var types = Syntax.ParseTypes(File.ReadAllText(input)); StringBuilder sb = new StringBuilder(); for (int i = 0; i < types.Count; i++) { sb.AppendLine(); var type = types[i]; if (type.comments != null) { sb.AppendLine("// " + type.comments); } if (type.is_abstract) { sb.AppendLine("public abstract class " + type.name + " : " + type.basename); } else { sb.AppendLine("public class " + type.name + " : " + type.basename); } sb.AppendLine("{"); for (int j = 0; j < type.fields.Count; j++) { if (j > 0) { sb.AppendLine(); } var field = type.fields[j]; if (field.comments != null) { sb.AppendLine("\t// " + field.comments); } sb.AppendLine("\tpublic " + field.type + " m" + field.name + ";"); } sb.AppendLine(); sb.Append("\tpublic " + type.name + "("); var fs = GetAllFields(types, type); for (int j = 0; j < fs.Count; j++) { if (j > 0) { sb.Append(", "); } var field = fs[j]; sb.Append(field.type + " " + field.name); } sb.AppendLine(")"); if (fs.Count != type.fields.Count) { sb.Append("\t\t: base("); for (int j = 0; j < fs.Count - type.fields.Count; j++) { if (j > 0) { sb.Append(", "); } var field = fs[j]; sb.Append(field.name); } sb.AppendLine(")"); } sb.AppendLine("\t{"); for (int j = fs.Count - type.fields.Count; j < fs.Count; j++) { var field = fs[j]; sb.AppendLine("\t\tthis.m" + field.name + " = " + field.name + ";"); } sb.AppendLine("\t}"); if (!type.is_abstract) { sb.AppendLine(); sb.AppendLine("\tpublic override int Count => " + fs.Count + ";"); sb.AppendLine("\tpublic override SyntaxNode GetAt(int index)"); sb.AppendLine("\t{"); if (fs.Count > 0) { sb.AppendLine("\t\tswitch (index)"); sb.AppendLine("\t\t{"); for (int j = 0; j < fs.Count; j++) { var field = fs[j]; sb.AppendLine("\t\tcase " + j + ":"); sb.AppendLine("\t\t\treturn m" + field.name + ";"); } sb.AppendLine("\t\t}"); } sb.AppendLine("\t\treturn null;"); sb.AppendLine("\t}"); sb.AppendLine("\tpublic override void Accept(SyntaxVisitor visitor)"); sb.AppendLine("\t{"); sb.AppendLine("\t\tvisitor.Visit" + type.name + "(this);"); sb.AppendLine("\t}"); } sb.AppendLine("}"); } File.WriteAllText(output + "gen_Syntax.cs", sb.ToString()); sb.Clear(); sb.AppendLine("public abstract class SyntaxVisitor"); sb.AppendLine("{"); sb.AppendLine("\tpublic virtual void DefaultVisit(SyntaxNode value)"); sb.AppendLine("\t{"); sb.AppendLine("\t}"); for (int i = 0; i < types.Count; i++) { sb.AppendLine(); var type = types[i]; if (type.comments != null) { sb.AppendLine("\t// " + type.comments); } sb.AppendLine("\tpublic virtual void Visit" + type.name + "(" + type.name + " value)"); sb.AppendLine("\t{"); sb.AppendLine("\t\tDefaultVisit(value);"); sb.AppendLine("\t}"); } sb.AppendLine("}"); File.WriteAllText(output + "gen_SyntaxVisitor.cs", sb.ToString()); }