move everything into base.moon, expose insert_loader

This commit is contained in:
leaf corcoran 2013-10-06 11:09:13 -07:00
parent 8035bc7338
commit 0f4f01d000
4 changed files with 206 additions and 207 deletions

View File

@ -1,2 +1,112 @@
_G.moon_no_loader = true
return require("moonscript")
local compile = require("moonscript.compile")
local parse = require("moonscript.parse")
local concat, insert
do
local _obj_0 = table
concat, insert = _obj_0.concat, _obj_0.insert
end
local split, dump, get_options, unpack
do
local _obj_0 = require("moonscript.util")
split, dump, get_options, unpack = _obj_0.split, _obj_0.dump, _obj_0.get_options, _obj_0.unpack
end
local lua = {
loadstring = loadstring,
load = load
}
local dirsep, line_tables, create_moonpath, to_lua, moon_loader, loadstring, loadfile, dofile, insert_loader
dirsep = "/"
line_tables = require("moonscript.line_tables")
create_moonpath = function(package_path)
local paths = split(package_path, ";")
for i, path in ipairs(paths) do
local p = path:match("^(.-)%.lua$")
if p then
paths[i] = p .. ".moon"
end
end
return concat(paths, ";")
end
to_lua = function(text, options)
if options == nil then
options = { }
end
if "string" ~= type(text) then
local t = type(text)
return nil, "expecting string (got " .. t .. ")", 2
end
local tree, err = parse.string(text)
if not tree then
return nil, err
end
local code, ltable, pos = compile.tree(tree, options)
if not code then
return nil, compile.format_error(ltable, pos, text), 2
end
return code, ltable
end
moon_loader = function(name)
local name_path = name:gsub("%.", dirsep)
local file, file_path
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")
file:close()
return loadstring(text, file_path)
else
return nil, "Could not find moon file"
end
end
loadstring = function(...)
local options, str, chunk_name, mode, env = get_options(...)
chunk_name = chunk_name or "=(moonscript.loadstring)"
local code, ltable_or_err = to_lua(str, options)
if not (code) then
return nil, ltable_or_err
end
if chunk_name then
line_tables[chunk_name] = ltable_or_err
end
return (lua.loadstring or lua.load)(code, chunk_name, unpack({
mode,
env
}))
end
loadfile = function(fname, ...)
local file, err = io.open(fname)
if not (file) then
return nil, err
end
local text = assert(file:read("*a"))
file:close()
return loadstring(text, fname, ...)
end
dofile = function(...)
local f = assert(loadfile(...))
return f()
end
insert_loader = function()
if not package.moonpath then
package.moonpath = create_moonpath(package.path)
end
return insert(package.loaders or package.searchers, 2, moon_loader)
end
return {
_NAME = "moonscript",
insert_loader = insert_loader,
to_lua = to_lua,
moon_chunk = moon_chunk,
moon_loader = moon_loader,
dirsep = dirsep,
dofile = dofile,
loadfile = loadfile,
loadstring = loadstring
}

View File

@ -1,2 +1,89 @@
_G.moon_no_loader = true
require "moonscript"
compile = require "moonscript.compile"
parse = require "moonscript.parse"
import concat, insert from table
import split, dump, get_options, unpack from require "moonscript.util"
lua = :loadstring, :load
local *
dirsep = "/"
line_tables = require "moonscript.line_tables"
-- create moon path package from lua package path
create_moonpath = (package_path) ->
paths = split package_path, ";"
for i, path in ipairs paths
p = path\match "^(.-)%.lua$"
if p then paths[i] = p..".moon"
concat paths, ";"
to_lua = (text, options={}) ->
if "string" != type text
t = type text
return nil, "expecting string (got ".. t ..")", 2
tree, err = parse.string text
if not tree
return nil, err
code, ltable, pos = compile.tree tree, options
if not code
return nil, compile.format_error(ltable, pos, text), 2
code, ltable
moon_loader = (name) ->
name_path = name\gsub "%.", dirsep
local file, file_path
for path in *split package.moonpath, ";"
file_path = path\gsub "?", name_path
file = io.open file_path
break if file
if file
text = file\read "*a"
file\close!
loadstring text, file_path
else
nil, "Could not find moon file"
loadstring = (...) ->
options, str, chunk_name, mode, env = get_options ...
chunk_name or= "=(moonscript.loadstring)"
code, ltable_or_err = to_lua str, options
unless code
return nil, ltable_or_err
line_tables[chunk_name] = ltable_or_err if chunk_name
-- the unpack prevents us from passing nil
(lua.loadstring or lua.load) code, chunk_name, unpack { mode, env }
loadfile = (fname, ...) ->
file, err = io.open fname
return nil, err unless file
text = assert file\read "*a"
file\close!
loadstring text, fname, ...
-- throws errros
dofile = (...) ->
f = assert loadfile ...
f!
insert_loader = ->
if not package.moonpath
package.moonpath = create_moonpath package.path
insert package.loaders or package.searchers, 2, moon_loader
{
_NAME: "moonscript"
:insert_loader, :to_lua, :moon_chunk, :moon_loader, :dirsep, :dofile,
:loadfile, :loadstring
}

View File

@ -1,114 +1,5 @@
local compile = require("moonscript.compile")
local parse = require("moonscript.parse")
local concat, insert
do
local _obj_0 = table
concat, insert = _obj_0.concat, _obj_0.insert
local _with_0 = require("moonscript.base")
_with_0.insert_loader()
return _with_0
end
local split, dump, get_options, unpack
do
local _obj_0 = require("moonscript.util")
split, dump, get_options, unpack = _obj_0.split, _obj_0.dump, _obj_0.get_options, _obj_0.unpack
end
local lua = {
loadstring = loadstring,
load = load
}
local dirsep, line_tables, create_moonpath, to_lua, moon_loader, init_loader, loadstring, loadfile, dofile
dirsep = "/"
line_tables = require("moonscript.line_tables")
create_moonpath = function(package_path)
local paths = split(package_path, ";")
for i, path in ipairs(paths) do
local p = path:match("^(.-)%.lua$")
if p then
paths[i] = p .. ".moon"
end
end
return concat(paths, ";")
end
to_lua = function(text, options)
if options == nil then
options = { }
end
if "string" ~= type(text) then
local t = type(text)
return nil, "expecting string (got " .. t .. ")", 2
end
local tree, err = parse.string(text)
if not tree then
return nil, err
end
local code, ltable, pos = compile.tree(tree, options)
if not code then
return nil, compile.format_error(ltable, pos, text), 2
end
return code, ltable
end
moon_loader = function(name)
local name_path = name:gsub("%.", dirsep)
local file, file_path
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")
file:close()
return loadstring(text, file_path)
else
return nil, "Could not find moon file"
end
end
if not package.moonpath then
package.moonpath = create_moonpath(package.path)
end
init_loader = function()
return insert(package.loaders or package.searchers, 2, moon_loader)
end
if not (_G.moon_no_loader) then
init_loader()
end
loadstring = function(...)
local options, str, chunk_name, mode, env = get_options(...)
chunk_name = chunk_name or "=(moonscript.loadstring)"
local code, ltable_or_err = to_lua(str, options)
if not (code) then
return nil, ltable_or_err
end
if chunk_name then
line_tables[chunk_name] = ltable_or_err
end
return (lua.loadstring or lua.load)(code, chunk_name, unpack({
mode,
env
}))
end
loadfile = function(fname, ...)
local file, err = io.open(fname)
if not (file) then
return nil, err
end
local text = assert(file:read("*a"))
file:close()
return loadstring(text, fname, ...)
end
dofile = function(...)
local f = assert(loadfile(...))
return f()
end
return {
_NAME = "moonscript",
to_lua = to_lua,
moon_chunk = moon_chunk,
moon_loader = moon_loader,
dirsep = dirsep,
dofile = dofile,
loadfile = loadfile,
loadstring = loadstring
}

View File

@ -1,91 +1,2 @@
compile = require "moonscript.compile"
parse = require "moonscript.parse"
import concat, insert from table
import split, dump, get_options, unpack from require "moonscript.util"
lua = :loadstring, :load
local *
dirsep = "/"
line_tables = require "moonscript.line_tables"
-- create moon path package from lua package path
create_moonpath = (package_path) ->
paths = split package_path, ";"
for i, path in ipairs paths
p = path\match "^(.-)%.lua$"
if p then paths[i] = p..".moon"
concat paths, ";"
to_lua = (text, options={}) ->
if "string" != type text
t = type text
return nil, "expecting string (got ".. t ..")", 2
tree, err = parse.string text
if not tree
return nil, err
code, ltable, pos = compile.tree tree, options
if not code
return nil, compile.format_error(ltable, pos, text), 2
code, ltable
moon_loader = (name) ->
name_path = name\gsub "%.", dirsep
local file, file_path
for path in *split package.moonpath, ";"
file_path = path\gsub "?", name_path
file = io.open file_path
break if file
if file
text = file\read "*a"
file\close!
loadstring text, file_path
else
nil, "Could not find moon file"
if not package.moonpath
package.moonpath = create_moonpath package.path
init_loader = ->
insert package.loaders or package.searchers, 2, moon_loader
init_loader! unless _G.moon_no_loader
loadstring = (...) ->
options, str, chunk_name, mode, env = get_options ...
chunk_name or= "=(moonscript.loadstring)"
code, ltable_or_err = to_lua str, options
unless code
return nil, ltable_or_err
line_tables[chunk_name] = ltable_or_err if chunk_name
-- the unpack prevents us from passing nil
(lua.loadstring or lua.load) code, chunk_name, unpack { mode, env }
loadfile = (fname, ...) ->
file, err = io.open fname
return nil, err unless file
text = assert file\read "*a"
file\close!
loadstring text, fname, ...
-- throws errros
dofile = (...) ->
f = assert loadfile ...
f!
{
_NAME: "moonscript"
:to_lua, :moon_chunk, :moon_loader, :dirsep, :dofile, :loadfile,
:loadstring
}
with require "moonscript.base"
.insert_loader!