start refactoring out calls to module

This commit is contained in:
leaf corcoran 2012-11-28 00:24:58 -08:00
parent 3e42d06464
commit 33eb9510dd
15 changed files with 146 additions and 102 deletions

View File

@ -1,10 +1,10 @@
#!/usr/bin/env lua #!/usr/bin/env lua
require "alt_getopt" require "alt_getopt"
require "moonscript.errors"
require "moonscript" require "moonscript"
local util = require "moonscript.util" local util = require "moonscript.util"
local errors = require "moonscript.errors"
-- moonloader and repl -- moonloader and repl
local opts, ind = alt_getopt.get_opts(arg, "cvhd", { version = "v", help = "h" }) local opts, ind = alt_getopt.get_opts(arg, "cvhd", { version = "v", help = "h" })
@ -77,8 +77,8 @@ if not opts.d then
end) end)
if err then if err then
local truncated = moonscript.errors.truncate_traceback(util.trim(trace)) local truncated = errors.truncate_traceback(util.trim(trace))
local rewritten = moonscript.errors.rewrite_traceback(truncated, err) local rewritten = errors.rewrite_traceback(truncated, err)
if rewritten then if rewritten then
print_err(rewritten) print_err(rewritten)

View File

@ -4,7 +4,7 @@ module("moonscript", package.seeall)
require "moonscript.parse" require "moonscript.parse"
require "moonscript.compile" require "moonscript.compile"
require "moonscript.util" local util = require "moonscript.util"
require "alt_getopt" require "alt_getopt"
require "lfs" require "lfs"

View File

@ -1,5 +1,5 @@
module("moonscript.data", package.seeall) local concat, remove, insert = table.concat, table.remove, table.insert
local concat = table.concat local Set
Set = function(items) Set = function(items)
local self = { } local self = { }
local _list_0 = items local _list_0 = items
@ -9,6 +9,7 @@ Set = function(items)
end end
return self return self
end end
local Stack
do do
local _parent_0 = nil local _parent_0 = nil
local _base_0 = { local _base_0 = {
@ -16,10 +17,10 @@ do
return "<Stack {" .. concat(self, ", ") .. "}>" return "<Stack {" .. concat(self, ", ") .. "}>"
end, end,
pop = function(self) pop = function(self)
return table.remove(self) return remove(self)
end, end,
push = function(self, value) push = function(self, value)
table.insert(self, value) insert(self, value)
return value return value
end, end,
top = function(self) top = function(self)
@ -65,7 +66,7 @@ do
end end
Stack = _class_0 Stack = _class_0
end end
lua_keywords = Set({ local lua_keywords = Set({
'and', 'and',
'break', 'break',
'do', 'do',
@ -88,4 +89,8 @@ lua_keywords = Set({
'until', 'until',
'while' 'while'
}) })
return nil return {
Set = Set,
Stack = Stack,
lua_keywords = lua_keywords
}

View File

@ -1,10 +1,6 @@
-- data structures & static data -- data structures & static data
module "moonscript.data", package.seeall
export Set, Stack import concat, remove, insert from table
export lua_keywords
import concat from table
Set = (items) -> Set = (items) ->
self = {} self = {}
@ -19,10 +15,10 @@ class Stack
nil nil
pop: => pop: =>
table.remove self remove self
push: (value) => push: (value) =>
table.insert self, value insert self, value
value value
top: => top: =>
@ -37,6 +33,7 @@ lua_keywords = Set{
'until', 'while' 'until', 'while'
} }
nil
{ :Set, :Stack, :lua_keywords }

View File

@ -1,4 +1,3 @@
module("moonscript.dump", package.seeall)
local flat_value local flat_value
flat_value = function(op, depth) flat_value = function(op, depth)
if depth == nil then if depth == nil then
@ -24,9 +23,11 @@ flat_value = function(op, depth)
local pos = op[-1] local pos = op[-1]
return "{" .. (pos and "[" .. pos .. "] " or "") .. table.concat(items, ", ") .. "}" return "{" .. (pos and "[" .. pos .. "] " or "") .. table.concat(items, ", ") .. "}"
end end
local value
value = function(op) value = function(op)
return flat_value(op) return flat_value(op)
end end
local tree
tree = function(block) tree = function(block)
local _list_0 = block local _list_0 = block
for _index_0 = 1, #_list_0 do for _index_0 = 1, #_list_0 do
@ -34,3 +35,7 @@ tree = function(block)
print(flat_value(value)) print(flat_value(value))
end end
end end
return {
value = value,
tree = tree
}

View File

@ -1,8 +1,4 @@
module "moonscript.dump", package.seeall
export value, tree
flat_value = (op, depth=1) -> flat_value = (op, depth=1) ->
return '"'..op..'"' if type(op) == "string" return '"'..op..'"' if type(op) == "string"
return tostring(op) if type(op) != "table" return tostring(op) if type(op) != "table"
@ -17,3 +13,6 @@ value = (op) ->
tree = (block) -> tree = (block) ->
print flat_value value for value in *block print flat_value value for value in *block
{ :value, :tree }

View File

@ -1,9 +1,9 @@
module("moonscript.errors", package.seeall)
local moon = require("moonscript") local moon = require("moonscript")
local util = require("moonscript.util") local util = require("moonscript.util")
require("lpeg") require("lpeg")
local concat, insert = table.concat, table.insert local concat, insert = table.concat, table.insert
local split, pos_to_line = util.split, util.pos_to_line local split, pos_to_line = util.split, util.pos_to_line
local user_error
user_error = function(...) user_error = function(...)
return error({ return error({
"user-error", "user-error",
@ -30,6 +30,7 @@ reverse_line_number = function(fname, line_table, line_num, cache)
end end
return "unknown" return "unknown"
end end
local truncate_traceback
truncate_traceback = function(traceback, chunk_func) truncate_traceback = function(traceback, chunk_func)
if chunk_func == nil then if chunk_func == nil then
chunk_func = "moonscript_chunk" chunk_func = "moonscript_chunk"
@ -58,6 +59,7 @@ truncate_traceback = function(traceback, chunk_func)
traceback[#traceback] = traceback[#traceback]:gsub(rep, "main chunk") traceback[#traceback] = traceback[#traceback]:gsub(rep, "main chunk")
return concat(traceback, "\n") return concat(traceback, "\n")
end end
local rewrite_traceback
rewrite_traceback = function(text, err) rewrite_traceback = function(text, err)
local line_tables = moon.line_tables local line_tables = moon.line_tables
local V, S, Ct, C = lpeg.V, lpeg.S, lpeg.Ct, lpeg.C local V, S, Ct, C = lpeg.V, lpeg.S, lpeg.Ct, lpeg.C
@ -103,3 +105,8 @@ rewrite_traceback = function(text, err)
"\t" .. concat(match, "\n\t") "\t" .. concat(match, "\n\t")
}, "\n") }, "\n")
end end
return {
rewrite_traceback = rewrite_traceback,
truncate_traceback = truncate_traceback,
user_error = user_error
}

View File

@ -1,5 +1,4 @@
module "moonscript.errors", package.seeall
moon = require "moonscript" moon = require "moonscript"
util = require "moonscript.util" util = require "moonscript.util"
@ -8,8 +7,6 @@ require "lpeg"
import concat, insert from table import concat, insert from table
import split, pos_to_line from util import split, pos_to_line from util
export rewrite_traceback, truncate_traceback, user_error
user_error = (...) -> user_error = (...) ->
error {"user-error", ...} error {"user-error", ...}
@ -86,3 +83,6 @@ rewrite_traceback = (text, err) ->
"\t" .. concat match, "\n\t" "\t" .. concat match, "\n\t"
}, "\n" }, "\n"
{ :rewrite_traceback, :truncate_traceback, :user_error }

View File

@ -1,9 +1,12 @@
module("moonscript", package.seeall) module("moonscript", package.seeall)
require("moonscript.compile") require("moonscript.compile")
require("moonscript.parse") require("moonscript.parse")
require("moonscript.util")
local concat, insert = table.concat, table.insert local concat, insert = table.concat, table.insert
local split, dump = util.split, util.dump local split, dump
do
local _table_0 = require("moonscript.util")
split, dump = _table_0.split, _table_0.dump
end
local lua = { local lua = {
loadstring = loadstring loadstring = loadstring
} }

View File

@ -3,10 +3,9 @@ module "moonscript", package.seeall
require "moonscript.compile" require "moonscript.compile"
require "moonscript.parse" require "moonscript.parse"
require "moonscript.util"
import concat, insert from table import concat, insert from table
import split, dump from util import split, dump from require "moonscript.util"
lua = :loadstring lua = :loadstring

View File

@ -1,14 +1,13 @@
module("moonscript.types", package.seeall)
local util = require("moonscript.util") local util = require("moonscript.util")
local data = require("moonscript.data") local data = require("moonscript.data")
local insert = table.insert local insert = table.insert
manual_return = data.Set({ local manual_return = data.Set({
"foreach", "foreach",
"for", "for",
"while", "while",
"return" "return"
}) })
cascading = data.Set({ local cascading = data.Set({
"if", "if",
"unless", "unless",
"with", "with",
@ -16,21 +15,7 @@ cascading = data.Set({
"class", "class",
"do" "do"
}) })
has_value = function(node) local ntype
if ntype(node) == "chain" then
local ctype = ntype(node[#node])
return ctype ~= "call" and ctype ~= "colon"
else
return true
end
end
is_value = function(stm)
local compile, transform = moonscript.compile, moonscript.transform
return compile.Block:is_value(stm) or transform.Value:can_transform(stm)
end
comprehension_has_value = function(comp)
return is_value(comp[2])
end
ntype = function(node) ntype = function(node)
local _exp_0 = type(node) local _exp_0 = type(node)
if "nil" == _exp_0 then if "nil" == _exp_0 then
@ -41,9 +26,29 @@ ntype = function(node)
return "value" return "value"
end end
end end
local has_value
has_value = function(node)
if ntype(node) == "chain" then
local ctype = ntype(node[#node])
return ctype ~= "call" and ctype ~= "colon"
else
return true
end
end
local is_value
is_value = function(stm)
local compile, transform = moonscript.compile, moonscript.transform
return compile.Block:is_value(stm) or transform.Value:can_transform(stm)
end
local comprehension_has_value
comprehension_has_value = function(comp)
return is_value(comp[2])
end
local value_is_singular
value_is_singular = function(node) value_is_singular = function(node)
return type(node) ~= "table" or node[1] ~= "exp" or #node == 2 return type(node) ~= "table" or node[1] ~= "exp" or #node == 2
end end
local is_slice
is_slice = function(node) is_slice = function(node)
return ntype(node) == "chain" and ntype(node[#node]) == "slice" return ntype(node) == "chain" and ntype(node[#node]) == "slice"
end end
@ -183,7 +188,7 @@ make_builder = function(name)
return node return node
end end
end end
build = nil local build = nil
build = setmetatable({ build = setmetatable({
group = function(body) group = function(body)
if body == nil then if body == nil then
@ -254,6 +259,7 @@ build = setmetatable({
return rawget(self, name) return rawget(self, name)
end end
}) })
local smart_node
smart_node = function(node) smart_node = function(node)
local index = key_table[ntype(node)] local index = key_table[ntype(node)]
if not index then if not index then
@ -275,4 +281,15 @@ smart_node = function(node)
end end
}) })
end end
return nil return {
ntype = ntype,
smart_node = smart_node,
build = build,
is_value = is_value,
is_slice = is_slice,
manual_return = manual_return,
cascading = cascading,
value_is_singular = value_is_singular,
comprehension_has_value = comprehension_has_value,
has_value = has_value
}

View File

@ -1,11 +1,7 @@
module "moonscript.types", package.seeall
util = require "moonscript.util" util = require "moonscript.util"
data = require "moonscript.data" data = require "moonscript.data"
export ntype, smart_node, build, is_value
export is_slice, manual_return, cascading, value_is_singular
export comprehension_has_value, has_value
import insert from table import insert from table
-- implicit return does not work on these statements -- implicit return does not work on these statements
@ -16,6 +12,16 @@ manual_return = data.Set{"foreach", "for", "while", "return"}
-- is the transformation to apply to the last statement in their body -- is the transformation to apply to the last statement in their body
cascading = data.Set{ "if", "unless", "with", "switch", "class", "do" } cascading = data.Set{ "if", "unless", "with", "switch", "class", "do" }
-- type of node as string
ntype = (node) ->
switch type node
when "nil"
"nil"
when "table"
node[1]
else
"value"
-- does this always return a value -- does this always return a value
has_value = (node) -> has_value = (node) ->
if ntype(node) == "chain" if ntype(node) == "chain"
@ -31,16 +37,6 @@ is_value = (stm) ->
comprehension_has_value = (comp) -> comprehension_has_value = (comp) ->
is_value comp[2] is_value comp[2]
-- type of node as string
ntype = (node) ->
switch type node
when "nil"
"nil"
when "table"
node[1]
else
"value"
value_is_singular = (node) -> value_is_singular = (node) ->
type(node) != "table" or node[1] != "exp" or #node == 2 type(node) != "table" or node[1] != "exp" or #node == 2
@ -160,4 +156,9 @@ smart_node = (node) ->
rawset node, key, value rawset node, key, value
} }
nil
{
:ntype, :smart_node, :build, :is_value, :is_slice, :manual_return,
:cascading, :value_is_singular, :comprehension_has_value, :has_value
}

View File

@ -1,6 +1,5 @@
module("moonscript.util", package.seeall)
local concat = table.concat local concat = table.concat
moon = { local moon = {
is_object = function(value) is_object = function(value)
return type(value) == "table" and value.__class return type(value) == "table" and value.__class
end, end,
@ -15,6 +14,7 @@ moon = {
return base_type return base_type
end end
} }
local pos_to_line
pos_to_line = function(str, pos) pos_to_line = function(str, pos)
local line = 1 local line = 1
for _ in str:sub(1, pos):gmatch("\n") do for _ in str:sub(1, pos):gmatch("\n") do
@ -22,14 +22,11 @@ pos_to_line = function(str, pos)
end end
return line return line
end end
get_closest_line = function(str, line_num) local trim
local line = get_line(str, line_num) trim = function(str)
if (not line or trim(line) == "") and line_num > 1 then return str:match("^%s*(.-)%s*$")
return get_closest_line(str, line_num - 1)
else
return line, line_num
end
end end
local get_line
get_line = function(str, line_num) get_line = function(str, line_num)
for line in str:gmatch("([^\n]*)\n?") do for line in str:gmatch("([^\n]*)\n?") do
if line_num == 1 then if line_num == 1 then
@ -38,6 +35,16 @@ get_line = function(str, line_num)
line_num = line_num - 1 line_num = line_num - 1
end end
end end
local get_closest_line
get_closest_line = function(str, line_num)
local line = get_line(str, line_num)
if (not line or trim(line) == "") and line_num > 1 then
return get_closest_line(str, line_num - 1)
else
return line, line_num
end
end
local reversed
reversed = function(seq) reversed = function(seq)
return coroutine.wrap(function() return coroutine.wrap(function()
for i = #seq, 1, -1 do for i = #seq, 1, -1 do
@ -45,9 +52,7 @@ reversed = function(seq)
end end
end) end)
end end
trim = function(str) local split
return str:match("^%s*(.-)%s*$")
end
split = function(str, delim) split = function(str, delim)
if str == "" then if str == "" then
return { } return { }
@ -63,6 +68,7 @@ split = function(str, delim)
return _accum_0 return _accum_0
end)() end)()
end end
local dump
dump = function(what) dump = function(what)
local seen = { } local seen = { }
local _dump local _dump
@ -99,6 +105,7 @@ dump = function(what)
end end
return _dump(what) return _dump(what)
end end
local debug_posmap
debug_posmap = function(posmap, moon_code, lua_code) debug_posmap = function(posmap, moon_code, lua_code)
local tuples = (function() local tuples = (function()
local _accum_0 = { } local _accum_0 = { }
@ -135,4 +142,14 @@ debug_posmap = function(posmap, moon_code, lua_code)
end)() end)()
return concat(lines, "\n") return concat(lines, "\n")
end end
return nil return {
moon = moon,
pos_to_line = pos_to_line,
get_closest_line = get_closest_line,
get_line = get_line,
reversed = reversed,
trim = trim,
split = split,
dump = dump,
debug_posmap = debug_posmap
}

View File

@ -1,11 +1,4 @@
module "moonscript.util", package.seeall
export moon
export pos_to_line, get_closest_line, get_line
export reversed, trim, split
export dump, debug_posmap
import concat from table import concat from table
moon = moon =
@ -25,12 +18,8 @@ pos_to_line = (str, pos) ->
line += 1 line += 1
line line
get_closest_line = (str, line_num) -> trim = (str) ->
line = get_line str, line_num str\match "^%s*(.-)%s*$"
if (not line or trim(line) == "") and line_num > 1
get_closest_line(str, line_num - 1)
else
line, line_num
get_line = (str, line_num) -> get_line = (str, line_num) ->
-- todo: this returns an extra blank line at the end -- todo: this returns an extra blank line at the end
@ -38,14 +27,18 @@ get_line = (str, line_num) ->
return line if line_num == 1 return line if line_num == 1
line_num -= 1 line_num -= 1
get_closest_line = (str, line_num) ->
line = get_line str, line_num
if (not line or trim(line) == "") and line_num > 1
get_closest_line(str, line_num - 1)
else
line, line_num
reversed = (seq) -> reversed = (seq) ->
coroutine.wrap -> coroutine.wrap ->
for i=#seq,1,-1 for i=#seq,1,-1
coroutine.yield i, seq[i] coroutine.yield i, seq[i]
trim = (str) ->
str\match "^%s*(.-)%s*$"
split = (str, delim) -> split = (str, delim) ->
return {} if str == "" return {} if str == ""
str ..= delim str ..= delim
@ -90,5 +83,8 @@ debug_posmap = (posmap, moon_code, lua_code) ->
concat(lines, "\n") concat(lines, "\n")
nil {
:moon, :pos_to_line, :get_closest_line, :get_line, :reversed, :trim, :split,
:dump, :debug_posmap
}

View File

@ -2,11 +2,9 @@
require "lfs" require "lfs"
require "busted" require "busted"
require "moonscript.parse" parse = require "moonscript.parse"
require "moonscript.compile" compile = require "moonscript.compile"
require "moonscript.util" util = require "moonscript.util"
import parse, compile, util from moonscript
pattern = ... pattern = ...