Skip to content

Commit eaec79a

Browse files
author
hartsantler
committed
coffee and lua backends: small fixes.
1 parent 7033bb2 commit eaec79a

4 files changed

Lines changed: 60 additions & 11 deletions

File tree

pythonjs/python_to_pythonjs.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ def visit_BinOp(self, node):
10331033
return '__sprintf( %s, %s )' %(left, right) ## assumes that right is a tuple, or list.
10341034

10351035
elif op == '*' and isinstance(node.left, ast.List):
1036-
if len(node.left.elts) == 1 and isinstance(node.left.elts[0], ast.Name) and node.left.elts[0].id == 'None':
1036+
if len(node.left.elts) == 1 and isinstance(node.left.elts[0], ast.Name) and node.left.elts[0].id == 'None' and not self._with_lua:
10371037
#if self._with_js:
10381038
# return 'new Array(%s)' %self.visit(node.right)
10391039
#else:
@@ -1055,7 +1055,10 @@ def visit_BinOp(self, node):
10551055
# return '[%s]' %','.join(expanded)
10561056
#else:
10571057
# return '__get__(list, "__call__")( [], {pointer:[%s]} )' %','.join(expanded)
1058-
return '[%s]' %','.join(expanded)
1058+
if self._with_lua:
1059+
return 'list.__call__([], {pointer:[%s], length:%s})' %(','.join(expanded), n)
1060+
else:
1061+
return '[%s]' %','.join(expanded)
10591062

10601063
elif op == '//':
10611064
return 'Math.floor(%s/%s)' %(left, right)
@@ -1483,6 +1486,10 @@ def _visit_assign_helper(self, node, target):
14831486
else:
14841487
writer.write('%s = %s' % (self.visit(target), node_value))
14851488

1489+
elif self._with_lua: ## Tuple - lua supports destructured assignment
1490+
elts = [self.visit(e) for e in target.elts]
1491+
writer.write('%s = %s' % (','.join(elts), self.visit(node.value)))
1492+
14861493
else: # it's a Tuple
14871494
id = self.identifier
14881495
self.identifier += 1
@@ -2453,7 +2460,10 @@ def visit_For(self, node):
24532460
writer.write('while %s < %s:' %(iter_name, iter_end))
24542461
writer.push()
24552462
map(self.visit, node.body)
2456-
writer.write('%s += 1' %iter_name )
2463+
if self._with_lua:
2464+
writer.write('%s = %s + 1' %(iter_name, iter_name) )
2465+
else:
2466+
writer.write('%s += 1' %iter_name )
24572467

24582468
if enumtar:
24592469
writer.write('%s += 1'%enumtar.id)

pythonjs/pythonjs_to_coffee.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ def _visit_function(self, node):
395395
def visit_Is(self, node):
396396
return ' is '
397397

398+
def visit_IsNot(self, node):
399+
return ' isnt '
400+
398401
def _visit_call_helper_instanceof(self, node):
399402
args = map(self.visit, node.args)
400403
if len(args) == 2:

pythonjs/pythonjs_to_lua.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ def visit_Or(self, node):
5555
def visit_Not(self, node):
5656
return ' not '
5757

58+
def visit_IsNot(self, node):
59+
return '~='
60+
61+
def visit_NotEq(self, node):
62+
return '~='
5863

5964
def visit_Subscript(self, node):
6065
if isinstance(node.slice, ast.Ellipsis):
@@ -106,8 +111,8 @@ def visit_While(self, node):
106111
self.push()
107112
for line in list( map(self.visit, node.body) ):
108113
body.append( self.indent()+line )
109-
body.append( self.indent() + 'end' )
110114
self.pull()
115+
body.append( self.indent() + 'end' )
111116
return '\n'.join( body )
112117

113118

@@ -182,7 +187,9 @@ def visit_Assign(self, node):
182187
assert len(node.targets) == 1
183188
target = node.targets[0]
184189
if isinstance(target, ast.Tuple):
185-
raise NotImplementedError
190+
elts = [self.visit(e) for e in target.elts]
191+
return '%s = %s' % (','.join(elts), self.visit(node.value))
192+
186193
else:
187194
target = self.visit(target)
188195
value = self.visit(node.value)

runtime/lua_builtins.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
return t1
1616
end
1717
18+
function table.shallow_copy(t)
19+
local t2 = {}
20+
for k,v in pairs(t) do
21+
t2[k] = v
22+
end
23+
return t2
24+
end
25+
1826
__test_if_true__ = function( x )
1927
if x == true then return true
2028
elseif x == false then return false
@@ -32,6 +40,10 @@
3240
end
3341
end
3442
43+
__set__ = function(ob, name, value)
44+
ob[ name ] = value
45+
end
46+
3547
__get__ = function(ob, name)
3648
if name == '__call__' then
3749
if type(ob)=='function' then
@@ -51,7 +63,11 @@
5163
end
5264
5365
__sprintf = function(s, args)
54-
return string.format(s, unpack(args))
66+
if type(args)=='table' then
67+
return string.format(s, unpack(args))
68+
else
69+
return string.format(s, args)
70+
end
5571
end
5672
5773
function string:to_array()
@@ -278,11 +294,12 @@ def __setitem__(self, index, value):
278294
self[...][index+1] = value
279295

280296
def __getslice__(self, start, stop, step):
281-
if stop == null and step == null:
282-
return self[...].sublist( start )
283-
elif stop < 0:
284-
stop = self[...].length + stop
285-
return self[...].sublist(start, stop)
297+
if stop == None and step == None:
298+
with lowlevel:
299+
copy = table.shallow_copy( self[...] )
300+
return list( pointer=copy, length=self.length )
301+
elif stop < 0: ## TODO
302+
pass
286303

287304
def __iter__(self):
288305
return __iterator_list(self, 0)
@@ -359,6 +376,18 @@ def index(self, obj):
359376
end
360377
''')
361378

379+
def range(num, stop):
380+
"""Emulates Python's range function"""
381+
if stop is not None:
382+
i = num
383+
num = stop
384+
else:
385+
i = 0
386+
arr = []
387+
while i < num:
388+
arr.append(i)
389+
i += 1
390+
return arr
362391

363392
class dict:
364393
def __init__(self, object, pointer=None):

0 commit comments

Comments
 (0)