fixed bug with assigning the names of statements

This commit is contained in:
leaf corcoran 2011-09-30 19:35:22 -07:00
parent 64fd33c089
commit 035b18cfff
3 changed files with 20 additions and 33 deletions

View File

@ -1,6 +1,7 @@
module("moonscript.compile", package.seeall)
local util = require("moonscript.util")
require("moonscript.compile.format")
local dump = require("moonscript.dump")
local reversed = util.reversed
local ntype, smart_node
do
@ -40,34 +41,24 @@ line_compile = {
end,
assign = function(self, node)
local _, names, values = unpack(node)
if #values == 1 and cascading[ntype(values[1])] then
return self:stm({
"assign",
names,
values[1]
})
end
local undeclared = self:declare(names)
local declare = "local " .. concat(undeclared, ", ")
if self:is_stm(values) then
if #values == 1 and self:is_stm(values[1]) and cascading[ntype(values[1])] then
local stm = values[1]
if #undeclared > 0 then
self:add(declare)
end
if cascading[ntype(values)] then
local decorate
decorate = function(value)
return {
"assign",
names,
{
value
}
local decorate
decorate = function(value)
return {
"assign",
names,
{
value
}
end
return self:stm(values, decorate)
else
return error("Assigning unsupported statement")
}
end
return self:stm(stm, decorate)
else
local has_fndef = false
local i = 1

View File

@ -3,6 +3,7 @@ module "moonscript.compile", package.seeall
util = require "moonscript.util"
require "moonscript.compile.format"
dump = require "moonscript.dump"
import reversed from util
import ntype, smart_node from require "moonscript.types"
@ -27,22 +28,17 @@ line_compile =
assign: (node) =>
_, names, values = unpack node
-- can we extract single cascading value
if #values == 1 and cascading[ntype values[1]]
return @stm {"assign", names, values[1]}
undeclared = @declare names
declare = "local "..concat(undeclared, ", ")
if @is_stm values
-- todo: tree transformation
if #values == 1 and @is_stm(values[1]) and cascading[ntype(values[1])]
stm = values[1]
@add declare if #undeclared > 0
if cascading[ntype(values)]
decorate = (value) ->
{"assign", names, {value}}
decorate = (value) ->
{"assign", names, {value}}
@stm values, decorate
else
error "Assigning unsupported statement"
@stm stm, decorate
else
has_fndef = false
i = 1

View File

@ -308,7 +308,7 @@ local build_grammar = wrap(function()
CompFor = key"for" * Ct(NameList) * key"in" * (sym"*" * Exp / mark"unpack" + Exp) / mark"for",
CompClause = CompFor + key"when" * Exp / mark"when",
Assign = Ct(AssignableList) * sym"=" * (With + If + Ct(TableBlock + ExpListLow)) / mark"assign",
Assign = Ct(AssignableList) * sym"=" * (Ct(With + If) + Ct(TableBlock + ExpListLow)) / mark"assign",
Update = Assignable * ((sym"..=" + sym"+=" + sym"-=" + sym"*=" + sym"/=" + sym"%=")/trim) * Exp / mark"update",
-- we can ignore precedence for now