Support for printing to a stream (PRINT_*_TO).

This commit is contained in:
Darryl Pogue
2011-09-18 00:35:28 -07:00
parent 81fe5d56dd
commit 521490c819
4 changed files with 25 additions and 3 deletions

View File

@@ -319,13 +319,15 @@ private:
class ASTPrint : public ASTNode {
public:
ASTPrint(PycRef<ASTNode> value)
: ASTNode(NODE_PRINT), m_value(value) { }
ASTPrint(PycRef<ASTNode> value, PycRef<ASTNode> stream = Node_NULL)
: ASTNode(NODE_PRINT), m_value(value), m_stream(stream) { }
PycRef<ASTNode> value() const { return m_value; }
PycRef<ASTNode> stream() const { return m_stream; }
private:
PycRef<ASTNode> m_value;
PycRef<ASTNode> m_stream;
};

View File

@@ -753,7 +753,9 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
if (!curblock->inited()) {
curblock.cast<ASTCondBlock>()->init();
break;
} else if (value->type() == ASTNode::NODE_INVALID) {
} else if (value->type() == ASTNode::NODE_INVALID
|| value->type() == ASTNode::NODE_BINARY
|| value->type() == ASTNode::NODE_NAME) {
break;
}
@@ -764,9 +766,22 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
curblock->append(new ASTPrint(stack.top()));
stack.pop();
break;
case Pyc::PRINT_ITEM_TO:
{
PycRef<ASTNode> stream = stack.top();
stack.pop();
curblock->append(new ASTPrint(stack.top(), stream));
stack.pop();
break;
}
case Pyc::PRINT_NEWLINE:
curblock->append(new ASTPrint(Node_NULL));
break;
case Pyc::PRINT_NEWLINE_TO:
stack.pop();
curblock->append(new ASTPrint(Node_NULL));
break;
case Pyc::RAISE_VARARGS_A:
{
ASTRaise::param_t paramList;
@@ -1433,6 +1448,11 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
inPrint = false;
} else if (!inPrint) {
printf("print ");
if (node.cast<ASTPrint>()->stream() != Node_NULL) {
printf(">>");
print_src(node.cast<ASTPrint>()->stream(), mod);
printf(", ");
}
print_src(node.cast<ASTPrint>()->value(), mod);
inPrint = true;
} else {

BIN
tests/27_print_to.pyc Normal file

Binary file not shown.

BIN
tests/27_test_print_to.pyc Normal file

Binary file not shown.