WIP: As per #11, this fixes the issue with multi-line literals #12

Draft
BrychanD wants to merge 1 commits from string-newline-fix into master
2 changed files with 31 additions and 16 deletions

1
.gitignore vendored
View File

@ -364,3 +364,4 @@ FodyWeavers.xsd
# Assignment 1 instructions
assignment_instructions.txt
159.341_Assignment_1.zip

View File

@ -180,7 +180,7 @@ namespace Assignment_1
result = Exit(source, initSourceLength, dynamicInput);
break;
case Statements.append:
result = AppendSet(source);
result = AppendSet(source, dynamicInput);
break;
case Statements.list:
long pos = FindNextWord(source, out string nextWord);
@ -195,19 +195,19 @@ namespace Assignment_1
}
break;
case Statements.print:
result = Print(source, 0);
result = Print(source, dynamicInput, 0);
break;
case Statements.printlength:
result = Print(source, 1);
result = Print(source, dynamicInput, 1);
break;
case Statements.printwords:
result = Print(source, 2);
result = Print(source, dynamicInput, 2);
break;
case Statements.printwordcount:
result = Print(source, 3);
result = Print(source, dynamicInput, 3);
break;
case Statements.set:
result = AppendSet(source, false);
result = AppendSet(source, dynamicInput, false);
break;
case Statements.reverse:
result = Reverse(source);
@ -321,9 +321,9 @@ namespace Assignment_1
/// <summary>
/// Checks if the next expression meets the requirements of being a value
/// </summary>
private string ValidateValue(Stream source)
private string ValidateValue(Stream source, bool dynamicInput)
{
long valuePos = FindExpression(source, out string value);
long valuePos = FindExpression(source, dynamicInput, out string value);
if (valuePos < 0)
{
throw new ParserException("Could not evaluate expression", 0, source.Position);
@ -339,10 +339,10 @@ namespace Assignment_1
/// Handles the 'append x y [ + z];' case <br />
/// And the 'set x y [ + z];' case
/// </summary>
Action AppendSet(Stream source, bool appendMode = true)
Action AppendSet(Stream source, bool dynamicInput, bool appendMode = true)
{
string key = ValidateKey(source, appendMode);
string value = ValidateValue(source);
string value = ValidateValue(source, dynamicInput);
if (appendMode)
{
return () => Symbols[key] = new Tuple<string, VariableFlags>(Symbols[key].Item1 + value, Symbols[key].Item2);
@ -446,10 +446,10 @@ namespace Assignment_1
/// 2: print the word count <br />
/// 3: print the words in the value <br />
/// </summary>
Action Print(Stream source, int mode = 0)
Action Print(Stream source,bool dynamicInput, int mode = 0)
{
StringBuilder outputString = new StringBuilder();
string expression = ValidateValue(source);
string expression = ValidateValue(source, dynamicInput);
if (mode == 0)
{
outputString.Append(expression + Environment.NewLine);
@ -516,7 +516,7 @@ namespace Assignment_1
/// <summary>
/// Parses & evaluates the expression from the stream, moving the stream to the end of the last value
/// </summary>
long FindExpression(Stream s, out string expression)
long FindExpression(Stream s, bool dynamicInput, out string expression)
{
string result = "";
bool IsAppendSet = true;
@ -529,7 +529,7 @@ namespace Assignment_1
}
else
{
long val = FindValue(s, out string value);
long val = FindValue(s, dynamicInput, out string value);
if (val == -1)
{
Console.WriteLine("Could not parse value");
@ -569,14 +569,28 @@ namespace Assignment_1
/// <summary>
/// Finds the next value expression in the stream
/// </summary>
long FindValue(Stream s, out string returnedValue)
long FindValue(Stream s, bool dynamicInput, out string returnedValue)
{
SkipWhitespace(s);
char result = PeekChar(s);
if (result == '\"')
{
// The first char is a ", i.e. the start of a literal - search as if it were a literal.
return FindLiteral(s, out returnedValue);
// If the literal reaches the end of the string & the last char is not ", then we are
// expecting more - insert a new line into the stream
long literalPos = FindLiteral(s, out returnedValue);
if (dynamicInput)
{
long orig = s.Position;
s.Position = literalPos - 1;
if (literalPos == s.Length && ReadChar(s) != '"')
{
s.WriteByte((byte)'\n');
literalPos++;
}
s.Position = orig;
}
return literalPos;
}
else
{