mirror of
https://github.com/leafo/moonscript.git
synced 2025-01-09 00:04:22 +00:00
remove module from compile, reorganize
This commit is contained in:
parent
33eb9510dd
commit
38463a078d
@ -3,7 +3,7 @@
|
|||||||
module("moonscript", package.seeall)
|
module("moonscript", package.seeall)
|
||||||
|
|
||||||
require "moonscript.parse"
|
require "moonscript.parse"
|
||||||
require "moonscript.compile"
|
local compile = require "moonscript.compile"
|
||||||
local util = require "moonscript.util"
|
local util = require "moonscript.util"
|
||||||
|
|
||||||
require "alt_getopt"
|
require "alt_getopt"
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
module("moonscript.compile", package.seeall)
|
|
||||||
local util = require("moonscript.util")
|
local util = require("moonscript.util")
|
||||||
local dump = require("moonscript.dump")
|
local dump = require("moonscript.dump")
|
||||||
require("moonscript.compile.statement")
|
|
||||||
require("moonscript.compile.value")
|
|
||||||
local transform = require("moonscript.transform")
|
local transform = require("moonscript.transform")
|
||||||
local NameProxy, LocalName
|
local NameProxy, LocalName
|
||||||
do
|
do
|
||||||
@ -19,11 +16,21 @@ do
|
|||||||
local _table_0 = require("moonscript.types")
|
local _table_0 = require("moonscript.types")
|
||||||
ntype, has_value = _table_0.ntype, _table_0.has_value
|
ntype, has_value = _table_0.ntype, _table_0.has_value
|
||||||
end
|
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 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 pos_to_line, get_closest_line, trim = util.pos_to_line, util.get_closest_line, util.trim
|
||||||
local mtype = util.moon.type
|
local mtype = util.moon.type
|
||||||
local indent_char = " "
|
local indent_char = " "
|
||||||
local Line, Lines
|
local Line, Lines, Block, RootBlock
|
||||||
do
|
do
|
||||||
local _parent_0 = nil
|
local _parent_0 = nil
|
||||||
local _base_0 = {
|
local _base_0 = {
|
||||||
@ -160,7 +167,7 @@ do
|
|||||||
end
|
end
|
||||||
local _list_0 = item
|
local _list_0 = item
|
||||||
for _index_0 = 1, #_list_0 do
|
for _index_0 = 1, #_list_0 do
|
||||||
value = _list_0[_index_0]
|
local value = _list_0[_index_0]
|
||||||
self:_append_single(value)
|
self:_append_single(value)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
@ -333,7 +340,7 @@ do
|
|||||||
self._name_whitelist = Set(names)
|
self._name_whitelist = Set(names)
|
||||||
end,
|
end,
|
||||||
put_name = function(self, name, ...)
|
put_name = function(self, name, ...)
|
||||||
value = ...
|
local value = ...
|
||||||
if select("#", ...) == 0 then
|
if select("#", ...) == 0 then
|
||||||
value = true
|
value = true
|
||||||
end
|
end
|
||||||
@ -423,11 +430,11 @@ do
|
|||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
is_stm = function(self, node)
|
is_stm = function(self, node)
|
||||||
return line_compile[ntype(node)] ~= nil
|
return statement_compilers[ntype(node)] ~= nil
|
||||||
end,
|
end,
|
||||||
is_value = function(self, node)
|
is_value = function(self, node)
|
||||||
local t = ntype(node)
|
local t = ntype(node)
|
||||||
return value_compile[t] ~= nil or t == "value"
|
return value_compilers[t] ~= nil or t == "value"
|
||||||
end,
|
end,
|
||||||
name = function(self, node)
|
name = function(self, node)
|
||||||
return self:value(node)
|
return self:value(node)
|
||||||
@ -440,7 +447,7 @@ do
|
|||||||
else
|
else
|
||||||
action = node[1]
|
action = node[1]
|
||||||
end
|
end
|
||||||
local fn = value_compile[action]
|
local fn = value_compilers[action]
|
||||||
if not fn then
|
if not fn then
|
||||||
error("Failed to compile value: " .. dump.value(node))
|
error("Failed to compile value: " .. dump.value(node))
|
||||||
end
|
end
|
||||||
@ -482,7 +489,7 @@ do
|
|||||||
node = self.transform.statement(node)
|
node = self.transform.statement(node)
|
||||||
local result
|
local result
|
||||||
do
|
do
|
||||||
local fn = line_compile[ntype(node)]
|
local fn = statement_compilers[ntype(node)]
|
||||||
if fn then
|
if fn then
|
||||||
result = fn(self, node, ...)
|
result = fn(self, node, ...)
|
||||||
else
|
else
|
||||||
@ -642,6 +649,7 @@ do
|
|||||||
end
|
end
|
||||||
RootBlock = _class_0
|
RootBlock = _class_0
|
||||||
end
|
end
|
||||||
|
local format_error
|
||||||
format_error = function(msg, pos, file_str)
|
format_error = function(msg, pos, file_str)
|
||||||
local line = pos_to_line(file_str, pos)
|
local line = pos_to_line(file_str, pos)
|
||||||
local line_str
|
local line_str
|
||||||
@ -652,6 +660,7 @@ format_error = function(msg, pos, file_str)
|
|||||||
(" [%d] >> %s"):format(line, trim(line_str))
|
(" [%d] >> %s"):format(line, trim(line_str))
|
||||||
}, "\n")
|
}, "\n")
|
||||||
end
|
end
|
||||||
|
local value
|
||||||
value = function(value)
|
value = function(value)
|
||||||
local out = nil
|
local out = nil
|
||||||
do
|
do
|
||||||
@ -661,6 +670,7 @@ value = function(value)
|
|||||||
end
|
end
|
||||||
return out
|
return out
|
||||||
end
|
end
|
||||||
|
local tree
|
||||||
tree = function(tree, options)
|
tree = function(tree, options)
|
||||||
if options == nil then
|
if options == nil then
|
||||||
options = { }
|
options = { }
|
||||||
@ -693,3 +703,10 @@ tree = function(tree, options)
|
|||||||
return lua_code, posmap
|
return lua_code, posmap
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return {
|
||||||
|
tree = tree,
|
||||||
|
value = value,
|
||||||
|
format_error = format_error,
|
||||||
|
Block = Block,
|
||||||
|
RootBlock = RootBlock
|
||||||
|
}
|
||||||
|
@ -1,17 +1,15 @@
|
|||||||
module "moonscript.compile", package.seeall
|
|
||||||
|
|
||||||
util = require "moonscript.util"
|
util = require "moonscript.util"
|
||||||
dump = require "moonscript.dump"
|
dump = require "moonscript.dump"
|
||||||
|
|
||||||
require "moonscript.compile.statement"
|
|
||||||
require "moonscript.compile.value"
|
|
||||||
|
|
||||||
transform = require "moonscript.transform"
|
transform = require "moonscript.transform"
|
||||||
|
|
||||||
import NameProxy, LocalName from require "moonscript.transform.names"
|
import NameProxy, LocalName from require "moonscript.transform.names"
|
||||||
import Set from require "moonscript.data"
|
import Set from require "moonscript.data"
|
||||||
import ntype, has_value from require "moonscript.types"
|
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 concat, insert from table
|
||||||
import pos_to_line, get_closest_line, trim from util
|
import pos_to_line, get_closest_line, trim from util
|
||||||
|
|
||||||
@ -19,10 +17,7 @@ mtype = util.moon.type
|
|||||||
|
|
||||||
indent_char = " "
|
indent_char = " "
|
||||||
|
|
||||||
export tree, value, format_error
|
local Line, Lines, Block, RootBlock
|
||||||
export Block, RootBlock
|
|
||||||
|
|
||||||
local Line, Lines
|
|
||||||
|
|
||||||
-- a buffer for building up lines
|
-- a buffer for building up lines
|
||||||
class Lines
|
class Lines
|
||||||
@ -277,11 +272,11 @@ class Block
|
|||||||
\append ...
|
\append ...
|
||||||
|
|
||||||
is_stm: (node) =>
|
is_stm: (node) =>
|
||||||
line_compile[ntype node] != nil
|
statement_compilers[ntype node] != nil
|
||||||
|
|
||||||
is_value: (node) =>
|
is_value: (node) =>
|
||||||
t = ntype node
|
t = ntype node
|
||||||
value_compile[t] != nil or t == "value"
|
value_compilers[t] != nil or t == "value"
|
||||||
|
|
||||||
-- line wise compile functions
|
-- line wise compile functions
|
||||||
name: (node) => @value node
|
name: (node) => @value node
|
||||||
@ -292,7 +287,7 @@ class Block
|
|||||||
else
|
else
|
||||||
node[1]
|
node[1]
|
||||||
|
|
||||||
fn = value_compile[action]
|
fn = value_compilers[action]
|
||||||
error "Failed to compile value: "..dump.value node if not fn
|
error "Failed to compile value: "..dump.value node if not fn
|
||||||
|
|
||||||
out = fn self, node, ...
|
out = fn self, node, ...
|
||||||
@ -314,7 +309,7 @@ class Block
|
|||||||
return if not node -- skip blank statements
|
return if not node -- skip blank statements
|
||||||
node = @transform.statement node
|
node = @transform.statement node
|
||||||
|
|
||||||
result = if fn = line_compile[ntype(node)]
|
result = if fn = statement_compilers[ntype(node)]
|
||||||
fn self, node, ...
|
fn self, node, ...
|
||||||
else
|
else
|
||||||
-- coerce value into statement
|
-- coerce value into statement
|
||||||
@ -399,3 +394,4 @@ tree = (tree, options={}) ->
|
|||||||
posmap = scope._lines\flatten_posmap!
|
posmap = scope._lines\flatten_posmap!
|
||||||
lua_code, posmap
|
lua_code, posmap
|
||||||
|
|
||||||
|
{ :tree, :value, :format_error, :Block, :RootBlock }
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
module("moonscript.compile", package.seeall)
|
local reversed
|
||||||
local util = require("moonscript.util")
|
do
|
||||||
local dump = require("moonscript.dump")
|
local _table_0 = require("moonscript.util")
|
||||||
local transform = require("moonscript.transform")
|
reversed = _table_0.reversed
|
||||||
local reversed = util.reversed
|
end
|
||||||
local ntype
|
local ntype
|
||||||
do
|
do
|
||||||
local _table_0 = require("moonscript.types")
|
local _table_0 = require("moonscript.types")
|
||||||
ntype = _table_0.ntype
|
ntype = _table_0.ntype
|
||||||
end
|
end
|
||||||
local concat, insert = table.concat, table.insert
|
local concat, insert = table.concat, table.insert
|
||||||
line_compile = {
|
local statement_compilers = {
|
||||||
raw = function(self, node)
|
raw = function(self, node)
|
||||||
return self:add(node[2])
|
return self:add(node[2])
|
||||||
end,
|
end,
|
||||||
@ -246,3 +246,6 @@ line_compile = {
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
return {
|
||||||
|
statement_compilers = statement_compilers
|
||||||
|
}
|
||||||
|
@ -1,17 +1,9 @@
|
|||||||
module "moonscript.compile", package.seeall
|
|
||||||
|
|
||||||
util = require "moonscript.util"
|
import reversed from require "moonscript.util"
|
||||||
|
|
||||||
dump = require "moonscript.dump"
|
|
||||||
transform = require "moonscript.transform"
|
|
||||||
|
|
||||||
import reversed from util
|
|
||||||
import ntype from require "moonscript.types"
|
import ntype from require "moonscript.types"
|
||||||
import concat, insert from table
|
import concat, insert from table
|
||||||
|
|
||||||
export line_compile
|
statement_compilers =
|
||||||
|
|
||||||
line_compile =
|
|
||||||
raw: (node) => @add node[2]
|
raw: (node) => @add node[2]
|
||||||
|
|
||||||
lines: (node) =>
|
lines: (node) =>
|
||||||
@ -139,3 +131,5 @@ line_compile =
|
|||||||
with @block!
|
with @block!
|
||||||
\stms node[2]
|
\stms node[2]
|
||||||
|
|
||||||
|
|
||||||
|
{ :statement_compilers }
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
module("moonscript.compile", package.seeall)
|
|
||||||
local util = require("moonscript.util")
|
local util = require("moonscript.util")
|
||||||
local data = require("moonscript.data")
|
local data = require("moonscript.data")
|
||||||
local ntype
|
local ntype
|
||||||
@ -13,7 +12,7 @@ do
|
|||||||
end
|
end
|
||||||
local concat, insert = table.concat, table.insert
|
local concat, insert = table.concat, table.insert
|
||||||
local table_delim = ","
|
local table_delim = ","
|
||||||
value_compile = {
|
local value_compilers = {
|
||||||
exp = function(self, node)
|
exp = function(self, node)
|
||||||
local _comp
|
local _comp
|
||||||
_comp = function(i, value)
|
_comp = function(i, value)
|
||||||
@ -302,3 +301,6 @@ value_compile = {
|
|||||||
return tostring(value)
|
return tostring(value)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
return {
|
||||||
|
value_compilers = value_compilers
|
||||||
|
}
|
||||||
|
@ -1,19 +1,14 @@
|
|||||||
|
|
||||||
module "moonscript.compile", package.seeall
|
|
||||||
|
|
||||||
util = require "moonscript.util"
|
util = require "moonscript.util"
|
||||||
data = require "moonscript.data"
|
data = require "moonscript.data"
|
||||||
|
|
||||||
import ntype from require "moonscript.types"
|
import ntype from require "moonscript.types"
|
||||||
import user_error from require "moonscript.errors"
|
import user_error from require "moonscript.errors"
|
||||||
|
|
||||||
import concat, insert from table
|
import concat, insert from table
|
||||||
|
|
||||||
export value_compile
|
|
||||||
|
|
||||||
table_delim = ","
|
table_delim = ","
|
||||||
|
|
||||||
value_compile =
|
value_compilers =
|
||||||
-- list of values separated by binary operators
|
-- list of values separated by binary operators
|
||||||
exp: (node) =>
|
exp: (node) =>
|
||||||
_comp = (i, value) ->
|
_comp = (i, value) ->
|
||||||
@ -189,3 +184,6 @@ value_compile =
|
|||||||
@send "varargs"
|
@send "varargs"
|
||||||
|
|
||||||
tostring value
|
tostring value
|
||||||
|
|
||||||
|
|
||||||
|
{ :value_compilers }
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
module("moonscript", package.seeall)
|
module("moonscript", package.seeall)
|
||||||
require("moonscript.compile")
|
local compile = require("moonscript.compile")
|
||||||
require("moonscript.parse")
|
require("moonscript.parse")
|
||||||
local concat, insert = table.concat, table.insert
|
local concat, insert = table.concat, table.insert
|
||||||
local split, dump
|
local split, dump
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
module "moonscript", package.seeall
|
module "moonscript", package.seeall
|
||||||
|
|
||||||
require "moonscript.compile"
|
compile = require "moonscript.compile"
|
||||||
require "moonscript.parse"
|
require "moonscript.parse"
|
||||||
|
|
||||||
import concat, insert from table
|
import concat, insert from table
|
||||||
|
@ -37,7 +37,8 @@ has_value = function(node)
|
|||||||
end
|
end
|
||||||
local is_value
|
local is_value
|
||||||
is_value = function(stm)
|
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)
|
return compile.Block:is_value(stm) or transform.Value:can_transform(stm)
|
||||||
end
|
end
|
||||||
local comprehension_has_value
|
local comprehension_has_value
|
||||||
|
@ -31,7 +31,9 @@ has_value = (node) ->
|
|||||||
true
|
true
|
||||||
|
|
||||||
is_value = (stm) ->
|
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
|
compile.Block\is_value(stm) or transform.Value\can_transform stm
|
||||||
|
|
||||||
comprehension_has_value = (comp) ->
|
comprehension_has_value = (comp) ->
|
||||||
|
Loading…
Reference in New Issue
Block a user