public static void Main(string[] args) { string gfile; string ifile; if (args.Length == 0) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "All stuff|*.*"; dlg.ShowDialog(); gfile = dlg.FileName; if (gfile.Trim().Length == 0) { return; } dlg.ShowDialog(); ifile = dlg.FileName; if (ifile.Trim().Length == 0) { return; } dlg.Dispose(); } else { gfile = args[0]; ifile = args[1]; } var root = Compiler.compile(gfile, ifile); dumpTree("tree.dot", root); Console.Read(); }
public static void Main(string[] args) { string srcfile = "xyz.txt"; string asmfile = "xyz.asm"; string objfile = "xyz.o"; string exefile = "xyz.exe"; var inputfile = "inputs.txt"; Console.WriteLine("Working directory: " + Environment.CurrentDirectory); Console.WriteLine("Reading inputs from " + inputfile); using (var sr = new StreamReader(inputfile)) { string txt = sr.ReadToEnd(); foreach (var testcase1 in txt.Split(new string[] { "//-" }, StringSplitOptions.RemoveEmptyEntries)) { var testcase = testcase1.Trim(); if (testcase.Length == 0) { continue; } using (var sw = new StreamWriter(srcfile, false)) { sw.Write(testcase); } int i = testcase.IndexOf("//returns "); string expectedReturn = testcase.Substring(i + 10).Split('\n')[0].Trim(); string expectedOutput = ""; var rex = new Regex(@"output is ""([^\n]*)"""); Match m = rex.Match(testcase); if (m != null) { expectedOutput = m.Groups[1].ToString().Replace("\\n", "\n"); } //testcase.Substring(i + 10).Split('\n')[0].Trim().Replace("\\n", "\n"); //each array has: filename, expected contents, actual contents var expectedFiles = new List <string[]>(); rex = new Regex(@"//output file (\S+) has ""([^\n]+)"""); foreach (Match mm in rex.Matches(testcase)) { string fname = mm.Groups[1].ToString(); string fcontents = mm.Groups[2].ToString().Replace("\\n", "\n"); expectedFiles.Add(new string[] { fname, fcontents, "" }); } string expectedInput = ""; rex = new Regex(@"//input is ""([^\n]+)"""); m = rex.Match(testcase); if (m != null) { expectedInput = m.Groups[1].ToString().Trim().Replace("\\n", "\n"); } ExitStatus exitStatus = ExitStatus.UNKNOWN; if (expectedReturn == "failure") { try { Compiler.compile(srcfile, asmfile, objfile, exefile); } catch (Exception) { exitStatus = ExitStatus.DID_NOT_COMPILE; } } else { try { Compiler.compile(srcfile, asmfile, objfile, exefile); } catch (Exception e) { Console.WriteLine("Did not compile: " + e.Message); exitStatus = ExitStatus.DID_NOT_COMPILE; throw; } } string stdout = ""; string stderr = ""; int exitcode = -1; if (exitStatus != ExitStatus.DID_NOT_COMPILE) { var si = new ProcessStartInfo(); si.FileName = exefile; si.UseShellExecute = false; si.RedirectStandardInput = true; si.RedirectStandardOutput = true; si.RedirectStandardError = true; using (var proc = Process.Start(si)) { proc.StandardInput.Write(expectedInput); proc.StandardInput.Flush(); var stdoutData = proc.StandardOutput.ReadToEndAsync(); var stderrData = proc.StandardError.ReadToEndAsync(); proc.WaitForExit(1000); if (!proc.HasExited) { proc.Kill(); proc.WaitForExit(); exitStatus = ExitStatus.INFINITE_LOOP; exitcode = -1; } else { exitcode = proc.ExitCode; exitStatus = ExitStatus.NORMAL; } stdout = stdoutData.Result.Replace("\r\n", "\n"); stderr = stderrData.Result; } } bool ok; if (expectedReturn == "failure") { ok = (exitStatus == ExitStatus.DID_NOT_COMPILE); } else if (expectedReturn == "infinite") { ok = (exitStatus == ExitStatus.INFINITE_LOOP); } else { if (exitStatus != ExitStatus.NORMAL) { ok = false; } else { if (expectedReturn == "nonzero") { ok = (exitcode != 0); } else { ok = (exitcode == Convert.ToInt32(expectedReturn)); } } } if (expectedOutput != "" && expectedOutput != stdout) { ok = false; } foreach (var t in expectedFiles) { try { using (StreamReader rdr = new StreamReader(t[0])) { t[2] = rdr.ReadToEnd().Replace("\r\n", "\n"); if (t[2] != t[1]) { ok = false; } } } catch (IOException) { ok = false; } } if (ok) { Console.WriteLine("OK!"); } else { Console.WriteLine(testcase); if (exitStatus == ExitStatus.DID_NOT_COMPILE) { Console.WriteLine("Error: Did not compile but it should have"); } else if (exitStatus == ExitStatus.INFINITE_LOOP) { Console.WriteLine("Infinite loop detected"); } else { Console.WriteLine("-----------------"); Console.WriteLine("Error: Expectation mismatch"); if (expectedReturn != "" + exitcode) { Console.WriteLine("Expected return code from main():"); Console.WriteLine(expectedReturn); Console.WriteLine("Actual return code from main():"); Console.WriteLine(exitcode); } if (expectedOutput != "" && expectedOutput != stdout) { Console.WriteLine("Expected program output: "); Console.WriteLine(expectedOutput); Console.WriteLine("Actual program output:"); Console.WriteLine(stdout); Console.WriteLine(stderr); } foreach (var t in expectedFiles) { if (t[1] != t[2]) { Console.WriteLine("Expected contents of file " + t[0]); Console.WriteLine(t[1]); Console.WriteLine("Actual contents of file " + t[0]); Console.WriteLine(t[2]); } } Console.WriteLine("-----------------"); } Console.ReadLine(); Environment.Exit(1); } } } Console.WriteLine("All OK!"); Console.ReadLine(); }
public static void Main(string[] args) { string srcfile = "xyz.txt"; string asmfile = "xyz.asm"; string objfile = "xyz.o"; string exefile = "xyz.exe"; var inputfile = "inputs.txt"; Console.WriteLine("Working directory: " + Environment.CurrentDirectory); Console.WriteLine("Reading inputs from " + inputfile); using (var sr = new StreamReader(inputfile)) { string txt = sr.ReadToEnd(); foreach (var testcase1 in txt.Split(new string[] { "//-" }, StringSplitOptions.RemoveEmptyEntries)) { var testcase = testcase1.Trim(); if (testcase.Length == 0) { continue; } using (var sw = new StreamWriter(srcfile, false)) { sw.Write(testcase); } int i = testcase.IndexOf("//"); string expected = testcase.Substring(i + 2).Split('\n')[0].Trim(); bool compiled; if (expected == "fail") { try { Compiler.compile(srcfile, asmfile, objfile, exefile); compiled = true; } catch (Exception e) { Console.WriteLine(e.Message); compiled = false; } } else { Compiler.compile(srcfile, asmfile, objfile, exefile); compiled = true; } int exitcode; bool infiniteLoop = false; if (compiled) { var si = new ProcessStartInfo(); si.FileName = exefile; si.UseShellExecute = false; si.RedirectStandardOutput = true; si.RedirectStandardError = true; var proc = Process.Start(si); proc.OutputDataReceived += (s, e) => { Console.WriteLine(e.Data); }; proc.ErrorDataReceived += (s, e) => { Console.WriteLine(e.Data); }; proc.BeginOutputReadLine(); proc.BeginErrorReadLine(); proc.WaitForExit(1000); if (!proc.HasExited) { proc.Kill(); proc.WaitForExit(); infiniteLoop = true; exitcode = -1; } else { exitcode = proc.ExitCode; } } else { exitcode = -1; } bool ok; if (!compiled) { if (expected == "fail") { ok = true; } else { ok = false; } } else if (expected == "fail") { ok = false; } else if (expected == "nonzero") { ok = (exitcode != 0 && !infiniteLoop); } else if (expected == "infinite") { ok = (infiniteLoop == true); } else { ok = (exitcode == Convert.ToInt32(expected) && !infiniteLoop); } if (ok) { Console.WriteLine("OK! " + (infiniteLoop ? "infinite":"" + exitcode) + " " + expected); } else { Console.WriteLine(testcase); if (!compiled) { Console.WriteLine("Error: Did not compile"); } else { Console.WriteLine("Error: Got " + exitcode + " but expected " + expected); } Console.ReadLine(); Environment.Exit(1); } } } Console.WriteLine("All OK!"); Console.ReadLine(); }
public static void Main(string[] args) { if (args.Length != 0) { Compiler.compile(args[0], args[0] + ".asm", args[0] + ".o", args[0] + ".exe"); return; } string srcfile = "xyz.txt"; string asmfile = "xyz.asm"; string objfile = "xyz.o"; string exefile = "xyz.exe"; var inputfile = "inputs.txt"; Console.WriteLine("Working directory: " + Environment.CurrentDirectory); Console.WriteLine("Reading inputs from " + inputfile); if (args.Length != 0) { Compiler.compile(args[0], asmfile, objfile, exefile); return; } //var doc = new System.Xml.XmlDocument(); XPathDocument doc; using (var sr = new StreamReader(inputfile)) { doc = new XPathDocument(sr); } //var root = doc.DocumentElement; var nav = doc.CreateNavigator(); //var tests = root.GetElementsByTagName("test"); foreach (XPathNavigator testelem in nav.SelectDescendants("test", "", true)) //System.Xml.XmlElement testelem in tests){ { int lineNumber = (testelem as IXmlLineInfo).LineNumber; var shouldBeNull = maybeGet(testelem, "return"); if (shouldBeNull != null) { throw new Exception("At line " + lineNumber + ": Use returns, not return"); } var expectedReturn = maybeGet(testelem, "returns"); var expectedOutput = maybeGet(testelem, "output"); var expectedInput = maybeGet(testelem, "input"); var testcase = maybeGet(testelem, "code"); //each array has: filename, expected contents, actual contents var expectedFiles = new List <string[]>(); foreach (XPathNavigator fnode in testelem.SelectChildren("file", "")) { var nm = maybeGet(fnode, "name"); var co = maybeGet(fnode, "content"); if (nm == null || co == null) { throw new Exception(); } expectedFiles.Add(new string[] { nm, co, "" }); } using (var sw = new StreamWriter(srcfile, false)) { sw.Write(testcase); } ExitStatus exitStatus = ExitStatus.UNKNOWN; if (expectedReturn == "failure") { try { Compiler.compile(srcfile, asmfile, objfile, exefile); } catch (Exception) { exitStatus = ExitStatus.DID_NOT_COMPILE; } } else { //try { Compiler.compile(srcfile, asmfile, objfile, exefile); // } catch(Exception e) { // Console.WriteLine("Test at line "+lineNumber+": Did not compile: " + e); // exitStatus = ExitStatus.DID_NOT_COMPILE; // throw; // } } string stdout = ""; string stderr = ""; int exitcode = -1; if (exitStatus != ExitStatus.DID_NOT_COMPILE) { var si = new ProcessStartInfo(); si.FileName = exefile; si.UseShellExecute = false; si.RedirectStandardInput = true; si.RedirectStandardOutput = true; si.RedirectStandardError = true; using (var proc = Process.Start(si)) { proc.StandardInput.Write(expectedInput); proc.StandardInput.Flush(); var stdoutData = proc.StandardOutput.ReadToEndAsync(); var stderrData = proc.StandardError.ReadToEndAsync(); proc.WaitForExit(1000); if (!proc.HasExited) { proc.Kill(); proc.WaitForExit(); exitStatus = ExitStatus.INFINITE_LOOP; exitcode = -1; } else { exitcode = proc.ExitCode; exitStatus = ExitStatus.NORMAL; } stdout = stdoutData.Result; stderr = stderrData.Result; } } bool ok = true; if (expectedReturn == "failure") { ok = (exitStatus == ExitStatus.DID_NOT_COMPILE); } else if (expectedReturn == "infinite") { ok = (exitStatus == ExitStatus.INFINITE_LOOP); } else { if (exitStatus != ExitStatus.NORMAL) { ok = false; } else if (expectedReturn == null) { //don't care what the value is } else { if (expectedReturn == "nonzero") { ok = (exitcode != 0); } else { ok = (exitcode == Convert.ToInt32(expectedReturn)); } } } if (expectedOutput != null) { if (expectedOutput != stdout.Replace("\r\n", "\n")) { ok = false; } } foreach (var t in expectedFiles) { try { using (StreamReader rdr = new StreamReader(t[0])) { t[2] = rdr.ReadToEnd(); if (t[2] != t[1]) { ok = false; } } } catch (IOException) { ok = false; } } if (ok) { Console.WriteLine(lineNumber + ": OK!"); foreach (var f in expectedFiles) { File.Delete(f[0]); } } else { Console.WriteLine("At input.txt line " + lineNumber + ": "); Console.WriteLine(testcase); if (exitStatus == ExitStatus.DID_NOT_COMPILE) { Console.WriteLine("Error: Did not compile but it should have (" + exitStatus + " " + expectedReturn + ")"); } else if (exitStatus == ExitStatus.INFINITE_LOOP) { Console.WriteLine("Infinite loop detected"); } else { Console.WriteLine("-----------------"); Console.WriteLine("Error: Expectation mismatch"); if (expectedReturn != "" + exitcode) { Console.WriteLine("Expected return code from main():"); Console.WriteLine(expectedReturn); Console.WriteLine("Actual return code from main():"); Console.WriteLine(exitcode); } if (expectedOutput != "" && expectedOutput != stdout) { Console.WriteLine("Expected program output: "); prettyprint(expectedOutput); Console.WriteLine("Actual program output:"); prettyprint(stdout); Console.WriteLine(stderr); } foreach (var t in expectedFiles) { if (t[1] != t[2]) { Console.WriteLine("Expected contents of file " + t[0]); prettyprint(t[1]); Console.WriteLine("Actual contents of file " + t[0]); prettyprint(t[2]); } } Console.WriteLine("-----------------"); } //Console.ReadLine(); Environment.Exit(1); } } Console.WriteLine("All OK!"); //Console.ReadLine(); }