updated compiler for new syntax

This commit is contained in:
leaf corcoran 2011-06-23 00:05:28 -07:00
parent c86a386d00
commit 2a5a34ac19
4 changed files with 78 additions and 78 deletions

View File

@ -22,7 +22,7 @@ pretty = (lines, indent) ->
-- add semicolons for ambiguities
fix = (i, left, k, right) ->
if left:sub(-1) == ")" and right:sub(1,1) == "("
if left\sub(-1) == ")" and right\sub(1,1) == "("
lines[i] = lines[i]..";"
fix(i,l, k,r) for i,l,k,r in itwos lines

View File

@ -309,7 +309,7 @@ line_compile = {
if parent_val ~= "" then
parent_val = self:value(parent_val)
end
local def = concat({ ("(function(%s)\n"):format(parent_loc), (def_scope:render()), ("\nend)(%s)"):format(parent_val) })
local def = concat({ ("(function(%s)\n"):format(parent_loc), def_scope:render(), ("\nend)(%s)"):format(parent_val) })
self:add_line("local", name)
self:put_name(name)
return self:stm({ "assign", { name }, { def } })
@ -326,7 +326,7 @@ line_compile = {
render_clause = function(self, clause)
local t = clause[1]
action = self:block()
action:set_indent(-1 + self.indent)
action:set_indent(self.indent - 1)
if "for" == t then
local names, iter
_, names, iter = unpack(clause)

View File

@ -54,7 +54,7 @@ line_compile =
update: (node) =>
_, name, op, exp = unpack node
op_final = op:match"(.)="
op_final = op\match "(.)="
error"unknown op: "..op if not op_final
@stm {"assign", {name}, {{"exp", name, op_final, exp}}}
@ -95,13 +95,13 @@ line_compile =
@add_line "do"
inner = @block()
tmp_name = inner:free_name "table"
inner:add_line "local", tmp_name , "=", @value source
tmp_name = inner\free_name "table"
inner\add_line "local", tmp_name , "=", @value source
source = tmp_name
inner:add_line name.." = "..get_value name for name in *final_names
inner\add_line name.." = "..get_value name for name in *final_names
@add_line inner:render()
@add_line inner\render!
@add_line "end"
@ -117,15 +117,15 @@ line_compile =
@add_line "elseif", (@value clause[2]), "then"
clause[3]
b = @block()
b:stms block, ret
@add_line b:render()
b = @block!
b\stms block, ret
@add_line b\render!
@add_line "if", (@value cond), "then"
b = @block()
b:stms block, ret
@add_line b:render()
b = @block!
b\stms block, ret
@add_line b\render!
add_clause cond for i, cond in ipairs node when i > 3
@ -134,25 +134,25 @@ line_compile =
while: (node) =>
_, cond, block = unpack node
inner = @block()
inner = @block!
if is_non_atomic cond
@add_line "while", "true", "do"
inner:stm {"if", {"not", cond}, {{"break"}}}
inner\stm {"if", {"not", cond}, {{"break"}}}
else
@add_line "while", @value(cond), "do"
inner:stms block
inner\stms block
@add_line inner:render()
@add_line inner\render!
@add_line "end"
for: (node) =>
_, name, bounds, block = unpack node
bounds = @value {"explist", unpack bounds}
@add_line "for", @name(name), "=", bounds, "do"
inner = @block()
inner:stms block
@add_line inner:render()
inner = @block!
inner\stms block
@add_line inner\render!
@add_line "end"
export: (node) =>
@ -175,11 +175,11 @@ line_compile =
find_special unpack entry for entry in *tbl[2]
tbl[2] = final_properties
def_scope = @block()
parent_loc = def_scope:free_name "parent"
def_scope = @block!
parent_loc = def_scope\free_name "parent"
def_scope:set "super" (block, chain) ->
calling_name = block:get"current_block"
def_scope\set "super" (block, chain) ->
calling_name = block\get"current_block"
slice = [item for i, item in ipairs chain when i > 2]
-- inject self
slice[1] = {"call", {"self", unpack slice[1][2]}}
@ -211,37 +211,37 @@ line_compile =
dests = [{"self", name} for name in *self_args]
insert body, 1, {"assign", dests, self_args} if #self_args > 0
base_name = def_scope:free_name "base"
def_scope:add_line ("local %s ="):format(base_name), def_scope:value tbl
def_scope:add_line ("%s.__index = %s"):format(base_name, base_name)
base_name = def_scope\free_name "base"
def_scope\add_line ("local %s =")\format(base_name), def_scope\value tbl
def_scope\add_line ("%s.__index = %s")\format(base_name, base_name)
cls = def_scope:value {"table", {
cls = def_scope\value {"table", {
{"__init", constructor}
}}
cls_mt = def_scope:value {"table", {
cls_mt = def_scope\value {"table", {
{"__index", base_name}
{"__call", {"fndef", {"mt", "..."}, "slim", {
{"raw", ("local self = setmetatable({}, %s)"):format(base_name)}
{"raw", ("local self = setmetatable({}, %s)")\format(base_name)}
{"chain", "mt.__init", {"call", {"self", "..."}}}
"self"
}}}
}}
if parent_val != ""
def_scope:stm {"if", parent_loc,
def_scope\stm {"if", parent_loc,
{{"chain", "setmetatable", {"call",
{base_name, {"chain", "getmetatable",
{"call", {parent_loc}}, {"dot", "__index"}}}}}}}
def_scope:add_line ("return setmetatable(%s, %s)"):format(cls, cls_mt)
def_scope\add_line ("return setmetatable(%s, %s)")\format(cls, cls_mt)
parent_val = @value parent_val if parent_val != ""
def = concat {
("(function(%s)\n"):format(parent_loc)
(def_scope:render())
("\nend)(%s)"):format(parent_val)
("(function(%s)\n")\format(parent_loc)
def_scope\render()
("\nend)(%s)")\format(parent_val)
}
@add_line "local", name
@ -252,16 +252,16 @@ line_compile =
_, exp, clauses = unpack node
if not action
action = @block()
action:stm exp
action = @block!
action\stm exp
depth = #clauses
action:set_indent @indent + depth
action\set_indent @indent + depth
render_clause = (clause) =>
t = clause[1]
action = @block()
action:set_indent -1 + @indent
action\set_indent @indent - 1
if "for" == t
_, names, iter = unpack clause
@ -272,24 +272,24 @@ line_compile =
items_tmp = @free_name "item"
index_tmp = @free_name "index"
insert self._lines, 1, ("local %s = %s[%s]"):format name_list, items_tmp, index_tmp
insert self._lines, 1, ("local %s = %s[%s]")\format name_list, items_tmp, index_tmp
action:add_lines {
("local %s = %s"):format items_tmp, @value iter
("for %s=1,#%s do"):format index_tmp, items_tmp
action\add_lines {
("local %s = %s")\format items_tmp, @value iter
("for %s=1,#%s do")\format index_tmp, items_tmp
@render true
"end"
}
else
action:add_lines {
("for %s in %s do"):format(name_list, @value iter)
action\add_lines {
("for %s in %s do")\format(name_list, @value iter)
@render true
"end"
}
elseif "when" == t
_, cond = unpack clause
action:add_lines {
("if %s then"):format @value cond
action\add_lines {
("if %s then")\format @value cond
@render true
"end"
}
@ -303,14 +303,14 @@ line_compile =
with: (node, ret) =>
_, exp, block = unpack node
inner = @block!
tmp_name = inner:free_name "with", true
tmp_name = inner\free_name "with", true
@set "scope_var", tmp_name
inner:stm {"assign", {tmp_name}, {exp}}
inner:stms block
inner:stm ret tmp_name if ret
inner\stm {"assign", {tmp_name}, {exp}}
inner\stms block
inner\stm ret tmp_name if ret
@add_line "do"
@add_line inner:render!
@add_line inner\render!
@add_line "end"

View File

@ -39,24 +39,24 @@ value_compile =
delim..inner..(delim_end or delim)
if: (node) =>
func = @block()
func:stm node, returner
@format "(function()", func:render(), "end)()"
func = @block!
func\stm node, returner
@format "(function()", func\render!, "end)()"
comprehension: (node) =>
exp = node[2]
func = @block()
tmp_name = func:free_name()
func = @block!
tmp_name = func\free_name!
func:add_line "local", tmp_name, "= {}"
func\add_line "local", tmp_name, "= {}"
action = func:block()
action:add_line ("table.insert(%s, %s)"):format(tmp_name, func:value exp)
func:stm node, action
action = func\block!
action\add_line ("table.insert(%s, %s)")\format(tmp_name, func\value exp)
func\stm node, action
func:add_line "return", tmp_name
func\add_line "return", tmp_name
@format "(function()", func:render(), "end)()"
@format "(function()", func\render!, "end)()"
chain: (node) =>
callee = node[2]
@ -98,9 +98,9 @@ value_compile =
if arrow == "fat"
insert args, 1, "self"
b = @block()
b:put_name name for name in *args
b:ret_stms block
b = @block!
b\put_name name for name in *args
b\ret_stms block
decl = "function("..(concat args, ", ")..")"
if #b._lines == 0
@ -113,7 +113,7 @@ value_compile =
table: (node) =>
_, items = unpack node
inner = @block() -- handle indent
inner = @block! -- handle indent
_comp = (i, tuple) ->
out = if #tuple == 2
key, value = unpack tuple
@ -123,17 +123,17 @@ value_compile =
key_val = @value key
key = if type(key) != "string"
("[%s]"):format key_val
("[%s]")\format key_val
else
key_val
inner:set "current_block", key_val
value = inner:value value
inner:set "current_block", nil
inner\set "current_block", key_val
value = inner\value value
inner\set "current_block", nil
("%s = %s"):format key, value
("%s = %s")\format key, value
else
inner:value tuple[1]
inner\value tuple[1]
out.."," if i != #items else out
@ -178,7 +178,7 @@ class Block
set_indent: (depth) =>
@indent = depth
@lead = indent_char:rep @indent
@lead = indent_char\rep @indent
declare: (names) =>
undeclared = [name for name in *names when type(name) == "string" and not @has_name name]
@ -301,7 +301,7 @@ build_compiler = ->
setmetatable {}, { __index: compiler_index }
tree = (tree) ->
scope = Block()
scope:stm line for line in *tree
scope:render()
scope = Block!
scope\stm line for line in *tree
scope\render!