Improved reassignment with 'set'.

Cleaned a few lines
This commit is contained in:
Brychan Dempsey 2021-03-19 12:04:03 +13:00
parent 83a8631d72
commit 9475079056

View File

@ -62,20 +62,11 @@ namespace Assignment_1
// 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;
Console.WriteLine("Input is piped");
/*byte[] bytes = new byte[sourceStream.Length];
sourceStream.Read(bytes);
string readBytes = Encoding.UTF8.GetString(bytes);
Console.WriteLine(readBytes);
sourceStream.Position = 0;
foreach (var item in bytes)
{
Console.Write(item+", ");
}
Console.WriteLine();*/
}
else
{
@ -266,7 +257,7 @@ namespace Assignment_1
}
else
{
Console.WriteLine("First read character was not \'{\'. Use the launch flag -ns for non-strict checking");
Console.WriteLine("First read character was not \'{\'. Use the launch flag -ns for non-strict syntax checking");
}
}
@ -274,8 +265,7 @@ namespace Assignment_1
private string ValidateKey(Stream source, bool checkExist)
{
string key;
long keyEndPos = FindIdentifier(source, out key);
long keyEndPos = FindIdentifier(source, out string key);
if (keyEndPos < 0 || key.Length == 0)
{
throw new ParserException("Could not identify object", 0, source.Position);
@ -290,11 +280,6 @@ namespace Assignment_1
{
throw new ParserException("Cannot assign a value to a reserved constant", 0, keyEndPos - (key.Length + 1));
}
else if (Symbols.ContainsKey(key) && !checkExist)
{
// key already exists, remove it
Symbols.Remove(key);
}
source.Position = keyEndPos;
}
return key;
@ -302,8 +287,7 @@ namespace Assignment_1
private string ValidateValue(Stream source)
{
string value;
long valuePos = FindExpression(source, out value);
long valuePos = FindExpression(source, out string value);
if (valuePos < 0)
{
throw new ParserException("Could not evaluate expression", 0, source.Position);
@ -329,10 +313,16 @@ namespace Assignment_1
return () => Symbols[key] = new Tuple<string, VariableFlags>(Symbols[key].Item1 + value, Symbols[key].Item2);
}
else
{
if (Symbols.ContainsKey(key))
{
return () => Symbols[key] = new Tuple<string, VariableFlags>(value, Symbols[key].Item2);
}
else
{
return () => Symbols.Add(key, new Tuple<string, VariableFlags>(value, VariableFlags.Empty));
}
}
}
/// <summary>
/// Creates and prints a list of all defined variables