Skip to content

Commit 2096f9a

Browse files
committed
lexer, parsers/source: Add support for creating local variables without assignment
1 parent 3500740 commit 2096f9a

File tree

9 files changed

+32
-10
lines changed

9 files changed

+32
-10
lines changed

fusion/core/lexer.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ local pattern = re.compile([[
210210
211211
assignment <- {| {:type: '' -> 'assignment' :}
212212
(variable_list ws '=' ws (expression_list / r) /
213+
{:is_local: 'local' -> true :} ws {:is_nil: '(' -> true :} ws local_name
214+
(ws ',' ws (local_name / r))* ws ')' /
213215
{:is_local: 'local' -> true :} space (name_list / r) ws ('=' / r) ws
214216
(expression_list / r))
215217
|}

fusion/core/parsers/source.lua

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function parser:transform_expression_list(node)
4242
for i=1, #list do
4343
output[#output + 1] = self:transform(list[i])
4444
end
45-
return table.concat(output, ",")
45+
return table.concat(output, ", ")
4646
end
4747

4848
--- Convert a variable_list to a transformed list of variable names.
@@ -53,7 +53,7 @@ function parser:transform_variable_list(node)
5353
for i=1, #list do
5454
output[#output + 1] = self:transform(list[i])
5555
end
56-
return table.concat(output, ",")
56+
return table.concat(output, ", ")
5757
end
5858

5959
local _tablegen_level = 0
@@ -489,7 +489,14 @@ handlers['assignment'] = function(self, node)
489489
if node.is_local then
490490
output[1] = "local "
491491
end
492-
if node.variable_list.is_destructuring then
492+
if node.is_nil then
493+
local names = {}
494+
for i, v in ipairs(node) do -- luacheck: ignore 213
495+
table.insert(names, self:transform(v))
496+
end
497+
table.insert(output, table.concat(names, ", "))
498+
return table.concat(output)
499+
elseif node.variable_list.is_destructuring then
493500
local expression = self:transform(node.expression_list[1])
494501
local name
495502
if node.expression_list[1].type == "variable" and

spec/in/assignment.fuse

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
local a = 5;
44

5+
-- assign local nil values --
6+
7+
local (a, b);
8+
59
-- assign literal to global variable --
610

711
b = 0;

spec/lexer_spec.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ describe("lexer", function()
4545
}
4646
}})
4747
end)
48+
it("can parse local nil assignment", function()
49+
assert.same(lexer:match("local (a, b);"), {{type = "assignment",
50+
{type = "variable", "a"},
51+
{type = "variable", "b"},
52+
is_local = true,
53+
is_nil = true
54+
}})
55+
end)
4856
it("can parse basic assignment", function()
4957
assert.same(lexer:match("a = b;"), {
5058
{type = "assignment",

spec/out/source/assignment.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local a = 5
2+
local a, b
23
b = 0
34
local c = a
45
local d = b

spec/out/source/class.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ ExampleToo = class({
2626
if not b then
2727
b = 10
2828
end
29-
self.a,self.b = a,b
29+
self.a, self.b = a, b
3030
end);
3131
print = (function()
3232
print(self.b)
@@ -37,7 +37,7 @@ c:print()
3737
Example.print(c)
3838
ExampleThree = class({
3939
__init = (function(self, a, b)
40-
ExampleToo.__init(self,a,b)
40+
ExampleToo.__init(self, a, b)
4141
end);
4242
}, {extends = ExampleToo}, "ExampleThree")
4343
X = {}

spec/out/source/functions.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ end
1212
for line in io.lines("test") do
1313
print(line)
1414
end
15-
for k,v in pairs(x) do
16-
print(k,v)
15+
for k, v in pairs(x) do
16+
print(k, v)
1717
end
1818
function a_1()
1919
return b
@@ -29,7 +29,7 @@ local function a_4()
2929
end
3030
function x()
3131
return coroutine.wrap(function()
32-
coroutine.yield(a,b,c)
32+
coroutine.yield(a, b, c)
3333
end)
3434
end
3535
function abcd.ef(self)

spec/out/source/literals.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ local transformed_array = (function()
4141
end)()
4242
local copy_transformed_array = (function()
4343
local _generator_1 = {}
44-
for k,v in pairs(transformed_array) do
44+
for k, v in pairs(transformed_array) do
4545
_generator_1[k] = v
4646
end
4747
return _generator_1

spec/out/source/loops.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ for x in y() do
2121
end
2222
for x in (y())() do
2323
end
24-
for x,y in z() do
24+
for x, y in z() do
2525
end
2626
while true do
2727
break

0 commit comments

Comments
 (0)