mirror of
https://github.com/leafo/moonscript.git
synced 2024-11-22 02:44:23 +00:00
more moonc functions to module
This commit is contained in:
parent
c60d83c546
commit
a4e03813fc
55
bin/moonc
55
bin/moonc
@ -67,50 +67,9 @@ local normalize_dir = moonc.normalize_dir
|
|||||||
local parse_dir = moonc.parse_dir
|
local parse_dir = moonc.parse_dir
|
||||||
local parse_file = moonc.parse_file
|
local parse_file = moonc.parse_file
|
||||||
local convert_path = moonc.convert_path
|
local convert_path = moonc.convert_path
|
||||||
local compile_file_text = moonc.compile_file_text
|
local compile_and_write = moonc.compile_and_write
|
||||||
|
|
||||||
function write_file(fname, code)
|
local function scan_directory(root, collected)
|
||||||
if opts.p then
|
|
||||||
if code ~= "" then print(code) end
|
|
||||||
else
|
|
||||||
mkdir(parse_dir(fname))
|
|
||||||
local out_f = io.open(fname, "w")
|
|
||||||
if not out_f then
|
|
||||||
return nil, "Failed to write output: "..fname
|
|
||||||
end
|
|
||||||
|
|
||||||
out_f:write(code.."\n")
|
|
||||||
out_f:close()
|
|
||||||
end
|
|
||||||
|
|
||||||
return "built"
|
|
||||||
end
|
|
||||||
|
|
||||||
function compile_and_write(from, to)
|
|
||||||
local f = io.open(from)
|
|
||||||
if not f then
|
|
||||||
return nil, "Can't find file"
|
|
||||||
end
|
|
||||||
local text = f:read("*a")
|
|
||||||
|
|
||||||
local code, err = compile_file_text(text, from, {
|
|
||||||
benchmark = opts.b,
|
|
||||||
show_posmap = opts.X,
|
|
||||||
show_parse_tree = opts.T,
|
|
||||||
})
|
|
||||||
|
|
||||||
if not code and err then
|
|
||||||
return nil, err
|
|
||||||
end
|
|
||||||
|
|
||||||
if not code then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
return write_file(to, code)
|
|
||||||
end
|
|
||||||
|
|
||||||
function scan_directory(root, collected)
|
|
||||||
root = normalize_dir(root)
|
root = normalize_dir(root)
|
||||||
collected = collected or {}
|
collected = collected or {}
|
||||||
|
|
||||||
@ -131,7 +90,7 @@ function scan_directory(root, collected)
|
|||||||
return collected
|
return collected
|
||||||
end
|
end
|
||||||
|
|
||||||
function remove_dups(tbl, key_fn)
|
local function remove_dups(tbl, key_fn)
|
||||||
local hash = {}
|
local hash = {}
|
||||||
local final = {}
|
local final = {}
|
||||||
|
|
||||||
@ -156,7 +115,7 @@ local function is_abs_path(path)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- creates tuples of input and target
|
-- creates tuples of input and target
|
||||||
function get_files(fname, files)
|
local function get_files(fname, files)
|
||||||
files = files or {}
|
files = files or {}
|
||||||
|
|
||||||
if lfs.attributes(fname, "mode") == "directory" then
|
if lfs.attributes(fname, "mode") == "directory" then
|
||||||
@ -227,7 +186,7 @@ files = remove_dups(files, function(f)
|
|||||||
return f[2]
|
return f[2]
|
||||||
end)
|
end)
|
||||||
|
|
||||||
function get_sleep_func()
|
local function get_sleep_func()
|
||||||
local sleep
|
local sleep
|
||||||
if not pcall(function()
|
if not pcall(function()
|
||||||
require "socket"
|
require "socket"
|
||||||
@ -242,7 +201,7 @@ function get_sleep_func()
|
|||||||
return sleep
|
return sleep
|
||||||
end
|
end
|
||||||
|
|
||||||
function plural(count, word)
|
local function plural(count, word)
|
||||||
if count ~= 1 then
|
if count ~= 1 then
|
||||||
word = word .. "s"
|
word = word .. "s"
|
||||||
end
|
end
|
||||||
@ -250,7 +209,7 @@ function plural(count, word)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- returns an iterator that returns files that have been updated
|
-- returns an iterator that returns files that have been updated
|
||||||
function create_watcher(files)
|
local function create_watcher(files)
|
||||||
local msg = "Starting watch loop (Ctrl-C to exit)"
|
local msg = "Starting watch loop (Ctrl-C to exit)"
|
||||||
|
|
||||||
local inotify
|
local inotify
|
||||||
|
@ -4,14 +4,13 @@ do
|
|||||||
local _obj_0 = require("moonscript.util")
|
local _obj_0 = require("moonscript.util")
|
||||||
split = _obj_0.split
|
split = _obj_0.split
|
||||||
end
|
end
|
||||||
local dirsep = package.config:sub(1, 1)
|
local dirsep, dirsep_chars, mkdir, normalize_dir, parse_dir, parse_file, convert_path, format_time, gettime, compile_file_text, write_file, compile_and_write
|
||||||
local dirsep_chars
|
dirsep = package.config:sub(1, 1)
|
||||||
if dirsep == "\\" then
|
if dirsep == "\\" then
|
||||||
dirsep_chars = "\\/"
|
dirsep_chars = "\\/"
|
||||||
else
|
else
|
||||||
dirsep_chars = dirsep
|
dirsep_chars = dirsep
|
||||||
end
|
end
|
||||||
local mkdir
|
|
||||||
mkdir = function(path)
|
mkdir = function(path)
|
||||||
local chunks = split(path, dirsep)
|
local chunks = split(path, dirsep)
|
||||||
local accum
|
local accum
|
||||||
@ -22,19 +21,15 @@ mkdir = function(path)
|
|||||||
end
|
end
|
||||||
return lfs.attributes(path, "mode")
|
return lfs.attributes(path, "mode")
|
||||||
end
|
end
|
||||||
local normalize_dir
|
|
||||||
normalize_dir = function(path)
|
normalize_dir = function(path)
|
||||||
return path:match("^(.-)[" .. tostring(dirsep_chars) .. "]*$") .. dirsep
|
return path:match("^(.-)[" .. tostring(dirsep_chars) .. "]*$") .. dirsep
|
||||||
end
|
end
|
||||||
local parse_dir
|
|
||||||
parse_dir = function(path)
|
parse_dir = function(path)
|
||||||
return (path:match("^(.-)[^" .. tostring(dirsep_chars) .. "]*$"))
|
return (path:match("^(.-)[^" .. tostring(dirsep_chars) .. "]*$"))
|
||||||
end
|
end
|
||||||
local parse_file
|
|
||||||
parse_file = function(path)
|
parse_file = function(path)
|
||||||
return (path:match("^.-([^" .. tostring(dirsep_chars) .. "]*)$"))
|
return (path:match("^.-([^" .. tostring(dirsep_chars) .. "]*)$"))
|
||||||
end
|
end
|
||||||
local convert_path
|
|
||||||
convert_path = function(path)
|
convert_path = function(path)
|
||||||
local new_path = path:gsub("%.moon$", ".lua")
|
local new_path = path:gsub("%.moon$", ".lua")
|
||||||
if new_path == path then
|
if new_path == path then
|
||||||
@ -42,11 +37,9 @@ convert_path = function(path)
|
|||||||
end
|
end
|
||||||
return new_path
|
return new_path
|
||||||
end
|
end
|
||||||
local format_time
|
|
||||||
format_time = function(time)
|
format_time = function(time)
|
||||||
return ("%.3fms"):format(time * 1000)
|
return ("%.3fms"):format(time * 1000)
|
||||||
end
|
end
|
||||||
local gettime
|
|
||||||
do
|
do
|
||||||
local socket
|
local socket
|
||||||
gettime = function()
|
gettime = function()
|
||||||
@ -65,7 +58,6 @@ do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local compile_file_text
|
|
||||||
compile_file_text = function(text, fname, opts)
|
compile_file_text = function(text, fname, opts)
|
||||||
if opts == nil then
|
if opts == nil then
|
||||||
opts = { }
|
opts = { }
|
||||||
@ -86,7 +78,7 @@ compile_file_text = function(text, fname, opts)
|
|||||||
if opts.show_parse_tree then
|
if opts.show_parse_tree then
|
||||||
local dump = require("moonscript.dump")
|
local dump = require("moonscript.dump")
|
||||||
dump.tree(tree)
|
dump.tree(tree)
|
||||||
return nil
|
return true
|
||||||
end
|
end
|
||||||
local compile_time
|
local compile_time
|
||||||
if opts.benchmark then
|
if opts.benchmark then
|
||||||
@ -107,7 +99,7 @@ compile_file_text = function(text, fname, opts)
|
|||||||
end
|
end
|
||||||
print("Pos", "Lua", ">>", "Moon")
|
print("Pos", "Lua", ">>", "Moon")
|
||||||
print(debug_posmap(posmap_or_err, text, code))
|
print(debug_posmap(posmap_or_err, text, code))
|
||||||
return nil
|
return true
|
||||||
end
|
end
|
||||||
if opts.benchmark then
|
if opts.benchmark then
|
||||||
print(table.concat({
|
print(table.concat({
|
||||||
@ -120,6 +112,40 @@ compile_file_text = function(text, fname, opts)
|
|||||||
end
|
end
|
||||||
return code
|
return code
|
||||||
end
|
end
|
||||||
|
write_file = function(fname, code)
|
||||||
|
mkdir(parse_dir(fname))
|
||||||
|
local f, err = io.open(fname, "w")
|
||||||
|
if not (f) then
|
||||||
|
return nil, err
|
||||||
|
end
|
||||||
|
assert(f:write(code))
|
||||||
|
assert(f:write("\n"))
|
||||||
|
f:close()
|
||||||
|
return "build"
|
||||||
|
end
|
||||||
|
compile_and_write = function(src, dest, opts)
|
||||||
|
if opts == nil then
|
||||||
|
opts = { }
|
||||||
|
end
|
||||||
|
local f = io.open(src)
|
||||||
|
if not (f) then
|
||||||
|
return nil, "Can't find file"
|
||||||
|
end
|
||||||
|
local text = assert(f:read("*a"))
|
||||||
|
f:close()
|
||||||
|
local code, err = compile_file_text(text, opts)
|
||||||
|
if not code then
|
||||||
|
return nil, err
|
||||||
|
end
|
||||||
|
if code == true then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
if opts.print then
|
||||||
|
print(text)
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
return write_file(dest, code)
|
||||||
|
end
|
||||||
return {
|
return {
|
||||||
dirsep = dirsep,
|
dirsep = dirsep,
|
||||||
mkdir = mkdir,
|
mkdir = mkdir,
|
||||||
@ -130,5 +156,6 @@ return {
|
|||||||
convert_path = convert_path,
|
convert_path = convert_path,
|
||||||
gettime = gettime,
|
gettime = gettime,
|
||||||
format_time = format_time,
|
format_time = format_time,
|
||||||
compile_file_text = compile_file_text
|
compile_file_text = compile_file_text,
|
||||||
|
compile_and_write = compile_and_write
|
||||||
}
|
}
|
||||||
|
@ -4,13 +4,14 @@ lfs = require "lfs"
|
|||||||
|
|
||||||
import split from require "moonscript.util"
|
import split from require "moonscript.util"
|
||||||
|
|
||||||
|
local *
|
||||||
|
|
||||||
dirsep = package.config\sub 1,1
|
dirsep = package.config\sub 1,1
|
||||||
dirsep_chars = if dirsep == "\\"
|
dirsep_chars = if dirsep == "\\"
|
||||||
"\\/" -- windows
|
"\\/" -- windows
|
||||||
else
|
else
|
||||||
dirsep
|
dirsep
|
||||||
|
|
||||||
|
|
||||||
-- similar to mkdir -p
|
-- similar to mkdir -p
|
||||||
mkdir = (path) ->
|
mkdir = (path) ->
|
||||||
chunks = split path, dirsep
|
chunks = split path, dirsep
|
||||||
@ -59,9 +60,9 @@ gettime = do
|
|||||||
else
|
else
|
||||||
nil, "LuaSocket needed for benchmark"
|
nil, "LuaSocket needed for benchmark"
|
||||||
|
|
||||||
-- compiles file to lua
|
-- compiles file to lua, returns lua code
|
||||||
-- returns nil, error on error
|
-- returns nil, error on error
|
||||||
-- returns just nil if some option handled the output instead
|
-- returns true if some option handled the output instead
|
||||||
compile_file_text = (text, fname, opts={}) ->
|
compile_file_text = (text, fname, opts={}) ->
|
||||||
parse = require "moonscript.parse"
|
parse = require "moonscript.parse"
|
||||||
compile = require "moonscript.compile"
|
compile = require "moonscript.compile"
|
||||||
@ -78,7 +79,7 @@ compile_file_text = (text, fname, opts={}) ->
|
|||||||
if opts.show_parse_tree
|
if opts.show_parse_tree
|
||||||
dump = require "moonscript.dump"
|
dump = require "moonscript.dump"
|
||||||
dump.tree tree
|
dump.tree tree
|
||||||
return nil
|
return true
|
||||||
|
|
||||||
compile_time = if opts.benchmark
|
compile_time = if opts.benchmark
|
||||||
gettime!
|
gettime!
|
||||||
@ -95,7 +96,7 @@ compile_file_text = (text, fname, opts={}) ->
|
|||||||
import debug_posmap from require "moonscript.util"
|
import debug_posmap from require "moonscript.util"
|
||||||
print "Pos", "Lua", ">>", "Moon"
|
print "Pos", "Lua", ">>", "Moon"
|
||||||
print debug_posmap posmap_or_err, text, code
|
print debug_posmap posmap_or_err, text, code
|
||||||
return nil
|
return true
|
||||||
|
|
||||||
if opts.benchmark
|
if opts.benchmark
|
||||||
print table.concat {
|
print table.concat {
|
||||||
@ -108,6 +109,39 @@ compile_file_text = (text, fname, opts={}) ->
|
|||||||
|
|
||||||
code
|
code
|
||||||
|
|
||||||
|
write_file = (fname, code) ->
|
||||||
|
mkdir parse_dir fname
|
||||||
|
f, err = io.open fname, "w"
|
||||||
|
unless f
|
||||||
|
return nil, err
|
||||||
|
|
||||||
|
assert f\write code
|
||||||
|
assert f\write "\n"
|
||||||
|
f\close!
|
||||||
|
"build"
|
||||||
|
|
||||||
|
compile_and_write = (src, dest, opts={}) ->
|
||||||
|
f = io.open src
|
||||||
|
unless f
|
||||||
|
return nil, "Can't find file"
|
||||||
|
|
||||||
|
text = assert f\read("*a")
|
||||||
|
f\close!
|
||||||
|
|
||||||
|
code, err = compile_file_text text, opts
|
||||||
|
|
||||||
|
if not code
|
||||||
|
return nil, err
|
||||||
|
|
||||||
|
if code == true
|
||||||
|
return true
|
||||||
|
|
||||||
|
if opts.print
|
||||||
|
print text
|
||||||
|
return true
|
||||||
|
|
||||||
|
write_file dest, code
|
||||||
|
|
||||||
{
|
{
|
||||||
:dirsep
|
:dirsep
|
||||||
:mkdir
|
:mkdir
|
||||||
@ -118,5 +152,7 @@ compile_file_text = (text, fname, opts={}) ->
|
|||||||
:convert_path
|
:convert_path
|
||||||
:gettime
|
:gettime
|
||||||
:format_time
|
:format_time
|
||||||
|
|
||||||
:compile_file_text
|
:compile_file_text
|
||||||
|
:compile_and_write
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ describe "moonc", ->
|
|||||||
same moonc.convert_path, "/hello/file.moon", "/hello/file.lua"
|
same moonc.convert_path, "/hello/file.moon", "/hello/file.lua"
|
||||||
same moonc.convert_path, "/hello/world/file", "/hello/world/file.lua"
|
same moonc.convert_path, "/hello/world/file", "/hello/world/file.lua"
|
||||||
|
|
||||||
it "chould compile file text", ->
|
it "should compile file text", ->
|
||||||
assert.same {
|
assert.same {
|
||||||
[[return print('hello')]]
|
[[return print('hello')]]
|
||||||
}, {
|
}, {
|
||||||
|
Loading…
Reference in New Issue
Block a user