From 2e2fd68e4fc24ebfbe3edb5eb936a36a9463cb14 Mon Sep 17 00:00:00 2001 From: premek Date: Sat, 3 Dec 2016 04:13:50 +0100 Subject: [PATCH] runtime basic knot/divert support --- examples/game.ink | 7 +++++++ pink/parser.lua | 7 ++++--- pink/runtime.lua | 33 +++++++++++++++++++++++++++++---- test/choices.lua | 46 +++++++++++++++++++++++++++------------------- 4 files changed, 67 insertions(+), 26 deletions(-) diff --git a/examples/game.ink b/examples/game.ink index 76c74e1..85a4b98 100644 --- a/examples/game.ink +++ b/examples/game.ink @@ -1,7 +1,14 @@ +=== start === + "What that's?" my master asked. * "I am somewhat tired[."]," I repeated. "Really," he responded. "How deleterious." * "Nothing, Monsieur!"[] I replied. "Very good, then." + -> finale * "I said, this journey is appalling[."] and I want no more of it." "Ah," he replied, not unkindly. "I see you are feeling frustrated. Tomorrow, things will improve." +* [Leave] -> finale + +=== finale === +Bye diff --git a/pink/parser.lua b/pink/parser.lua index 46a9e35..ed184fa 100644 --- a/pink/parser.lua +++ b/pink/parser.lua @@ -36,11 +36,12 @@ local ink = P({ stmt = glue + divert + V'knott' + V'stitch' + optionDiv + comm, text = C((1-nl-V'stmt')^1) *wh, + textE = C((1-nl-V'stmt')^0) *wh, - optionAnsWithDiv = V'text' * optionDiv * V'text' * wh, - optionAnsWithoutDiv = V'text' * Cc ''* Cc ''* wh, -- huh? + optionAnsWithDiv = V'textE' * optionDiv * V'textE' * wh, + optionAnsWithoutDiv = V'textE' * Cc ''* Cc ''* wh, -- huh? optionHead = P'*'/'option' * sp * (V'optionAnsWithDiv' + V'optionAnsWithoutDiv'), - option = Ct(V'optionHead' * (V'line'-V'optionHead')^0 * wh), + option = Ct(V'optionHead' * (V'line'-V'optionHead'-V'knott'-V'stitch')^0 * wh), --TODO which can by toplevel only? choice = Ct(Cc'choice' * V'option'^1), para = Ct(Cc'para' * V'text'), diff --git a/pink/runtime.lua b/pink/runtime.lua index 6e40e3c..32bdb2a 100644 --- a/pink/runtime.lua +++ b/pink/runtime.lua @@ -10,15 +10,37 @@ end return function (tree) - --print(to_string(tree)) + print(to_string(tree)) local s = {} local pointer = 1 local tab = tree + local knots = {} + + local process = function () + for i, v in ipairs(tree) do + if is('knot', v) then + knots[v[2]] = v + end + end + end + local update = function () local next = tab[pointer] + if is('knot', next) then + tab = next + pointer = 3 + next = tab[pointer] + end + + if is('divert', next) then + tab = knots[next[2]] + pointer = 3 + next = tab[pointer] + end + s.canContinue = is('para', next) s.currentChoices = {} @@ -26,14 +48,14 @@ return function (tree) for i=2, #next do --print(to_string(next[i])) table.insert(s.currentChoices, { - text = next[i][2] .. (next[i][3] or ''), + text = (next[i][2] or '') .. (next[i][3] or ''), choiceText = next[i][2] .. (next[i][4] or ''), }) end end end - s.canContinue = true + s.canContinue = nil s.continue = function() local res = getPara(tab[pointer]) @@ -42,7 +64,7 @@ return function (tree) return res; end - s.currentChoices = {} + s.currentChoices = nil s.chooseChoiceIndex = function(index) s.currentChoices = {} @@ -56,5 +78,8 @@ return function (tree) s.choosePathString = function(knotName) end s.variablesState = {} -- s.state.ToJson();s.state.LoadJson(savedJson); + + update() + process() return s end diff --git a/test/choices.lua b/test/choices.lua index 06321fc..d11c1ae 100644 --- a/test/choices.lua +++ b/test/choices.lua @@ -1,5 +1,6 @@ return { ink=[[ +== start == * I dont know * "I am somewhat tired[."]," I repeated. "Really," he responded. @@ -8,26 +9,33 @@ ink=[[ "Very good, *then." * I said no more "Ah,". "I see you" +== finale == ]], expected= { - {'choice', - {"option", "I dont know", "", ""}, { - "option", - '"I am somewhat tired', - '."', - '," I repeated.', - {"para", '"Really," he responded.'}, - {"para", '"How deleterious."'} + "knot", + "start", + { + "choice", + {"option", "I dont know", "", ""}, + { + "option", + '"I am somewhat tired', + '."', + '," I repeated.', + {"para", '"Really," he responded.'}, + {"para", '"How deleterious."'} + }, + { + "option", + '"Nothing, Monsieur!"', + "", + " I replied.", + {"para", '"Very good, *then."'} + }, + {"option", "I said no more", "", "", {"para", '"Ah,". "I see you"'}} + } }, - { - "option", - '"Nothing, Monsieur!"', - "", - " I replied.", - {"para", '"Very good, *then."'} - }, - {"option", 'I said no more', '', '', - {"para", '"Ah,". "I see you"'}} - } -}} + {"knot", "finale"} +} +}