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

adim at codespeak.net adim at codespeak.net
Mon Aug 22 11:05:07 CEST 2005


Author: adim
Date: Mon Aug 22 11:05:05 2005
New Revision: 16185

Modified:
   pypy/dist/pypy/interpreter/astcompiler/pycodegen.py
   pypy/dist/pypy/interpreter/pyparser/astbuilder.py
Log:
- Created three specific class for string consts, number consts, and
  None consts instead of using the original "Const" one
  This improves the SomeObjectness situation. (when using 'rpython_parse_fpdef'
  instead of 'working_parse_fpdef', only 3 calls remain SomeObject-ed)

- Updated astcompiler.pycodegen to be able to process these new classes.

- added small hack to test equality between SpecializedConst and original
  Const nodes (to make test pass)




Modified: pypy/dist/pypy/interpreter/astcompiler/pycodegen.py
==============================================================================
--- pypy/dist/pypy/interpreter/astcompiler/pycodegen.py	(original)
+++ pypy/dist/pypy/interpreter/astcompiler/pycodegen.py	Mon Aug 22 11:05:05 2005
@@ -818,6 +818,15 @@
     def visitConst(self, node):
         self.emit('LOAD_CONST', node.value)
 
+    def visitNoneConst(self, node):
+        self.emit('LOAD_CONST', None)
+
+    def visitNumberConst(self, node):
+        self.emit('LOAD_CONST', node.number_value)
+
+    def visitStringConst(self, node):
+        self.emit('LOAD_CONST', node.string_value)
+
     def visitKeyword(self, node):
         self.emit('LOAD_CONST', node.name)
         self.visit(node.expr)

Modified: pypy/dist/pypy/interpreter/pyparser/astbuilder.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/astbuilder.py	(original)
+++ pypy/dist/pypy/interpreter/pyparser/astbuilder.py	Mon Aug 22 11:05:05 2005
@@ -189,8 +189,8 @@
     assert len(stack) == 1, "At the end of parse_fpdef, len(stack) should be 1, got %s" % stack
     return tokens_read, tuple(stack[0])
 
-parse_fpdef = rpython_parse_fpdef
-# parse_fpdef = working_parse_fpdef
+# parse_fpdef = rpython_parse_fpdef
+parse_fpdef = working_parse_fpdef
 
 def parse_arglist(tokens):
     """returns names, defaults, flags"""
@@ -351,12 +351,12 @@
         first_child = stmt.nodes[0]
         if isinstance(first_child, ast.Discard):
             expr = first_child.expr
-            if isinstance(expr, ast.Const):
+            if isinstance(expr, StringConst):
                 # This *is* a docstring, remove it from stmt list
                 del stmt.nodes[0]
-                doc = expr.value
+                doc = expr.string_value
     return doc
-
+    
 
 def to_lvalue(ast_node, flags):
     if isinstance( ast_node, ast.Name ):
@@ -566,15 +566,16 @@
         elif top.name == tok.NAME:
             builder.push( ast.Name(top.get_value()) )
         elif top.name == tok.NUMBER:
-            builder.push( ast.Const(eval_number(top.get_value())) )
+            # builder.push( ast.Const(eval_number(top.get_value())) )
+            builder.push(NumberConst(eval_number(top.get_value())))
         elif top.name == tok.STRING:
             # need to concatenate strings in atoms
             s = ''
             for token in atoms:
                 assert isinstance(token, TokenObject)
                 s += eval_string(token.get_value())
-            builder.push( ast.Const(s) )
-            # assert False, "TODO (String)"
+            # builder.push( ast.Const(s) )
+            builder.push(StringConst(s))
         elif top.name == tok.BACKQUOTE:
             builder.push(ast.Backquote(atoms[1]))
         else:
@@ -780,7 +781,8 @@
     for n in range(0,l,2):
         node = atoms[n]
         if isinstance(node, TokenObject) and node.name == tok.NEWLINE:
-            nodes.append(ast.Discard(ast.Const(None)))
+            # nodes.append(ast.Discard(ast.Const(None)))
+            nodes.append(ast.Discard(NoneConst()))
         else:
             nodes.append(node)
     builder.push(ast.Stmt(nodes))
@@ -790,7 +792,8 @@
     if len(atoms) > 2:
         assert False, "return several stmts not implemented"
     elif len(atoms) == 1:
-        builder.push(ast.Return(ast.Const(None), None)) # XXX lineno
+        # builder.push(ast.Return(ast.Const(None), None)) # XXX lineno
+        builder.push(ast.Return(NoneConst(), None)) # XXX lineno
     else:
         builder.push(ast.Return(atoms[1], None)) # XXX lineno
 
@@ -921,7 +924,8 @@
                 sliceobj_infos = []
                 for value in sliceinfos:
                     if value is None:
-                        sliceobj_infos.append(ast.Const(None))
+                        # sliceobj_infos.append(ast.Const(None))
+                        sliceobj_infos.append(NoneConst())
                     else:
                         sliceobj_infos.append(value)
                 builder.push(SlicelistObject('sliceobj', sliceobj_infos, None))
@@ -1390,6 +1394,54 @@
     }
 
 ## Stack elements definitions ###################################
+class StringConst(ast.Const):
+    """specicifc Const node for strings"""
+    def __init__(self, value, lineno=None):
+        self.lineno = lineno
+        # don't use "value" for attribute's name to avoid confusing
+        # the annotator
+        self.string_value = value
+
+    def __repr__(self):
+        return "Const(%s)" % (repr(self.string_value),)
+
+    def __eq__(self, other):
+        # XXX yurk : to make test pass
+        if other.__class__.__name__ == 'Const' and \
+               other.value == self.string_value:
+            return True
+        return False
+
+class NumberConst(ast.Const):
+    """specific Const node for numbers"""
+    def __init__(self, value, lineno=None):
+        self.lineno = lineno
+        # don't use "value" for attribute's name to avoid confusing
+        # the annotator
+        self.number_value = value
+
+    def __repr__(self):
+        return "Const(%s)" % (repr(self.number_value),)
+
+    def __eq__(self, other):
+        if other.__class__.__name__ == 'Const' and \
+               other.value == self.number_value:
+            return True
+        return False
+
+class NoneConst(ast.Const):
+    """specific Const node for None (probably not really needed)"""
+    def __init__(self, lineno=None):
+        self.lineno = lineno
+        self.value = None
+
+    def __eq__(self, other):
+        if other.__class__.__name__ == 'Const' and \
+               other.value is None:
+            return True
+        return False
+
+    
 class RuleObject(ast.Node):
     """A simple object used to wrap a rule or token"""
     def __init__(self, name, count, src ):
@@ -1539,7 +1591,7 @@
         return self.rule_stack.pop(-1)
 
     def push(self, obj):
-        self.rule_stack.append( obj )
+        self.rule_stack.append(obj)
         if not isinstance(obj, RuleObject) and not isinstance(obj, TokenObject):
 ##             if DEBUG_MODE:
 ##                 print "Pushed:", str(obj), len(self.rule_stack)



More information about the Pypy-commit mailing list