Skip to content

Commit 1b29838

Browse files
author
hartsantler
committed
lua backend: added try/except. fixed try/except for coffeescript backend.
1 parent 34784b2 commit 1b29838

5 files changed

Lines changed: 61 additions & 3 deletions

File tree

pythonjs/pythonjs_to_coffee.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,18 @@ def _visit_call_helper_instanceof(self, node):
404404
else:
405405
raise SyntaxError( args )
406406

407+
def visit_TryExcept(self, node):
408+
out = ['try']
409+
self.push()
410+
for n in node.body:
411+
out.append( self.indent() + self.visit(n) )
412+
self.pull()
413+
out.append( self.indent() + 'catch error' )
414+
self.push()
415+
for n in node.handlers:
416+
out.append( self.indent() + self.visit(n) )
417+
self.pull()
418+
return '\n'.join( out )
407419

408420

409421
def main(script):

pythonjs/pythonjs_to_lua.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ def _visit_function(self, node):
241241
for child in node.body:
242242
if isinstance(child, ast.Str):
243243
continue
244+
elif isinstance(child, ast.Expr) and isinstance(child.value, ast.Str):
245+
continue
244246
else:
245247
body.append( self.indent() + self.visit(child) )
246248

@@ -264,7 +266,25 @@ def _visit_call_helper_instanceof(self, node):
264266
else:
265267
raise SyntaxError( args )
266268

269+
def visit_TryExcept(self, node):
270+
out = ['__try__ = function()']
271+
self.push()
272+
for n in node.body:
273+
out.append( self.indent() + self.visit(n) )
274+
self.pull()
275+
out.append( 'end' )
267276

277+
out.append( self.indent() + 'if pcall(__try__) then' )
278+
self.push()
279+
out.append('--no errors--')
280+
self.pull()
281+
out.append( self.indent() + 'else' )
282+
self.push()
283+
for n in node.handlers:
284+
out.append( self.indent() + self.visit(n) )
285+
self.pull()
286+
out.append( self.indent() + 'end' )
287+
return '\n'.join( out )
268288

269289
def main(script):
270290
tree = ast.parse(script)

regtests/lang/try_except.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'''
2+
try except
3+
'''
4+
5+
def main():
6+
a = [1,2,3]
7+
b = False
8+
try:
9+
a.no_such_method()
10+
b = 'this should not happen'
11+
except:
12+
b = True
13+
TestError( b == True )
14+

regtests/run.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ def runnable(command):
4141
dart2js = os.path.expanduser( '~/dart/dart-sdk/bin/dart2js')
4242
dart2js_runnable = runnable( dart2js )
4343
coffee_runnable = runnable( "coffee -v" )
44-
lua_runnable = runnable( "lua -v" )
44+
lua_runnable = luajit_runnable = runnable( "luajit -v" )
45+
if not luajit_runnable:
46+
lua_runnable = runnable( "lua -v" )
47+
4548
assert rhino_runnable or node_runnable
4649

4750
if show_details:
@@ -328,7 +331,10 @@ def run_lua_lua(content):
328331
"""Run Lua using Lua"""
329332
builtins = '' #read('../runtime/builtins.lua')
330333
write("%s.lua" % tmpname, builtins + '\n' + content)
331-
return run_command("lua %s.lua" % tmpname)
334+
if luajit_runnable:
335+
return run_command("luajit %s.lua" % tmpname)
336+
else:
337+
return run_command("lua %s.lua" % tmpname)
332338

333339

334340
table_header = "%-12.12s %-28.28s"
@@ -407,7 +413,10 @@ def run():
407413
headers.append("Dart\nNode")
408414
if coffee_runnable:
409415
headers.append("Coffee\nNode")
410-
if lua_runnable:
416+
417+
if luajit_runnable:
418+
headers.append("Lua\nJIT")
419+
elif lua_runnable:
411420
headers.append("Lua\nLua")
412421

413422
print(table_header % ("", "Regtest run on")

runtime/lua_builtins.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
elseif x.__class__ and x.__class__.__name__ == 'list' then
2525
if x.length > 0 then return true
2626
else return false end
27+
elseif x.__class__ and x.__class__.__name__ == 'dict' then
28+
if x.keys().length > 0 then return true
29+
else return false end
2730
else
2831
return true
2932
end

0 commit comments

Comments
 (0)