Skip to content

Commit 84a073c

Browse files
Merge pull request #1087 from Thirumalai-Shaktivel/ternary_if
2 parents fcafb63 + 7fddf7b commit 84a073c

File tree

8 files changed

+85
-14
lines changed

8 files changed

+85
-14
lines changed

src/lpython/parser/parser.yy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ void yyerror(YYLTYPE *yyloc, LFortran::Parser &p, const std::string &msg)
272272

273273
%precedence ":="
274274
%precedence LAMBDA
275+
%left KW_IF KW_ELSE
275276
%left "or"
276277
%left "and"
277278
%precedence "not"
@@ -401,7 +402,6 @@ yield_expr
401402
expression_statment
402403
: tuple_list { $$ = EXPR_01($1, @$); }
403404
| yield_expr { $$ = EXPR_01($1, @$); }
404-
| ternary_if_statement { $$ = EXPR_01($1, @$); }
405405
;
406406

407407
pass_statement
@@ -435,7 +435,6 @@ target_list
435435
assignment_statement
436436
: target_list tuple_list { $$ = ASSIGNMENT($1, $2, @$); }
437437
| target_list yield_expr { $$ = ASSIGNMENT($1, $2, @$); }
438-
| target_list ternary_if_statement { $$ = ASSIGNMENT($1, $2, @$); }
439438
| target_list tuple_list TK_TYPE_COMMENT {
440439
$$ = ASSIGNMENT2($1, $2, $3, @$); }
441440
;
@@ -1070,6 +1069,7 @@ expr
10701069
| "not" expr { $$ = UNARY($2, Not, @$); }
10711070

10721071
| comprehension { $$ = $1; }
1072+
| ternary_if_statement { $$ = $1; }
10731073
| lambda_expression { $$ = $1; }
10741074
;
10751075

tests/parser/conditional_expr1.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Ternary operator (Conditional expressions)
2+
b = 6 if a == 2 else 8
3+
4+
'true' if True else 'false'
5+
6+
result = x if not (a > b) else y
7+
8+
print(a,"is greater") if (a > b) else print(b,"is Greater")
9+
10+
x('True' if True else 'False')
11+
12+
test = (x(123) if a else b.c)
13+
14+
res = test.x(
15+
'' if z is None else var)
16+
17+
size = sum(2 if ord(c) > 0xFFFF else 1 for c in init) + 1
18+
19+
(x, y, 'a' if a else 'b')
20+
21+
x += -1 if true else 1
22+
23+
(self.assertTrue if tktype in whentrue else self.assertFalse)\
24+
(func())
25+
26+
x = {
27+
f.parts[0] if len(f.parts) > 1 else f.with_suffix('').name
28+
for f in always_iterable(dist.files)
29+
if f.suffix == ".py"
30+
}
31+
32+
return_value = {
33+
key: value if not isinstance(value, str) else eval(value, globals, locals)
34+
for key, value in ann.items()
35+
}
36+
37+
kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
38+
"s" if kwonly_given != 1 else ""))
39+
40+
struct.pack_into(
41+
"".join(_formats),
42+
self.shm.buf,
43+
self._offset_data_start,
44+
*(v.encode(_enc) if isinstance(v, str) else v for v in sequence)
45+
)
46+
47+
for name in dirs if entries is None else zip(dirs, entries): ...
48+
49+
if (root or drv) if n == 0 else cf(abs_parts[:n]) != cf(to_abs_parts): ...
50+
51+
x, y = thing, name if isinstance(name, str) else None
52+
53+
print('%*d%s ' % (offset_width, start, ':' if start in labels else '.'),
54+
end=' '*(level-1))
55+
56+
Signals = {
57+
C: tuple(C.getcontext().flags.keys()) if C else None,
58+
P: tuple(P.getcontext().flags.keys())
59+
}
60+
61+
# FIXME: Generated AST is not similar to CPython Parser
62+
chunks = [b'z' if cond1 else b'y' if cond2 else x for word in words]

tests/parser/if2.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,3 @@
3030
if x.shape == () or y.shape == () or x.shape[1] != y.shape[0]:
3131
assert arr[()] is ArrayLike
3232
pass
33-
34-
# Ternary operator (Conditional expressions)
35-
b = 6 if a == 2 else 8
36-
37-
'true' if True else 'false'
38-
39-
result = x if not (a > b) else y
40-
41-
print(a,"is greater") if (a > b) else print(b,"is Greater")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "ast_new-conditional_expr1-07ccb9e",
3+
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
4+
"infile": "tests/parser/conditional_expr1.py",
5+
"infile_hash": "5846a6c58bf238c4276f733b5fc9876a948ed63558444210d9f79c55",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": "ast_new-conditional_expr1-07ccb9e.stdout",
9+
"stdout_hash": "251284c9d204fc5bea30416beb5eae7c591f6fdc2fa050307a249157",
10+
"stderr": null,
11+
"stderr_hash": null,
12+
"returncode": 0
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(Module [(Assign [(Name b Store)] (IfExp (Compare (Name a Load) Eq [(ConstantInt 2 ())]) (ConstantInt 6 ()) (ConstantInt 8 ())) ()) (Expr (IfExp (ConstantBool .true. ()) (ConstantStr "true" ()) (ConstantStr "false" ()))) (Assign [(Name result Store)] (IfExp (UnaryOp Not (Compare (Name a Load) Gt [(Name b Load)])) (Name x Load) (Name y Load)) ()) (Expr (IfExp (Compare (Name a Load) Gt [(Name b Load)]) (Call (Name print Load) [(Name a Load) (ConstantStr "is greater" ())] []) (Call (Name print Load) [(Name b Load) (ConstantStr "is Greater" ())] []))) (Expr (Call (Name x Load) [(IfExp (ConstantBool .true. ()) (ConstantStr "True" ()) (ConstantStr "False" ()))] [])) (Assign [(Name test Store)] (IfExp (Name a Load) (Call (Name x Load) [(ConstantInt 123 ())] []) (Attribute (Name b Load) c Load)) ()) (Assign [(Name res Store)] (Call (Attribute (Name test Load) x Load) [(IfExp (Compare (Name z Load) Is [(ConstantNone ())]) (ConstantStr "" ()) (Name var Load))] []) ()) (Assign [(Name size Store)] (BinOp (Call (Name sum Load) [(GeneratorExp (IfExp (Compare (Call (Name ord Load) [(Name c Load)] []) Gt [(ConstantInt 65535 ())]) (ConstantInt 2 ()) (ConstantInt 1 ())) [((Name c Store) (Name init Load) [] 0)])] []) Add (ConstantInt 1 ())) ()) (Expr (Tuple [(Name x Load) (Name y Load) (IfExp (Name a Load) (ConstantStr "a" ()) (ConstantStr "b" ()))] Load)) (AugAssign (Name x Store) Add (IfExp (Name true Load) (UnaryOp USub (ConstantInt 1 ())) (ConstantInt 1 ()))) (Expr (Call (IfExp (Compare (Name tktype Load) In [(Name whentrue Load)]) (Attribute (Name self Load) assertTrue Load) (Attribute (Name self Load) assertFalse Load)) [(Call (Name func Load) [] [])] [])) (Assign [(Name x Store)] (SetComp (IfExp (Compare (Call (Name len Load) [(Attribute (Name f Load) parts Load)] []) Gt [(ConstantInt 1 ())]) (Subscript (Attribute (Name f Load) parts Load) (ConstantInt 0 ()) Load) (Attribute (Call (Attribute (Name f Load) with_suffix Load) [(ConstantStr "" ())] []) name Load)) [((Name f Store) (Call (Name always_iterable Load) [(Attribute (Name dist Load) files Load)] []) [(Compare (Attribute (Name f Load) suffix Load) Eq [(ConstantStr ".py" ())])] 0)]) ()) (Assign [(Name return_value Store)] (DictComp (Name key Load) (IfExp (UnaryOp Not (Call (Name isinstance Load) [(Name value Load) (Name str Load)] [])) (Name value Load) (Call (Name eval Load) [(Name value Load) (Name globals Load) (Name locals Load)] [])) [((Tuple [(Name key Store) (Name value Store)] Store) (Call (Attribute (Name ann Load) items Load) [] []) [] 0)]) ()) (Assign [(Name kwonly_sig Store)] (BinOp (Name msg Load) Mod (Tuple [(IfExp (Compare (Name given Load) NotEq [(ConstantInt 1 ())]) (ConstantStr "s" ()) (ConstantStr "" ())) (Name kwonly_given Load) (IfExp (Compare (Name kwonly_given Load) NotEq [(ConstantInt 1 ())]) (ConstantStr "s" ()) (ConstantStr "" ()))] Load)) ()) (Expr (Call (Attribute (Name struct Load) pack_into Load) [(Call (Attribute (ConstantStr "" ()) join Load) [(Name _formats Load)] []) (Attribute (Attribute (Name self Load) shm Load) buf Load) (Attribute (Name self Load) _offset_data_start Load) (Starred (GeneratorExp (IfExp (Call (Name isinstance Load) [(Name v Load) (Name str Load)] []) (Call (Attribute (Name v Load) encode Load) [(Name _enc Load)] []) (Name v Load)) [((Name v Store) (Name sequence Load) [] 0)]) Load)] [])) (For (Name name Store) (IfExp (Compare (Name entries Load) Is [(ConstantNone ())]) (Name dirs Load) (Call (Name zip Load) [(Name dirs Load) (Name entries Load)] [])) [(Expr (ConstantEllipsis ()))] [] ()) (If (IfExp (Compare (Name n Load) Eq [(ConstantInt 0 ())]) (BoolOp Or [(Name root Load) (Name drv Load)]) (Compare (Call (Name cf Load) [(Subscript (Name abs_parts Load) (Slice () (Name n Load) ()) Load)] []) NotEq [(Call (Name cf Load) [(Name to_abs_parts Load)] [])])) [(Expr (ConstantEllipsis ()))] []) (Assign [(Tuple [(Name x Store) (Name y Store)] Store)] (Tuple [(Name thing Load) (IfExp (Call (Name isinstance Load) [(Name name Load) (Name str Load)] []) (Name name Load) (ConstantNone ()))] Load) ()) (Expr (Call (Name print Load) [(BinOp (ConstantStr "%*d%s " ()) Mod (Tuple [(Name offset_width Load) (Name start Load) (IfExp (Compare (Name start Load) In [(Name labels Load)]) (ConstantStr ":" ()) (ConstantStr "." ()))] Load))] [(end (BinOp (ConstantStr " " ()) Mult (BinOp (Name level Load) Sub (ConstantInt 1 ()))))])) (Assign [(Name Signals Store)] (Dict [(Name C Load) (Name P Load)] [(IfExp (Name C Load) (Call (Name tuple Load) [(Call (Attribute (Attribute (Call (Attribute (Name C Load) getcontext Load) [] []) flags Load) keys Load) [] [])] []) (ConstantNone ())) (Call (Name tuple Load) [(Call (Attribute (Attribute (Call (Attribute (Name P Load) getcontext Load) [] []) flags Load) keys Load) [] [])] [])]) ()) (Assign [(Name chunks Store)] (ListComp (IfExp (Name cond2 Load) (IfExp (Name cond1 Load) (ConstantBytes "b'z'" ()) (ConstantBytes "b'y'" ())) (Name x Load)) [((Name word Store) (Name words Load) [] 0)]) ())] [])

tests/reference/ast_new-if2-c3b6022.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "ast_new-if2-c3b6022",
33
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
44
"infile": "tests/parser/if2.py",
5-
"infile_hash": "519806e6b51d47084255a725210dcae7d7451b8aa5e9b7e684941831",
5+
"infile_hash": "09292d52335f9d0c6e9011355eb63afc6c32e6130ce7e03ff913cec9",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "ast_new-if2-c3b6022.stdout",
9-
"stdout_hash": "acb8fee9254dc2a1fff51fd70b7350768c8696f9740a5abf5631cf78",
9+
"stdout_hash": "78ba145bd97727929e0eb9bc9a23e9a47f9fcfa53195f39a91b78097",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(Module [(If (Compare (Call (Name type Load) [(Name x Load)] []) Is [(Name int Load)]) [(Pass)] []) (If (Compare (Compare (BinOp (BinOp (BinOp (ConstantInt 2 ()) Add (ConstantInt 3 ())) Div (ConstantInt 2 ())) Sub (ConstantInt 1 ())) Is [(ConstantInt 5 ())]) Is [(ConstantBool .true. ())]) [(Pass)] []) (If (Compare (Name x Load) IsNot [(Call (Name type Load) [(Name float Load)] [])]) [(Pass)] []) (If (Compare (Name x Load) IsNot [(Call (Name type Load) [(Name int Load)] [])]) [(Pass)] []) (Assign [(Name a Store)] (List [(ConstantInt 1 ()) (ConstantInt 2 ()) (ConstantInt 3 ())] Load) ()) (If (Compare (Name a Load) NotIn [(List [(ConstantInt 1 ()) (ConstantInt 2 ())] Load)]) [(Pass)] []) (If (Compare (Compare (Name a Load) NotIn [(List [(ConstantInt 1 ()) (ConstantInt 2 ())] Load)]) NotIn [(List [(ConstantBool .true. ()) (ConstantBool .false. ())] Load)]) [(Pass)] []) (If (Compare (Name field Load) In [(List [(ConstantStr "vararg" ()) (ConstantStr "kwarg" ())] Load)]) [(If (Compare (Name value Load) Is [(ConstantNone ())]) [(Pass)] [])] []) (If (Compare (Name a Load) In [(Name list1 Load)]) [(Pass)] []) (If (BoolOp Or [(BoolOp Or [(Compare (Attribute (Name x Load) shape Load) Eq [(Tuple [] Load)]) (Compare (Attribute (Name y Load) shape Load) Eq [(Tuple [] Load)])]) (Compare (Subscript (Attribute (Name x Load) shape Load) (ConstantInt 1 ()) Load) NotEq [(Subscript (Attribute (Name y Load) shape Load) (ConstantInt 0 ()) Load)])]) [(Assert (Compare (Subscript (Name arr Load) (Tuple [] Load) Load) Is [(Name ArrayLike Load)]) ()) (Pass)] []) (Assign [(Name b Store)] (IfExp (Compare (Name a Load) Eq [(ConstantInt 2 ())]) (ConstantInt 6 ()) (ConstantInt 8 ())) ()) (Expr (IfExp (ConstantBool .true. ()) (ConstantStr "true" ()) (ConstantStr "false" ()))) (Assign [(Name result Store)] (IfExp (UnaryOp Not (Compare (Name a Load) Gt [(Name b Load)])) (Name x Load) (Name y Load)) ()) (Expr (IfExp (Compare (Name a Load) Gt [(Name b Load)]) (Call (Name print Load) [(Name a Load) (ConstantStr "is greater" ())] []) (Call (Name print Load) [(Name b Load) (ConstantStr "is Greater" ())] [])))] [])
1+
(Module [(If (Compare (Call (Name type Load) [(Name x Load)] []) Is [(Name int Load)]) [(Pass)] []) (If (Compare (Compare (BinOp (BinOp (BinOp (ConstantInt 2 ()) Add (ConstantInt 3 ())) Div (ConstantInt 2 ())) Sub (ConstantInt 1 ())) Is [(ConstantInt 5 ())]) Is [(ConstantBool .true. ())]) [(Pass)] []) (If (Compare (Name x Load) IsNot [(Call (Name type Load) [(Name float Load)] [])]) [(Pass)] []) (If (Compare (Name x Load) IsNot [(Call (Name type Load) [(Name int Load)] [])]) [(Pass)] []) (Assign [(Name a Store)] (List [(ConstantInt 1 ()) (ConstantInt 2 ()) (ConstantInt 3 ())] Load) ()) (If (Compare (Name a Load) NotIn [(List [(ConstantInt 1 ()) (ConstantInt 2 ())] Load)]) [(Pass)] []) (If (Compare (Compare (Name a Load) NotIn [(List [(ConstantInt 1 ()) (ConstantInt 2 ())] Load)]) NotIn [(List [(ConstantBool .true. ()) (ConstantBool .false. ())] Load)]) [(Pass)] []) (If (Compare (Name field Load) In [(List [(ConstantStr "vararg" ()) (ConstantStr "kwarg" ())] Load)]) [(If (Compare (Name value Load) Is [(ConstantNone ())]) [(Pass)] [])] []) (If (Compare (Name a Load) In [(Name list1 Load)]) [(Pass)] []) (If (BoolOp Or [(BoolOp Or [(Compare (Attribute (Name x Load) shape Load) Eq [(Tuple [] Load)]) (Compare (Attribute (Name y Load) shape Load) Eq [(Tuple [] Load)])]) (Compare (Subscript (Attribute (Name x Load) shape Load) (ConstantInt 1 ()) Load) NotEq [(Subscript (Attribute (Name y Load) shape Load) (ConstantInt 0 ()) Load)])]) [(Assert (Compare (Subscript (Name arr Load) (Tuple [] Load) Load) Is [(Name ArrayLike Load)]) ()) (Pass)] [])] [])

tests/tests.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,10 @@ ast_new = true
464464
filename = "parser/comprehension1.py"
465465
ast_new = true
466466

467+
[[test]]
468+
filename = "parser/conditional_expr1.py"
469+
ast_new = true
470+
467471
[[test]]
468472
filename = "parser/try1.py"
469473
ast_new = true

0 commit comments

Comments
 (0)