Skip to content

Commit 0161dd5

Browse files
author
hartsantler
committed
lua backend: fixed augassignment a += 1
1 parent 109885c commit 0161dd5

4 files changed

Lines changed: 36 additions & 22 deletions

File tree

pythonjs/python_to_pythonjs.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -554,13 +554,13 @@ def visit_AugAssign(self, node):
554554

555555
if self._with_lua:
556556
if op == '+=':
557-
a = '__add__(%s,%s)' %(target, self.visit(node.value))
557+
a = '__add_op(%s,%s)' %(target, self.visit(node.value))
558558
elif op == '-=':
559-
a = '__sub__(%s,%s)' %(target, self.visit(node.value))
559+
a = '(%s - %s)' %(target, self.visit(node.value))
560560
elif op == '*=':
561-
a = '__mul__(%s,%s)' %(target, self.visit(node.value))
561+
a = '(%s * %s)' %(target, self.visit(node.value))
562562
elif op == '/=' or op == '//=':
563-
a = '__div__(%s,%s)' %(target, self.visit(node.value))
563+
a = '(%s / %s)' %(target, self.visit(node.value))
564564
elif op == '%=':
565565
a = '__mod__(%s,%s)' %(target, self.visit(node.value))
566566
elif op == '&=':
@@ -1268,6 +1268,15 @@ def visit_USub(self, node):
12681268

12691269

12701270
def visit_Attribute(self, node):
1271+
## TODO - in some cases the translator knows what type a node is and what attribute's it has, in those cases the call to `__get__` can be optimized away,
1272+
## this is disabled because if the user wants the best performance, they should instead use javascript-mode.
1273+
#typedef = None
1274+
#if isinstance(node.value, Name):
1275+
# typedef = self.get_typedef( instance=node.value )
1276+
#elif hasattr(node.value, 'returns_type'):
1277+
# typedef = self.get_typedef( class_name=node.value.returns_type )
1278+
1279+
12711280
node_value = self.visit(node.value)
12721281

12731282
if self._with_dart or self._with_ll:
@@ -1280,15 +1289,10 @@ def visit_Attribute(self, node):
12801289
#else:
12811290
# return '__ternary_operator__(%s.%s is not undefined, %s.%s, __getattr__(%s, "%s"))' %(node_value, node.attr, node_value, node.attr, node_value, node.attr)
12821291

1283-
## TODO - in some cases the translator knows what type a node is and what attribute's it has, in those cases the call to `__get__` can be optimized away,
1284-
## this is disabled because if the user wants the best performance, they should instead use javascript-mode.
1285-
#typedef = None
1286-
#if isinstance(node.value, Name):
1287-
# typedef = self.get_typedef( instance=node.value )
1288-
#elif hasattr(node.value, 'returns_type'):
1289-
# typedef = self.get_typedef( class_name=node.value.returns_type )
1292+
elif self._with_lua and self._in_assign_target: ## this is required because lua has no support for inplace assignment ops like "+="
1293+
return '%s.%s' %(node_value, node.attr)
12901294

1291-
if hasattr(node, 'lineno'):
1295+
elif hasattr(node, 'lineno'):
12921296
src = self._source[ node.lineno-1 ]
12931297
src = src.replace('"', '\\"')
12941298
err = 'missing attribute `%s` - line %s: %s' %(node.attr, node.lineno, src.strip())

pythonjs/translator.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
import sys
2+
import sys, traceback
33

44
from python_to_pythonjs import main as python_to_pythonjs
55
from pythonjs import main as pythonjs_to_javascript
@@ -14,22 +14,27 @@ def main(script):
1414
import python_to_visjs
1515
return python_to_visjs.main( script )
1616
else:
17+
code = ''
1718
if '--dart' in sys.argv:
1819
a = python_to_pythonjs(script, dart=True)
19-
return pythonjs_to_dart( a )
20+
code = pythonjs_to_dart( a )
2021
elif '--coffee' in sys.argv:
2122
a = python_to_pythonjs(script, coffee=True)
22-
return pythonjs_to_coffee( a )
23+
code = pythonjs_to_coffee( a )
2324
elif '--lua' in sys.argv:
2425
a = python_to_pythonjs(script, lua=True)
25-
return pythonjs_to_lua( a )
26+
try: code = pythonjs_to_lua( a )
27+
except SyntaxError:
28+
sys.stderr.write( '\n'.join([traceback.format_exc(),a]) )
29+
2630
elif '--luajs' in sys.argv:
2731
a = python_to_pythonjs(script, lua=True)
28-
return pythonjs_to_luajs( a )
32+
code = pythonjs_to_luajs( a )
2933
else:
3034
a = python_to_pythonjs(script)
31-
return pythonjs_to_javascript( a )
35+
code = pythonjs_to_javascript( a )
3236

37+
return code
3338

3439
def command():
3540
scripts = []

regtests/bench/float.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def maximize(points):
3737
def benchmark(n):
3838
points = [None] * n
3939
for i in range(n):
40-
points[i] = Point(i)
40+
a = Point(i)
41+
points[i] = a #Point(i)
4142
for p in points:
4243
p.normalize()
4344
return maximize(points)
@@ -54,6 +55,11 @@ def test(arg):
5455
return times
5556

5657
def main():
58+
if PYTHON=='PYTHONJS': ## about 25% faster with normal and javascript backends
59+
pythonjs.configure( direct_operator='+' )
60+
pass
61+
62+
5763
times = test( 3 )
5864
avg = sum(times) / len(times)
5965
print( avg )

regtests/run.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,8 @@ def run_command(command, returns_stdout_stderr=False):
101101

102102
#########################
103103

104-
if stdout:
105-
if show_details:
106-
print(stdout)
104+
if show_details and stdout:
105+
print(stdout)
107106

108107
unknown = []
109108
for line in stdout.splitlines():

0 commit comments

Comments
 (0)