Support for conditonal expression (if-expression) - review findings
This commit is contained in:
15
ASTree.cpp
15
ASTree.cpp
@@ -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:
|
||||
|
BIN
tests/compiled/conditional_expressions.3.9.pyc
Normal file
BIN
tests/compiled/conditional_expressions.3.9.pyc
Normal file
Binary file not shown.
42
tests/input/conditional_expressions.py
Normal file
42
tests/input/conditional_expressions.py
Normal 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
|
||||
|
29
tests/tokenized/conditional_expressions.txt
Normal file
29
tests/tokenized/conditional_expressions.txt
Normal 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>
|
Reference in New Issue
Block a user