Commit 47ef853a authored by Kevin Modzelewski's avatar Kevin Modzelewski

Implement list unpacking

ie "[a, b] = 1, 2"
parent e93d36d7
......@@ -376,24 +376,33 @@ private:
assign->targets.push_back(a_target);
push_back(assign);
} else if (target->type == AST_TYPE::Tuple) {
AST_Tuple* old_target = ast_cast<AST_Tuple>(target);
} else if (target->type == AST_TYPE::Tuple || target->type == AST_TYPE::List) {
std::vector<AST_expr*>* elts;
if (target->type == AST_TYPE::Tuple) {
AST_Tuple* _t = ast_cast<AST_Tuple>(target);
assert(_t->ctx_type == AST_TYPE::Store);
elts = &_t->elts;
} else {
AST_List* _t = ast_cast<AST_List>(target);
assert(_t->ctx_type == AST_TYPE::Store);
elts = &_t->elts;
}
AST_Tuple* new_target = new AST_Tuple();
new_target->ctx_type = old_target->ctx_type;
new_target->lineno = old_target->lineno;
new_target->col_offset = old_target->col_offset;
new_target->ctx_type = AST_TYPE::Store;
new_target->lineno = target->lineno;
new_target->col_offset = target->col_offset;
// A little hackery: push the assign, even though we're not done constructing it yet,
// so that we can iteratively push more stuff after it
assign->targets.push_back(new_target);
push_back(assign);
for (int i = 0; i < old_target->elts.size(); i++) {
for (int i = 0; i < elts->size(); i++) {
std::string tmp_name = nodeName(target, "", i);
new_target->elts.push_back(makeName(tmp_name, AST_TYPE::Store));
pushAssign(old_target->elts[i], makeName(tmp_name, AST_TYPE::Load));
pushAssign((*elts)[i], makeName(tmp_name, AST_TYPE::Load));
}
} else {
RELEASE_ASSERT(0, "%d", target->type);
......
......@@ -19,4 +19,11 @@ def sort(l):
return l
print sort([1, 3, 5, 7, 2, 4, 9, 9, 4])
a, b, c = 1,
try:
a, b, c = 1,
except ValueError, e:
print e
# Ok this isn't really "tuple" unpacking but it's pretty much the same thing:
[a, [b, c], d] = (1, [3, 4], 5)
print a, b, c, d
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment