diff options
author | Guido van Rossum <guido@python.org> | 2006-08-28 15:27:34 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2006-08-28 15:27:34 (GMT) |
commit | 86e58e239e39845e706c4afa392423f0fedcdf39 (patch) | |
tree | 1d0f4d942e644ee5c903636d87176b98a7203371 /Lib/compiler | |
parent | ecfd0b2f3bfd622c3ba148e53d3feebb8c1ae721 (diff) | |
download | cpython-86e58e239e39845e706c4afa392423f0fedcdf39.zip cpython-86e58e239e39845e706c4afa392423f0fedcdf39.tar.gz cpython-86e58e239e39845e706c4afa392423f0fedcdf39.tar.bz2 |
SF patch 1547796 by Georg Brandl -- set literals.
Diffstat (limited to 'Lib/compiler')
-rw-r--r-- | Lib/compiler/ast.py | 20 | ||||
-rw-r--r-- | Lib/compiler/pyassem.py | 2 | ||||
-rw-r--r-- | Lib/compiler/pycodegen.py | 6 | ||||
-rw-r--r-- | Lib/compiler/transformer.py | 21 |
4 files changed, 39 insertions, 10 deletions
diff --git a/Lib/compiler/ast.py b/Lib/compiler/ast.py index 94a6262..6953325 100644 --- a/Lib/compiler/ast.py +++ b/Lib/compiler/ast.py @@ -542,7 +542,6 @@ class Function(Node): self.kwargs = 1 - def getChildren(self): children = [] children.append(self.decorators) @@ -572,6 +571,7 @@ class GenExpr(Node): self.argnames = ['.0'] self.varargs = self.kwargs = None + def getChildren(self): return self.code, @@ -589,7 +589,6 @@ class GenExprFor(Node): self.lineno = lineno self.is_outmost = False - def getChildren(self): children = [] children.append(self.assign) @@ -766,7 +765,6 @@ class Lambda(Node): self.kwargs = 1 - def getChildren(self): children = [] children.append(self.argnames) @@ -1091,6 +1089,22 @@ class RightShift(Node): def __repr__(self): return "RightShift((%s, %s))" % (repr(self.left), repr(self.right)) +class Set(Node): + def __init__(self, items, lineno=None): + self.items = items + self.lineno = lineno + + def getChildren(self): + return tuple(flatten(self.items)) + + def getChildNodes(self): + nodelist = [] + nodelist.extend(flatten_nodes(self.items)) + return tuple(nodelist) + + def __repr__(self): + return "Set(%s)" % (repr(self.items),) + class Slice(Node): def __init__(self, expr, flags, lower, upper, lineno=None): self.expr = expr diff --git a/Lib/compiler/pyassem.py b/Lib/compiler/pyassem.py index 95f5c3e..542d704 100644 --- a/Lib/compiler/pyassem.py +++ b/Lib/compiler/pyassem.py @@ -793,6 +793,8 @@ class StackDepthTracker: return -count+1 def BUILD_LIST(self, count): return -count+1 + def BUILD_SET(self, count): + return -count+1 def CALL_FUNCTION(self, argc): hi, lo = divmod(argc, 256) return -(lo + hi * 2) diff --git a/Lib/compiler/pycodegen.py b/Lib/compiler/pycodegen.py index cc197e3..af99045 100644 --- a/Lib/compiler/pycodegen.py +++ b/Lib/compiler/pycodegen.py @@ -1241,6 +1241,12 @@ class CodeGenerator: self.visit(elt) self.emit('BUILD_LIST', len(node.nodes)) + def visitSet(self, node): + self.set_lineno(node) + for elt in node.items: + self.visit(elt) + self.emit('BUILD_SET', len(node.items)) + def visitSliceobj(self, node): for child in node.nodes: self.visit(child) diff --git a/Lib/compiler/transformer.py b/Lib/compiler/transformer.py index b564300..42640e8 100644 --- a/Lib/compiler/transformer.py +++ b/Lib/compiler/transformer.py @@ -738,7 +738,7 @@ class Transformer: def atom_lbrace(self, nodelist): if nodelist[1][0] == token.RBRACE: return Dict((), lineno=nodelist[0][2]) - return self.com_dictmaker(nodelist[1]) + return self.com_dictsetmaker(nodelist[1]) def atom_backquote(self, nodelist): return Backquote(self.com_node(nodelist[1])) @@ -1182,13 +1182,20 @@ class Transformer: assert node[0] == symbol.gen_iter return node[1] - def com_dictmaker(self, nodelist): - # dictmaker: test ':' test (',' test ':' value)* [','] + def com_dictsetmaker(self, nodelist): + # dictsetmaker: (test ':' test (',' test ':' value)* [',']) | (test (',' test)* [',']) items = [] - for i in range(1, len(nodelist), 4): - items.append((self.com_node(nodelist[i]), - self.com_node(nodelist[i+2]))) - return Dict(items, lineno=items[0][0].lineno) + if nodelist[2] != ':': + # it's a set + for i in range(1, len(nodelist), 2): + items.append(self.com_node(nodelist[i])) + return Set(items, lineno=items[0].lineno) + else: + # it's a dict + for i in range(1, len(nodelist), 4): + items.append((self.com_node(nodelist[i]), + self.com_node(nodelist[i+2]))) + return Dict(items, lineno=items[0][0].lineno) def com_apply_trailer(self, primaryNode, nodelist): t = nodelist[1][0] |