[pypy-svn] r15835 - in pypy/dist/pypy/interpreter/pyparser: . test test/samples

adim at codespeak.net adim at codespeak.net
Tue Aug 9 17:24:43 CEST 2005


Author: adim
Date: Tue Aug  9 17:24:40 2005
New Revision: 15835

Added:
   pypy/dist/pypy/interpreter/pyparser/test/samples/snippet_while.py
Modified:
   pypy/dist/pypy/interpreter/pyparser/astbuilder.py
   pypy/dist/pypy/interpreter/pyparser/test/samples/snippet_import_statements.py
   pypy/dist/pypy/interpreter/pyparser/test/test_astbuilder.py
Log:
- implemented while stmts
- implemented import statements



Modified: pypy/dist/pypy/interpreter/pyparser/astbuilder.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/astbuilder.py	(original)
+++ pypy/dist/pypy/interpreter/pyparser/astbuilder.py	Tue Aug  9 17:24:40 2005
@@ -513,6 +513,107 @@
     builder.push(ast.For(assign, iterable, body, else_))
 
 
+def build_while_stmt(builder, nb):
+    """while_stmt: 'while' test ':' suite ['else' ':' suite]"""
+    L = get_atoms(builder, nb)
+    else_ = None
+    # skip 'while'
+    test =  L[1]
+    # skip ':'
+    body = L[3]
+    # if there is a "else" statement
+    if len(L) > 4:
+        # skip 'else' and ':'
+        else_ = L[6]
+    builder.push(ast.While(test, body, else_))
+
+
+def build_import_name(builder, nb):
+    """import_name: 'import' dotted_as_names
+
+    dotted_as_names: dotted_as_name (',' dotted_as_name)*
+    dotted_as_name: dotted_name [NAME NAME]
+    dotted_name: NAME ('.' NAME)*
+
+    written in an unfolded way:
+    'import' NAME(.NAME)* [NAME NAME], (NAME(.NAME)* [NAME NAME],)*
+
+    XXX: refactor build_import_name and build_import_from
+    """
+    L = get_atoms(builder, nb)
+    index = 1 # skip 'import'
+    l = len(L)
+    names = []
+    while index < l:
+        as_name = None
+        # dotted name (a.b.c)
+        incr, name = parse_dotted_names(L[index:])
+        index += incr
+        # 'as' value
+        if index < l and L[index].value == 'as':
+            as_name = L[index+1].value
+            index += 2
+        names.append((name, as_name))
+        # move forward until next ','
+        while index < l and L[index].name != tok.COMMA:
+            index += 1
+        index += 1
+    builder.push(ast.Import(names))
+
+
+def build_import_from(builder, nb):
+    """
+    import_from: 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)
+
+    import_as_names: import_as_name (',' import_as_name)* [',']
+    import_as_name: NAME [NAME NAME]
+    """
+    L = get_atoms(builder, nb)
+    index = 1
+    incr, from_name = parse_dotted_names(L[index:])
+    index += (incr + 1) # skip 'import'
+    if L[index].name == tok.STAR:
+        names = [('*', None)]
+    else:
+        if L[index].name == tok.LPAR:
+            # mutli-line imports
+            tokens = L[index+1:-1]
+        else:
+            tokens = L[index:]
+        index = 0
+        l = len(tokens)
+        names = []
+        while index < l:
+            name = tokens[index].value
+            as_name = None
+            index += 1
+            if index < l:
+                if tokens[index].value == 'as':
+                    as_name = tokens[index+1].value
+                    index += 2
+            names.append((name, as_name))
+            if index < l: # case ','
+                index += 1
+    builder.push(ast.From(from_name, names))
+
+
+def parse_dotted_names(tokens):
+    """parses NAME('.' NAME)* and returns full dotted name
+
+    this function doesn't assume that the <tokens> list ends after the
+    last 'NAME' element
+    """
+    name = tokens[0].value
+    l = len(tokens)
+    index = 1
+    for index in range(1, l, 2):
+        token = tokens[index]
+        assert isinstance(token, TokenObject)
+        if token.name != tok.DOT:
+            break
+        name = name + '.' + tokens[index+1].value
+    return (index, name)
+
 def parse_argument(tokens):
     """parses function call arguments"""
     l = len(tokens)
@@ -693,6 +794,9 @@
     sym.pass_stmt : build_pass_stmt,
     sym.break_stmt : build_break_stmt,
     sym.for_stmt : build_for_stmt,
+    sym.while_stmt : build_while_stmt,
+    sym.import_name : build_import_name,
+    sym.import_from : build_import_from,
     # sym.parameters : build_parameters,
     }
 

Modified: pypy/dist/pypy/interpreter/pyparser/test/samples/snippet_import_statements.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/test/samples/snippet_import_statements.py	(original)
+++ pypy/dist/pypy/interpreter/pyparser/test/samples/snippet_import_statements.py	Tue Aug  9 17:24:40 2005
@@ -1,2 +1,5 @@
 import os
 import os.path as osp
+from sets import Set, ImmutableSet
+
+

Added: pypy/dist/pypy/interpreter/pyparser/test/samples/snippet_while.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/interpreter/pyparser/test/samples/snippet_while.py	Tue Aug  9 17:24:40 2005
@@ -0,0 +1,8 @@
+index = 0
+while index < 10:
+    index += 1
+    foo = 10 - index
+    if False:
+        break
+else:
+    foo = 12

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	Tue Aug  9 17:24:40 2005
@@ -100,6 +100,24 @@
     'x = a.b',
     ]
 
+imports = [
+    'import os',
+    'import sys, os',
+    'import os.path',
+    'import os.path, sys',
+    'import sys, os.path as osp',
+    'import os.path as osp',
+    'import os.path as osp, sys as _sys',
+    'import a.b.c.d',
+    'import a.b.c.d as abcd',
+    'from os import path',
+    'from os import path, system',
+    'from os import path, system,',
+    'from os import path as P, system as S,',
+    'from os import (path as P, system as S,)',
+    'from os import *',
+    ]
+
 if_stmts = [
     "if a == 1: a+= 2",
     """if a == 1:
@@ -133,6 +151,7 @@
     dictmakers,
     multiexpr,
     attraccess,
+    imports,
     ]
 
 EXEC_INPUTS = [
@@ -183,6 +202,8 @@
     'snippet_several_statements.py',
     'snippet_simple_function.py',
     'snippet_simple_for_loop.py',
+    'snippet_while.py',
+    'snippet_import_statements.py',
 #    'snippet_2.py',
 #    'snippet_3.py',
 #    'snippet_4.py',



More information about the Pypy-commit mailing list