Skip to content

Commit

Permalink
Logic lines like ~ x = f() should be followed by newlines in case f()…
Browse files Browse the repository at this point in the history
… outputs text
  • Loading branch information
joethephish committed Jun 4, 2018
1 parent 20b8bff commit 0cc058c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
23 changes: 14 additions & 9 deletions compiler/InkParser/InkParser_Logic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,22 @@ protected Parsed.Object LogicLine()
}
}

// A function call on its own line could result in a text side effect, in which case
// it needs a newline on the end. e.g.
// Line is pure function call? e.g.
// ~ f()
// Add extra pop to make sure we tidy up after ourselves.
// We no longer need anything on the evaluation stack.
var funCall = result as FunctionCall;
if (funCall) funCall.shouldPopReturnedValue = true;

// If the expression contains a function call, then it could produce a text side effect,
// in which case it needs a newline on the end. e.g.
// ~ printMyName()
// ~ x = 1 + returnAValueAndAlsoPrintStuff()
// If no text gets printed, then the extra newline will have to be culled later.
if (result is FunctionCall) {

// Add extra pop to make sure we tidy up after ourselves - we no longer need anything on the evaluation stack.
var funCall = result as FunctionCall;
funCall.shouldPopReturnedValue = true;

result = new ContentList (funCall, new Parsed.Text ("\n"));
// Multiple newlines on the output will be removed, so there will be no "leak" for
// long running calculations. It's disappointingly messy though :-/
if (result.Find<FunctionCall>() != null ) {
result = new ContentList (result, new Parsed.Text ("\n"));
}

Expect(EndOfLine, "end of line", recoveryRule: SkipToNextLine);
Expand Down
26 changes: 26 additions & 0 deletions tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3430,6 +3430,32 @@ public void TestTurns ()
story.ChooseChoiceIndex (0);
}
}




[Test ()]
public void TestLogicLinesWithNewlines ()
{
// Both "~" lines should be followed by newlines
// since func() has a text output side effect.
var storyStr =
@"
~ func ()
text 2
~temp tempVar = func ()
text 2
== function func ()
text1
~ return true
";

var story = CompileString (storyStr);

Assert.AreEqual("text1\ntext 2\ntext1\ntext 2\n", story.ContinueMaximally ());
}


// Helper compile function
Expand Down

0 comments on commit 0cc058c

Please sign in to comment.