Turned the debug writer into a method

This commit is contained in:
Brychan Dempsey 2021-03-12 11:29:46 +13:00
parent 5ebdb9fc06
commit e953b1cc90

View File

@ -152,17 +152,7 @@ namespace Assignment_1
long keyEndPos = FindIdentifier(source, out key); long keyEndPos = FindIdentifier(source, out key);
if (keyEndPos < 0 || !Symbols.ContainsKey(key)) if (keyEndPos < 0 || !Symbols.ContainsKey(key))
{ {
// Error on finding object WriteDebugLine(lineStart, lineStart + "append ".Length, "could not identify object", source);
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);
return false; return false;
} }
else else
@ -174,15 +164,7 @@ namespace Assignment_1
if (valuePos < 0) if (valuePos < 0)
{ {
// Error on finding object // Error on finding object
source.Position = lineStart; WriteDebugLine(lineStart, keyEndPos, "could not evaluate expression", source);
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);
return false; return false;
} }
else else
@ -193,26 +175,12 @@ namespace Assignment_1
FindNextWord(source, out eol); FindNextWord(source, out eol);
if (eol.Length == 0 || eol[0] != ';') if (eol.Length == 0 || eol[0] != ';')
{ {
// reset our position to the start of this line WriteDebugLine(lineStart, valuePos, "expected a semicolon", source);
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);
return false; return false;
} }
if (Symbols[key].Item2.HasFlag(VariableFlags.Reserved)) if (Symbols[key].Item2.HasFlag(VariableFlags.Reserved))
{ {
// reset our position to the start of this line WriteDebugLine(lineStart, keyEndPos - (key.Length + 1), "cannot assign a value to a reserved constant", source);
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);
return false; return false;
} }
Symbols[key] = new Tuple<string, VariableFlags>(Symbols[key].Item1 + value, Symbols[key].Item2); Symbols[key] = new Tuple<string, VariableFlags>(Symbols[key].Item1 + value, Symbols[key].Item2);
@ -284,20 +252,28 @@ namespace Assignment_1
return true; return true;
} }
bool Set(Stream source) bool Set(Stream source, long lineStart=-1)
{ {
if(lineStart == -1)
{
lineStart = source.Position - "set ".Length;
}
string identifier; string identifier;
long resultPos = FindIdentifier(source, out identifier); long identifierEndPos = FindIdentifier(source, out identifier);
if (resultPos < 0) if (identifierEndPos < 0 || identifierEndPos == source.Position)
{ {
// Couldn't match an identifier string fullLine = GetNextLine(source); // Grab a copy of the line to show the user
// If ID Doesn't exist, we should make it // set x
string errorMSG = " ^ could not identify object";
Console.WriteLine(fullLine);
Console.WriteLine(errorMSG);
source.SetLength(source.Position);
return false; return false;
} }
source.Position = resultPos; source.Position = identifierEndPos;
string expression; string expression;
resultPos = FindExpression(source, out expression); long expressionEndPos = FindExpression(source, out expression);
if (resultPos < 0) if (expressionEndPos < 0)
{ {
// Couldn't match expression // Couldn't match expression
return false; return false;
@ -316,7 +292,7 @@ namespace Assignment_1
{ {
Symbols.Add(identifier, new Tuple<string, VariableFlags>(expression, VariableFlags.Empty)); Symbols.Add(identifier, new Tuple<string, VariableFlags>(expression, VariableFlags.Empty));
} }
source.Position = resultPos; source.Position = expressionEndPos;
return true; return true;
} }
@ -342,6 +318,24 @@ namespace Assignment_1
Symbols[identifier] = new Tuple<string, VariableFlags>(reversed.ToString(), Symbols[identifier].Item2); Symbols[identifier] = new Tuple<string, VariableFlags>(reversed.ToString(), Symbols[identifier].Item2);
return true; return true;
} }
/// <summary>
/// Writes the debug info to the screen in the form:<br/>
/// line read from stream (lineStart) to line end<br/>
/// &lt;whitespace@caratPos&gt; ^ &lt;errorMessage&gt;
/// </summary>
/// <param name="lineStart"></param>
/// <param name="caratPos"></param>
/// <param name="errorMessage"></param>
/// <param name="source"></param>
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 #endregion
#region Data Handling #region Data Handling