Skip to content

Commit

Permalink
Handle Boolop operands similar to CPython AST
Browse files Browse the repository at this point in the history
  • Loading branch information
Thirumalai-Shaktivel committed Jul 16, 2022
1 parent afc4804 commit 7b7f8c9
Showing 1 changed file with 24 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

0 comments on commit 7b7f8c9

Please sign in to comment.