[pypy-svn] r14416 - in pypy/dist/pypy/interpreter: astcompiler pyparser pyparser/test

ludal at codespeak.net ludal at codespeak.net
Fri Jul 8 01:26:41 CEST 2005


Author: ludal
Date: Fri Jul  8 01:26:38 2005
New Revision: 14416

Modified:
   pypy/dist/pypy/interpreter/astcompiler/ast.py
   pypy/dist/pypy/interpreter/astcompiler/astgen.py
   pypy/dist/pypy/interpreter/pyparser/astbuilder.py
   pypy/dist/pypy/interpreter/pyparser/test/test_astbuilder.py
Log:
 more work on astbuilder


Modified: pypy/dist/pypy/interpreter/astcompiler/ast.py
==============================================================================
--- pypy/dist/pypy/interpreter/astcompiler/ast.py	(original)
+++ pypy/dist/pypy/interpreter/astcompiler/ast.py	Fri Jul  8 01:26:38 2005
@@ -33,6 +33,13 @@
         pass # implemented by subclasses
     def visit(self, visitor, *args):
         return visitor.visitNode(self, *args)
+    def __eq__(self, right):
+        if type(self)!=type(right):
+            return False
+        for i,j in zip(self.getChildren(),right.getChildren()):
+            if not i==j:
+                return False
+        return True
 
 class EmptyNode(Node):
     def visit(self, visitor, *args):

Modified: pypy/dist/pypy/interpreter/astcompiler/astgen.py
==============================================================================
--- pypy/dist/pypy/interpreter/astcompiler/astgen.py	(original)
+++ pypy/dist/pypy/interpreter/astcompiler/astgen.py	Fri Jul  8 01:26:38 2005
@@ -296,6 +296,13 @@
         pass # implemented by subclasses
     def visit(self, visitor, *args):
         return visitor.visitNode(self, *args)
+    def __eq__(self, right):
+        if type(self)!=type(right):
+            return False
+        for i,j in zip(self.getChildren(),right.getChildren()):
+            if not i==j:
+                return False
+        return True
 
 class EmptyNode(Node):
     def visit(self, visitor, *args):

Modified: pypy/dist/pypy/interpreter/pyparser/astbuilder.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/astbuilder.py	(original)
+++ pypy/dist/pypy/interpreter/pyparser/astbuilder.py	Fri Jul  8 01:26:38 2005
@@ -1,7 +1,28 @@
 
 
 from grammar import BaseGrammarBuilder
-import pypy.interpreter.astcompiler.ast as ast
+from pypy.interpreter.astcompiler import ast, consts
+
+## these tests should be methods of the ast objects
+
+def is_lvalue( ast_node ):
+    return True
+
+def to_lvalue( ast_node, OP ):
+    if isinstance( ast_node, ast.Name ):
+        return ast.AssName( ast_node.name, OP )
+    else:
+        assert False, "TODO"
+
+def is_augassign( ast_node ):
+    if ( isinstance( ast_node, ast.Name ) or
+         isinstance( ast_node, ast.Slice ) or
+         isinstance( ast_node, ast.Subscript ) or
+         isinstance( ast_node, ast.Getattr ) ):
+        return True
+    return False
+
+## building functions
 
 def get_atoms( builder, nb ):
     L = []
@@ -108,7 +129,100 @@
             raise ValueError, "unexpected token: %s : %s" % L[i-1]
     builder.push( left )
 
+
+def build_binary_expr( builder, nb, OP ):
+    L = get_atoms( builder, nb )
+    l = len(L)
+    if l==1:
+        builder.push( L[0] )
+        return
+    items = []
+    for i in range(0,l,2): # this is L not 1
+        items.append( L[i] )
+    builder.push( OP( items ) )
+    return
+
+def build_and_expr( builder, nb ):
+    return build_binary_expr( builder, nb, ast.Bitand )
+
+def build_xor_expr( builder, nb ):
+    return build_binary_expr( builder, nb, ast.Bitxor )
+
+def build_expr( builder, nb ):
+    return build_binary_expr( builder, nb, ast.Bitor )
+
+def build_comparison( builder, nb ):
+    L = get_atoms( builder, nb )
+    l = len(L)
+    if l==1:
+        builder.push( L[0] )
+        return
+    # TODO
+    assert False
+
+def build_and_test( builder, nb ):
+    return build_binary_expr( builder, nb, ast.And )
+
+def build_test( builder, nb ):
+    return build_binary_expr( builder, nb, ast.Or )
+
+def build_testlist( builder, nb ):
+    return build_binary_expr( builder, nb, ast.Tuple )
+
+def build_not_test( builder, nb ):
+    L = get_atoms( builder, nb )
+    l = len(L)
+    if l==1:
+        builder.push( L[0] )
+        return
+
+def build_expr_stmt( builder, nb ):
+    L = get_atoms( builder, nb )
+    l = len(L)
+    if l==1:
+        builder.push( ast.Discard( L[0] ) )
+        return
+    op = L[1]
+    if op.name == '=':
+        nodes = []
+        for i in range(0,l-2,2):
+            lvalue = to_lvalue( L[i], consts.OP_ASSIGN )
+            nodes.append( lvalue )
+        rvalue = L[-1]
+        builder.push( ast.Assign( nodes, rvalue ) )
+        pass
+    else:
+        assert l==3
+        lvalue = L[0]
+        assert is_augassign( lvalue )
+        builder.push( ast.AugAssign( lvalue, op, L[2] ) )
+
+def return_one( builder, nb ):
+    L = get_atoms( builder, nb )
+    l = len(L)
+    if l==1:
+        builder.push( L[0] )
+        return
+    raise WalkerError("missing one node in stack")
+
+def build_simple_stmt( builder, nb ):
+    L = get_atoms( builder, nb )
+    l = len(L)
+    nodes = []
+    for n in range(0,l,2):
+        nodes.append(L[n])
+    builder.push( ast.Stmt( nodes ) )
+    return
+
+def build_single_input( builder, nb ):
+    L = get_atoms( builder, nb )
+    l = len(L)
+    if l>=1:
+        builder.push( ast.Module( None, L[0] ) )
+        return
+    raise WalkerError("error")
     
+
 ASTRULES = {
 #    "single_input" : build_single_input,
     "atom" : build_atom,
@@ -117,6 +231,17 @@
     "term" : build_term,
     "arith_expr" : build_arith_expr,
     "shift_expr" : build_shift_expr,
+    "and_expr" : build_and_expr,
+    "xor_expr" : build_xor_expr,
+    "expr" : build_expr,
+    "comparison" : build_comparison,
+    "and_test" : build_and_test,
+    "test" : build_test,
+    "testlist" : build_testlist,
+    "expr_stmt" : build_expr_stmt,
+    "small_stmt" : return_one,
+    "simple_stmt" : build_simple_stmt,
+    "single_input" : build_single_input,
     }
 
 class RuleObject(ast.Node):

Modified: pypy/dist/pypy/interpreter/pyparser/test/test_astbuilder.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/test/test_astbuilder.py	(original)
+++ pypy/dist/pypy/interpreter/pyparser/test/test_astbuilder.py	Fri Jul  8 01:26:38 2005
@@ -5,10 +5,38 @@
 from pypy.interpreter.stablecompiler.transformer import Transformer
 import py.test
 
-expr1 = "x = a + 1"
-
-
+from pypy.interpreter.astcompiler import ast
 
+expressions = [
+    "x = a + 1",
+    "x = 1 - a",
+    "x = a * b",
+    "x = a ** 2",
+    "x = a / b",
+    "x = a & b",
+    "x = a | b",
+    "x = a ^ b",
+    "x = a // b",
+    "x = a * b + 1",
+    "x = a + 1 * b",
+    "x = a * b / c",
+    "x = a * (1 + c)",
+    "f = lambda x: x+1",
+    "x, y, z = 1, 2, 3",
+]    
+expression_tests = [ 0, 1, 2, 3, 4, 5, 6,7, 8, 9, 10, 11, ] # = range(len(expressions))
+failed_expression_tests = [ 12, 13, 14 ]
+
+comparisons = [
+    "a < b",
+    "a > b",
+    "a not in b",
+    "a in b",
+    "3 < x < 5",
+    "(3 < x) < 5",
+    ]
+comparison_tests = []
+failed_comparison_tests = range( len(comparisons) )
 def ast_parse_expr( expr ):
     builder = AstBuilder()
     PYTHON_PARSER.parse_source( expr, "single_input", builder )
@@ -18,9 +46,19 @@
     t = Transformer()
     return ast_from_input( expr, "single", t )
 
-def test_expr1():
-    py.test.skip("work in progress")
-    r1 = ast_parse_expr( expr1 )
-    ast = tuple_parse_expr( expr1 )
-    print ast
-    
+def check_expression( expr ):
+    r1 = ast_parse_expr( expr )
+    ast = tuple_parse_expr( expr )
+    print "ORIG :", ast
+    print "BUILT:", r1.rule_stack[-1]
+    assert ast == r1.rule_stack[-1]
+
+def test_expressions():
+##    py.test.skip("work in progress")
+    for i in expression_tests:
+        yield check_expression, expressions[i]
+
+def test_comparisons():
+##    py.test.skip("work in progress")
+    for i in comparison_tests:
+        yield check_expression, comparisons[i]



More information about the Pypy-commit mailing list