From 38463a078d90b53fb7c8e0265bfe96e69b2d808a Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 28 Nov 2012 00:42:43 -0800 Subject: [PATCH] remove module from compile, reorganize --- bin/moonc | 2 +- moonscript/compile.lua | 37 ++++++++++++++++++++++--------- moonscript/compile.moon | 22 ++++++++---------- moonscript/compile/statement.lua | 15 ++++++++----- moonscript/compile/statement.moon | 14 ++++-------- moonscript/compile/value.lua | 6 +++-- moonscript/compile/value.moon | 10 ++++----- moonscript/init.lua | 2 +- moonscript/init.moon | 2 +- moonscript/types.lua | 3 ++- moonscript/types.moon | 4 +++- 11 files changed, 65 insertions(+), 52 deletions(-) diff --git a/bin/moonc b/bin/moonc index 5ed80aa..1a1f4fe 100755 --- a/bin/moonc +++ b/bin/moonc @@ -3,7 +3,7 @@ module("moonscript", package.seeall) require "moonscript.parse" -require "moonscript.compile" +local compile = require "moonscript.compile" local util = require "moonscript.util" require "alt_getopt" diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 9bfc282..72f8ab3 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -1,8 +1,5 @@ -module("moonscript.compile", package.seeall) local util = require("moonscript.util") local dump = require("moonscript.dump") -require("moonscript.compile.statement") -require("moonscript.compile.value") local transform = require("moonscript.transform") local NameProxy, LocalName do @@ -19,11 +16,21 @@ do local _table_0 = require("moonscript.types") ntype, has_value = _table_0.ntype, _table_0.has_value end +local statement_compilers +do + local _table_0 = require("moonscript.compile.statement") + statement_compilers = _table_0.statement_compilers +end +local value_compilers +do + local _table_0 = require("moonscript.compile.value") + value_compilers = _table_0.value_compilers +end local concat, insert = table.concat, table.insert local pos_to_line, get_closest_line, trim = util.pos_to_line, util.get_closest_line, util.trim local mtype = util.moon.type local indent_char = " " -local Line, Lines +local Line, Lines, Block, RootBlock do local _parent_0 = nil local _base_0 = { @@ -160,7 +167,7 @@ do end local _list_0 = item for _index_0 = 1, #_list_0 do - value = _list_0[_index_0] + local value = _list_0[_index_0] self:_append_single(value) end else @@ -333,7 +340,7 @@ do self._name_whitelist = Set(names) end, put_name = function(self, name, ...) - value = ... + local value = ... if select("#", ...) == 0 then value = true end @@ -423,11 +430,11 @@ do end end, is_stm = function(self, node) - return line_compile[ntype(node)] ~= nil + return statement_compilers[ntype(node)] ~= nil end, is_value = function(self, node) local t = ntype(node) - return value_compile[t] ~= nil or t == "value" + return value_compilers[t] ~= nil or t == "value" end, name = function(self, node) return self:value(node) @@ -440,7 +447,7 @@ do else action = node[1] end - local fn = value_compile[action] + local fn = value_compilers[action] if not fn then error("Failed to compile value: " .. dump.value(node)) end @@ -482,7 +489,7 @@ do node = self.transform.statement(node) local result do - local fn = line_compile[ntype(node)] + local fn = statement_compilers[ntype(node)] if fn then result = fn(self, node, ...) else @@ -642,6 +649,7 @@ do end RootBlock = _class_0 end +local format_error format_error = function(msg, pos, file_str) local line = pos_to_line(file_str, pos) local line_str @@ -652,6 +660,7 @@ format_error = function(msg, pos, file_str) (" [%d] >> %s"):format(line, trim(line_str)) }, "\n") end +local value value = function(value) local out = nil do @@ -661,6 +670,7 @@ value = function(value) end return out end +local tree tree = function(tree, options) if options == nil then options = { } @@ -693,3 +703,10 @@ tree = function(tree, options) return lua_code, posmap end end +return { + tree = tree, + value = value, + format_error = format_error, + Block = Block, + RootBlock = RootBlock +} diff --git a/moonscript/compile.moon b/moonscript/compile.moon index 5399486..b262644 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -1,17 +1,15 @@ -module "moonscript.compile", package.seeall util = require "moonscript.util" dump = require "moonscript.dump" - -require "moonscript.compile.statement" -require "moonscript.compile.value" - transform = require "moonscript.transform" import NameProxy, LocalName from require "moonscript.transform.names" import Set from require "moonscript.data" import ntype, has_value from require "moonscript.types" +import statement_compilers from require "moonscript.compile.statement" +import value_compilers from require "moonscript.compile.value" + import concat, insert from table import pos_to_line, get_closest_line, trim from util @@ -19,10 +17,7 @@ mtype = util.moon.type indent_char = " " -export tree, value, format_error -export Block, RootBlock - -local Line, Lines +local Line, Lines, Block, RootBlock -- a buffer for building up lines class Lines @@ -277,11 +272,11 @@ class Block \append ... is_stm: (node) => - line_compile[ntype node] != nil + statement_compilers[ntype node] != nil is_value: (node) => t = ntype node - value_compile[t] != nil or t == "value" + value_compilers[t] != nil or t == "value" -- line wise compile functions name: (node) => @value node @@ -292,7 +287,7 @@ class Block else node[1] - fn = value_compile[action] + fn = value_compilers[action] error "Failed to compile value: "..dump.value node if not fn out = fn self, node, ... @@ -314,7 +309,7 @@ class Block return if not node -- skip blank statements node = @transform.statement node - result = if fn = line_compile[ntype(node)] + result = if fn = statement_compilers[ntype(node)] fn self, node, ... else -- coerce value into statement @@ -399,3 +394,4 @@ tree = (tree, options={}) -> posmap = scope._lines\flatten_posmap! lua_code, posmap +{ :tree, :value, :format_error, :Block, :RootBlock } diff --git a/moonscript/compile/statement.lua b/moonscript/compile/statement.lua index fe7dd5b..471364e 100644 --- a/moonscript/compile/statement.lua +++ b/moonscript/compile/statement.lua @@ -1,15 +1,15 @@ -module("moonscript.compile", package.seeall) -local util = require("moonscript.util") -local dump = require("moonscript.dump") -local transform = require("moonscript.transform") -local reversed = util.reversed +local reversed +do + local _table_0 = require("moonscript.util") + reversed = _table_0.reversed +end local ntype do local _table_0 = require("moonscript.types") ntype = _table_0.ntype end local concat, insert = table.concat, table.insert -line_compile = { +local statement_compilers = { raw = function(self, node) return self:add(node[2]) end, @@ -246,3 +246,6 @@ line_compile = { end end } +return { + statement_compilers = statement_compilers +} diff --git a/moonscript/compile/statement.moon b/moonscript/compile/statement.moon index 59d6021..4d68701 100644 --- a/moonscript/compile/statement.moon +++ b/moonscript/compile/statement.moon @@ -1,17 +1,9 @@ -module "moonscript.compile", package.seeall -util = require "moonscript.util" - -dump = require "moonscript.dump" -transform = require "moonscript.transform" - -import reversed from util +import reversed from require "moonscript.util" import ntype from require "moonscript.types" import concat, insert from table -export line_compile - -line_compile = +statement_compilers = raw: (node) => @add node[2] lines: (node) => @@ -139,3 +131,5 @@ line_compile = with @block! \stms node[2] + +{ :statement_compilers } diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index a6d9d54..3416ccc 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -1,4 +1,3 @@ -module("moonscript.compile", package.seeall) local util = require("moonscript.util") local data = require("moonscript.data") local ntype @@ -13,7 +12,7 @@ do end local concat, insert = table.concat, table.insert local table_delim = "," -value_compile = { +local value_compilers = { exp = function(self, node) local _comp _comp = function(i, value) @@ -302,3 +301,6 @@ value_compile = { return tostring(value) end } +return { + value_compilers = value_compilers +} diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index a066c2f..1087244 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -1,19 +1,14 @@ -module "moonscript.compile", package.seeall - util = require "moonscript.util" data = require "moonscript.data" import ntype from require "moonscript.types" import user_error from require "moonscript.errors" - import concat, insert from table -export value_compile - table_delim = "," -value_compile = +value_compilers = -- list of values separated by binary operators exp: (node) => _comp = (i, value) -> @@ -189,3 +184,6 @@ value_compile = @send "varargs" tostring value + + +{ :value_compilers } diff --git a/moonscript/init.lua b/moonscript/init.lua index 61c65a4..b284adf 100644 --- a/moonscript/init.lua +++ b/moonscript/init.lua @@ -1,5 +1,5 @@ module("moonscript", package.seeall) -require("moonscript.compile") +local compile = require("moonscript.compile") require("moonscript.parse") local concat, insert = table.concat, table.insert local split, dump diff --git a/moonscript/init.moon b/moonscript/init.moon index 660c2f1..34d4f9c 100644 --- a/moonscript/init.moon +++ b/moonscript/init.moon @@ -1,7 +1,7 @@ module "moonscript", package.seeall -require "moonscript.compile" +compile = require "moonscript.compile" require "moonscript.parse" import concat, insert from table diff --git a/moonscript/types.lua b/moonscript/types.lua index 2cd4c0b..e86b9b1 100644 --- a/moonscript/types.lua +++ b/moonscript/types.lua @@ -37,7 +37,8 @@ has_value = function(node) end local is_value is_value = function(stm) - local compile, transform = moonscript.compile, moonscript.transform + local compile = require("moonscript.compile") + local transform = require("moonscript.transform") return compile.Block:is_value(stm) or transform.Value:can_transform(stm) end local comprehension_has_value diff --git a/moonscript/types.moon b/moonscript/types.moon index 72dc72f..6f25cae 100644 --- a/moonscript/types.moon +++ b/moonscript/types.moon @@ -31,7 +31,9 @@ has_value = (node) -> true is_value = (stm) -> - import compile, transform from moonscript + compile = require "moonscript.compile" + transform = require "moonscript.transform" + compile.Block\is_value(stm) or transform.Value\can_transform stm comprehension_has_value = (comp) ->