public static void CompileFile(string directory, string inputFile) { // Create datapack Directory.CreateDirectory(directory + "generated"); try { // Open file string[] lines = File.ReadAllLines(inputFile); string file = ""; foreach (string line in lines) { file += line + "\n"; } IntoAST intast = new IntoAST(file); IntoAST.CompilationOfFiles.FileContents.CreateFunctionFolders(directory + "generated\\", IntoAST.nameSpace); intast.CreateFiles(directory + "generated\\"); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); Console.ReadKey(); return; } Console.Write("Finished Compilation"); }
// Parse into function public int Parse(string[] parts, int start) { // If doesn't start with keyword func if (parts[start] != "context") { throw new Exception("Context change starts with '" + parts[start] + "' not 'context'!"); } start++; // Get the change in context change = context + "execute "; // While hasn't reached code block start while (parts[start] != "{") { // Add change change += parts[start] + " "; start++; } change += "run "; // This is impossible if (parts[start] != "{") { throw new Exception("Expected { not " + parts[start]); } else { // Skip { start += 1; // Keep identifying expression until end of function declaration while (start < parts.Length) { if (parts[start] == "}") { return(start + 1); } // Identify from function context IntoAST.IdentifyResponse ir = IntoAST.Identify("function", change, parts, start); // Move to next statement and add function child to todo start = ir.next; if (ir.IChild is IChildFunctionDeclaration icfd) { todo.Add(icfd); } else { throw new Exception("Context child '" + ir.IChild + "' cannot follow context change '" + change + "'"); } } // Didn't find end of function throw new Exception("No } found for context change " + change); } }
// Parse into function public int Parse(string[] parts, int start) { // Set compilation time context string currContext = context; // If doesn't start with keyword func if (parts[start] != "func") { throw new Exception("Func starts with '" + parts[start] + "' not 'func'!"); } // If the name is already taken else if (Reserved.Exists(parts[start + 1], true)) { throw new Exception("Name " + parts[start + 1] + " already exists!"); } else if (parts[start + 2] != "{") { throw new Exception("Expected { not " + parts[start + 2]); } else { // Save function name funcName = parts[start + 1]; Reserved.fEx.Add(this); // Skip to first expression start += 3; // Keep identifying expression until end of function declaration while (start < parts.Length) { if (parts[start] == "}") { return(start + 1); } // Identify from function context IntoAST.IdentifyResponse ir = IntoAST.Identify("function", currContext, parts, start); // Move to next statement and add function child to todo start = ir.next; if (ir.IChild is IChildFunctionDeclaration icfd) { todo.Add(icfd); } else { throw new Exception("Function child '" + ir.IChild + "' cannot follow function '" + funcName + "'"); } } // Didn't find end of function throw new Exception("No } found for function " + funcName); } }
// Parses the variable assignation public int Parse(string[] parts, int start) { // Variable is not declared if (!Reserved.dEx.Exists(p => p.desigName == parts[start])) { throw new Exception("Desig '" + parts[start] + "' is not declared!"); } // The second part is not proper assigning operator else if (!desigOps.Contains(parts[start + 1])) { throw new Exception("Desig assignation has improper assigning operator '" + parts[start + 1] + "'!"); } // Get expression else { // Save operator op = parts[start + 1]; // Save desig desig = Reserved.dEx.Find(d => d.desigName == parts[start]); // Identify type IntoAST.IdentifyResponse ir = IntoAST.Identify("desig expression", context, parts, start + 2); // Check type if (ir.IChild is IChildDesigAssignation icda) { // Move to next start = ir.next; exp = icda; } else { throw new Exception("Invalid desig subexpression '" + ir.IChild + "'"); } // Return index after expression return(start); } }
// Parses the variable assignation public int Parse(string[] parts, int start) { // Variable is not declared if (!Reserved.vEx.Exists(p => p.varName == parts[start])) { throw new Exception("Variable '" + parts[start] + "' is not declared!"); } // The second part is not proper assigning operator else if (!assignOps.Contains(parts[start + 1])) { throw new Exception("Variable assignation has improper assigning operator\t\tFormat: [varname] [=, +=, -=, *=, /=, or %=] [const or literal integer] ;!"); } // Get expression else { // Save operator op = parts[start + 1]; // Save Variable var = Reserved.vEx.Find(v => v.varName == parts[start]); // Identify type IntoAST.IdentifyResponse ir = IntoAST.Identify("variable expression", context, parts, start + 2); // Check type if (ir.IChild is IChildVariableAssignation icva) { // Move to next start = ir.next; exp = icva; } else { throw new Exception("Invalid variable subexpression '" + ir.IChild + "'"); } // Return index after expression return(start); } }
static void Main(string[] args) { IntoAST.CompileFile("C:\\Users\\caleb\\source\\repos\\MCFCompiler\\bin\\Debug\\", "C:\\Users\\caleb\\source\\repos\\MCFCompiler\\input.txt"); Console.ReadKey(); }