Permalink
Browse files

workarounds for compiler limitations; adding support for AssertionErr…

…or (needs a test!)"

git-svn-id: https://pyjamas.svn.sourceforge.net/svnroot/pyjamas/trunk@1834 7a2bd370-bda8-463c-979e-2900ccfb811e
  • Loading branch information...
lkcl
lkcl committed Sep 18, 2009
1 parent c700a30 commit 1965cfeaa76ec871b41a423305a40a050e400ce3
View
13 TODO
@@ -40,6 +40,19 @@ From http://code.google.com/p/pyjamas/issues, for 0.6 release:
Python Builtins TODO
---------------
+translator support for:
+
+ * GenExpr and GenExprInner
+ - if any(isinstance(child, Node) for child in node.getChildren()):
+ - write(', '.join(repr(child) for child in node.getChildren()))
+
+ * Conditional in classes:
+ class foo:
+ if True:
+ def fn(self):
+ pass
+
+
#234 from Beni Cherniavsky:
Python builtins missing under pyjs:
View
@@ -40,18 +40,25 @@ def rec_node(node, level, indent, write):
write(node.__class__.__name__)
write('(')
- if any(isinstance(child, Node) for child in node.getChildren()):
- for i, child in enumerate(node.getChildren()):
- if i != 0:
- write(',')
- write('\n')
- rec_node(child, level+1, indent, write)
+ i = 0
+ for child in node.getChildren():
+ if not isinstance(child, Node):
+ continue
+ if i != 0:
+ write(',')
write('\n')
- write(pfx)
- else:
+ rec_node(child, level+1, indent, write)
+ i += 1
+ if i == 0:
# None of the children as nodes, simply join their repr on a single
# line.
- write(', '.join(repr(child) for child in node.getChildren()))
+ res = []
+ for child in node.getChildren():
+ res.append(repr(child))
+ write(', '.join(res))
+ else:
+ write('\n')
+ write(pfx)
write(')')
@@ -1103,103 +1103,95 @@ def com_append_stmt(self, stmts, node):
else:
stmts.append(result)
- if hasattr(symbol, 'comp_for'):
- def com_list_constructor(self, nodelist):
- # listmaker: test ( list_for | (',' test)* [','] )
- values = []
- for i in range(len(nodelist.children)):
- if nodelist.children[i].type == symbol.comp_for:
- assert len(nodelist.children[i:]) == 1
- return self.com_list_comprehension(values[0],
- nodelist.children[i])
- elif nodelist.children[i].type == token.COMMA:
- continue
- values.append(self.com_node(nodelist.children[i]))
- return List(values, lineno=values[0].lineno)
-
- def com_list_comprehension(self, expr, node):
- # list_iter: list_for | list_if
- # list_for: 'for' exprlist 'in' testlist [list_iter]
- # list_if: 'if' test [list_iter]
-
- # XXX should raise SyntaxError for assignment
-
- lineno = node.children[0].context
- fors = []
- while node:
- t = node.children[0].value
- if t == 'for':
- assignNode = self.com_assign(node.children[1], OP_ASSIGN)
- listNode = self.com_node(node.children[3])
- newfor = ListCompFor(assignNode, listNode, [])
- newfor.lineno = node.children[0].context
- fors.append(newfor)
- if len(node.children) == 4:
- node = None
- else:
- node = self.com_list_iter(node.children[4])
- elif t == 'if':
- test = self.com_node(node.children[1])
- newif = ListCompIf(test, lineno=node.children[0].context)
- newfor.ifs.append(newif)
- if len(node.children) == 2:
- node = None
- else:
- node = self.com_list_iter(node.children[2])
+ def com_list_constructor(self, nodelist):
+ # listmaker: test ( list_for | (',' test)* [','] )
+ values = []
+ for i in range(len(nodelist.children)):
+ if nodelist.children[i].type == symbol.comp_for:
+ assert len(nodelist.children[i:]) == 1
+ return self.com_list_comprehension(values[0],
+ nodelist.children[i])
+ elif nodelist.children[i].type == token.COMMA:
+ continue
+ values.append(self.com_node(nodelist.children[i]))
+ return List(values, lineno=values[0].lineno)
+
+ def com_list_comprehension(self, expr, node):
+ # list_iter: list_for | list_if
+ # list_for: 'for' exprlist 'in' testlist [list_iter]
+ # list_if: 'if' test [list_iter]
+
+ # XXX should raise SyntaxError for assignment
+
+ lineno = node.children[0].context
+ fors = []
+ while node:
+ t = node.children[0].value
+ if t == 'for':
+ assignNode = self.com_assign(node.children[1], OP_ASSIGN)
+ listNode = self.com_node(node.children[3])
+ newfor = ListCompFor(assignNode, listNode, [])
+ newfor.lineno = node.children[0].context
+ fors.append(newfor)
+ if len(node.children) == 4:
+ node = None
else:
- raise SyntaxError, \
- ("unexpected list comprehension element: %s %d"
- % (node, lineno))
- return ListComp(expr, fors, lineno=lineno)
-
- def com_list_iter(self, node):
- assert node.type == symbol.comp_iter
- return node.children[0]
- else:
- def com_list_constructor(self, nodelist):
- values = []
- for i in range(0, len(nodelist.children), 2):
- values.append(self.com_node(nodelist.children[i]))
- return List(values, lineno=values[0].lineno)
-
- if hasattr(symbol, 'gen_for'):
- def com_generator_expression(self, expr, node):
- # gen_iter: gen_for | gen_if
- # gen_for: 'for' exprlist 'in' test [gen_iter]
- # gen_if: 'if' test [gen_iter]
-
- lineno = node.children[0].context
- fors = []
- while node:
- t = node.children[0].value
- if t == 'for':
- assignNode = self.com_assign(node.children[1], OP_ASSIGN)
- genNode = self.com_node(node.children[3])
- newfor = GenExprFor(assignNode, genNode, [],
- lineno=node.children[0].context)
- fors.append(newfor)
- if (len(node.children)) == 4:
- node = None
- else:
- node = self.com_gen_iter(node.children[4])
- elif t == 'if':
- test = self.com_node(node.children[1])
- newif = GenExprIf(test, lineno=node.children[0].context)
- newfor.ifs.append(newif)
- if len(node.children) == 2:
- node = None
- else:
- node = self.com_gen_iter(node.children[2])
+ node = self.com_list_iter(node.children[4])
+ elif t == 'if':
+ test = self.com_node(node.children[1])
+ newif = ListCompIf(test, lineno=node.children[0].context)
+ newfor.ifs.append(newif)
+ if len(node.children) == 2:
+ node = None
+ else:
+ node = self.com_list_iter(node.children[2])
+ else:
+ raise SyntaxError, \
+ ("unexpected list comprehension element: %s %d"
+ % (node, lineno))
+ return ListComp(expr, fors, lineno=lineno)
+
+ def com_list_iter(self, node):
+ assert node.type == symbol.comp_iter
+ return node.children[0]
+
+ def com_generator_expression(self, expr, node):
+ # gen_iter: gen_for | gen_if
+ # gen_for: 'for' exprlist 'in' test [gen_iter]
+ # gen_if: 'if' test [gen_iter]
+
+ lineno = node.children[0].context
+ fors = []
+ while node:
+ t = node.children[0].value
+ if t == 'for':
+ assignNode = self.com_assign(node.children[1], OP_ASSIGN)
+ genNode = self.com_node(node.children[3])
+ newfor = GenExprFor(assignNode, genNode, [],
+ lineno=node.children[0].context)
+ fors.append(newfor)
+ if (len(node.children)) == 4:
+ node = None
else:
- raise SyntaxError, \
- ("unexpected generator expression element: %s %d"
- % (node, lineno))
- fors[0].is_outmost = True
- return GenExpr(GenExprInner(expr, fors), lineno=lineno)
-
- def com_gen_iter(self, node):
- assert node.type == symbol.gen_iter
- return node.children[0]
+ node = self.com_gen_iter(node.children[4])
+ elif t == 'if':
+ test = self.com_node(node.children[1])
+ newif = GenExprIf(test, lineno=node.children[0].context)
+ newfor.ifs.append(newif)
+ if len(node.children) == 2:
+ node = None
+ else:
+ node = self.com_gen_iter(node.children[2])
+ else:
+ raise SyntaxError, \
+ ("unexpected generator expression element: %s %d"
+ % (node, lineno))
+ fors[0].is_outmost = True
+ return GenExpr(GenExprInner(expr, fors), lineno=lineno)
+
+ def com_gen_iter(self, node):
+ assert node.type == symbol.gen_iter
+ return node.children[0]
def com_dictmaker(self, nodelist):
# dictmaker: test ':' test (',' test ':' value)* [',']
@@ -21,7 +21,7 @@
import sys
# Pgen imports
-from . import grammar, parse, token, tokenize, pgen
+from lib2to3.pgen import grammar, parse, token, tokenize, pgen
class Driver(object):
@@ -2,7 +2,7 @@
# Licensed to PSF under a Contributor Agreement.
# Pgen imports
-from . import grammar, token, tokenize
+from lib2to3.pgen2 import grammar, token, tokenize
class PgenGrammar(grammar.Grammar):
pass
View
@@ -4,14 +4,16 @@
from astpprint import getAststr, printAst
-from lib2to3 import compiler as test_compiler
-from lib2to3.compiler.transformer import Transformer
-from lib2to3.compiler import parser as test_parser
+if test_pyjs:
+ from lib2to3 import compiler as test_compiler
+ from lib2to3.compiler.transformer import Transformer
+ from lib2to3.compiler import parser as test_parser
#g = Grammar()
-import compiler
-import parser
+if test_std:
+ import compiler
+ import parser
def compare_compilers(fname):
@@ -21,37 +23,37 @@ def compare_compilers(fname):
txt = f.read()
f.close()
- try:
- x1 = compiler.parseFile(fname)
- ys1 = getAststr(x1)
- except SyntaxError:
- ys1 = traceback.format_exc(limit=0)
-
-
- try:
- #x = test_parser.suite(txt)
- #t = Transformer()
- #y = t.compile_node(x)
- y = test_compiler.parseFile(fname)
- ys = getAststr(y)
-
- except SyntaxError:
- ys = traceback.format_exc(limit=1)
- #traceback.print_exc()
-
- if ys == ys1:
- print "passed"
- return
+ if test_std:
+ try:
+ x1 = compiler.parseFile(fname)
+ ys1 = getAststr(x1)
+ except SyntaxError:
+ ys1 = traceback.format_exc(limit=0)
+
+ if test_pyjs:
+ try:
+ y = test_compiler.parseFile(fname)
+ ys = getAststr(y)
+
+ except SyntaxError:
+ ys = traceback.format_exc(limit=1)
+
+ if test_pyjs and test_std:
+ if ys == ys1:
+ print "passed"
+ return
print "failed."
- f = open(fname_out+".ast", "w")
- f.write(ys)
- f.close()
+ if test_pyjs:
+ f = open(fname_out+".ast", "w")
+ f.write(ys)
+ f.close()
- f = open(fname_out+".ast.std", "w")
- f.write(ys1)
- f.close()
+ if test_std:
+ f = open(fname_out+".ast.std", "w")
+ f.write(ys1)
+ f.close()
import sys
for arg in sys.argv[1:]:
@@ -135,6 +135,9 @@ class Exception(BaseException):
class StandardError(Exception):
pass
+class AssertionError(Exception):
+ pass
+
class GeneratorExit(Exception):
pass
@@ -1783,6 +1783,15 @@ def _function(self, node, current_klass, top_level, local):
self.pop_options()
self.pop_lookup()
+ def _assert(self, node, current_klass):
+ expr = self.expr(node.test, current_klass)
+ if node.fail:
+ fail = self.expr(node.fail, current_klass)
+ else:
+ fail = ''
+ print >>self.output, self.spacing() + "if !( " + expr + " ) {;"
+ print >>self.output, self.spacing() + " throw pyjslib['AssertionError'](%s);" % fail
+ print >>self.output, self.spacing() + " ) {;"
def _return(self, node, current_klass):
expr = self.expr(node.value, current_klass)
@@ -2243,6 +2252,8 @@ def _class(self, node):
self.push_lookup(private_scope)
self.track_lineno(child, True)
rhs = self.expr(child.expr, current_klass)
+ print "rhs", rhs
+ print child.nodes[0]
name = "%s.%s" % (local_prefix, child.nodes[0].name)
lhs = self.add_lookup('attribute', child.nodes[0].name, name)
print >>self.output, self.spacing() + "%s = %s;" % (lhs, rhs)
@@ -2484,6 +2495,8 @@ def _stmt(self, node, current_klass, top_level = False):
self._from(node, current_klass, top_level)
elif isinstance(node, self.ast.AssAttr):
self._assattr(node, current_klass)
+ elif isinstance(node, self.ast.Assert):
+ self._assert(node, current_klass)
else:
raise TranslationError(
"unsupported type (in _stmt)", node, self.module_name)

0 comments on commit 1965cfe

Please sign in to comment.