diff --git a/examples/game.ink b/examples/game.ink index 4bde4a5..312cf77 100644 --- a/examples/game.ink +++ b/examples/game.ink @@ -6,18 +6,21 @@ We arrived into London at 9.45pm exactly. -> hurry_outside * "Monsieur, let us savour this moment!"[] I declared. - My master clouted me firmly around the head and dragged me out of the door. + My master clouted me firmly around the head +** A +**B + and dragged me out of the door. <> -> dragged_outside * [We hurried home] -> hurry_outside === hurry_outside === -We hurried home to Savile Row -> as_fast_as_we_could +We hurried home to Savile Row -> as_fast_as_we_could === dragged_outside === -He insisted that we hurried home to Savile Row +He insisted that we hurried <> home to Savile Row -> as_fast_as_we_could diff --git a/pink/parser.lua b/pink/parser.lua index c1eb162..e49b73e 100644 --- a/pink/parser.lua +++ b/pink/parser.lua @@ -22,7 +22,7 @@ local commOL = sp * '//' * sp * (1-nl)^0 * wh local commML = sp * '/*' * wh * (P(1)-'*/')^0 * '*/' * wh local comm = commOL + commML + todo -local glue = P'<>'/'glue' *wh +local glue = P'<>'/'glue' *wh -- FIXME do not consume spaces after glue local divertSym = '->' *wh local divertEndSym = C('END') *wh diff --git a/pink/runtime.lua b/pink/runtime.lua index 32bdb2a..7e02370 100644 --- a/pink/runtime.lua +++ b/pink/runtime.lua @@ -1,25 +1,26 @@ require('util') -- XXX local is = function (what, node) - return node ~= nil and type(node) == "table" and node[1] == what + return node ~= nil + and (type(node) == "table" and node[1] == what) + or (type(node) == "string" and node == what) end local getPara = function (node) if is('para', node) then return node[2] end end - return function (tree) - print(to_string(tree)) + --print(to_string(tree)) local s = {} - local pointer = 1 - local tab = tree + local pointer = nil + local tab = {} local knots = {} local process = function () - for i, v in ipairs(tree) do + for _, v in ipairs(tree) do if is('knot', v) then knots[v[2]] = v end @@ -55,12 +56,31 @@ return function (tree) end end + + local step = function () + pointer = pointer + 1 + update() + return tab[pointer] + end + + local stepTo = function (table, pos) + tab = table + pointer = pos + update() + return tab[pointer] + end + s.canContinue = nil s.continue = function() local res = getPara(tab[pointer]) - pointer = pointer + 1 - update() + local next = step() + + if is('glue', next) then + step() + res = res .. s.continue() + end + return res; end @@ -70,16 +90,14 @@ return function (tree) s.currentChoices = {} local choice = tab[pointer] local option = choice[1 + index] - tab = option - pointer = 5 - update() + stepTo(option, 5) end s.choosePathString = function(knotName) end s.variablesState = {} -- s.state.ToJson();s.state.LoadJson(savedJson); - update() + stepTo(tree, 1) process() return s end diff --git a/test.lua b/test.lua index 71221b9..4fa1684 100644 --- a/test.lua +++ b/test.lua @@ -24,6 +24,7 @@ function testNest() doTest('nested') end function testNest2() doTest('nested2') end function testKnot() doTest('knot') end function testBranching() doTest('branching') end +function testGlue() doTest('glue') end diff --git a/test/branching.lua b/test/branching.lua index 7dfe2a9..59f6429 100644 --- a/test/branching.lua +++ b/test/branching.lua @@ -8,7 +8,7 @@ We arrived into London at 9.45pm exactly. -> hurry_outside * "Monsieur, let us savour this moment!"[] I declared. - My master clouted me firmly around the head and dragged me out of the door. + My master clouted me firmly around the head and dragged me out of the door.<> -> dragged_outside * [We hurried home] -> hurry_outside @@ -49,6 +49,7 @@ expected= { "para", "My master clouted me firmly around the head and dragged me out of the door." }, + 'glue', {"divert", "dragged_outside"} }, {"option", "", "We hurried home", " ", {"divert", "hurry_outside"}} diff --git a/test/glue.lua b/test/glue.lua new file mode 100644 index 0000000..0171984 --- /dev/null +++ b/test/glue.lua @@ -0,0 +1,30 @@ +return { +ink=[[ +* "Monsieur, let us savour this moment!"[] I declared. + My master clouted me firmly around the head and dragged me out of the door. <> + -> dragged_outside +* [We hurried home] -> hurry_outside +=== as_fast_as_we_could === +<> as fast as we could. + +]], +expected={ + { + "choice", + { + "option", + '"Monsieur, let us savour this moment!"', + "", + " I declared.", + { + "para", + "My master clouted me firmly around the head and dragged me out of the door. " + }, + "glue", + {"divert", "dragged_outside"} + }, + {"option", "", "We hurried home", " ", {"divert", "hurry_outside"}} + }, + {"knot", "as_fast_as_we_could", "glue", {"para", "as fast as we could."}} -- TODO should be space before 'as' +} +}