Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reimplemented litmoon #217

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/moonc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ local function scan_directory(root, collected)
scan_directory(full_path, collected)
end

if fname:match("%.moon$") then
if fname:match("%.moon$") or fname:match("%.litmoon$") then
table.insert(collected, full_path)
end
end
Expand Down Expand Up @@ -225,7 +225,7 @@ local function create_watcher(files)

for _, ev in ipairs(events) do
local fname = ev.name
if fname:match("%.moon$") then
if fname:match("%.moon$") or fname:match("%.litmoon$") then
local dir = wd_table[ev.wd]
if dir ~= "./" then
fname = dir .. fname
Expand Down
48 changes: 29 additions & 19 deletions moonscript/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ local lua = {
loadstring = loadstring,
load = load
}
local dirsep, line_tables, create_moonpath, to_lua, moon_loader, loadstring, loadfile, dofile, insert_loader, remove_loader
local dirsep, line_tables, create_moonpath, to_lua, moon_loader, loadstring_factory, loadstring, loadfile, dofile, insert_loader, remove_loader
dirsep = "/"
line_tables = require("moonscript.line_tables")
create_moonpath = function(package_path)
Expand All @@ -32,7 +32,7 @@ create_moonpath = function(package_path)
_continue_0 = true
break
end
local _value_0 = prefix .. ".moon"
local _value_0 = prefix .. ".moon;" .. prefix .. ".litmoon"
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
_continue_0 = true
Expand All @@ -45,15 +45,18 @@ create_moonpath = function(package_path)
end
return concat(moonpaths, ";")
end
to_lua = function(text, options)
to_lua = function(text, options, litmoon)
if options == nil then
options = { }
end
if litmoon == nil then
litmoon = false
end
if "string" ~= type(text) then
local t = type(text)
return nil, "expecting string (got " .. t .. ")"
end
local tree, err = parse.string(text)
local tree, err = parse.string(text, litmoon)
if not tree then
return nil, err
end
Expand All @@ -76,37 +79,43 @@ moon_loader = function(name)
if file then
local text = file:read("*a")
file:close()
local res, err = loadstring(text, "@" .. tostring(file_path))
local res, err = loadstring_factory(file_path:sub(-8) == ".litmoon")(text, "@" .. tostring(file_path))
if not res then
error(file_path .. ": " .. err)
end
return res
end
return nil, "Could not find moon file"
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
loadstring_factory = function(litmoon)
if litmoon == nil then
litmoon = false
end
if chunk_name then
line_tables[chunk_name] = ltable_or_err
return 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, litmoon)
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
return (lua.loadstring or lua.load)(code, chunk_name, unpack({
mode,
env
}))
end
loadstring = loadstring_factory()
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, "@" .. tostring(fname), ...)
return loadstring_factory(fname:sub(-8) == ".litmoon")(text, "@" .. tostring(fname), ...)
end
dofile = function(...)
local f = assert(loadfile(...))
Expand Down Expand Up @@ -149,5 +158,6 @@ return {
dofile = dofile,
loadfile = loadfile,
loadstring = loadstring,
create_moonpath = create_moonpath
create_moonpath = create_moonpath,
loadlitstring = loadstring_factory(true)
}
34 changes: 19 additions & 15 deletions moonscript/base.moon
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ create_moonpath = (package_path) ->
moonpaths = for path in *split package_path, ";"
prefix = path\match "^(.-)%.lua$"
continue unless prefix
prefix .. ".moon"
prefix .. ".moon;"..prefix .. ".litmoon"
concat moonpaths, ";"

to_lua = (text, options={}) ->
to_lua = (text, options={}, litmoon=false) ->
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think adding another argument specific to litmoon is a good change to this interface. (If we add more formats are we supposed to keep adding new arguments?) Any reason you didn't re-purpose options?

if "string" != type text
t = type text
return nil, "expecting string (got ".. t ..")"

tree, err = parse.string text
tree, err = parse.string text, litmoon
if not tree
return nil, err

Expand All @@ -46,7 +46,7 @@ moon_loader = (name) ->
if file
text = file\read "*a"
file\close!
res, err = loadstring text, "@#{file_path}"
res, err = loadstring_factory(file_path\sub(-8)==".litmoon") text, "@#{file_path}"
if not res
error file_path .. ": " .. err

Expand All @@ -55,24 +55,27 @@ moon_loader = (name) ->
return nil, "Could not find moon file"


loadstring = (...) ->
options, str, chunk_name, mode, env = get_options ...
chunk_name or= "=(moonscript.loadstring)"
loadstring_factory = (litmoon=false)->
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

factory unnecessary if we use options for passing litmoon

(...) ->
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
code, ltable_or_err = to_lua str, options, litmoon
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 }
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 }

loadstring = loadstring_factory!

loadfile = (fname, ...) ->
file, err = io.open fname
return nil, err unless file
text = assert file\read "*a"
file\close!
loadstring text, "@#{fname}", ...
loadstring_factory(fname\sub(-8)==".litmoon") text, "@#{fname}", ...

-- throws errros
dofile = (...) ->
Expand Down Expand Up @@ -103,6 +106,7 @@ remove_loader = ->
{
_NAME: "moonscript"
:insert_loader, :remove_loader, :to_lua, :moon_loader, :dirsep,
:dofile, :loadfile, :loadstring, :create_moonpath
:dofile, :loadfile, :loadstring, :create_moonpath,
loadlitstring: loadstring_factory true
}

107 changes: 57 additions & 50 deletions moonscript/cmd/moonc.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local lfs = require("lfs")
local split
split = require("moonscript.util").split
local dirsep, dirsep_chars, mkdir, normalize_dir, parse_dir, parse_file, convert_path, format_time, gettime, compile_file_text, write_file, compile_and_write, is_abs_path, path_to_target
local dirsep, dirsep_chars, mkdir, normalize_dir, parse_dir, parse_file, convert_path, format_time, gettime, compile_file_text_factory, compile_file_text, write_file, compile_and_write, is_abs_path, path_to_target
dirsep = package.config:sub(1, 1)
if dirsep == "\\" then
dirsep_chars = "\\/"
Expand Down Expand Up @@ -55,57 +55,63 @@ do
end
end
end
compile_file_text = function(text, opts)
if opts == nil then
opts = { }
end
local parse = require("moonscript.parse")
local compile = require("moonscript.compile")
local parse_time
if opts.benchmark then
parse_time = assert(gettime())
end
local tree, err = parse.string(text)
if not (tree) then
return nil, err
end
if parse_time then
parse_time = gettime() - parse_time
compile_file_text_factory = function(litmoon)
if litmoon == nil then
litmoon = false
end
if opts.show_parse_tree then
local dump = require("moonscript.dump")
dump.tree(tree)
return true
end
local compile_time
if opts.benchmark then
compile_time = gettime()
end
local code, posmap_or_err, err_pos = compile.tree(tree)
if not (code) then
return nil, compile.format_error(posmap_or_err, err_pos, text)
end
if compile_time then
compile_time = gettime() - compile_time
end
if opts.show_posmap then
local debug_posmap
debug_posmap = require("moonscript.util").debug_posmap
print("Pos", "Lua", ">>", "Moon")
print(debug_posmap(posmap_or_err, text, code))
return true
end
if opts.benchmark then
print(table.concat({
opts.fname or "stdin",
"Parse time \t" .. format_time(parse_time),
"Compile time\t" .. format_time(compile_time),
""
}, "\n"))
return nil
return function(text, opts)
if opts == nil then
opts = { }
end
local parse = require("moonscript.parse")
local compile = require("moonscript.compile")
local parse_time
if opts.benchmark then
parse_time = assert(gettime())
end
local tree, err = parse.string(text, litmoon)
if not (tree) then
return nil, err
end
if parse_time then
parse_time = gettime() - parse_time
end
if opts.show_parse_tree then
local dump = require("moonscript.dump")
dump.tree(tree)
return true
end
local compile_time
if opts.benchmark then
compile_time = gettime()
end
local code, posmap_or_err, err_pos = compile.tree(tree)
if not (code) then
return nil, compile.format_error(posmap_or_err, err_pos, text)
end
if compile_time then
compile_time = gettime() - compile_time
end
if opts.show_posmap then
local debug_posmap
debug_posmap = require("moonscript.util").debug_posmap
print("Pos", "Lua", ">>", "Moon")
print(debug_posmap(posmap_or_err, text, code))
return true
end
if opts.benchmark then
print(table.concat({
opts.fname or "stdin",
"Parse time \t" .. format_time(parse_time),
"Compile time\t" .. format_time(compile_time),
""
}, "\n"))
return nil
end
return code
end
return code
end
compile_file_text = compile_file_text_factory()
write_file = function(fname, code)
mkdir(parse_dir(fname))
local f, err = io.open(fname, "w")
Expand All @@ -127,7 +133,7 @@ compile_and_write = function(src, dest, opts)
end
local text = assert(f:read("*a"))
f:close()
local code, err = compile_file_text(text, opts)
local code, err = compile_file_text_factory(src:sub(-8) == ".litmoon")(text, opts)
if not code then
return nil, err
end
Expand Down Expand Up @@ -187,5 +193,6 @@ return {
format_time = format_time,
path_to_target = path_to_target,
compile_file_text = compile_file_text,
compile_file_text_factory = compile_file_text_factory,
compile_and_write = compile_and_write
}
Loading