Fixed naming rule violation
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded

Moved Exit to main program flow, rather than the called function
This commit is contained in:
Brychan Dempsey 2021-03-19 12:31:16 +13:00
parent 1bd6b1b040
commit f7203440a8

View File

@ -6,6 +6,7 @@ using System.Text;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.Runtime.InteropServices;
using System.Threading;
namespace Assignment_1
{
@ -28,7 +29,10 @@ namespace Assignment_1
/// </summary>
static readonly List<char> ForbiddenChars = new()
{
'$', '\\', '\"', '\''
'$',
'\\',
'\"',
'\''
};
static bool nonStrict = false;
@ -40,7 +44,7 @@ namespace Assignment_1
Console.WriteLine("│ Submitted by Brychan Dempsey, 14299890 │");
Console.WriteLine("└──────────────────────────────────────────┘");
// Parse the source from the memory stream
MemoryStream sourceStream = new MemoryStream(1024);
MemoryStream sourceStream = new(1024);
Parser parser = new();
bool dynamicInput = false;
foreach (var arg in args)
@ -67,9 +71,22 @@ namespace Assignment_1
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
{
Dictionary<string, Tuple<string, VariableFlags>> Symbols = new()
@ -80,7 +97,7 @@ namespace Assignment_1
{ "CARRIAGE_RETURN", new Tuple<string, VariableFlags>("\r", VariableFlags.Reserved | VariableFlags.NoPrint) }
};
public enum statements
public enum Statements
{
exit,
append,
@ -135,7 +152,7 @@ namespace Assignment_1
long position = FindNextWord(source, out string word);
try
{
if (Enum.TryParse(typeof(statements), word, out object statementType))
if (Enum.TryParse(typeof(Statements), word, out object statementType))
{
// By turning the result of the command into an action,
// we can defer processing the final result until the end of this control flow
@ -144,15 +161,15 @@ namespace Assignment_1
// In some ways, it makes more sense. The action is determined by the interpreter's result
Action result = () => { };
source.Position = position;
switch ((statements)statementType)
switch ((Statements)statementType)
{
case statements.exit:
case Statements.exit:
result = Exit(source, initSourceLength);
break;
case statements.append:
case Statements.append:
result = AppendSet(source);
break;
case statements.list:
case Statements.list:
long pos = FindNextWord(source, out string nextWord);
if (nextWord == "all")
{
@ -164,36 +181,36 @@ namespace Assignment_1
result = List();
}
break;
case statements.print:
case Statements.print:
result = Print(source, 0);
break;
case statements.printlength:
case Statements.printlength:
result = Print(source, 1);
break;
case statements.printwordcount:
case Statements.printwordcount:
result = Print(source, 2);
break;
case statements.printwords:
case Statements.printwords:
result = Print(source, 3);
break;
case statements.set:
case Statements.set:
result = AppendSet(source, false);
break;
case statements.reverse:
case Statements.reverse:
result = Reverse(source);
break;
// These are additional helper functions. Thier input gets excluded from the MemoryStream
case statements.h:
case Statements.h:
Console.WriteLine("Commands are: ");
foreach (var item in Enum.GetValues(typeof(statements)))
foreach (var item in Enum.GetValues(typeof(Statements)))
{
Console.WriteLine("\t{0}", ((statements)item).ToString());
Console.WriteLine("\t{0}", ((Statements)item).ToString());
}
// Ignore these as actual commands
source.Position = initPos;
source.SetLength(initPos);
break;
case statements.writeout:
case Statements.writeout:
// Writes the full command history to the stream.
Console.WriteLine("Writing input commands to {0}...");
source.Position = initPos;
@ -212,6 +229,10 @@ namespace Assignment_1
source.WriteByte((byte)'\n');
}
result();
if (((Statements)statementType).Equals(Statements.exit))
{
return;
}
}
else if (source.Position != lastLinePos - 1)
{
@ -396,10 +417,8 @@ namespace Assignment_1
{
Console.WriteLine("Enter an output file (default {0}):", Environment.CurrentDirectory);
string path = Console.ReadLine();
if (path == "")
if (path != "")
{
Environment.Exit(0);
}
path = Path.Combine(Environment.CurrentDirectory, path);
// insert the final closing bracket
source.WriteByte((byte)'}');
@ -411,7 +430,7 @@ namespace Assignment_1
source.Close();
}
}
Environment.Exit(0);
}
};
return exitAction;
}