Support for the yield statement.

This commit is contained in:
Darryl Pogue
2011-09-18 22:31:09 -07:00
parent dfd8a620f2
commit 51ca98d766
2 changed files with 27 additions and 4 deletions

View File

@@ -160,13 +160,19 @@ private:
class ASTReturn : public ASTNode {
public:
ASTReturn(PycRef<ASTNode> value)
: ASTNode(NODE_RETURN), m_value(value) { }
enum RetType {
RETURN, YIELD
};
ASTReturn(PycRef<ASTNode> value, RetType rettype = RETURN)
: ASTNode(NODE_RETURN), m_value(value), m_rettype(rettype) { }
PycRef<ASTNode> value() const { return m_value; }
RetType rettype() const { return m_rettype; }
private:
PycRef<ASTNode> m_value;
RetType m_rettype;
};

View File

@@ -1260,6 +1260,13 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack.push(new ASTTuple(vals));
}
break;
case Pyc::YIELD_VALUE:
{
PycRef<ASTNode> value = stack.top();
stack.pop();
curblock->append(new ASTReturn(value, ASTReturn::YIELD));
}
break;
default:
fprintf(stderr, "Unsupported opcode: %s\n", Pyc::OpcodeName(opcode & 0xFF));
cleanBuild = false;
@@ -1600,8 +1607,18 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
}
break;
case ASTNode::NODE_RETURN:
printf("return ");
print_src(node.cast<ASTReturn>()->value(), mod);
{
PycRef<ASTReturn> ret = node.cast<ASTReturn>();
switch (ret->rettype()) {
case ASTReturn::RETURN:
printf("return ");
break;
case ASTReturn::YIELD:
printf("yield ");
break;
}
print_src(ret->value(), mod);
}
break;
case ASTNode::NODE_SLICE:
{