From 60d81c633210bd209591afdf15f241aac68d3992 Mon Sep 17 00:00:00 2001 From: Brychan Dempsey Date: Fri, 19 Mar 2021 12:42:38 +1300 Subject: [PATCH] Set a basic loop for multiple runs of the program --- Assignment 1/Program.cs | 83 +++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 37 deletions(-) diff --git a/Assignment 1/Program.cs b/Assignment 1/Program.cs index 37cb5dc..e5e356f 100644 --- a/Assignment 1/Program.cs +++ b/Assignment 1/Program.cs @@ -44,47 +44,56 @@ namespace Assignment_1 Console.WriteLine("│ Submitted by Brychan Dempsey, 14299890 │"); Console.WriteLine("└──────────────────────────────────────────┘"); // Parse the source from the memory stream - MemoryStream sourceStream = new(1024); - Parser parser = new(); - bool dynamicInput = false; - foreach (var arg in args) + bool exit = false; + while (!exit) { - if (arg == "-ns") + MemoryStream sourceStream = new(1024); + Parser parser = new(); + bool dynamicInput = false; + foreach (var arg in args) { - nonStrict = true; + if (arg == "-ns") + { + nonStrict = true; + } } + // From https://stackoverflow.com/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected + // Reading from pipes is equivalent to reading user input, though the input is redirected + if (Console.IsInputRedirected) + { + // To simplify reading, we read all input bytes from the piped input to the stream. + // This is by far not the best way to do it; reading line-by-line would reduce memory space, + // but it allows a simple read into the console + sourceStream.Write(Encoding.UTF8.GetBytes(Console.In.ReadToEnd())); + // Dispose will close a piped input, or piped file in further iterations of the program + Console.In.Dispose(); + sourceStream.Position = 0; + } + else + { + sourceStream.Write(Encoding.UTF8.GetBytes("{ \r\n")); + sourceStream.Position = 0; + dynamicInput = true; + } + parser.StartParsing(sourceStream, dynamicInput); + Console.WriteLine(Environment.NewLine + new string('─', 40)); + // Not strictly required, but could have problems if we try loading a large program immediately after unloading the last + GC.Collect(); + Console.WriteLine("\nProgram parsing complete."); + if (dynamicInput == false) + { + Thread.Sleep(2000); + Environment.Exit(0); + } + Console.WriteLine("Would you like to parse more programs?"); + string answer = Console.ReadLine(); + if (answer.ToLower() == "no") + { + Environment.Exit(0); + } + Console.WriteLine("Not yet implemented"); + Console.ReadLine(); } - // From https://stackoverflow.com/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected - // Reading from pipes is equivalent to reading user input, though the input is redirected - if (Console.IsInputRedirected) - { - // To simplify reading, we read all input bytes from the piped input to the stream. - // This is by far not the best way to do it; reading line-by-line would reduce memory space, - // but it allows a simple read into the console - sourceStream.Write(Encoding.UTF8.GetBytes(Console.In.ReadToEnd())); - sourceStream.Position = 0; - } - else - { - sourceStream.Write(Encoding.UTF8.GetBytes("{ \r\n")); - sourceStream.Position = 0; - dynamicInput = true; - } - parser.StartParsing(sourceStream, dynamicInput); - Console.WriteLine("\nProgram parsing complete."); - if (dynamicInput == false) - { - Thread.Sleep(2000); - Environment.Exit(0); - } - Console.WriteLine("Would you like to parse more programs?"); - string answer = Console.ReadLine(); - if (answer.ToLower() == "no") - { - Environment.Exit(0); - } - Console.WriteLine("Not yet implemented"); - Console.ReadLine(); } public class Parser