diff --git a/Assignment 1/Program.cs b/Assignment 1/Program.cs index 33742a7..2f6d99c 100644 --- a/Assignment 1/Program.cs +++ b/Assignment 1/Program.cs @@ -88,6 +88,7 @@ namespace Assignment_1 } public void StartParsing(Stream source, bool dynamicInput = false) { + long initSourceLength = source.Length; if ((byte)source.ReadByte() == '{') { while (true) @@ -122,7 +123,7 @@ namespace Assignment_1 switch ((statements)statementType) { case statements.exit: - result = Exit(); + result = Exit(source, initSourceLength); break; case statements.append: result = AppendSet(source); @@ -169,6 +170,13 @@ namespace Assignment_1 // Do a check semicolons etc if (IsNextEoS(source)) { + // Increment the source pos past the semi-colon + source.Position++; + if (dynamicInput) + { + // Nicely format the output stream, so we may print it cleanly + source.WriteByte((byte)'\n'); + } result(); } else @@ -317,10 +325,41 @@ namespace Assignment_1 return () => Console.WriteLine(consoleOutput.ToString()); } - Action Exit() + Action Exit(Stream source, long initialStreamLength) { - // Should do some save command here - return () => Environment.Exit(0); + Action exitAction = () => + { + if (source.Length != initialStreamLength) + { + Console.WriteLine("Commands list has been modified; would you like to save it to a file?"); + string commandState = ""; + while (commandState.ToLower() != "y" && commandState.ToLower() != "n") + { + Console.Write("Y/n: "); + commandState = Console.ReadLine(); + } + if (commandState.ToLower() == "y") + { + Console.WriteLine("Enter an output file (default {0}):", Environment.CurrentDirectory); + string path = Console.ReadLine(); + if (path == "") + { + Environment.Exit(0); + } + path = Path.Combine(Environment.CurrentDirectory, path); + // insert the final closing bracket + source.WriteByte((byte)'}'); + source.Position = 0; + using (FileStream fs = File.OpenWrite(path)) + { + source.CopyTo(fs); + } + source.Close(); + } + } + Environment.Exit(0); + }; + return exitAction; } Action Print(Stream source, int mode=0) { @@ -370,6 +409,7 @@ namespace Assignment_1 return () => Symbols[key] = new Tuple(reversed.ToString(), Symbols[key].Item2); } + /// /// Writes the debug info to the screen in the form:
/// line read from stream (lineStart) to line end
@@ -379,7 +419,7 @@ namespace Assignment_1 /// /// /// - void WriteDebugLine(long lineStart, long caratPos, string errorMessage, Stream source) + static void WriteDebugLine(long lineStart, long caratPos, string errorMessage, Stream source) { source.Position = lineStart; string fullLine = GetNextLine(source);