Skip to content

Commit

Permalink
Experimental new way of consuming excess newlines
Browse files Browse the repository at this point in the history
Removes the concept of left/right glue that were previously added to all inline logic, and instead automatically chomps through any whitespace at the start and end of a function call. Main reason is that left/right glue was leaking when you divert out of a piece of inline logic. This is possibly/probably more elegant, but not sure if there are weird edge cases. Tests are passing.
  • Loading branch information
joethephish committed Apr 5, 2018
1 parent 32fa48a commit a478f1b
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 135 deletions.
8 changes: 7 additions & 1 deletion ink-engine-runtime/CallStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ internal class Element
// so that we know whether there was any return value.
public int evaluationStackHeightWhenPushed;

// When functions are called, we trim whitespace from the start and end of what
// they generate, so we make sure know where the function's start and end are.
public int functionStartInOuputStream;

public Runtime.Object currentObject {
get {
if (currentContainer && currentContentIndex < currentContainer.content.Count) {
Expand Down Expand Up @@ -63,6 +67,7 @@ public Element Copy()
var copy = new Element (this.type, this.currentContainer, this.currentContentIndex, this.inExpressionEvaluation);
copy.temporaryVariables = new Dictionary<string,Object>(this.temporaryVariables);
copy.evaluationStackHeightWhenPushed = evaluationStackHeightWhenPushed;
copy.functionStartInOuputStream = functionStartInOuputStream;
return copy;
}
}
Expand Down Expand Up @@ -276,7 +281,7 @@ public bool elementIsEvaluateFromGame
}
}

public void Push(PushPopType type, int externalEvaluationStackHeight = 0)
public void Push(PushPopType type, int externalEvaluationStackHeight = 0, int outputStreamLengthWithPushed = 0)
{
// When pushing to callstack, maintain the current content path, but jump out of expressions by default
var element = new Element (
Expand All @@ -287,6 +292,7 @@ public void Push(PushPopType type, int externalEvaluationStackHeight = 0)
);

element.evaluationStackHeightWhenPushed = externalEvaluationStackHeight;
element.functionStartInOuputStream = outputStreamLengthWithPushed;

callStack.Add (element);
}
Expand Down
39 changes: 2 additions & 37 deletions ink-engine-runtime/Glue.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,12 @@
namespace Ink.Runtime
{
internal enum GlueType
{
Bidirectional,
Left,
Right
}

internal class Glue : Runtime.Object
{
public GlueType glueType { get; set; }

public bool isLeft {
get {
return glueType == GlueType.Left;
}
}

public bool isBi {
get {
return glueType == GlueType.Bidirectional;
}
}

public bool isRight {
get {
return glueType == GlueType.Right;
}
}

public Glue(GlueType type) {
glueType = type;
}
public Glue() { }

public override string ToString ()
{
switch (glueType) {
case GlueType.Bidirectional: return "BidirGlue";
case GlueType.Left: return "LeftGlue";
case GlueType.Right: return "RightGlue";
}

return "UnexpectedGlueType";
return "Glue";
}
}
}
Expand Down
16 changes: 2 additions & 14 deletions ink-engine-runtime/JsonSerialisation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,7 @@ public static Runtime.Object JTokenToRuntimeObject(object token)
return new StringValue ("\n");

// Glue
if (str == "<>")
return new Runtime.Glue (GlueType.Bidirectional);
else if(str == "G<")
return new Runtime.Glue (GlueType.Left);
else if(str == "G>")
return new Runtime.Glue (GlueType.Right);
if (str == "<>") return new Runtime.Glue ();

// Control commands (would looking up in a hash set be faster?)
for (int i = 0; i < _controlCommandNames.Length; ++i) {
Expand Down Expand Up @@ -400,14 +395,7 @@ public static object RuntimeObjectToJToken(Runtime.Object obj)
}

var glue = obj as Runtime.Glue;
if (glue) {
if (glue.isBi)
return "<>";
else if (glue.isLeft)
return "G<";
else
return "G>";
}
if (glue) return "<>";

var controlCmd = obj as ControlCommand;
if (controlCmd) {
Expand Down
17 changes: 10 additions & 7 deletions ink-engine-runtime/Story.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Story : Runtime.Object
/// <summary>
/// The current version of the ink story file format.
/// </summary>
public const int inkVersionCurrent = 17;
public const int inkVersionCurrent = 18;

// Version numbers are for engine itself and story file, rather
// than the story state save format (which is um, currently nonexistant)
Expand All @@ -33,7 +33,7 @@ public class Story : Runtime.Object
/// <summary>
/// The minimum legacy version of ink that can be loaded by the current version of the code.
/// </summary>
const int inkVersionMinimumCompatible = 16;
const int inkVersionMinimumCompatible = 18;

/// <summary>
/// The list of Choice objects available at the current point in
Expand Down Expand Up @@ -804,7 +804,10 @@ bool PerformLogicAndFlowControl(Runtime.Object contentObj)
}

if (currentDivert.pushesToStack) {
state.callStack.Push (currentDivert.stackPushType);
state.callStack.Push (
currentDivert.stackPushType,
outputStreamLengthWithPushed:state.outputStream.Count
);
}

if (state.divertedTargetObject == null && !currentDivert.isExternal) {
Expand Down Expand Up @@ -904,7 +907,7 @@ bool PerformLogicAndFlowControl(Runtime.Object contentObj)
}

else {
state.callStack.Pop ();
state.PopCallstack ();

// Does tunnel onwards override by diverting to a new ->-> target?
if( overrideTunnelReturnTarget )
Expand Down Expand Up @@ -1396,7 +1399,7 @@ internal Runtime.Object EvaluateExpression(Runtime.Container exprContainer)
// have auto-popped, but just in case we didn't for some reason,
// manually pop to restore the state (including currentPath).
if (state.callStack.elements.Count > startCallStackHeight) {
state.callStack.Pop ();
state.PopCallstack ();
}

int endStackHeight = state.evaluationStack.Count;
Expand Down Expand Up @@ -1938,9 +1941,9 @@ private void NextContent()
bool didPop = false;

if (state.callStack.CanPop (PushPopType.Function)) {

// Pop from the call stack
state.callStack.Pop (PushPopType.Function);
state.PopCallstack (PushPopType.Function);

// This pop was due to dropping off the end of a function that didn't return anything,
// so in this case, we make sure that the evaluator has something to chomp on if it needs it
Expand Down
Loading

0 comments on commit a478f1b

Please sign in to comment.