Implemented an optional save function on exit

This commit is contained in:
Brychan Dempsey 2021-03-17 14:25:39 +13:00
parent b371be43d6
commit c7858791f4

View File

@ -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<string, VariableFlags>(reversed.ToString(), Symbols[key].Item2);
}
/// <summary>
/// Writes the debug info to the screen in the form:<br/>
/// line read from stream (lineStart) to line end<br/>
@ -379,7 +419,7 @@ namespace Assignment_1
/// <param name="caratPos"></param>
/// <param name="errorMessage"></param>
/// <param name="source"></param>
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);