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