Skip to content

Commit

Permalink
Support for conditonal expression (if-expression)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenad Čaklović committed Mar 29, 2022
1 parent f19b38d commit 46affe2
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 13 deletions.
31 changes: 30 additions & 1 deletion ASTNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ASTNode {
NODE_CONVERT, NODE_KEYWORD, NODE_RAISE, NODE_EXEC, NODE_BLOCK,
NODE_COMPREHENSION, NODE_LOADBUILDCLASS, NODE_AWAITABLE,
NODE_FORMATTEDVALUE, NODE_JOINEDSTR, NODE_CONST_MAP,
NODE_ANNOTATED_VAR, NODE_CHAINSTORE,
NODE_ANNOTATED_VAR, NODE_CHAINSTORE, NODE_TERNARY,

// Empty node types
NODE_LOCALS,
Expand Down Expand Up @@ -699,4 +699,33 @@ class ASTAnnotatedVar : public ASTNode {
PycRef<ASTNode> m_type;
};

class ASTTernary : public ASTNode
{
public:
ASTTernary(PycRef<ASTNode> if_block, PycRef<ASTNode> if_expr, PycRef<ASTNode> else_expr)
: ASTNode(NODE_TERNARY),
m_if_block(std::move(if_block)),
m_if_expr(std::move(if_expr)),
m_else_expr(std::move(else_expr))
{
}
const PycRef<ASTNode>& if_block() const noexcept
{
return m_if_block;
}
const PycRef<ASTNode>& if_expr() const noexcept
{
return m_if_expr;
}
const PycRef<ASTNode>& else_expr() const noexcept
{
return m_else_expr;
}

private:
PycRef<ASTNode> m_if_block; // contains "condition" and "negative"
PycRef<ASTNode> m_if_expr;
PycRef<ASTNode> m_else_expr;
};

#endif
73 changes: 61 additions & 12 deletions ASTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,43 @@ static bool printDocstringAndGlobals = false;
/* Use this to keep track of whether we need to print a class or module docstring */
static bool printClassDocstring = true;

// shortcut for all top/pop calls
PycRef<ASTNode> StackPopTop(FastStack& stack)
{
const auto node{ stack.top() };
stack.pop();
return node;
}

/* compiler generates very, VERY similar byte code for if/else statement block and if-expression
* statement
* if a: b = 1
* else: b = 2
* expression:
* b = 1 if a else 2
* (see for instance https://stackoverflow.com/a/52202007)
* here, try to guess if just finished else statement is part of if-expression (ternary operator)
* if it is, remove statements from the block and put a ternary node on top of stack
*/
void CheckIfExpr(FastStack& stack, PycRef<ASTBlock> curblock)
{
if (stack.empty())
return;
if (curblock->nodes().size() < 2)
return;
auto rit{ curblock->nodes().crbegin() };
++rit; // the last is "else" block, the one before should be "if" (could be "for", ...)
if ((*rit)->type() != ASTNode::NODE_BLOCK ||
(*rit).cast<ASTBlock>()->blktype() != ASTBlock::BLK_IF)
return;
auto else_expr{ StackPopTop(stack) };
curblock->removeLast();
auto if_block{ curblock->nodes().back() };
auto if_expr{ StackPopTop(stack) };
curblock->removeLast();
stack.push(new ASTTernary(std::move(if_block), std::move(if_expr), std::move(else_expr)));
}

PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
{
PycBuffer source(code->code()->value(), code->code()->length());
Expand Down Expand Up @@ -109,6 +146,8 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
curblock->append(prev.cast<ASTNode>());

prev = curblock;

CheckIfExpr(stack, curblock);
}
}

Expand Down Expand Up @@ -1363,19 +1402,9 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
break;
}

if ((curblock->blktype() == ASTBlock::BLK_WHILE
&& !curblock->inited())
|| (curblock->blktype() == ASTBlock::BLK_IF
&& curblock->size() == 0)) {
PycRef<PycObject> fakeint = new PycInt(1);
PycRef<ASTNode> truthy = new ASTObject(fakeint);

stack.push(truthy);
break;
}

if (!stack_hist.empty()) {
stack = stack_hist.top();
if (stack.empty()) // if it's part of if-expression, TOS at the moment is the result of "if" part
stack = stack_hist.top();
stack_hist.pop();
}

Expand Down Expand Up @@ -3256,6 +3285,26 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
print_src(type, mod);
}
break;
case ASTNode::NODE_TERNARY:
{
/* parenthesis might not be needed,
* but when if-expr is part of numerical expression, ternary has the LOWEST precedence
* print(a + b if False else c)
* output is c, not a+c (a+b is calculated first)
*/
PycRef<ASTTernary> ternary = node.cast<ASTTernary>();
fputs("( ", pyc_output);
print_src(ternary->if_expr(), mod);
const auto if_block = ternary->if_block().require_cast<ASTCondBlock>();
fputs(" if ", pyc_output);
if (if_block->negative())
fputs("not ", pyc_output);
print_src(if_block->cond(), mod);
fputs(" else ", pyc_output);
print_src(ternary->else_expr(), mod);
fputs(" )", pyc_output);
}
break;
default:
fprintf(pyc_output, "<NODE:%d>", node->type());
fprintf(stderr, "Unsupported Node type: %d\n", node->type());
Expand Down
5 changes: 5 additions & 0 deletions FastStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class FastStack {
return nullptr;
}

bool empty() const
{
return m_ptr == -1;
}

private:
std::vector<PycRef<ASTNode>> m_stack;
int m_ptr;
Expand Down

0 comments on commit 46affe2

Please sign in to comment.