Update the compiler package to compile the with-statement.

Jeremy, please review!
This commit is contained in:
Guido van Rossum 2006-02-28 00:32:16 +00:00
parent 40d8459dbf
commit 7ad94f011e
3 changed files with 63 additions and 1 deletions

View file

@ -536,6 +536,12 @@ class Transformer:
return self.com_try_except(nodelist)
def with_stmt(self, nodelist):
return self.com_with(nodelist)
def with_var(self, nodelist):
return self.com_with_var(nodelist)
def suite(self, nodelist):
# simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
if len(nodelist) == 1:
@ -926,6 +932,20 @@ class Transformer:
return TryExcept(self.com_node(nodelist[2]), clauses, elseNode,
lineno=nodelist[0][2])
def com_with(self, nodelist):
# with_stmt: 'with' expr [with_var] ':' suite
expr = self.com_node(nodelist[1])
body = self.com_node(nodelist[-1])
if nodelist[2][0] == token.COLON:
var = None
else:
var = self.com_node(nodelist[2])
return With(expr, var, body, lineno=nodelist[0][2])
def com_with_var(self, nodelist):
# with_var: 'as' expr
return self.com_node(nodelist[1])
def com_augassign_op(self, node):
assert node[0] == symbol.augassign
return node[1]
@ -1390,6 +1410,7 @@ _legal_node_types = [
symbol.while_stmt,
symbol.for_stmt,
symbol.try_stmt,
symbol.with_stmt,
symbol.suite,
symbol.testlist,
symbol.testlist_safe,