Skip to content

Commit 71530f0

Browse files
Added dictionary comprehension
1 parent 90c3331 commit 71530f0

File tree

4 files changed

+69
-17
lines changed

4 files changed

+69
-17
lines changed

pythonlua/luainit.lua

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ end
4848

4949
local function list(t)
5050
local result = {}
51+
52+
result.is_list = true
53+
54+
result._data = {}
55+
for _, v in ipairs(t) do
56+
table.insert(result._data, v)
57+
end
5158

5259
local methods = {}
5360

@@ -65,6 +72,9 @@ local function list(t)
6572

6673
return methods[index]
6774
end,
75+
__newindex = function(self, index, value)
76+
result._data[index] = value
77+
end,
6878
__call = function(self, _, idx)
6979
if idx == nil and iterator_index ~= nil then
7080
iterator_index = nil
@@ -77,19 +87,19 @@ local function list(t)
7787
end,
7888
})
7989

80-
result.is_list = true
81-
82-
result._data = {}
83-
for _, v in ipairs(t) do
84-
table.insert(result._data, v)
85-
end
86-
8790
return result
8891
end
8992

9093
function dict(t)
9194
local result = {}
9295

96+
result.is_dict = true
97+
98+
result._data = {}
99+
for k, v in pairs(t) do
100+
result._data[k] = v
101+
end
102+
93103
local methods = {}
94104

95105
methods.items = function()
@@ -98,8 +108,13 @@ function dict(t)
98108

99109
local key_index = nil
100110

101-
setmetatable(t, {
102-
__index = methods,
111+
setmetatable(result, {
112+
__index = function(self, index)
113+
return methods[index]
114+
end,
115+
__newindex = function(self, index, value)
116+
result._data[index] = value
117+
end,
103118
__call = function(self, _, idx)
104119
if idx == nil and key_index ~= nil then
105120
key_index = nil
@@ -110,13 +125,6 @@ function dict(t)
110125
return key_index
111126
end,
112127
})
113-
114-
result.is_dict = true
115-
116-
result._data = {}
117-
for k, v in pairs(t) do
118-
result._data[k] = v
119-
end
120128

121129
return result
122130
end

pythonlua/nodevisitor.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,40 @@ def visit_Dict(self, node):
192192
elements = ", ".join(elements)
193193
self.emit("dict {{{}}}".format(elements))
194194

195+
def visit_DictComp(self, node):
196+
"""Visit dictionary comprehension"""
197+
self.emit("(function()")
198+
self.emit("local result = dict {}")
199+
200+
ends_count = 0
201+
202+
for comp in node.generators:
203+
line = "for {target} in {iterator} do"
204+
values = {
205+
"target": self.visit_all(comp.target, inline=True),
206+
"iterator": self.visit_all(comp.iter, inline=True),
207+
}
208+
line = line.format(**values)
209+
self.emit(line)
210+
ends_count += 1
211+
212+
for if_ in comp.ifs:
213+
line = "if {} then".format(self.visit_all(if_, inline=True))
214+
self.emit(line)
215+
ends_count += 1
216+
217+
line = "result[{key}] = {value}"
218+
values = {
219+
"key": self.visit_all(node.key, inline=True),
220+
"value": self.visit_all(node.value, inline=True),
221+
}
222+
self.emit(line.format(**values))
223+
224+
self.emit(" ".join(["end"] * ends_count))
225+
226+
self.emit("return result")
227+
self.emit("end)()")
228+
195229
def visit_Ellipsis(self, node):
196230
"""Visit ellipsis"""
197231
self.emit("...")

tests/comprehensions.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
a = [i * j for i in range(5) for j in range(3) if i * j % 2 == 0 and i > 0 and j > 0]
22

33
for item in a:
4-
print(item)
4+
print(item)
5+
6+
b = {i: i ** 2 for i in range(5)}
7+
8+
for k, v in b.items():
9+
print(k, v)

tests/comprehensions.py.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@
77
8
88
12
99
10
10+
1 1.0
11+
2 4.0
12+
3 9.0
13+
4 16.0
14+
5 25.0

0 commit comments

Comments
 (0)