2017-01-22 01:21:12 +02:00
|
|
|
-- FIXME clean up
|
2016-12-10 01:01:22 +02:00
|
|
|
|
2017-01-16 02:12:43 +02: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 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
|
|
|
|
|
|
|
|
2017-01-16 02:12:43 +02:00
|
|
|
local api = {
|
2016-12-04 12:33:32 +02:00
|
|
|
getStory = function (filename)
|
2017-01-16 02:12:43 +02:00
|
|
|
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)
|
2017-01-16 02:12:43 +02:00
|
|
|
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
|
|
|
|
2017-01-16 02:12:43 +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,'"', '\\"')..'",'
|
2017-01-16 02:12:43 +02:00
|
|
|
else
|
2017-01-22 01:21:12 +02:00
|
|
|
b = b .. tostring(val) .. ','
|
2017-01-16 02:12:43 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if arg[1] == 'parse' and arg[2] then
|
2017-01-22 01:21:12 +02:00
|
|
|
print(dump(parse(arg[2])))
|
2017-01-16 02:12:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-01-22 01:02:26 +02:00
|
|
|
return api
|