diff --git a/Assignment 1/Program.cs b/Assignment 1/Program.cs index 4e5d8e8..95902da 100644 --- a/Assignment 1/Program.cs +++ b/Assignment 1/Program.cs @@ -152,17 +152,7 @@ namespace Assignment_1 long keyEndPos = FindIdentifier(source, out key); if (keyEndPos < 0 || !Symbols.ContainsKey(key)) { - // Error on finding object - if (lineStart != -1) - { - source.Position = lineStart; - } - string fullLine = GetNextLine(source); // Grab a copy of the line to show the user - // append x - string errorMSG = " ^ could not identify object"; - Console.WriteLine(fullLine); - Console.WriteLine(errorMSG); - source.SetLength(source.Position); + WriteDebugLine(lineStart, lineStart + "append ".Length, "could not identify object", source); return false; } else @@ -174,15 +164,7 @@ namespace Assignment_1 if (valuePos < 0) { // Error on finding object - source.Position = lineStart; - string fullLine = GetNextLine(source); // Grab a copy of the line to show the user - // append x - string errorMSG = new string(' ', (keyEndPos - source.Position) > 0 ? (int)(keyEndPos - source.Position) : "append".Length) + "^ could not evaluate value"; - Console.WriteLine(fullLine); - Console.WriteLine(errorMSG); - // Value didn't parse, set stream length to current length (removes excess characters from the stream - // so the next command is parsed correctly) - source.SetLength(source.Position); + WriteDebugLine(lineStart, keyEndPos, "could not evaluate expression", source); return false; } else @@ -193,26 +175,12 @@ namespace Assignment_1 FindNextWord(source, out eol); if (eol.Length == 0 || eol[0] != ';') { - // reset our position to the start of this line - source.Position = lineStart; - string fullLine = GetNextLine(source); // Grab a copy of the line to show the user - // Align the message carat to the point which could not be parsed - string errorMSG = new string(' ', (valuePos - source.Position) > 0 ? (int)(valuePos - source.Position) : "append".Length) + "^ expected a semicolon"; - Console.WriteLine(fullLine); - Console.WriteLine(errorMSG); - source.SetLength(source.Position); + WriteDebugLine(lineStart, valuePos, "expected a semicolon", source); return false; } if (Symbols[key].Item2.HasFlag(VariableFlags.Reserved)) { - // reset our position to the start of this line - source.Position = lineStart; - string fullLine = GetNextLine(source); // Grab a copy of the line to show the user - // Align the message carat to the point which could not be parsed - string errorMSG = new string(' ', (keyEndPos - (key.Length + 1) - source.Position) > 0 ? (int)(keyEndPos-(key.Length + 1) - source.Position) : "append".Length) + "^ cannot assign a value to a reserved constant"; - Console.WriteLine(fullLine); - Console.WriteLine(errorMSG); - source.SetLength(source.Position); + WriteDebugLine(lineStart, keyEndPos - (key.Length + 1), "cannot assign a value to a reserved constant", source); return false; } Symbols[key] = new Tuple(Symbols[key].Item1 + value, Symbols[key].Item2); @@ -284,20 +252,28 @@ namespace Assignment_1 return true; } - bool Set(Stream source) + bool Set(Stream source, long lineStart=-1) { - string identifier; - long resultPos = FindIdentifier(source, out identifier); - if (resultPos < 0) + if(lineStart == -1) { - // Couldn't match an identifier - // If ID Doesn't exist, we should make it + lineStart = source.Position - "set ".Length; + } + string identifier; + long identifierEndPos = FindIdentifier(source, out identifier); + if (identifierEndPos < 0 || identifierEndPos == source.Position) + { + string fullLine = GetNextLine(source); // Grab a copy of the line to show the user + // set x + string errorMSG = " ^ could not identify object"; + Console.WriteLine(fullLine); + Console.WriteLine(errorMSG); + source.SetLength(source.Position); return false; } - source.Position = resultPos; + source.Position = identifierEndPos; string expression; - resultPos = FindExpression(source, out expression); - if (resultPos < 0) + long expressionEndPos = FindExpression(source, out expression); + if (expressionEndPos < 0) { // Couldn't match expression return false; @@ -316,7 +292,7 @@ namespace Assignment_1 { Symbols.Add(identifier, new Tuple(expression, VariableFlags.Empty)); } - source.Position = resultPos; + source.Position = expressionEndPos; return true; } @@ -342,6 +318,24 @@ namespace Assignment_1 Symbols[identifier] = new Tuple(reversed.ToString(), Symbols[identifier].Item2); return true; } + /// + /// Writes the debug info to the screen in the form:
+ /// line read from stream (lineStart) to line end
+ /// <whitespace@caratPos> ^ <errorMessage> + ///
+ /// + /// + /// + /// + void WriteDebugLine(long lineStart, long caratPos, string errorMessage, Stream source) + { + source.Position = lineStart; + string fullLine = GetNextLine(source); + string errorMSG = new string (' ', (int)(caratPos - lineStart)) + "^ " + errorMessage; + Console.WriteLine(fullLine); + Console.WriteLine(errorMSG); + source.SetLength(source.Position); + } #endregion #region Data Handling