Skip to content

Commit

Permalink
Merge pull request lcompilers#773 from Thirumalai-Shaktivel/boolop
Browse files Browse the repository at this point in the history
[New] Handle Boolop operands similar to CPython AST
  • Loading branch information
Thirumalai-Shaktivel committed Jul 17, 2022
2 parents afc4804 + b979843 commit 87639da
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/lpython/parser/semantics.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ int dot_count = 0;
static inline char *extract_type_comment(Allocator &al, LFortran::Str &s) {
std::string str = s.str();
std::string kw{"type:"};

str.erase(str.begin()); // removes "#" at the beginning
str.erase(0, str.find_first_not_of(' ')); // trim left spaces

Expand Down Expand Up @@ -427,16 +427,31 @@ static inline Args *FUNC_ARGS(Allocator &al, Location &l,
#define WHILE_02(e, stmts, orelse, l) make_While_t(p.m_a, l, \
EXPR(e), STMTS(stmts), stmts.size(), STMTS(orelse), orelse.size())

Vec<ast_t*> MERGE_EXPR(Allocator &al, ast_t *x, ast_t *y) {
Vec<ast_t*> v;
v.reserve(al, 2);
v.push_back(al, x);
v.push_back(al, y);
return v;
static inline ast_t* BOOLOP_01(Allocator &al, Location &loc,
boolopType op, ast_t *x, ast_t *y) {
expr_t* x1 = EXPR(x);
expr_t* y1 = EXPR(y);
Vec<expr_t*> v;
v.reserve(al, 4);
if (is_a<BoolOp_t>(*x1)) {
BoolOp_t* tmp = down_cast<BoolOp_t>(x1);
if (op == tmp->m_op) {
for(size_t i = 0; i < tmp->n_values; i++) {
v.push_back(al, tmp->m_values[i]);
}
v.push_back(al, y1);
} else {
v.push_back(al, x1);
v.push_back(al, y1);
}
} else {
v.push_back(al, x1);
v.push_back(al, y1);
}
return make_BoolOp_t(al, loc, op, v.p, v.n);
}

#define BOOLOP(x, op, y, l) make_BoolOp_t(p.m_a, l, \
boolopType::op, EXPRS(MERGE_EXPR(p.m_a, x, y)), 2)
#define BOOLOP(x, op, y, l) BOOLOP_01(p.m_a, l, op, x, y)
#define BINOP(x, op, y, l) make_BinOp_t(p.m_a, l, \
EXPR(x), operatorType::op, EXPR(y))
#define UNARY(x, op, l) make_UnaryOp_t(p.m_a, l, unaryopType::op, EXPR(x))
Expand Down
10 changes: 10 additions & 0 deletions tests/parser/statements2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
a and b
a and b or c
a or b
a or b and c

(a and b and c or (x or (y and (z or i)) or j))
(a and (b and (c or d)) and (e and f) or x) and y

# # TODO: Make this work similar to Old Parser
# (a or b and c) or z
13 changes: 13 additions & 0 deletions tests/reference/ast_new-statements2-c4cdc5f.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "ast_new-statements2-c4cdc5f",
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
"infile": "tests/parser/statements2.py",
"infile_hash": "a20fee19d7b8e6f8689f6badc2726fd618524af0aba84a7d0d0e5af5",
"outfile": null,
"outfile_hash": null,
"stdout": "ast_new-statements2-c4cdc5f.stdout",
"stdout_hash": "8b8d1514ec03d6081c0e716170ff41615cb00f9745a71e153f25179c",
"stderr": null,
"stderr_hash": null,
"returncode": 0
}
1 change: 1 addition & 0 deletions tests/reference/ast_new-statements2-c4cdc5f.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(Module [(Expr (BoolOp And [(Name a Load) (Name b Load)])) (Expr (BoolOp Or [(BoolOp And [(Name a Load) (Name b Load)]) (Name c Load)])) (Expr (BoolOp Or [(Name a Load) (Name b Load)])) (Expr (BoolOp Or [(Name a Load) (BoolOp And [(Name b Load) (Name c Load)])])) (Expr (BoolOp Or [(BoolOp And [(Name a Load) (Name b Load) (Name c Load)]) (BoolOp Or [(Name x Load) (BoolOp And [(Name y Load) (BoolOp Or [(Name z Load) (Name i Load)])]) (Name j Load)])])) (Expr (BoolOp And [(BoolOp Or [(BoolOp And [(Name a Load) (BoolOp And [(Name b Load) (BoolOp Or [(Name c Load) (Name d Load)])]) (BoolOp And [(Name e Load) (Name f Load)])]) (Name x Load)]) (Name y Load)]))] [])
4 changes: 4 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ ast_new = true
filename = "parser/statements1.py"
ast_new = true

[[test]]
filename = "parser/statements2.py"
ast_new = true

[[test]]
filename = "parser/if1.py"
ast_new = true
Expand Down

0 comments on commit 87639da

Please sign in to comment.