Improved reassignment with 'set'.
Cleaned a few lines
This commit is contained in:
parent
83a8631d72
commit
9475079056
@ -62,20 +62,11 @@ namespace Assignment_1
|
|||||||
// Reading from pipes is equivalent to reading user input, though the input is redirected
|
// Reading from pipes is equivalent to reading user input, though the input is redirected
|
||||||
if (Console.IsInputRedirected)
|
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.Write(Encoding.UTF8.GetBytes(Console.In.ReadToEnd()));
|
||||||
|
|
||||||
sourceStream.Position = 0;
|
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
|
else
|
||||||
{
|
{
|
||||||
@ -266,7 +257,7 @@ namespace Assignment_1
|
|||||||
}
|
}
|
||||||
else
|
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)
|
private string ValidateKey(Stream source, bool checkExist)
|
||||||
{
|
{
|
||||||
string key;
|
long keyEndPos = FindIdentifier(source, out string key);
|
||||||
long keyEndPos = FindIdentifier(source, out key);
|
|
||||||
if (keyEndPos < 0 || key.Length == 0)
|
if (keyEndPos < 0 || key.Length == 0)
|
||||||
{
|
{
|
||||||
throw new ParserException("Could not identify object", 0, source.Position);
|
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));
|
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;
|
source.Position = keyEndPos;
|
||||||
}
|
}
|
||||||
return key;
|
return key;
|
||||||
@ -302,8 +287,7 @@ namespace Assignment_1
|
|||||||
|
|
||||||
private string ValidateValue(Stream source)
|
private string ValidateValue(Stream source)
|
||||||
{
|
{
|
||||||
string value;
|
long valuePos = FindExpression(source, out string value);
|
||||||
long valuePos = FindExpression(source, out value);
|
|
||||||
if (valuePos < 0)
|
if (valuePos < 0)
|
||||||
{
|
{
|
||||||
throw new ParserException("Could not evaluate expression", 0, source.Position);
|
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);
|
return () => Symbols[key] = new Tuple<string, VariableFlags>(Symbols[key].Item1 + value, Symbols[key].Item2);
|
||||||
}
|
}
|
||||||
else
|
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));
|
return () => Symbols.Add(key, new Tuple<string, VariableFlags>(value, VariableFlags.Empty));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates and prints a list of all defined variables
|
/// Creates and prints a list of all defined variables
|
||||||
|
Loading…
x
Reference in New Issue
Block a user