diff --git a/Assignment 1/Program.cs b/Assignment 1/Program.cs index ef186a9..1f9f0e1 100644 --- a/Assignment 1/Program.cs +++ b/Assignment 1/Program.cs @@ -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); @@ -330,9 +314,15 @@ namespace Assignment_1 } else { - return () => Symbols.Add(key, new Tuple(value, VariableFlags.Empty)); + if (Symbols.ContainsKey(key)) + { + return () => Symbols[key] = new Tuple(value, Symbols[key].Item2); + } + else + { + return () => Symbols.Add(key, new Tuple(value, VariableFlags.Empty)); + } } - } /// /// Creates and prints a list of all defined variables