1
0
Fork 0
mirror of https://github.com/Reuh/candran.git synced 2025-10-27 09:59:29 +00:00

Add #set to define preprocessors constants from an imported file

This commit is contained in:
Étienne Fildadut 2021-06-18 14:37:45 +02:00
parent d4102f1af6
commit 53f715cc6f
2 changed files with 177 additions and 357 deletions

View file

@ -184,6 +184,10 @@ function candran.preprocess(input, options={})
error("invalid macro type %s":format(tostring(iast.tag))) error("invalid macro type %s":format(tostring(iast.tag)))
end end
end end
env.set = function(identifier, value)
options.preprocessorEnv[identifier] = value
env[identifier] = value
end
-- default macros -- default macros
if options.builtInMacros then if options.builtInMacros then

View file

@ -2220,14 +2220,6 @@ end, -- ./compiler/lua54.can:876
}, { ["__index"] = function(self, key) -- ./compiler/lua54.can:889 }, { ["__index"] = function(self, key) -- ./compiler/lua54.can:889
error("don't know how to compile a " .. tostring(key) .. " to " .. targetName) -- ./compiler/lua54.can:890 error("don't know how to compile a " .. tostring(key) .. " to " .. targetName) -- ./compiler/lua54.can:890
end }) -- ./compiler/lua54.can:890 end }) -- ./compiler/lua54.can:890
targetName = "Lua 5.3" -- ./compiler/lua53.can:1
tags["AttributeId"] = function(t) -- ./compiler/lua53.can:4
if t[2] then -- ./compiler/lua53.can:5
error("target " .. targetName .. " does not support variable attributes") -- ./compiler/lua53.can:6
else -- ./compiler/lua53.can:6
return t[1] -- ./compiler/lua53.can:8
end -- ./compiler/lua53.can:8
end -- ./compiler/lua53.can:8
local code = lua(ast) .. newline() -- ./compiler/lua54.can:896 local code = lua(ast) .. newline() -- ./compiler/lua54.can:896
return requireStr .. code -- ./compiler/lua54.can:897 return requireStr .. code -- ./compiler/lua54.can:897
end -- ./compiler/lua54.can:897 end -- ./compiler/lua54.can:897
@ -3167,39 +3159,6 @@ end, -- ./compiler/lua54.can:876
}, { ["__index"] = function(self, key) -- ./compiler/lua54.can:889 }, { ["__index"] = function(self, key) -- ./compiler/lua54.can:889
error("don't know how to compile a " .. tostring(key) .. " to " .. targetName) -- ./compiler/lua54.can:890 error("don't know how to compile a " .. tostring(key) .. " to " .. targetName) -- ./compiler/lua54.can:890
end }) -- ./compiler/lua54.can:890 end }) -- ./compiler/lua54.can:890
targetName = "Lua 5.3" -- ./compiler/lua53.can:1
tags["AttributeId"] = function(t) -- ./compiler/lua53.can:4
if t[2] then -- ./compiler/lua53.can:5
error("target " .. targetName .. " does not support variable attributes") -- ./compiler/lua53.can:6
else -- ./compiler/lua53.can:6
return t[1] -- ./compiler/lua53.can:8
end -- ./compiler/lua53.can:8
end -- ./compiler/lua53.can:8
targetName = "Lua 5.2" -- ./compiler/lua52.can:1
APPEND = function(t, toAppend) -- ./compiler/lua52.can:3
return "do" .. indent() .. "local " .. var("a") .. ", " .. var("p") .. " = { " .. toAppend .. " }, #" .. t .. "+1" .. newline() .. "for i=1, #" .. var("a") .. " do" .. indent() .. t .. "[" .. var("p") .. "] = " .. var("a") .. "[i]" .. newline() .. "" .. var("p") .. " = " .. var("p") .. " + 1" .. unindent() .. "end" .. unindent() .. "end" -- ./compiler/lua52.can:4
end -- ./compiler/lua52.can:4
tags["_opid"]["idiv"] = function(left, right) -- ./compiler/lua52.can:7
return "math.floor(" .. lua(left) .. " / " .. lua(right) .. ")" -- ./compiler/lua52.can:8
end -- ./compiler/lua52.can:8
tags["_opid"]["band"] = function(left, right) -- ./compiler/lua52.can:10
return "bit32.band(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/lua52.can:11
end -- ./compiler/lua52.can:11
tags["_opid"]["bor"] = function(left, right) -- ./compiler/lua52.can:13
return "bit32.bor(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/lua52.can:14
end -- ./compiler/lua52.can:14
tags["_opid"]["bxor"] = function(left, right) -- ./compiler/lua52.can:16
return "bit32.bxor(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/lua52.can:17
end -- ./compiler/lua52.can:17
tags["_opid"]["shl"] = function(left, right) -- ./compiler/lua52.can:19
return "bit32.lshift(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/lua52.can:20
end -- ./compiler/lua52.can:20
tags["_opid"]["shr"] = function(left, right) -- ./compiler/lua52.can:22
return "bit32.rshift(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/lua52.can:23
end -- ./compiler/lua52.can:23
tags["_opid"]["bnot"] = function(right) -- ./compiler/lua52.can:25
return "bit32.bnot(" .. lua(right) .. ")" -- ./compiler/lua52.can:26
end -- ./compiler/lua52.can:26
local code = lua(ast) .. newline() -- ./compiler/lua54.can:896 local code = lua(ast) .. newline() -- ./compiler/lua54.can:896
return requireStr .. code -- ./compiler/lua54.can:897 return requireStr .. code -- ./compiler/lua54.can:897
end -- ./compiler/lua54.can:897 end -- ./compiler/lua54.can:897
@ -4143,67 +4102,6 @@ end, -- ./compiler/lua54.can:876
}, { ["__index"] = function(self, key) -- ./compiler/lua54.can:889 }, { ["__index"] = function(self, key) -- ./compiler/lua54.can:889
error("don't know how to compile a " .. tostring(key) .. " to " .. targetName) -- ./compiler/lua54.can:890 error("don't know how to compile a " .. tostring(key) .. " to " .. targetName) -- ./compiler/lua54.can:890
end }) -- ./compiler/lua54.can:890 end }) -- ./compiler/lua54.can:890
targetName = "Lua 5.3" -- ./compiler/lua53.can:1
tags["AttributeId"] = function(t) -- ./compiler/lua53.can:4
if t[2] then -- ./compiler/lua53.can:5
error("target " .. targetName .. " does not support variable attributes") -- ./compiler/lua53.can:6
else -- ./compiler/lua53.can:6
return t[1] -- ./compiler/lua53.can:8
end -- ./compiler/lua53.can:8
end -- ./compiler/lua53.can:8
targetName = "Lua 5.2" -- ./compiler/lua52.can:1
APPEND = function(t, toAppend) -- ./compiler/lua52.can:3
return "do" .. indent() .. "local " .. var("a") .. ", " .. var("p") .. " = { " .. toAppend .. " }, #" .. t .. "+1" .. newline() .. "for i=1, #" .. var("a") .. " do" .. indent() .. t .. "[" .. var("p") .. "] = " .. var("a") .. "[i]" .. newline() .. "" .. var("p") .. " = " .. var("p") .. " + 1" .. unindent() .. "end" .. unindent() .. "end" -- ./compiler/lua52.can:4
end -- ./compiler/lua52.can:4
tags["_opid"]["idiv"] = function(left, right) -- ./compiler/lua52.can:7
return "math.floor(" .. lua(left) .. " / " .. lua(right) .. ")" -- ./compiler/lua52.can:8
end -- ./compiler/lua52.can:8
tags["_opid"]["band"] = function(left, right) -- ./compiler/lua52.can:10
return "bit32.band(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/lua52.can:11
end -- ./compiler/lua52.can:11
tags["_opid"]["bor"] = function(left, right) -- ./compiler/lua52.can:13
return "bit32.bor(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/lua52.can:14
end -- ./compiler/lua52.can:14
tags["_opid"]["bxor"] = function(left, right) -- ./compiler/lua52.can:16
return "bit32.bxor(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/lua52.can:17
end -- ./compiler/lua52.can:17
tags["_opid"]["shl"] = function(left, right) -- ./compiler/lua52.can:19
return "bit32.lshift(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/lua52.can:20
end -- ./compiler/lua52.can:20
tags["_opid"]["shr"] = function(left, right) -- ./compiler/lua52.can:22
return "bit32.rshift(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/lua52.can:23
end -- ./compiler/lua52.can:23
tags["_opid"]["bnot"] = function(right) -- ./compiler/lua52.can:25
return "bit32.bnot(" .. lua(right) .. ")" -- ./compiler/lua52.can:26
end -- ./compiler/lua52.can:26
targetName = "LuaJIT" -- ./compiler/luajit.can:1
UNPACK = function(list, i, j) -- ./compiler/luajit.can:3
return "unpack(" .. list .. (i and (", " .. i .. (j and (", " .. j) or "")) or "") .. ")" -- ./compiler/luajit.can:4
end -- ./compiler/luajit.can:4
tags["_opid"]["band"] = function(left, right) -- ./compiler/luajit.can:7
addRequire("bit", "band", "band") -- ./compiler/luajit.can:8
return var("band") .. "(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/luajit.can:9
end -- ./compiler/luajit.can:9
tags["_opid"]["bor"] = function(left, right) -- ./compiler/luajit.can:11
addRequire("bit", "bor", "bor") -- ./compiler/luajit.can:12
return var("bor") .. "(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/luajit.can:13
end -- ./compiler/luajit.can:13
tags["_opid"]["bxor"] = function(left, right) -- ./compiler/luajit.can:15
addRequire("bit", "bxor", "bxor") -- ./compiler/luajit.can:16
return var("bxor") .. "(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/luajit.can:17
end -- ./compiler/luajit.can:17
tags["_opid"]["shl"] = function(left, right) -- ./compiler/luajit.can:19
addRequire("bit", "lshift", "lshift") -- ./compiler/luajit.can:20
return var("lshift") .. "(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/luajit.can:21
end -- ./compiler/luajit.can:21
tags["_opid"]["shr"] = function(left, right) -- ./compiler/luajit.can:23
addRequire("bit", "rshift", "rshift") -- ./compiler/luajit.can:24
return var("rshift") .. "(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/luajit.can:25
end -- ./compiler/luajit.can:25
tags["_opid"]["bnot"] = function(right) -- ./compiler/luajit.can:27
addRequire("bit", "bnot", "bnot") -- ./compiler/luajit.can:28
return var("bnot") .. "(" .. lua(right) .. ")" -- ./compiler/luajit.can:29
end -- ./compiler/luajit.can:29
local code = lua(ast) .. newline() -- ./compiler/lua54.can:896 local code = lua(ast) .. newline() -- ./compiler/lua54.can:896
return requireStr .. code -- ./compiler/lua54.can:897 return requireStr .. code -- ./compiler/lua54.can:897
end -- ./compiler/lua54.can:897 end -- ./compiler/lua54.can:897
@ -5151,92 +5049,6 @@ end, -- ./compiler/lua54.can:876
}, { ["__index"] = function(self, key) -- ./compiler/lua54.can:889 }, { ["__index"] = function(self, key) -- ./compiler/lua54.can:889
error("don't know how to compile a " .. tostring(key) .. " to " .. targetName) -- ./compiler/lua54.can:890 error("don't know how to compile a " .. tostring(key) .. " to " .. targetName) -- ./compiler/lua54.can:890
end }) -- ./compiler/lua54.can:890 end }) -- ./compiler/lua54.can:890
targetName = "Lua 5.3" -- ./compiler/lua53.can:1
tags["AttributeId"] = function(t) -- ./compiler/lua53.can:4
if t[2] then -- ./compiler/lua53.can:5
error("target " .. targetName .. " does not support variable attributes") -- ./compiler/lua53.can:6
else -- ./compiler/lua53.can:6
return t[1] -- ./compiler/lua53.can:8
end -- ./compiler/lua53.can:8
end -- ./compiler/lua53.can:8
targetName = "Lua 5.2" -- ./compiler/lua52.can:1
APPEND = function(t, toAppend) -- ./compiler/lua52.can:3
return "do" .. indent() .. "local " .. var("a") .. ", " .. var("p") .. " = { " .. toAppend .. " }, #" .. t .. "+1" .. newline() .. "for i=1, #" .. var("a") .. " do" .. indent() .. t .. "[" .. var("p") .. "] = " .. var("a") .. "[i]" .. newline() .. "" .. var("p") .. " = " .. var("p") .. " + 1" .. unindent() .. "end" .. unindent() .. "end" -- ./compiler/lua52.can:4
end -- ./compiler/lua52.can:4
tags["_opid"]["idiv"] = function(left, right) -- ./compiler/lua52.can:7
return "math.floor(" .. lua(left) .. " / " .. lua(right) .. ")" -- ./compiler/lua52.can:8
end -- ./compiler/lua52.can:8
tags["_opid"]["band"] = function(left, right) -- ./compiler/lua52.can:10
return "bit32.band(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/lua52.can:11
end -- ./compiler/lua52.can:11
tags["_opid"]["bor"] = function(left, right) -- ./compiler/lua52.can:13
return "bit32.bor(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/lua52.can:14
end -- ./compiler/lua52.can:14
tags["_opid"]["bxor"] = function(left, right) -- ./compiler/lua52.can:16
return "bit32.bxor(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/lua52.can:17
end -- ./compiler/lua52.can:17
tags["_opid"]["shl"] = function(left, right) -- ./compiler/lua52.can:19
return "bit32.lshift(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/lua52.can:20
end -- ./compiler/lua52.can:20
tags["_opid"]["shr"] = function(left, right) -- ./compiler/lua52.can:22
return "bit32.rshift(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/lua52.can:23
end -- ./compiler/lua52.can:23
tags["_opid"]["bnot"] = function(right) -- ./compiler/lua52.can:25
return "bit32.bnot(" .. lua(right) .. ")" -- ./compiler/lua52.can:26
end -- ./compiler/lua52.can:26
targetName = "LuaJIT" -- ./compiler/luajit.can:1
UNPACK = function(list, i, j) -- ./compiler/luajit.can:3
return "unpack(" .. list .. (i and (", " .. i .. (j and (", " .. j) or "")) or "") .. ")" -- ./compiler/luajit.can:4
end -- ./compiler/luajit.can:4
tags["_opid"]["band"] = function(left, right) -- ./compiler/luajit.can:7
addRequire("bit", "band", "band") -- ./compiler/luajit.can:8
return var("band") .. "(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/luajit.can:9
end -- ./compiler/luajit.can:9
tags["_opid"]["bor"] = function(left, right) -- ./compiler/luajit.can:11
addRequire("bit", "bor", "bor") -- ./compiler/luajit.can:12
return var("bor") .. "(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/luajit.can:13
end -- ./compiler/luajit.can:13
tags["_opid"]["bxor"] = function(left, right) -- ./compiler/luajit.can:15
addRequire("bit", "bxor", "bxor") -- ./compiler/luajit.can:16
return var("bxor") .. "(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/luajit.can:17
end -- ./compiler/luajit.can:17
tags["_opid"]["shl"] = function(left, right) -- ./compiler/luajit.can:19
addRequire("bit", "lshift", "lshift") -- ./compiler/luajit.can:20
return var("lshift") .. "(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/luajit.can:21
end -- ./compiler/luajit.can:21
tags["_opid"]["shr"] = function(left, right) -- ./compiler/luajit.can:23
addRequire("bit", "rshift", "rshift") -- ./compiler/luajit.can:24
return var("rshift") .. "(" .. lua(left) .. ", " .. lua(right) .. ")" -- ./compiler/luajit.can:25
end -- ./compiler/luajit.can:25
tags["_opid"]["bnot"] = function(right) -- ./compiler/luajit.can:27
addRequire("bit", "bnot", "bnot") -- ./compiler/luajit.can:28
return var("bnot") .. "(" .. lua(right) .. ")" -- ./compiler/luajit.can:29
end -- ./compiler/luajit.can:29
targetName = "Lua 5.1" -- ./compiler/lua51.can:1
states["continue"] = {} -- ./compiler/lua51.can:3
CONTINUE_START = function() -- ./compiler/lua51.can:5
return "local " .. var("break") .. newline() .. "repeat" .. indent() .. push("continue", var("break")) -- ./compiler/lua51.can:6
end -- ./compiler/lua51.can:6
CONTINUE_STOP = function() -- ./compiler/lua51.can:8
return pop("continue") .. unindent() .. "until true" .. newline() .. "if " .. var("break") .. " then break end" -- ./compiler/lua51.can:9
end -- ./compiler/lua51.can:9
tags["Continue"] = function() -- ./compiler/lua51.can:12
return "break" -- ./compiler/lua51.can:13
end -- ./compiler/lua51.can:13
tags["Break"] = function() -- ./compiler/lua51.can:15
local inContinue = peek("continue") -- ./compiler/lua51.can:16
if inContinue then -- ./compiler/lua51.can:17
return inContinue .. " = true" .. newline() .. "break" -- ./compiler/lua51.can:18
else -- ./compiler/lua51.can:18
return "break" -- ./compiler/lua51.can:20
end -- ./compiler/lua51.can:20
end -- ./compiler/lua51.can:20
tags["Goto"] = function() -- ./compiler/lua51.can:25
error("target " .. targetName .. " does not support gotos") -- ./compiler/lua51.can:26
end -- ./compiler/lua51.can:26
tags["Label"] = function() -- ./compiler/lua51.can:28
error("target " .. targetName .. " does not support goto labels") -- ./compiler/lua51.can:29
end -- ./compiler/lua51.can:29
local code = lua(ast) .. newline() -- ./compiler/lua54.can:896 local code = lua(ast) .. newline() -- ./compiler/lua54.can:896
return requireStr .. code -- ./compiler/lua54.can:897 return requireStr .. code -- ./compiler/lua54.can:897
end -- ./compiler/lua54.can:897 end -- ./compiler/lua54.can:897
@ -7158,182 +6970,186 @@ else -- candran.can:182
error(("invalid macro type %s"):format(tostring(iast["tag"]))) -- candran.can:184 error(("invalid macro type %s"):format(tostring(iast["tag"]))) -- candran.can:184
end -- candran.can:184 end -- candran.can:184
end -- candran.can:184 end -- candran.can:184
if options["builtInMacros"] then -- candran.can:189 env["set"] = function(identifier, value) -- candran.can:187
env["define"]("__STR__(x)", function(x) -- candran.can:190 options["preprocessorEnv"][identifier] = value -- candran.can:188
return ("%q"):format(x) -- candran.can:190 env[identifier] = value -- candran.can:189
end) -- candran.can:190 end -- candran.can:189
local s = require("candran.serpent") -- candran.can:191 if options["builtInMacros"] then -- candran.can:193
env["define"]("__CONSTEXPR__(expr)", function(expr) -- candran.can:192 env["define"]("__STR__(x)", function(x) -- candran.can:194
return s["block"](assert(candran["load"](expr))(), { ["fatal"] = true }) -- candran.can:193 return ("%q"):format(x) -- candran.can:194
end) -- candran.can:193 end) -- candran.can:194
end -- candran.can:193 local s = require("candran.serpent") -- candran.can:195
local preprocess, err = candran["compile"](preprocessor, options) -- candran.can:198 env["define"]("__CONSTEXPR__(expr)", function(expr) -- candran.can:196
if not preprocess then -- candran.can:199 return s["block"](assert(candran["load"](expr))(), { ["fatal"] = true }) -- candran.can:197
return nil, "in preprocessor: " .. err -- candran.can:200 end) -- candran.can:197
end -- candran.can:200 end -- candran.can:197
preprocess, err = util["load"](preprocessor, "candran preprocessor", env) -- candran.can:203 local preprocess, err = candran["compile"](preprocessor, options) -- candran.can:202
if not preprocess then -- candran.can:204 if not preprocess then -- candran.can:203
return nil, "in preprocessor: " .. err -- candran.can:205 return nil, "in preprocessor: " .. err -- candran.can:204
end -- candran.can:205 end -- candran.can:204
local success, output = pcall(preprocess) -- candran.can:209 preprocess, err = util["load"](preprocessor, "candran preprocessor", env) -- candran.can:207
if not success then -- candran.can:210 if not preprocess then -- candran.can:208
return nil, "in preprocessor: " .. output -- candran.can:211 return nil, "in preprocessor: " .. err -- candran.can:209
end -- candran.can:211 end -- candran.can:209
return output, macros -- candran.can:214 local success, output = pcall(preprocess) -- candran.can:213
end -- candran.can:214 if not success then -- candran.can:214
candran["compile"] = function(input, options, macros) -- candran.can:224 return nil, "in preprocessor: " .. output -- candran.can:215
if options == nil then options = {} end -- candran.can:224 end -- candran.can:215
options = util["merge"](candran["default"], options) -- candran.can:225 return output, macros -- candran.can:218
local ast, errmsg = parser["parse"](input, options["chunkname"]) -- candran.can:227 end -- candran.can:218
if not ast then -- candran.can:229 candran["compile"] = function(input, options, macros) -- candran.can:228
return nil, errmsg -- candran.can:230 if options == nil then options = {} end -- candran.can:228
end -- candran.can:230 options = util["merge"](candran["default"], options) -- candran.can:229
return require("compiler." .. options["target"])(input, ast, options, macros) -- candran.can:233 local ast, errmsg = parser["parse"](input, options["chunkname"]) -- candran.can:231
end -- candran.can:233 if not ast then -- candran.can:233
candran["make"] = function(code, options) -- candran.can:242 return nil, errmsg -- candran.can:234
local r, err = candran["preprocess"](code, options) -- candran.can:243 end -- candran.can:234
if r then -- candran.can:244 return require("compiler." .. options["target"])(input, ast, options, macros) -- candran.can:237
r, err = candran["compile"](r, options, err) -- candran.can:245 end -- candran.can:237
if r then -- candran.can:246 candran["make"] = function(code, options) -- candran.can:246
return r -- candran.can:247 local r, err = candran["preprocess"](code, options) -- candran.can:247
end -- candran.can:247 if r then -- candran.can:248
end -- candran.can:247 r, err = candran["compile"](r, options, err) -- candran.can:249
return r, err -- candran.can:250 if r then -- candran.can:250
end -- candran.can:250 return r -- candran.can:251
local errorRewritingActive = false -- candran.can:253 end -- candran.can:251
local codeCache = {} -- candran.can:254 end -- candran.can:251
candran["loadfile"] = function(filepath, env, options) -- candran.can:257 return r, err -- candran.can:254
local f, err = io["open"](filepath) -- candran.can:258 end -- candran.can:254
if not f then -- candran.can:259 local errorRewritingActive = false -- candran.can:257
return nil, ("cannot open %s"):format(tostring(err)) -- candran.can:260 local codeCache = {} -- candran.can:258
end -- candran.can:260 candran["loadfile"] = function(filepath, env, options) -- candran.can:261
local content = f:read("*a") -- candran.can:262 local f, err = io["open"](filepath) -- candran.can:262
f:close() -- candran.can:263 if not f then -- candran.can:263
return candran["load"](content, filepath, env, options) -- candran.can:265 return nil, ("cannot open %s"):format(tostring(err)) -- candran.can:264
end -- candran.can:265 end -- candran.can:264
candran["load"] = function(chunk, chunkname, env, options) -- candran.can:270 local content = f:read("*a") -- candran.can:266
if options == nil then options = {} end -- candran.can:270 f:close() -- candran.can:267
options = util["merge"]({ ["chunkname"] = tostring(chunkname or chunk) }, options) -- candran.can:271 return candran["load"](content, filepath, env, options) -- candran.can:269
local code, err = candran["make"](chunk, options) -- candran.can:273 end -- candran.can:269
if not code then -- candran.can:274 candran["load"] = function(chunk, chunkname, env, options) -- candran.can:274
return code, err -- candran.can:275 if options == nil then options = {} end -- candran.can:274
end -- candran.can:275 options = util["merge"]({ ["chunkname"] = tostring(chunkname or chunk) }, options) -- candran.can:275
codeCache[options["chunkname"]] = code -- candran.can:278 local code, err = candran["make"](chunk, options) -- candran.can:277
local f -- candran.can:279 if not code then -- candran.can:278
f, err = util["load"](code, ("=%s(%s)"):format(options["chunkname"], "compiled candran"), env) -- candran.can:280 return code, err -- candran.can:279
if f == nil then -- candran.can:285 end -- candran.can:279
return f, "candran unexpectedly generated invalid code: " .. err -- candran.can:286 codeCache[options["chunkname"]] = code -- candran.can:282
end -- candran.can:286 local f -- candran.can:283
if options["rewriteErrors"] == false then -- candran.can:289 f, err = util["load"](code, ("=%s(%s)"):format(options["chunkname"], "compiled candran"), env) -- candran.can:284
return f -- candran.can:290 if f == nil then -- candran.can:289
else -- candran.can:290 return f, "candran unexpectedly generated invalid code: " .. err -- candran.can:290
return function(...) -- candran.can:292 end -- candran.can:290
if not errorRewritingActive then -- candran.can:293 if options["rewriteErrors"] == false then -- candran.can:293
errorRewritingActive = true -- candran.can:294 return f -- candran.can:294
local t = { xpcall(f, candran["messageHandler"], ...) } -- candran.can:295 else -- candran.can:294
errorRewritingActive = false -- candran.can:296 return function(...) -- candran.can:296
if t[1] == false then -- candran.can:297 if not errorRewritingActive then -- candran.can:297
error(t[2], 0) -- candran.can:298 errorRewritingActive = true -- candran.can:298
end -- candran.can:298 local t = { xpcall(f, candran["messageHandler"], ...) } -- candran.can:299
return unpack(t, 2) -- candran.can:300 errorRewritingActive = false -- candran.can:300
else -- candran.can:300 if t[1] == false then -- candran.can:301
return f(...) -- candran.can:302 error(t[2], 0) -- candran.can:302
end -- candran.can:302 end -- candran.can:302
end -- candran.can:302 return unpack(t, 2) -- candran.can:304
end -- candran.can:302 else -- candran.can:304
end -- candran.can:302 return f(...) -- candran.can:306
candran["dofile"] = function(filename, options) -- candran.can:310 end -- candran.can:306
local f, err = candran["loadfile"](filename, nil, options) -- candran.can:311 end -- candran.can:306
if f == nil then -- candran.can:313 end -- candran.can:306
error(err) -- candran.can:314 end -- candran.can:306
else -- candran.can:314 candran["dofile"] = function(filename, options) -- candran.can:314
return f() -- candran.can:316 local f, err = candran["loadfile"](filename, nil, options) -- candran.can:315
end -- candran.can:316 if f == nil then -- candran.can:317
end -- candran.can:316 error(err) -- candran.can:318
candran["messageHandler"] = function(message, noTraceback) -- candran.can:322 else -- candran.can:318
message = tostring(message) -- candran.can:323 return f() -- candran.can:320
end -- candran.can:320
end -- candran.can:320
candran["messageHandler"] = function(message, noTraceback) -- candran.can:326
message = tostring(message) -- candran.can:327
if not noTraceback and not message:match("\ if not noTraceback and not message:match("\
stack traceback:\ stack traceback:\
") then -- candran.can:324 ") then -- candran.can:328
message = debug["traceback"](message, 2) -- candran.can:325 message = debug["traceback"](message, 2) -- candran.can:329
end -- candran.can:325 end -- candran.can:329
return message:gsub("(\ return message:gsub("(\
?%s*)([^\ ?%s*)([^\
]-)%:(%d+)%:", function(indentation, source, line) -- candran.can:327 ]-)%:(%d+)%:", function(indentation, source, line) -- candran.can:331
line = tonumber(line) -- candran.can:328 line = tonumber(line) -- candran.can:332
local originalFile -- candran.can:330 local originalFile -- candran.can:334
local strName = source:match("^(.-)%(compiled candran%)$") -- candran.can:331 local strName = source:match("^(.-)%(compiled candran%)$") -- candran.can:335
if strName then -- candran.can:332 if strName then -- candran.can:336
if codeCache[strName] then -- candran.can:333 if codeCache[strName] then -- candran.can:337
originalFile = codeCache[strName] -- candran.can:334 originalFile = codeCache[strName] -- candran.can:338
source = strName -- candran.can:335 source = strName -- candran.can:339
end -- candran.can:335 end -- candran.can:339
else -- candran.can:335 else -- candran.can:339
do -- candran.can:338 do -- candran.can:342
local fi -- candran.can:338 local fi -- candran.can:342
fi = io["open"](source, "r") -- candran.can:338 fi = io["open"](source, "r") -- candran.can:342
if fi then -- candran.can:338 if fi then -- candran.can:342
originalFile = fi:read("*a") -- candran.can:339 originalFile = fi:read("*a") -- candran.can:343
fi:close() -- candran.can:340 fi:close() -- candran.can:344
end -- candran.can:340 end -- candran.can:344
end -- candran.can:340 end -- candran.can:344
end -- candran.can:340 end -- candran.can:344
if originalFile then -- candran.can:344 if originalFile then -- candran.can:348
local i = 0 -- candran.can:345 local i = 0 -- candran.can:349
for l in (originalFile .. "\ for l in (originalFile .. "\
"):gmatch("([^\ "):gmatch("([^\
]*)\ ]*)\
") do -- candran.can:346 ") do -- candran.can:350
i = i + 1 -- candran.can:347 i = i + 1 -- candran.can:351
if i == line then -- candran.can:348 if i == line then -- candran.can:352
local extSource, lineMap = l:match(".*%-%- (.-)%:(%d+)$") -- candran.can:349 local extSource, lineMap = l:match(".*%-%- (.-)%:(%d+)$") -- candran.can:353
if lineMap then -- candran.can:350 if lineMap then -- candran.can:354
if extSource ~= source then -- candran.can:351 if extSource ~= source then -- candran.can:355
return indentation .. extSource .. ":" .. lineMap .. "(" .. extSource .. ":" .. line .. "):" -- candran.can:352 return indentation .. extSource .. ":" .. lineMap .. "(" .. extSource .. ":" .. line .. "):" -- candran.can:356
else -- candran.can:352 else -- candran.can:356
return indentation .. extSource .. ":" .. lineMap .. "(" .. line .. "):" -- candran.can:354 return indentation .. extSource .. ":" .. lineMap .. "(" .. line .. "):" -- candran.can:358
end -- candran.can:354 end -- candran.can:358
end -- candran.can:354 end -- candran.can:358
break -- candran.can:357 break -- candran.can:361
end -- candran.can:357 end -- candran.can:361
end -- candran.can:357 end -- candran.can:361
end -- candran.can:357 end -- candran.can:361
end) -- candran.can:357 end) -- candran.can:361
end -- candran.can:357 end -- candran.can:361
candran["searcher"] = function(modpath) -- candran.can:365 candran["searcher"] = function(modpath) -- candran.can:369
local filepath = util["search"](modpath, { "can" }) -- candran.can:366 local filepath = util["search"](modpath, { "can" }) -- candran.can:370
if not filepath then -- candran.can:367 if not filepath then -- candran.can:371
if _VERSION == "Lua 5.4" then -- candran.can:368 if _VERSION == "Lua 5.4" then -- candran.can:372
return "no candran file in package.path" -- candran.can:369 return "no candran file in package.path" -- candran.can:373
else -- candran.can:369 else -- candran.can:373
return "\ return "\
\9no candran file in package.path" -- candran.can:371 \9no candran file in package.path" -- candran.can:375
end -- candran.can:371 end -- candran.can:375
end -- candran.can:371 end -- candran.can:375
return function(modpath) -- candran.can:374 return function(modpath) -- candran.can:378
local r, s = candran["loadfile"](filepath) -- candran.can:375 local r, s = candran["loadfile"](filepath) -- candran.can:379
if r then -- candran.can:376 if r then -- candran.can:380
return r(modpath, filepath) -- candran.can:377 return r(modpath, filepath) -- candran.can:381
else -- candran.can:377 else -- candran.can:381
error(("error loading candran module '%s' from file '%s':\ error(("error loading candran module '%s' from file '%s':\
\9%s"):format(modpath, filepath, tostring(s)), 0) -- candran.can:379 \9%s"):format(modpath, filepath, tostring(s)), 0) -- candran.can:383
end -- candran.can:379 end -- candran.can:383
end, filepath -- candran.can:381 end, filepath -- candran.can:385
end -- candran.can:381 end -- candran.can:385
candran["setup"] = function() -- candran.can:385 candran["setup"] = function() -- candran.can:389
local searchers = (function() -- candran.can:386 local searchers = (function() -- candran.can:390
if _VERSION == "Lua 5.1" then -- candran.can:386 if _VERSION == "Lua 5.1" then -- candran.can:390
return package["loaders"] -- candran.can:387 return package["loaders"] -- candran.can:391
else -- candran.can:387 else -- candran.can:391
return package["searchers"] -- candran.can:389 return package["searchers"] -- candran.can:393
end -- candran.can:389 end -- candran.can:393
end)() -- candran.can:389 end)() -- candran.can:393
for _, s in ipairs(searchers) do -- candran.can:392 for _, s in ipairs(searchers) do -- candran.can:396
if s == candran["searcher"] then -- candran.can:393 if s == candran["searcher"] then -- candran.can:397
return candran -- candran.can:394 return candran -- candran.can:398
end -- candran.can:394 end -- candran.can:398
end -- candran.can:394 end -- candran.can:398
table["insert"](searchers, 1, candran["searcher"]) -- candran.can:398 table["insert"](searchers, 1, candran["searcher"]) -- candran.can:402
return candran -- candran.can:399 return candran -- candran.can:403
end -- candran.can:399 end -- candran.can:403
return candran -- candran.can:402 return candran -- candran.can:406