Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New] Handle Boolop operands similar to CPython AST #773

Merged
merged 2 commits into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(a or b) or c

The problem here is that, as parenthesis information is not used or stored by the macros, it is difficult to handle the above example:
OLD Parser AST:

$ lpython --show-ast examples/expr2.py --tree
└-Module
  |-body=↧
  | └-Expr
  |   └-value=BoolOp
  |     |-boolopType=Or
  |     └-values=↧
  |       |-BoolOp
  |       | |-boolopType=Or
  |       | └-values=↧
  |       |   |-Name
  |       |   | |-id=a
  |       |   | └-expr_contextType=Load
  |       |   └-Name
  |       |     |-id=b
  |       |     └-expr_contextType=Load
  |       └-Name
  |         |-id=c
  |         └-expr_contextType=Load
  └-type_ignores=↧

NEW Parser AST (this PR):

$ lpython --show-ast examples/expr2.py --tree --new-parser
└-Module
  |-body=↧
  | └-Expr
  |   └-value=BoolOp
  |     |-boolopType=Or
  |     └-values=↧
  |       |-Name
  |       | |-id=a
  |       | └-expr_contextType=Load
  |       |-Name
  |       | |-id=b
  |       | └-expr_contextType=Load
  |       └-Name
  |         |-id=c
  |         └-expr_contextType=Load
  └-type_ignores=↧

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