more moonc functions to module

This commit is contained in:
leaf corcoran 2014-06-18 09:48:25 -07:00
parent c60d83c546
commit a4e03813fc
4 changed files with 89 additions and 67 deletions

View File

@ -67,50 +67,9 @@ local normalize_dir = moonc.normalize_dir
local parse_dir = moonc.parse_dir
local parse_file = moonc.parse_file
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)
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)
local function scan_directory(root, collected)
root = normalize_dir(root)
collected = collected or {}
@ -131,7 +90,7 @@ function scan_directory(root, collected)
return collected
end
function remove_dups(tbl, key_fn)
local function remove_dups(tbl, key_fn)
local hash = {}
local final = {}
@ -156,7 +115,7 @@ local function is_abs_path(path)
end
-- creates tuples of input and target
function get_files(fname, files)
local function get_files(fname, files)
files = files or {}
if lfs.attributes(fname, "mode") == "directory" then
@ -227,7 +186,7 @@ files = remove_dups(files, function(f)
return f[2]
end)
function get_sleep_func()
local function get_sleep_func()
local sleep
if not pcall(function()
require "socket"
@ -242,7 +201,7 @@ function get_sleep_func()
return sleep
end
function plural(count, word)
local function plural(count, word)
if count ~= 1 then
word = word .. "s"
end
@ -250,7 +209,7 @@ function plural(count, word)
end
-- 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 inotify

View File

@ -4,14 +4,13 @@ do
local _obj_0 = require("moonscript.util")
split = _obj_0.split
end
local dirsep = package.config:sub(1, 1)
local dirsep_chars
local dirsep, dirsep_chars, mkdir, normalize_dir, parse_dir, parse_file, convert_path, format_time, gettime, compile_file_text, write_file, compile_and_write
dirsep = package.config:sub(1, 1)
if dirsep == "\\" then
dirsep_chars = "\\/"
else
dirsep_chars = dirsep
end
local mkdir
mkdir = function(path)
local chunks = split(path, dirsep)
local accum
@ -22,19 +21,15 @@ mkdir = function(path)
end
return lfs.attributes(path, "mode")
end
local normalize_dir
normalize_dir = function(path)
return path:match("^(.-)[" .. tostring(dirsep_chars) .. "]*$") .. dirsep
end
local parse_dir
parse_dir = function(path)
return (path:match("^(.-)[^" .. tostring(dirsep_chars) .. "]*$"))
end
local parse_file
parse_file = function(path)
return (path:match("^.-([^" .. tostring(dirsep_chars) .. "]*)$"))
end
local convert_path
convert_path = function(path)
local new_path = path:gsub("%.moon$", ".lua")
if new_path == path then
@ -42,11 +37,9 @@ convert_path = function(path)
end
return new_path
end
local format_time
format_time = function(time)
return ("%.3fms"):format(time * 1000)
end
local gettime
do
local socket
gettime = function()
@ -65,7 +58,6 @@ do
end
end
end
local compile_file_text
compile_file_text = function(text, fname, opts)
if opts == nil then
opts = { }
@ -86,7 +78,7 @@ compile_file_text = function(text, fname, opts)
if opts.show_parse_tree then
local dump = require("moonscript.dump")
dump.tree(tree)
return nil
return true
end
local compile_time
if opts.benchmark then
@ -107,7 +99,7 @@ compile_file_text = function(text, fname, opts)
end
print("Pos", "Lua", ">>", "Moon")
print(debug_posmap(posmap_or_err, text, code))
return nil
return true
end
if opts.benchmark then
print(table.concat({
@ -120,6 +112,40 @@ compile_file_text = function(text, fname, opts)
end
return code
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 {
dirsep = dirsep,
mkdir = mkdir,
@ -130,5 +156,6 @@ return {
convert_path = convert_path,
gettime = gettime,
format_time = format_time,
compile_file_text = compile_file_text
compile_file_text = compile_file_text,
compile_and_write = compile_and_write
}

View File

@ -4,13 +4,14 @@ lfs = require "lfs"
import split from require "moonscript.util"
local *
dirsep = package.config\sub 1,1
dirsep_chars = if dirsep == "\\"
"\\/" -- windows
else
dirsep
-- similar to mkdir -p
mkdir = (path) ->
chunks = split path, dirsep
@ -59,9 +60,9 @@ gettime = do
else
nil, "LuaSocket needed for benchmark"
-- compiles file to lua
-- compiles file to lua, returns lua code
-- 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={}) ->
parse = require "moonscript.parse"
compile = require "moonscript.compile"
@ -78,7 +79,7 @@ compile_file_text = (text, fname, opts={}) ->
if opts.show_parse_tree
dump = require "moonscript.dump"
dump.tree tree
return nil
return true
compile_time = if opts.benchmark
gettime!
@ -95,7 +96,7 @@ compile_file_text = (text, fname, opts={}) ->
import debug_posmap from require "moonscript.util"
print "Pos", "Lua", ">>", "Moon"
print debug_posmap posmap_or_err, text, code
return nil
return true
if opts.benchmark
print table.concat {
@ -108,6 +109,39 @@ compile_file_text = (text, fname, opts={}) ->
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
:mkdir
@ -118,5 +152,7 @@ compile_file_text = (text, fname, opts={}) ->
:convert_path
:gettime
:format_time
:compile_file_text
:compile_and_write
}

View File

@ -31,7 +31,7 @@ describe "moonc", ->
same moonc.convert_path, "/hello/file.moon", "/hello/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 {
[[return print('hello')]]
}, {