Skip to content

Commit 34784b2

Browse files
author
hartsantler
committed
lua backend: added dict.keys and dict.items, for x in list __contains__ for list, multiple target for loops for x,y in list. new regression tests.
1 parent 6c17cfb commit 34784b2

6 files changed

Lines changed: 65 additions & 6 deletions

File tree

pythonjs/python_to_pythonjs.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,9 @@ def visit_Tuple(self, node):
412412
def visit_List(self, node):
413413
node.returns_type = 'list'
414414
a = '[%s]' % ', '.join(map(self.visit, node.elts))
415-
if self._with_lua:
415+
if self._with_ll:
416+
pass
417+
elif self._with_lua:
416418
a = '__get__(list, "__call__")({}, {pointer:%s, length:%s})'%(a, len(node.elts))
417419
return a
418420

@@ -1819,7 +1821,7 @@ def visit_Call(self, node):
18191821
# return '%s()' %name
18201822

18211823
## special method calls ##
1822-
if isinstance(node.func, ast.Attribute) and node.func.attr in ('get', 'keys', 'values', 'pop', 'items'):
1824+
if isinstance(node.func, ast.Attribute) and node.func.attr in ('get', 'keys', 'values', 'pop', 'items') and not self._with_lua:
18231825
anode = node.func
18241826
if anode.attr == 'get':
18251827
if args:
@@ -1845,7 +1847,7 @@ def visit_Call(self, node):
18451847
else:
18461848
return '%s(%s)' %( self.visit(node.func), args )
18471849

1848-
elif isinstance(node.func, ast.Attribute) and isinstance(node.func.value, Name) and node.func.value.id in self._func_typedefs:
1850+
elif not self._with_lua and not self._with_dart and isinstance(node.func, ast.Attribute) and isinstance(node.func.value, Name) and node.func.value.id in self._func_typedefs:
18491851
type = self._func_typedefs[ node.func.value.id ]
18501852
if type == 'list' and node.func.attr == 'append':
18511853
return '%s.push(%s)' %(node.func.value.id, self.visit(node.args[0]))
@@ -2454,7 +2456,10 @@ def visit_For(self, node):
24542456
if multi_target:
24552457
writer.write('__mtarget__ = __next__()')
24562458
for i,elt in enumerate(multi_target):
2457-
writer.write('%s = __mtarget__[%s]' %(elt,i))
2459+
if self._with_lua:
2460+
writer.write('%s = __mtarget__[...][%s]' %(elt,i+1))
2461+
else:
2462+
writer.write('%s = __mtarget__[%s]' %(elt,i))
24582463
else:
24592464
writer.write('%s = __next__()' % target.id)
24602465

pythonjs/pythonjs_to_lua.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,14 @@ def visit_ClassDef(self, node):
153153
raise NotImplementedError
154154

155155
def visit_For(self, node):
156-
a = ['for __i,%s in pairs(%s) do' %(self.visit(node.target), self.visit(node.iter))]
156+
if isinstance(node.target, ast.Name):
157+
a = ['for __i,%s in pairs(%s) do' %(self.visit(node.target), self.visit(node.iter))]
158+
elif isinstance(node.target, ast.List):
159+
x = ','.join([self.visit(elt) for elt in node.target.elts])
160+
a = ['for %s in %s do' %(x, self.visit(node.iter))]
161+
else:
162+
raise SyntaxError( node.target )
163+
157164
for n in node.body:
158165
a.append( self.visit(n) )
159166
a.append('end')

regtests/dict/keys.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""dict.keys()"""
2+
3+
def main():
4+
a = {'foo':'bar'}
5+
keys = a.keys()
6+
TestError( 'foo' in keys )
7+

regtests/list/contains.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""if x in list"""
2+
def main():
3+
a = ['foo', 'bar']
4+
TestError( 'foo' in a )

regtests/loop/for_loop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ def main():
1414
arr = ['a', 'b', 'c']
1515
for v in arr:
1616
z += v
17-
1817
TestError( z == 'abc' )
1918

2019
b = False
2120
if 'a' in arr:
2221
b = True
22+
TestError( b == True )
2323

2424
s = 'hello world'
2525
z = ''

runtime/lua_builtins.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ def __init__(self, items, pointer=None, length=0):
209209
else:
210210
self[...] = {}
211211

212+
def __contains__(self, value):
213+
with lowlevel:
214+
for v in self[...]:
215+
if v == value:
216+
return True
217+
return False
218+
212219
def __getitem__(self, index):
213220
with lowlevel:
214221
if index < 0:
@@ -259,6 +266,12 @@ def index(self, obj):
259266
return string.sub(s, args[1]+1, args[1]+1)
260267
end
261268
269+
elseif name == '__contains__' then
270+
wrapper = function(args, kwargs)
271+
if s:find( args[1] ) then return true
272+
else return false end
273+
end
274+
262275
elseif name == '__getslice__' then
263276
wrapper = function(args, kwargs)
264277
if args[1]==nil and args[2]==nil and args[3]==-1 then
@@ -297,6 +310,7 @@ def index(self, obj):
297310
end
298311
''')
299312

313+
300314
class dict:
301315
def __init__(self, object, pointer=None):
302316
with lowlevel:
@@ -316,3 +330,25 @@ def __setitem__(self, key, value):
316330
with lowlevel:
317331
self[...][ key ] = value
318332

333+
def keys(self):
334+
with lowlevel:
335+
ptr = []
336+
i = 1
337+
for k,v in pairs(self[...]):
338+
ptr[ i ] = k
339+
i = i + 1
340+
return list( pointer=ptr, length=i-1 )
341+
342+
def __iter__(self):
343+
return self.keys().__iter__()
344+
345+
def items(self):
346+
with lowlevel:
347+
ptr = []
348+
i = 1
349+
for k,v in pairs(self[...]):
350+
p = [k,v]
351+
item = list.__call__([], {pointer:p, length:2})
352+
ptr[ i ] = item
353+
i = i + 1
354+
return list( pointer=ptr, length=i-1 )

0 commit comments

Comments
 (0)