Skip to content

Commit 1313013

Browse files
committed
with can take an assignment statement #46
1 parent 0d1f25c commit 1313013

File tree

5 files changed

+99
-7
lines changed

5 files changed

+99
-7
lines changed

moonscript/parse.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ end
205205

206206
-- the if statement only takes a single lhs, so we wrap in table to git to
207207
-- "assign" tuple format
208-
local function format_assign_for_if(lhs, assign)
208+
local function format_single_assign(lhs, assign)
209209
if assign then
210210
return format_assign({lhs}, assign)
211211
end
@@ -413,15 +413,16 @@ local build_grammar = wrap_env(function()
413413

414414
Return = key"return" * (ExpListLow/mark"explist" + C"") / mark"return",
415415

416-
With = key"with" * DisableDo * ensure(Exp, PopDo) * key"do"^-1 * Body / mark"with",
416+
WithExp = Ct(ExpList) * Assign^-1 / format_assign,
417+
With = key"with" * DisableDo * ensure(WithExp, PopDo) * key"do"^-1 * Body / mark"with",
417418

418419
Switch = key"switch" * DisableDo * ensure(Exp, PopDo) * key"do"^-1 * Space^-1 * Break * SwitchBlock / mark"switch",
419420

420421
SwitchBlock = EmptyLine^0 * Advance * Ct(SwitchCase * (Break^1 * SwitchCase)^0 * (Break^1 * SwitchElse)^-1) * PopIndent,
421422
SwitchCase = key"when" * Exp * key"then"^-1 * Body / mark"case",
422423
SwitchElse = key"else" * Body / mark"else",
423424

424-
IfCond = Exp * Assign^-1 / format_assign_for_if,
425+
IfCond = Exp * Assign^-1 / format_single_assign,
425426

426427
If = key"if" * IfCond * key"then"^-1 * Body *
427428
((Break * CheckIndent)^-1 * EmptyLine^0 * key"elseif" * pos(IfCond) * key"then"^-1 * Body / mark"elseif")^0 *

moonscript/transform.lua

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,11 +746,26 @@ Statement = Transformer({
746746
with = function(self, node, ret)
747747
local _, exp, block = unpack(node)
748748
local scope_name = NameProxy("with")
749+
local named_assign
750+
if ntype(exp) == "assign" then
751+
local names, values = unpack(exp, 2)
752+
local assign_name = names[1]
753+
exp = values[1]
754+
values[1] = scope_name
755+
named_assign = {
756+
"assign",
757+
names,
758+
values
759+
}
760+
end
749761
return build["do"]({
750-
build.assign_one(scope_name, exp),
751762
Run(function(self)
752763
return self:set("scope_var", scope_name)
753764
end),
765+
build.assign_one(scope_name, exp),
766+
build.group({
767+
named_assign
768+
}),
754769
build.group(block),
755770
(function()
756771
if ret then

moonscript/transform.moon

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,11 +347,22 @@ Statement = Transformer {
347347

348348
with: (node, ret) =>
349349
_, exp, block = unpack node
350+
350351
scope_name = NameProxy "with"
351-
build["do"] {
352-
build.assign_one scope_name, exp
352+
353+
named_assign = if ntype(exp) == "assign"
354+
names, values = unpack exp, 2
355+
assign_name = names[1]
356+
exp = values[1]
357+
values[1] = scope_name
358+
{"assign", names, values}
359+
360+
build.do {
353361
Run => @set "scope_var", scope_name
362+
build.assign_one scope_name, exp
363+
build.group { named_assign }
354364
build.group block
365+
355366
if ret
356367
ret scope_name
357368
}

tests/inputs/with.moon

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,29 @@ with foo
3636
.prop\send(one)
3737
.prop\send one
3838

39+
40+
--
41+
42+
with a, b -- b is lost
43+
print .world
44+
45+
mod = with _M = {}
46+
.Thing = "hi"
47+
48+
-- operate on a only
49+
with a, b = something, pooh
50+
print .world
51+
52+
x = with a, b = 1, 2
53+
print a + b
54+
55+
print with a, b = 1, 2
56+
print a + b
57+
58+
-- assignment lhs must be evaluated in the order they appear
59+
p = with hello!.x, world!.y = 1, 2
60+
print a + b
61+
62+
63+
64+

tests/outputs/with.lua

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,44 @@ do
4545
local _ = _with_0:prop("something").hello
4646
_with_0.prop:send(one)
4747
_with_0.prop:send(one)
48-
return _with_0
48+
end
49+
do
50+
local _with_0 = a, b
51+
print(_with_0.world)
52+
end
53+
local mod
54+
do
55+
local _with_0 = { }
56+
local _M = _with_0
57+
_with_0.Thing = "hi"
58+
mod = _with_0
59+
end
60+
do
61+
local _with_0 = something
62+
local b
63+
a, b = _with_0, pooh
64+
print(_with_0.world)
65+
end
66+
do
67+
local _with_0 = 1
68+
local b
69+
a, b = _with_0, 2
70+
print(a + b)
71+
x = _with_0
72+
end
73+
print((function()
74+
do
75+
local _with_0 = 1
76+
local b
77+
a, b = _with_0, 2
78+
print(a + b)
79+
return _with_0
80+
end
81+
end)())
82+
local p
83+
do
84+
local _with_0 = 1
85+
hello().x, world().y = _with_0, 2
86+
print(a + b)
87+
p = _with_0
4988
end

0 commit comments

Comments
 (0)