experimenting with node constructors

This commit is contained in:
leaf corcoran 2011-09-26 12:43:13 -07:00
parent f174d96f2f
commit 445ef4314a
4 changed files with 115 additions and 17 deletions

View File

@ -469,6 +469,15 @@ 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
value = function(value)
local out = nil
do
local _with_0 = RootBlock()
_with_0:add(_with_0:value(value))
out = _with_0:render()
end
return out
end
tree = function(tree) tree = function(tree)
local scope = RootBlock() local scope = RootBlock()
local runner = coroutine.create(function() local runner = coroutine.create(function()

View File

@ -12,7 +12,7 @@ import ntype, Set from data
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
export tree, format_error export tree, value, format_error
export Block export Block
bubble_names = { "has_varargs" } bubble_names = { "has_varargs" }
@ -287,6 +287,13 @@ format_error = (msg, pos, file_str) ->
(" [%d] >> %s")\format line, trim line_str (" [%d] >> %s")\format line, trim line_str
}, "\n" }, "\n"
value = (value) ->
out = nil
with RootBlock!
\add \value value
out = \render!
out
tree = (tree) -> tree = (tree) ->
scope = RootBlock! scope = RootBlock!

View File

@ -2,25 +2,77 @@ 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 = data.ntype local ntype = data.ntype
local key_table = { local t = { }
local node_types = {
fndef = { fndef = {
{
"args", "args",
t
},
{
"whitelist", "whitelist",
t
},
{
"arrow", "arrow",
"body" "slim"
},
{
"body",
t
}
} }
} }
local build_table local build_table
build_table = function() build_table = function()
for key, value in pairs(key_table) do local key_table = { }
for name, args in pairs(node_types) do
local index = { } local index = { }
for i, name in ipairs(value) do for i, tuple in ipairs(args) do
local name = tuple[1]
index[name] = i + 1 index[name] = i + 1
end end
key_table[key] = index key_table[name] = index
end
return key_table
end
local key_table = build_table()
local make_builder
make_builder = function(name)
local spec = node_types[name]
if not spec then
error("don't know how to build node: " .. name)
end
return function(props)
if props == nil then
props = { }
end
local node = {
name
}
for i, arg in ipairs(spec) do
local default_value
name, default_value = unpack(arg)
local val
if props[name] then
val = props[name]
else
val = default_value
end
if val == t then
val = { }
end
node[i + 1] = val
end
return node
end end
end end
build_table() build = setmetatable({ }, {
__index = function(self, name)
self[name] = make_builder(name)
return rawget(self, name)
end
})
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

View File

@ -4,22 +4,52 @@ util = require "moonscript.util"
data = require "moonscript.data" data = require "moonscript.data"
import ntype from data import ntype from data
export smart_node export smart_node, build
-- todo: this should be merged into data -- todo: this should be merged into data
-- lets us index a node by item name based on it's type -- lets us index a node by item name based on it's type
key_table = { t = {}
fndef: {"args", "whitelist", "arrow", "body"}
node_types = {
fndef: {
{"args", t}
{"whitelist", t}
{"arrow", "slim"}
{"body", t}
}
} }
build_table = -> build_table = ->
for key, value in pairs key_table key_table = {}
for name, args in pairs node_types
index = {} index = {}
index[name] = i + 1 for i, name in ipairs value for i, tuple in ipairs args
key_table[key] = index name = tuple[1]
index[name] = i + 1
key_table[name] = index
key_table
build_table! key_table = build_table!
make_builder = (name) ->
spec = node_types[name]
error "don't know how to build node: "..name if not spec
(props={}) ->
node = { name }
for i, arg in ipairs spec
name, default_value = unpack arg
val = if props[name] then props[name] else default_value
val = {} if val == t
node[i + 1] = val
node
build = setmetatable {}, {
__index: (name) =>
self[name] = make_builder name
rawget self, name
}
smart_node = (node) -> smart_node = (node) ->
index = key_table[ntype node] index = key_table[ntype node]