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
require "alt_getopt"
require "moonscript.errors"
require "moonscript"
local util = require "moonscript.util"
local errors = require "moonscript.errors"
-- moonloader and repl
local opts, ind = alt_getopt.get_opts(arg, "cvhd", { version = "v", help = "h" })
@ -77,8 +77,8 @@ if not opts.d then
end)
if err then
local truncated = moonscript.errors.truncate_traceback(util.trim(trace))
local rewritten = moonscript.errors.rewrite_traceback(truncated, err)
local truncated = errors.truncate_traceback(util.trim(trace))
local rewritten = errors.rewrite_traceback(truncated, err)
if rewritten then
print_err(rewritten)

View File

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

View File

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

View File

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

View File

@ -1,4 +1,3 @@
module("moonscript.dump", package.seeall)
local flat_value
flat_value = function(op, depth)
if depth == nil then
@ -24,9 +23,11 @@ flat_value = function(op, depth)
local pos = op[-1]
return "{" .. (pos and "[" .. pos .. "] " or "") .. table.concat(items, ", ") .. "}"
end
local value
value = function(op)
return flat_value(op)
end
local tree
tree = function(block)
local _list_0 = block
for _index_0 = 1, #_list_0 do
@ -34,3 +35,7 @@ tree = function(block)
print(flat_value(value))
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) ->
return '"'..op..'"' if type(op) == "string"
return tostring(op) if type(op) != "table"
@ -17,3 +13,6 @@ value = (op) ->
tree = (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 util = require("moonscript.util")
require("lpeg")
local concat, insert = table.concat, table.insert
local split, pos_to_line = util.split, util.pos_to_line
local user_error
user_error = function(...)
return error({
"user-error",
@ -30,6 +30,7 @@ reverse_line_number = function(fname, line_table, line_num, cache)
end
return "unknown"
end
local truncate_traceback
truncate_traceback = function(traceback, chunk_func)
if chunk_func == nil then
chunk_func = "moonscript_chunk"
@ -58,6 +59,7 @@ truncate_traceback = function(traceback, chunk_func)
traceback[#traceback] = traceback[#traceback]:gsub(rep, "main chunk")
return concat(traceback, "\n")
end
local rewrite_traceback
rewrite_traceback = function(text, err)
local line_tables = moon.line_tables
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")
}, "\n")
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"
util = require "moonscript.util"
@ -8,8 +7,6 @@ require "lpeg"
import concat, insert from table
import split, pos_to_line from util
export rewrite_traceback, truncate_traceback, user_error
user_error = (...) ->
error {"user-error", ...}
@ -86,3 +83,6 @@ rewrite_traceback = (text, err) ->
"\t" .. concat match, "\n\t"
}, "\n"
{ :rewrite_traceback, :truncate_traceback, :user_error }

View File

@ -1,9 +1,12 @@
module("moonscript", package.seeall)
require("moonscript.compile")
require("moonscript.parse")
require("moonscript.util")
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 = {
loadstring = loadstring
}

View File

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

View File

@ -1,14 +1,13 @@
module("moonscript.types", package.seeall)
local util = require("moonscript.util")
local data = require("moonscript.data")
local insert = table.insert
manual_return = data.Set({
local manual_return = data.Set({
"foreach",
"for",
"while",
"return"
})
cascading = data.Set({
local cascading = data.Set({
"if",
"unless",
"with",
@ -16,21 +15,7 @@ cascading = data.Set({
"class",
"do"
})
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
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
local ntype
ntype = function(node)
local _exp_0 = type(node)
if "nil" == _exp_0 then
@ -41,9 +26,29 @@ ntype = function(node)
return "value"
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)
return type(node) ~= "table" or node[1] ~= "exp" or #node == 2
end
local is_slice
is_slice = function(node)
return ntype(node) == "chain" and ntype(node[#node]) == "slice"
end
@ -183,7 +188,7 @@ make_builder = function(name)
return node
end
end
build = nil
local build = nil
build = setmetatable({
group = function(body)
if body == nil then
@ -254,6 +259,7 @@ build = setmetatable({
return rawget(self, name)
end
})
local smart_node
smart_node = function(node)
local index = key_table[ntype(node)]
if not index then
@ -275,4 +281,15 @@ smart_node = function(node)
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"
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
-- 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
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
has_value = (node) ->
if ntype(node) == "chain"
@ -31,16 +37,6 @@ is_value = (stm) ->
comprehension_has_value = (comp) ->
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) ->
type(node) != "table" or node[1] != "exp" or #node == 2
@ -160,4 +156,9 @@ smart_node = (node) ->
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
moon = {
local moon = {
is_object = function(value)
return type(value) == "table" and value.__class
end,
@ -15,6 +14,7 @@ moon = {
return base_type
end
}
local pos_to_line
pos_to_line = function(str, pos)
local line = 1
for _ in str:sub(1, pos):gmatch("\n") do
@ -22,14 +22,11 @@ pos_to_line = function(str, pos)
end
return line
end
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
local trim
trim = function(str)
return str:match("^%s*(.-)%s*$")
end
local get_line
get_line = function(str, line_num)
for line in str:gmatch("([^\n]*)\n?") do
if line_num == 1 then
@ -38,6 +35,16 @@ get_line = function(str, line_num)
line_num = line_num - 1
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)
return coroutine.wrap(function()
for i = #seq, 1, -1 do
@ -45,9 +52,7 @@ reversed = function(seq)
end
end)
end
trim = function(str)
return str:match("^%s*(.-)%s*$")
end
local split
split = function(str, delim)
if str == "" then
return { }
@ -63,6 +68,7 @@ split = function(str, delim)
return _accum_0
end)()
end
local dump
dump = function(what)
local seen = { }
local _dump
@ -99,6 +105,7 @@ dump = function(what)
end
return _dump(what)
end
local debug_posmap
debug_posmap = function(posmap, moon_code, lua_code)
local tuples = (function()
local _accum_0 = { }
@ -135,4 +142,14 @@ debug_posmap = function(posmap, moon_code, lua_code)
end)()
return concat(lines, "\n")
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
moon =
@ -25,12 +18,8 @@ pos_to_line = (str, pos) ->
line += 1
line
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
trim = (str) ->
str\match "^%s*(.-)%s*$"
get_line = (str, line_num) ->
-- 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
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) ->
coroutine.wrap ->
for i=#seq,1,-1
coroutine.yield i, seq[i]
trim = (str) ->
str\match "^%s*(.-)%s*$"
split = (str, delim) ->
return {} if str == ""
str ..= delim
@ -90,5 +83,8 @@ debug_posmap = (posmap, moon_code, lua_code) ->
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 "busted"
require "moonscript.parse"
require "moonscript.compile"
require "moonscript.util"
import parse, compile, util from moonscript
parse = require "moonscript.parse"
compile = require "moonscript.compile"
util = require "moonscript.util"
pattern = ...