[pypy-svn] r39269 - pypy/extradoc/talk/pycon2007/demos
mwh at codespeak.net
mwh at codespeak.net
Wed Feb 21 13:02:49 CET 2007
Author: mwh
Date: Wed Feb 21 13:02:46 2007
New Revision: 39269
Added:
pypy/extradoc/talk/pycon2007/demos/
pypy/extradoc/talk/pycon2007/demos/enabledo.py (contents, props changed)
Log:
demo no 1: code stolen from test_dyngram to add a do:until: statement at
runtime.
Added: pypy/extradoc/talk/pycon2007/demos/enabledo.py
==============================================================================
--- (empty file)
+++ pypy/extradoc/talk/pycon2007/demos/enabledo.py Wed Feb 21 13:02:46 2007
@@ -0,0 +1,43 @@
+import dyngram, parser
+
+newrules = """
+compound_stmt: if_stmt | on_stmt | unless_stmt | dountil_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef
+dountil_stmt: 'do' ':' suite 'until' test
+unless_stmt: 'unless' test ':' suite
+on_stmt: 'on' NAME '=' test ':' suite ['else' ':' suite]
+"""
+
+def build_dountil_stmt(items):
+ """ 'do' ':' suite 'until' ':' test """
+ lineno = items[0].lineno
+ suite = items[2]
+ test = items[-1]
+ while_stmt = parser.ASTWhile(parser.ASTNot(test), suite, None, lineno)
+ return parser.ASTStmt([suite, while_stmt], lineno)
+
+def build_unless_stmt(its):
+ """ 'unless' test ':' suite """
+ lineno = its[0].lineno
+ return parser.ASTIf([(parser.ASTNot(its[1]), its[3])], None, lineno)
+
+def make_assignment(var, node):
+ # XXX: consts.OP_APPLY
+ return parser.ASTAssign([parser.ASTAssName('x', 0)], node)
+
+def build_on_stmt(items):
+ """ 'on' NAME = test ':' suite 'else' ':' suite"""
+ varname = items[1].value
+ test = items[3]
+ suite = items[5]
+ assign = make_assignment(varname, test)
+ if len(items) == 9:
+ else_ = items[-1]
+ else:
+ else_ = None
+ test = parser.ASTIf([(parser.ASTName(varname), suite)], else_, items[0].lineno)
+ return parser.ASTStmt([assign, test], items[0].lineno)
+
+dyngram.insert_grammar_rule(newrules, {'dountil_stmt' : build_dountil_stmt,
+ 'unless_stmt': build_unless_stmt,
+ 'on_stmt' : build_on_stmt,
+ })
More information about the Pypy-commit
mailing list