From bfef2bd06abd71d3259c61e2e9297d3304d2d210 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 30 Oct 2011 09:22:00 -0700 Subject: [PATCH] loadstring, loadfile, and dofile moonscript functions --- Makefile | 3 +++ bin/moon | 2 +- moonscript/init.lua | 59 +++++++++++++++++++++++++++++++-------------- 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index fb2cbac..12e552a 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,7 @@ +compile: + moonc moon/ moonscript/ + local: luarocks make --local moonscript-dev-1.rockspec diff --git a/bin/moon b/bin/moon index 17d7e01..5705727 100755 --- a/bin/moon +++ b/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], diff --git a/moonscript/init.lua b/moonscript/init.lua index e8ed93a..6ab2cf7 100644 --- a/moonscript/init.lua +++ b/moonscript/init.lua @@ -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] - file_path = path:gsub("?", name_path) - file = io.open(file_path) - if file then - break - end + 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 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