[pypy-svn] r66445 - in pypy/branch/parser-compiler: lib-python/modified-2.5.2 pypy/interpreter/astcompiler

benjamin at codespeak.net benjamin at codespeak.net
Mon Jul 20 18:13:03 CEST 2009


Author: benjamin
Date: Mon Jul 20 18:13:02 2009
New Revision: 66445

Added:
   pypy/branch/parser-compiler/lib-python/modified-2.5.2/symbol.py   (contents, props changed)
   pypy/branch/parser-compiler/lib-python/modified-2.5.2/token.py   (contents, props changed)
Modified:
   pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py
Log:
generate symbol and token from our parser's data

Added: pypy/branch/parser-compiler/lib-python/modified-2.5.2/symbol.py
==============================================================================
--- (empty file)
+++ pypy/branch/parser-compiler/lib-python/modified-2.5.2/symbol.py	Mon Jul 20 18:13:02 2009
@@ -0,0 +1,114 @@
+#! /usr/bin/env python
+
+"""Non-terminal symbols of Python grammar (from "graminit.h")."""
+
+#  This file is automatically generated; please don't muck it up!
+#
+#  To update the symbols in this file, 'cd' to the top directory of
+#  the python source tree after building the interpreter and run:
+#
+#    python Lib/symbol.py
+
+#--start constants--
+dotted_as_names = 276
+import_as_name = 297
+try_stmt = 332
+eval_input = 279
+small_stmt = 320
+augassign = 264
+argument = 260
+or_test = 310
+fplist = 290
+import_as_names = 298
+return_stmt = 316
+testlist_safe = 330
+not_test = 307
+listmaker = 306
+except_clause = 280
+list_if = 304
+old_test = 309
+arglist = 259
+import_from = 299
+gen_iter = 294
+break_stmt = 265
+dictmaker = 274
+comp_op = 267
+import_stmt = 301
+with_var = 336
+parameters = 311
+continue_stmt = 270
+fpdef = 289
+shift_expr = 317
+dotted_as_name = 275
+testlist_gexp = 329
+list_iter = 305
+exec_stmt = 281
+factor = 285
+list_for = 303
+global_stmt = 295
+subscript = 322
+decorators = 272
+compound_stmt = 269
+and_expr = 257
+yield_stmt = 339
+dotted_name = 277
+yield_expr = 338
+power = 313
+print_stmt = 314
+gen_for = 292
+subscriptlist = 323
+testlist = 327
+classdef = 266
+and_test = 258
+encoding_decl = 278
+assert_stmt = 262
+test = 326
+for_stmt = 288
+stmt = 321
+lambdef = 302
+atom = 263
+funcdef = 291
+expr_stmt = 283
+old_lambdef = 308
+exprlist = 284
+decorator = 271
+pass_stmt = 312
+sliceop = 319
+comparison = 268
+term = 325
+if_stmt = 296
+arith_expr = 261
+expr = 282
+raise_stmt = 315
+import_name = 300
+gen_if = 293
+del_stmt = 273
+while_stmt = 334
+varargslist = 333
+testlist1 = 328
+suite = 324
+single_input = 256
+simple_stmt = 318
+with_stmt = 335
+xor_expr = 337
+flow_stmt = 287
+trailer = 331
+file_input = 286
+#--end constants--
+
+sym_name = {}
+for _name, _value in globals().items():
+    if type(_value) is type(0):
+        sym_name[_value] = _name
+
+
+if __name__ == "__main__":
+    import os
+    import sys
+    import token
+    sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), "../../"))
+    try:
+        from pypy.interpreter.pyparser import pygram
+        token.main(pygram.python_grammar.symbol_ids, __file__)
+    finally:
+        sys.path.pop()

Added: pypy/branch/parser-compiler/lib-python/modified-2.5.2/token.py
==============================================================================
--- (empty file)
+++ pypy/branch/parser-compiler/lib-python/modified-2.5.2/token.py	Mon Jul 20 18:13:02 2009
@@ -0,0 +1,127 @@
+#! /usr/bin/env python
+
+"""Token constants (from "token.h")."""
+
+#  This file is automatically generated; please don't muck it up!
+#
+#  To update the symbols in this file, 'cd' to the top directory of
+#  the python source tree after building the interpreter and run:
+#
+#    python Lib/token.py
+
+#--start constants--
+DEDENT = 6
+LPAR = 7
+STAR = 16
+AMPER = 19
+LESS = 20
+SLASHEQUAL = 40
+NUMBER = 2
+RPAR = 8
+CIRCUMFLEX = 33
+NOTEQUAL = 29
+VBAR = 18
+BACKQUOTE = 25
+DOUBLESTAR = 36
+MINUS = 15
+DOT = 23
+STRING = 3
+STAREQUAL = 39
+GREATEREQUAL = 31
+MINEQUAL = 38
+LEFTSHIFTEQUAL = 45
+SEMI = 13
+CIRCUMFLEXEQUAL = 44
+NEWLINE = 4
+DOUBLESLASHEQUAL = 49
+COLON = 11
+PERCENTEQUAL = 41
+TILDE = 32
+PLUS = 14
+ERRORTOKEN = 52
+RSQB = 10
+EQEQUAL = 28
+COMMENT = 53
+AMPEREQUAL = 42
+RIGHTSHIFT = 35
+RBRACE = 27
+NT_OFFSET = 256
+PERCENT = 24
+DOUBLESLASH = 48
+DOUBLESTAREQUAL = 47
+EQUAL = 22
+PLUSEQUAL = 37
+AT = 50
+SLASH = 17
+LESSEQUAL = 30
+NL = 54
+LSQB = 9
+N_TOKENS = 55
+RIGHTSHIFTEQUAL = 46
+GREATER = 21
+LBRACE = 26
+INDENT = 5
+NAME = 1
+VBAREQUAL = 43
+LEFTSHIFT = 34
+COMMA = 12
+ENDMARKER = 0
+OP = 51
+#--end constants--
+
+tok_name = {}
+for _name, _value in globals().items():
+    if type(_value) is type(0):
+        tok_name[_value] = _name
+
+
+def ISTERMINAL(x):
+    return x < NT_OFFSET
+
+def ISNONTERMINAL(x):
+    return x >= NT_OFFSET
+
+def ISEOF(x):
+    return x == ENDMARKER
+
+
+def main(data, outFileName):
+    # load the output skeleton from the target:
+    try:
+        fp = open(outFileName)
+    except IOError, err:
+        sys.stderr.write("I/O error: %s\n" % str(err))
+        sys.exit(2)
+    format = fp.read().split("\n")
+    fp.close()
+    try:
+        start = format.index("#--start constants--") + 1
+        end = format.index("#--end constants--")
+    except ValueError:
+        sys.stderr.write("target does not contain format markers")
+        sys.exit(3)
+    lines = []
+    for key, val in data.iteritems():
+        lines.append("%s = %d" % (key, val))
+    format[start:end] = lines
+    try:
+        fp = open(outFileName, 'w')
+    except IOError, err:
+        sys.stderr.write("I/O error: %s\n" % str(err))
+        sys.exit(4)
+    fp.write("\n".join(format))
+    fp.close()
+
+
+if __name__ == "__main__":
+    import os
+    import sys
+    sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), "../../"))
+    try:
+        from pypy.interpreter.pyparser import pytoken
+        data = pytoken.python_tokens.copy()
+        data["N_TOKENS"] = len(data)
+        data["NT_OFFSET"] = 256
+        main(data, __file__)
+    finally:
+        sys.path.pop()

Modified: pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py	(original)
+++ pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py	Mon Jul 20 18:13:02 2009
@@ -1007,19 +1007,20 @@
     def visit_Attribute(self, attr):
         self.update_position(attr.lineno)
         names = self.names
-        if attr.ctx != ast.AugStore:
+        ctx = attr.ctx
+        if ctx != ast.AugStore:
             attr.value.walkabout(self)
-        if attr.ctx == ast.AugLoad:
+        if ctx == ast.AugLoad:
             self.emit_op(ops.DUP_TOP)
             self.emit_op_name(ops.LOAD_ATTR, names, attr.attr)
-        elif attr.ctx == ast.Load:
+        elif ctx == ast.Load:
             self.emit_op_name(ops.LOAD_ATTR, names, attr.attr)
-        elif attr.ctx == ast.AugStore:
+        elif ctx == ast.AugStore:
             self.emit_op(ops.ROT_TWO)
             self.emit_op_name(ops.STORE_ATTR, names, attr.attr)
-        elif attr.ctx == ast.Store:
+        elif ctx == ast.Store:
             self.emit_op_name(ops.STORE_ATTR, names, attr.attr)
-        elif attr.ctx == ast.Del:
+        elif ctx == ast.Del:
             self.emit_op_name(ops.DELETE_ATTR, names, attr.attr)
         else:
             raise AssertionError("unknown context")



More information about the Pypy-commit mailing list