mirror of
https://github.com/leafo/moonscript.git
synced 2024-11-22 02:44:23 +00:00
loadstring, loadfile, and dofile moonscript functions
This commit is contained in:
parent
74060bccae
commit
bfef2bd06a
3
Makefile
3
Makefile
@ -1,4 +1,7 @@
|
||||
|
||||
compile:
|
||||
moonc moon/ moonscript/
|
||||
|
||||
local:
|
||||
luarocks make --local moonscript-dev-1.rockspec
|
||||
|
||||
|
2
bin/moon
2
bin/moon
@ -36,7 +36,7 @@ if not script_fname then
|
||||
end
|
||||
|
||||
local file, err = io.open(script_fname)
|
||||
if not file then error(err) end
|
||||
if not file then error(err, 2) end
|
||||
|
||||
local new_arg = {
|
||||
[-1] = arg[0],
|
||||
|
@ -4,6 +4,10 @@ require("moonscript.parse")
|
||||
require("moonscript.util")
|
||||
local concat, insert = table.concat, table.insert
|
||||
local split, dump = util.split, util.dump
|
||||
local lua = {
|
||||
loadstring = loadstring,
|
||||
load = load
|
||||
}
|
||||
dirsep = "/"
|
||||
line_tables = { }
|
||||
local create_moonpath
|
||||
@ -17,20 +21,22 @@ create_moonpath = function(package_path)
|
||||
end
|
||||
return concat(paths, ";")
|
||||
end
|
||||
moon_chunk = function(file, file_path)
|
||||
local text = file:read("*a")
|
||||
if not text then
|
||||
error("Could not read file")
|
||||
end
|
||||
to_lua = function(text)
|
||||
local tree, err = parse.string(text)
|
||||
if not tree then
|
||||
error("Parse error: " .. err)
|
||||
error("Parse error: " .. err, 2)
|
||||
end
|
||||
local code, ltable, pos = compile.tree(tree)
|
||||
if not code then
|
||||
error(compile.format_error(ltable, pos, text))
|
||||
error(compile.format_error(ltable, pos, text), 2)
|
||||
end
|
||||
return code, ltable
|
||||
end
|
||||
moon_chunk = function(text, source_path)
|
||||
local code, ltable = to_lua(text)
|
||||
if source_path then
|
||||
line_tables[source_path] = ltable
|
||||
end
|
||||
line_tables[file_path] = ltable
|
||||
local runner
|
||||
runner = function()
|
||||
do
|
||||
@ -39,23 +45,25 @@ moon_chunk = function(file, file_path)
|
||||
return _with_0
|
||||
end
|
||||
end
|
||||
return load(runner, file_path)
|
||||
return lua.load(runner, source_path)
|
||||
end
|
||||
moon_loader = function(name)
|
||||
local name_path = name:gsub("%.", dirsep)
|
||||
local file, file_path = nil, nil
|
||||
do
|
||||
local _item_0 = split(package.moonpath, ";")
|
||||
for _index_0 = 1, #_item_0 do
|
||||
local path = _item_0[_index_0]
|
||||
local _list_0 = split(package.moonpath, ";")
|
||||
for _index_0 = 1, #_list_0 do
|
||||
local path = _list_0[_index_0]
|
||||
file_path = path:gsub("?", name_path)
|
||||
file = io.open(file_path)
|
||||
if file then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
if file then
|
||||
local text = file:read("*a")
|
||||
if not text then
|
||||
error("Could not read file", 2)
|
||||
end
|
||||
return moon_chunk(file, file_path)
|
||||
else
|
||||
return nil, "Could not find moon file"
|
||||
@ -71,3 +79,18 @@ end
|
||||
if not _G.moon_no_loader then
|
||||
init_loader()
|
||||
end
|
||||
loadstring = function(str)
|
||||
local code = to_lua(str)
|
||||
return lua.loadstring(code)
|
||||
end
|
||||
loadfile = function(fname)
|
||||
local file, err = io.open(fname)
|
||||
if not file then
|
||||
return nil, err
|
||||
end
|
||||
return loadstring(file:read("*a"))
|
||||
end
|
||||
dofile = function(fname)
|
||||
local f = assert(loadfile(fname))
|
||||
return f()
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user