Support for conditonal expression (if-expression) - review findings

This commit is contained in:
Nenad Čaklović
2022-04-02 13:57:10 +02:00
parent 46affe279c
commit 38a1ee59c3
4 changed files with 80 additions and 6 deletions

View File

@@ -30,7 +30,7 @@ static bool printDocstringAndGlobals = false;
static bool printClassDocstring = true;
// shortcut for all top/pop calls
PycRef<ASTNode> StackPopTop(FastStack& stack)
static PycRef<ASTNode> StackPopTop(FastStack& stack)
{
const auto node{ stack.top() };
stack.pop();
@@ -47,7 +47,7 @@ PycRef<ASTNode> StackPopTop(FastStack& stack)
* 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)
static void CheckIfExpr(FastStack& stack, PycRef<ASTBlock> curblock)
{
if (stack.empty())
return;
@@ -3287,13 +3287,16 @@ void print_src(PycRef<ASTNode> node, PycModule* 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
/* parenthesis might be needed
*
* 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)
*
* but, let's not add parenthesis - to keep the source as close to original as possible in most cases
*/
PycRef<ASTTernary> ternary = node.cast<ASTTernary>();
fputs("( ", pyc_output);
//fputs("(", pyc_output);
print_src(ternary->if_expr(), mod);
const auto if_block = ternary->if_block().require_cast<ASTCondBlock>();
fputs(" if ", pyc_output);
@@ -3302,7 +3305,7 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
print_src(if_block->cond(), mod);
fputs(" else ", pyc_output);
print_src(ternary->else_expr(), mod);
fputs(" )", pyc_output);
//fputs(")", pyc_output);
}
break;
default:

Binary file not shown.

View File

@@ -0,0 +1,42 @@
a = 1
result = 'even' if a % 2 == 0 else 'odd'
print(result)
# odd
a = 2
result = 'even' if a % 2 == 0 else 'odd'
print(result)
# even
a = 1
result = a * 2 if a % 2 == 0 else a * 3
print(result)
# 3
a = 2
result = a * 2 if a % 2 == 0 else a * 3
print(result)
# 4
a = 1
print('even') if a % 2 == 0 else print('odd')
# odd
a = 1
if a % 2 == 0:
print('even')
else:
print('odd')
# odd
a = -2
result = 'negative and even' if a < 0 and a % 2 == 0 else 'positive or odd'
print(result)
# negative and even
a = -1
result = 'negative and even' if a < 0 and a % 2 == 0 else 'positive or odd'
print(result)
# positive or odd

View File

@@ -0,0 +1,29 @@
a = 1 <EOL>
result = 'even' if a % 2 == 0 else 'odd' <EOL>
print ( result ) <EOL>
a = 2 <EOL>
result = 'even' if a % 2 == 0 else 'odd' <EOL>
print ( result ) <EOL>
a = 1 <EOL>
result = a * 2 if a % 2 == 0 else a * 3 <EOL>
print ( result ) <EOL>
a = 2 <EOL>
result = a * 2 if a % 2 == 0 else a * 3 <EOL>
print ( result ) <EOL>
a = 1 <EOL>
print ( 'even' ) if a % 2 == 0 else print ( 'odd' ) <EOL>
a = 1 <EOL>
if a % 2 == 0 : <EOL>
<INDENT>
print ( 'even' ) <EOL>
<OUTDENT>
else : <EOL>
<INDENT>
print ( 'odd' ) <EOL>
<OUTDENT>
a = - 2 <EOL>
result = 'negative and even' if a < 0 and a % 2 == 0 else 'positive or odd' <EOL>
print ( result ) <EOL>
a = - 1 <EOL>
result = 'negative and even' if a < 0 and a % 2 == 0 else 'positive or odd' <EOL>
print ( result ) <EOL>