mirror of
https://github.com/leafo/moonscript.git
synced 2025-01-09 00:04:22 +00:00
fixed bug with assigning the names of statements
This commit is contained in:
parent
64fd33c089
commit
035b18cfff
@ -1,6 +1,7 @@
|
|||||||
module("moonscript.compile", package.seeall)
|
module("moonscript.compile", package.seeall)
|
||||||
local util = require("moonscript.util")
|
local util = require("moonscript.util")
|
||||||
require("moonscript.compile.format")
|
require("moonscript.compile.format")
|
||||||
|
local dump = require("moonscript.dump")
|
||||||
local reversed = util.reversed
|
local reversed = util.reversed
|
||||||
local ntype, smart_node
|
local ntype, smart_node
|
||||||
do
|
do
|
||||||
@ -40,34 +41,24 @@ line_compile = {
|
|||||||
end,
|
end,
|
||||||
assign = function(self, node)
|
assign = function(self, node)
|
||||||
local _, names, values = unpack(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 undeclared = self:declare(names)
|
||||||
local declare = "local " .. concat(undeclared, ", ")
|
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
|
if #undeclared > 0 then
|
||||||
self:add(declare)
|
self:add(declare)
|
||||||
end
|
end
|
||||||
if cascading[ntype(values)] then
|
local decorate
|
||||||
local decorate
|
decorate = function(value)
|
||||||
decorate = function(value)
|
return {
|
||||||
return {
|
"assign",
|
||||||
"assign",
|
names,
|
||||||
names,
|
{
|
||||||
{
|
value
|
||||||
value
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
end
|
}
|
||||||
return self:stm(values, decorate)
|
|
||||||
else
|
|
||||||
return error("Assigning unsupported statement")
|
|
||||||
end
|
end
|
||||||
|
return self:stm(stm, decorate)
|
||||||
else
|
else
|
||||||
local has_fndef = false
|
local has_fndef = false
|
||||||
local i = 1
|
local i = 1
|
||||||
|
@ -3,6 +3,7 @@ module "moonscript.compile", package.seeall
|
|||||||
util = require "moonscript.util"
|
util = require "moonscript.util"
|
||||||
|
|
||||||
require "moonscript.compile.format"
|
require "moonscript.compile.format"
|
||||||
|
dump = require "moonscript.dump"
|
||||||
|
|
||||||
import reversed from util
|
import reversed from util
|
||||||
import ntype, smart_node from require "moonscript.types"
|
import ntype, smart_node from require "moonscript.types"
|
||||||
@ -27,22 +28,17 @@ line_compile =
|
|||||||
assign: (node) =>
|
assign: (node) =>
|
||||||
_, names, values = unpack 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
|
undeclared = @declare names
|
||||||
declare = "local "..concat(undeclared, ", ")
|
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
|
@add declare if #undeclared > 0
|
||||||
if cascading[ntype(values)]
|
decorate = (value) ->
|
||||||
decorate = (value) ->
|
{"assign", names, {value}}
|
||||||
{"assign", names, {value}}
|
|
||||||
|
|
||||||
@stm values, decorate
|
@stm stm, decorate
|
||||||
else
|
|
||||||
error "Assigning unsupported statement"
|
|
||||||
else
|
else
|
||||||
has_fndef = false
|
has_fndef = false
|
||||||
i = 1
|
i = 1
|
||||||
|
@ -308,7 +308,7 @@ local build_grammar = wrap(function()
|
|||||||
CompFor = key"for" * Ct(NameList) * key"in" * (sym"*" * Exp / mark"unpack" + Exp) / mark"for",
|
CompFor = key"for" * Ct(NameList) * key"in" * (sym"*" * Exp / mark"unpack" + Exp) / mark"for",
|
||||||
CompClause = CompFor + key"when" * Exp / mark"when",
|
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",
|
Update = Assignable * ((sym"..=" + sym"+=" + sym"-=" + sym"*=" + sym"/=" + sym"%=")/trim) * Exp / mark"update",
|
||||||
|
|
||||||
-- we can ignore precedence for now
|
-- we can ignore precedence for now
|
||||||
|
Loading…
Reference in New Issue
Block a user