From 73e2ecc8b9d0e86c3a9e2a3df16a55ad52377808 Mon Sep 17 00:00:00 2001 From: premek <1145361+premek@users.noreply.github.com> Date: Sun, 2 Apr 2023 23:49:18 +0200 Subject: [PATCH] initial pink support --- drivers/pink-runner.lua | 91 +++++++++++++++++++++++++++ drivers/pink_compiler_compiler_driver | 9 +++ drivers/pink_runtime_driver | 9 +++ install_deps.py | 6 ++ 4 files changed, 115 insertions(+) create mode 100755 drivers/pink-runner.lua create mode 100755 drivers/pink_compiler_compiler_driver create mode 100755 drivers/pink_runtime_driver diff --git a/drivers/pink-runner.lua b/drivers/pink-runner.lua new file mode 100755 index 0000000..8ca10d3 --- /dev/null +++ b/drivers/pink-runner.lua @@ -0,0 +1,91 @@ +#!/bin/env lua + +local parser = require('pink.parser') +local runtime = require('pink.runtime') + +function dump(o, indent) + indent = indent or 1; + local sp = function (ind) + return (" "):rep(ind*2) + end + + if type(o) == 'table' then + local s = '{\n' + for k,v in pairs(o) do + if type(k) ~= 'number' then k = '"'..k..'"' end + s = s .. sp(indent)..'['..k..'] = ' .. dump(v, indent+1) .. ',\n' + end + return s .. sp(indent-1) .. '}' + elseif type(o) == 'string' then + return "'"..(o:gsub("\\", "\\\\"):gsub("'", "\\'")).."'" + else + return tostring(o) + end +end + +function read(filename) + local f = io.open(filename, "rb") + if not f then error('failed to open "'..filename..'"') end + local content = f:read("*all") + f:close() + return content +end + +function write(filename, content) + local f = io.open(filename, "wb") + if not f then error('failed to open "'..filename..'"') end + f:write(content) + f:close() +end + +function exists(filename) + local f = io.open(filename, "r") + return f ~= nil and io.close(f) +end + + +local action = arg[1] + +if action == 'compile' then + local inkFile = arg[2] + local jsonFile = arg[3] -- needs to be created to indicate successful compilation + local luaFile = jsonFile:gsub('.json', '_pink.lua') -- compile result + write(luaFile, "return "..dump(parser(read(inkFile), inkFile))) + write(jsonFile, "[\"json not supported\"]") + +elseif action == 'run' then + local jsonFile = arg[2] + local reqLuaFile = jsonFile:gsub('.json', '_pink') -- compile result; .lua added when calling require + + if not exists(reqLuaFile..".lua") then + io.stderr:write('only internal pink format supported\n') + os.exit(1) + end + + local story = runtime(require(reqLuaFile)) + + while true do + while story.canContinue do + print(story.continue()) + end + if #story.currentChoices == 0 then break end -- cannot continue and there are no choices + print() + for i = 1, #story.currentChoices do + print(i .. ": " .. story.currentChoices[i].text) + end + -- TODO tags + local answer = tonumber(io.read()) + if not answer or answer > #story.currentChoices then + io.stderr:write('invalid answer '..tostring(answer)..'\n') + os.exit(1) + end + print ('?> '..story.currentChoices[answer].choiceText) + story.chooseChoiceIndex(answer) + end + + + + +end + + diff --git a/drivers/pink_compiler_compiler_driver b/drivers/pink_compiler_compiler_driver new file mode 100755 index 0000000..ecd8ee7 --- /dev/null +++ b/drivers/pink_compiler_compiler_driver @@ -0,0 +1,9 @@ +#!/usr/bin/env python3 + +import sys +import os + +SRC = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'pink-runner.lua') +ROOTDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +PINKDIR = os.path.join(ROOTDIR, 'deps', 'pink') +os.execlpe(SRC, 'pink-runner.lua', "compile", sys.argv[3], sys.argv[2], {"LUA_PATH": PINKDIR+'/?.lua'}) diff --git a/drivers/pink_runtime_driver b/drivers/pink_runtime_driver new file mode 100755 index 0000000..d8cc1e0 --- /dev/null +++ b/drivers/pink_runtime_driver @@ -0,0 +1,9 @@ +#!/usr/bin/env python3 + +import sys +import os + +SRC = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'pink-runner.lua') +ROOTDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +PINKDIR = os.path.join(ROOTDIR, 'deps', 'pink') +os.execlpe(SRC, 'pink-runner.lua', "run", sys.argv[1], {"LUA_PATH": PINKDIR+'/?.lua;'+ROOTDIR+'/?.lua'}) diff --git a/install_deps.py b/install_deps.py index ef7b34f..f73ac42 100755 --- a/install_deps.py +++ b/install_deps.py @@ -156,6 +156,12 @@ 'c7818565141ec4a42bc0c6ccf415510c21729db2', 'all'), + # Pink + ('deps/pink.zip', + 'https://github.com/premek/pink/archive/8d753c3cce63f1d2d85bdb96d60f8161f63a5a69.zip', + '547e15b7aaa1a2a416ff9a5b6f5255a8acf61131', + 'all'), + # Tachyons ('deps/tachyons.min.css', 'https://unpkg.com/tachyons@4.12.0/css/tachyons.min.css',