loadstring, loadfile, and dofile moonscript functions

This commit is contained in:
leaf corcoran 2011-10-30 09:22:00 -07:00
parent 74060bccae
commit bfef2bd06a
3 changed files with 45 additions and 19 deletions

View File

@ -1,4 +1,7 @@
compile:
moonc moon/ moonscript/
local: local:
luarocks make --local moonscript-dev-1.rockspec luarocks make --local moonscript-dev-1.rockspec

View File

@ -36,7 +36,7 @@ if not script_fname then
end end
local file, err = io.open(script_fname) 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 = { local new_arg = {
[-1] = arg[0], [-1] = arg[0],

View File

@ -4,6 +4,10 @@ require("moonscript.parse")
require("moonscript.util") require("moonscript.util")
local concat, insert = table.concat, table.insert local concat, insert = table.concat, table.insert
local split, dump = util.split, util.dump local split, dump = util.split, util.dump
local lua = {
loadstring = loadstring,
load = load
}
dirsep = "/" dirsep = "/"
line_tables = { } line_tables = { }
local create_moonpath local create_moonpath
@ -17,20 +21,22 @@ create_moonpath = function(package_path)
end end
return concat(paths, ";") return concat(paths, ";")
end end
moon_chunk = function(file, file_path) to_lua = function(text)
local text = file:read("*a")
if not text then
error("Could not read file")
end
local tree, err = parse.string(text) local tree, err = parse.string(text)
if not tree then if not tree then
error("Parse error: " .. err) error("Parse error: " .. err, 2)
end end
local code, ltable, pos = compile.tree(tree) local code, ltable, pos = compile.tree(tree)
if not code then 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 end
line_tables[file_path] = ltable
local runner local runner
runner = function() runner = function()
do do
@ -39,23 +45,25 @@ moon_chunk = function(file, file_path)
return _with_0 return _with_0
end end
end end
return load(runner, file_path) return lua.load(runner, source_path)
end end
moon_loader = function(name) moon_loader = function(name)
local name_path = name:gsub("%.", dirsep) local name_path = name:gsub("%.", dirsep)
local file, file_path = nil, nil local file, file_path = nil, nil
do local _list_0 = split(package.moonpath, ";")
local _item_0 = split(package.moonpath, ";") for _index_0 = 1, #_list_0 do
for _index_0 = 1, #_item_0 do local path = _list_0[_index_0]
local path = _item_0[_index_0] file_path = path:gsub("?", name_path)
file_path = path:gsub("?", name_path) file = io.open(file_path)
file = io.open(file_path) if file then
if file then break
break
end
end end
end end
if file then 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) return moon_chunk(file, file_path)
else else
return nil, "Could not find moon file" return nil, "Could not find moon file"
@ -71,3 +79,18 @@ end
if not _G.moon_no_loader then if not _G.moon_no_loader then
init_loader() init_loader()
end 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