1
0
Fork 0
mirror of https://github.com/Oreolek/ink-instead.git synced 2024-05-20 18:08:22 +03:00
ink-instead/pink/pink.lua

90 lines
2.6 KiB
Lua
Raw Normal View History

2017-01-22 01:21:12 +02:00
-- FIXME clean up
2016-12-10 01:01:22 +02:00
2018-04-24 11:31:27 +03:00
-- if not arg[1] and not (...) then error("Usage: `require` this file from a script or call `lua pink/pink.lua parse game.ink`") end
-- local folderOfThisFile = arg[1] and string.sub(..., 1, string.len(arg[1]))==arg[1] and arg[0]:match("(.-)[^/\\]+$") or (...):match("(.-)[^%.]+$")
local folderOfThisFile = './pink/'
local getParser = function () return require(folderOfThisFile .. 'parser') end
2016-12-10 01:01:22 +02:00
local runtime = require(folderOfThisFile .. 'runtime')
2016-12-04 12:33:32 +02:00
2017-01-15 02:50:55 +02:00
local function read(file) -- TODO should this be here or in client code? At lease allow to pass an ink content in a string
2017-01-26 01:20:23 +02:00
if love and love.filesystem then
if not love.filesystem.isFile(file) then error('failed to open "'..file..'"') end
2017-01-15 02:50:55 +02:00
local content, size = love.filesystem.read(file)
return content
else
2016-12-04 12:33:32 +02:00
local f = io.open(file, "rb")
2016-12-31 16:02:22 +02:00
if not f then error('failed to open "'..file..'"') end
2016-12-04 12:33:32 +02:00
local content = f:read("*all")
f:close()
return content
2017-01-15 02:50:55 +02:00
end
2016-12-04 12:33:32 +02:00
end
2017-01-22 01:02:26 +02:00
local function basename(str) return string.gsub(str, "(.*/)(.*)", "%2") end
local function basedir(str) return string.gsub(str, "(.*)(/.*)", "%1") end
2016-12-04 12:33:32 +02:00
2017-01-22 01:21:12 +02:00
local parse;
parse = function(f)
local parsed = {}
for _,t in ipairs(getParser():match(read(f))) do
if t[2] and t[1]=='include' then
for _,included in ipairs(parse(basedir(f)..'/'..t[2])) do
table.insert(parsed, included)
2017-01-22 01:02:26 +02:00
end
2017-01-22 01:21:12 +02:00
else
table.insert(parsed, t)
2017-01-22 01:02:26 +02:00
end
2017-01-22 01:21:12 +02:00
end
return parsed
end
2016-12-04 12:33:32 +02:00
local api = {
2016-12-04 12:33:32 +02:00
getStory = function (filename)
local parsed
if not pcall(function ()
parsed = require (string.sub(filename, 1, -5))
print('loaded precompiled story')
end) then
2017-01-22 01:21:12 +02:00
parsed = parse(filename)
print('story compiled')
end
return runtime(parsed)
2017-01-22 01:02:26 +02:00
end
2016-12-04 12:33:32 +02:00
}
2017-01-22 01:02:26 +02:00
2016-12-04 12:33:32 +02:00
2017-01-22 01:21:12 +02:00
local function dump ( t ) -- tables only
local function sub_print_r(t)
if (type(t)=="table") then
local b = ""
for pos,val in pairs(t) do
if (type(val)=="table") then
b = b .. "{"..sub_print_r(val).."},"
elseif (type(val)=="string") then
2017-01-22 01:21:12 +02:00
b = b .. '"'..string.gsub(val,'"', '\\"')..'",'
else
2017-01-22 01:21:12 +02:00
b = b .. tostring(val) .. ','
end
end
return b
else
return tostring(t)
end
end
return "-- This file was generated from an .ink file using the pink library - do not edit\nreturn {" .. sub_print_r(t) .. "}"
end
2018-04-24 11:31:27 +03:00
--[[
if arg[1] == 'parse' and arg[2] then
2017-01-22 01:21:12 +02:00
print(dump(parse(arg[2])))
end
2018-04-24 11:31:27 +03:00
]]
2017-01-22 01:02:26 +02:00
return api