mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
update parse_expression
This commit is contained in:
parent
068ec948ec
commit
8e3c335449
@ -781,7 +781,9 @@ static void BinaryExpr__emit_(Expr* self_, Ctx* ctx) {
|
|||||||
case TK_DIV: arg = __truediv__ | (__rtruediv__ << 8); break;
|
case TK_DIV: arg = __truediv__ | (__rtruediv__ << 8); break;
|
||||||
case TK_FLOORDIV: arg = __floordiv__ | (__rfloordiv__ << 8); break;
|
case TK_FLOORDIV: arg = __floordiv__ | (__rfloordiv__ << 8); break;
|
||||||
case TK_MOD: arg = __mod__ | (__rmod__ << 8); break;
|
case TK_MOD: arg = __mod__ | (__rmod__ << 8); break;
|
||||||
|
// right-associated
|
||||||
case TK_POW: arg = __pow__ | (__rpow__ << 8); break;
|
case TK_POW: arg = __pow__ | (__rpow__ << 8); break;
|
||||||
|
case TK_DECORATOR: arg = __matmul__; break;
|
||||||
|
|
||||||
case TK_LT: arg = __lt__ | (__gt__ << 8); break;
|
case TK_LT: arg = __lt__ | (__gt__ << 8); break;
|
||||||
case TK_LE: arg = __le__ | (__ge__ << 8); break;
|
case TK_LE: arg = __le__ | (__ge__ << 8); break;
|
||||||
@ -812,7 +814,6 @@ static void BinaryExpr__emit_(Expr* self_, Ctx* ctx) {
|
|||||||
case TK_AND: arg = __and__; break;
|
case TK_AND: arg = __and__; break;
|
||||||
case TK_OR: arg = __or__; break;
|
case TK_OR: arg = __or__; break;
|
||||||
case TK_XOR: arg = __xor__; break;
|
case TK_XOR: arg = __xor__; break;
|
||||||
case TK_DECORATOR: arg = __matmul__; break;
|
|
||||||
default: assert(false);
|
default: assert(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1395,7 +1396,7 @@ static Error* parse_expression(Compiler* self, int precedence, bool allow_slice)
|
|||||||
advance();
|
advance();
|
||||||
Error* err;
|
Error* err;
|
||||||
check(prefix(self));
|
check(prefix(self));
|
||||||
while(rules[curr()->type].precedence >= precedence &&
|
while(rules[curr()->type].precedence > precedence &&
|
||||||
(allow_slice || curr()->type != TK_COLON)) {
|
(allow_slice || curr()->type != TK_COLON)) {
|
||||||
TokenIndex op = curr()->type;
|
TokenIndex op = curr()->type;
|
||||||
advance();
|
advance();
|
||||||
@ -1410,14 +1411,14 @@ static Error* parse_expression(Compiler* self, int precedence, bool allow_slice)
|
|||||||
|
|
||||||
static Error* EXPR_TUPLE_ALLOW_SLICE(Compiler* self, bool allow_slice) {
|
static Error* EXPR_TUPLE_ALLOW_SLICE(Compiler* self, bool allow_slice) {
|
||||||
Error* err;
|
Error* err;
|
||||||
check(parse_expression(self, PREC_LOWEST + 1, allow_slice));
|
check(parse_expression(self, PREC_LOWEST, allow_slice));
|
||||||
if(!match(TK_COMMA)) return NULL;
|
if(!match(TK_COMMA)) return NULL;
|
||||||
// tuple expression // (a, )
|
// tuple expression // (a, )
|
||||||
int count = 1;
|
int count = 1;
|
||||||
do {
|
do {
|
||||||
if(curr()->brackets_level) match_newlines();
|
if(curr()->brackets_level) match_newlines();
|
||||||
if(!is_expression(self, allow_slice)) break;
|
if(!is_expression(self, allow_slice)) break;
|
||||||
check(parse_expression(self, PREC_LOWEST + 1, allow_slice));
|
check(parse_expression(self, PREC_LOWEST, allow_slice));
|
||||||
count += 1;
|
count += 1;
|
||||||
if(curr()->brackets_level) match_newlines();
|
if(curr()->brackets_level) match_newlines();
|
||||||
} while(match(TK_COMMA));
|
} while(match(TK_COMMA));
|
||||||
@ -1432,7 +1433,7 @@ static Error* EXPR_TUPLE_ALLOW_SLICE(Compiler* self, bool allow_slice) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a simple expression.
|
/// Parse a simple expression.
|
||||||
static Error* EXPR(Compiler* self) { return parse_expression(self, PREC_LOWEST + 1, false); }
|
static Error* EXPR(Compiler* self) { return parse_expression(self, PREC_LOWEST, false); }
|
||||||
|
|
||||||
/// Parse a simple expression or a tuple of expressions.
|
/// Parse a simple expression or a tuple of expressions.
|
||||||
static Error* EXPR_TUPLE(Compiler* self) { return EXPR_TUPLE_ALLOW_SLICE(self, false); }
|
static Error* EXPR_TUPLE(Compiler* self) { return EXPR_TUPLE_ALLOW_SLICE(self, false); }
|
||||||
@ -1602,7 +1603,7 @@ static Error* exprLambda(Compiler* self) {
|
|||||||
consume(TK_COLON);
|
consume(TK_COLON);
|
||||||
}
|
}
|
||||||
// https://github.com/pocketpy/pocketpy/issues/37
|
// https://github.com/pocketpy/pocketpy/issues/37
|
||||||
check(parse_expression(self, PREC_LAMBDA + 1, false));
|
check(parse_expression(self, PREC_LAMBDA, false));
|
||||||
Ctx__s_emit_top(ctx());
|
Ctx__s_emit_top(ctx());
|
||||||
Ctx__emit_(ctx(), OP_RETURN_VALUE, BC_NOARG, BC_KEEPLINE);
|
Ctx__emit_(ctx(), OP_RETURN_VALUE, BC_NOARG, BC_KEEPLINE);
|
||||||
check(pop_context(self));
|
check(pop_context(self));
|
||||||
@ -1614,7 +1615,7 @@ static Error* exprLambda(Compiler* self) {
|
|||||||
static Error* exprOr(Compiler* self) {
|
static Error* exprOr(Compiler* self) {
|
||||||
Error* err;
|
Error* err;
|
||||||
int line = prev()->line;
|
int line = prev()->line;
|
||||||
check(parse_expression(self, PREC_LOGICAL_OR + 1, false));
|
check(parse_expression(self, PREC_LOGICAL_OR, false));
|
||||||
LogicBinaryExpr* e = LogicBinaryExpr__new(line, OP_JUMP_IF_TRUE_OR_POP);
|
LogicBinaryExpr* e = LogicBinaryExpr__new(line, OP_JUMP_IF_TRUE_OR_POP);
|
||||||
e->rhs = Ctx__s_popx(ctx());
|
e->rhs = Ctx__s_popx(ctx());
|
||||||
e->lhs = Ctx__s_popx(ctx());
|
e->lhs = Ctx__s_popx(ctx());
|
||||||
@ -1625,7 +1626,7 @@ static Error* exprOr(Compiler* self) {
|
|||||||
static Error* exprAnd(Compiler* self) {
|
static Error* exprAnd(Compiler* self) {
|
||||||
Error* err;
|
Error* err;
|
||||||
int line = prev()->line;
|
int line = prev()->line;
|
||||||
check(parse_expression(self, PREC_LOGICAL_AND + 1, false));
|
check(parse_expression(self, PREC_LOGICAL_AND, false));
|
||||||
LogicBinaryExpr* e = LogicBinaryExpr__new(line, OP_JUMP_IF_FALSE_OR_POP);
|
LogicBinaryExpr* e = LogicBinaryExpr__new(line, OP_JUMP_IF_FALSE_OR_POP);
|
||||||
e->rhs = Ctx__s_popx(ctx());
|
e->rhs = Ctx__s_popx(ctx());
|
||||||
e->lhs = Ctx__s_popx(ctx());
|
e->lhs = Ctx__s_popx(ctx());
|
||||||
@ -1637,9 +1638,9 @@ static Error* exprTernary(Compiler* self) {
|
|||||||
// [true_expr]
|
// [true_expr]
|
||||||
Error* err;
|
Error* err;
|
||||||
int line = prev()->line;
|
int line = prev()->line;
|
||||||
check(parse_expression(self, PREC_TERNARY + 1, false)); // [true_expr, cond]
|
check(parse_expression(self, PREC_TERNARY, false)); // [true_expr, cond]
|
||||||
consume(TK_ELSE);
|
consume(TK_ELSE);
|
||||||
check(parse_expression(self, PREC_TERNARY + 1, false)); // [true_expr, cond, false_expr]
|
check(parse_expression(self, PREC_TERNARY, false)); // [true_expr, cond, false_expr]
|
||||||
TernaryExpr* e = TernaryExpr__new(line);
|
TernaryExpr* e = TernaryExpr__new(line);
|
||||||
e->false_expr = Ctx__s_popx(ctx());
|
e->false_expr = Ctx__s_popx(ctx());
|
||||||
e->cond = Ctx__s_popx(ctx());
|
e->cond = Ctx__s_popx(ctx());
|
||||||
@ -1652,7 +1653,7 @@ static Error* exprBinaryOp(Compiler* self) {
|
|||||||
Error* err;
|
Error* err;
|
||||||
int line = prev()->line;
|
int line = prev()->line;
|
||||||
TokenIndex op = prev()->type;
|
TokenIndex op = prev()->type;
|
||||||
check(parse_expression(self, rules[op].precedence + 1, false));
|
check(parse_expression(self, rules[op].precedence, false));
|
||||||
BinaryExpr* e = BinaryExpr__new(line, op, false);
|
BinaryExpr* e = BinaryExpr__new(line, op, false);
|
||||||
if(op == TK_IN || op == TK_NOT_IN) {
|
if(op == TK_IN || op == TK_NOT_IN) {
|
||||||
e->lhs = Ctx__s_popx(ctx());
|
e->lhs = Ctx__s_popx(ctx());
|
||||||
@ -1668,7 +1669,7 @@ static Error* exprBinaryOp(Compiler* self) {
|
|||||||
static Error* exprNot(Compiler* self) {
|
static Error* exprNot(Compiler* self) {
|
||||||
Error* err;
|
Error* err;
|
||||||
int line = prev()->line;
|
int line = prev()->line;
|
||||||
check(parse_expression(self, PREC_LOGICAL_NOT + 1, false));
|
check(parse_expression(self, PREC_LOGICAL_NOT, false));
|
||||||
UnaryExpr* e = UnaryExpr__new(line, Ctx__s_popx(ctx()), OP_UNARY_NOT);
|
UnaryExpr* e = UnaryExpr__new(line, Ctx__s_popx(ctx()), OP_UNARY_NOT);
|
||||||
Ctx__s_push(ctx(), (Expr*)e);
|
Ctx__s_push(ctx(), (Expr*)e);
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -1678,7 +1679,7 @@ static Error* exprUnaryOp(Compiler* self) {
|
|||||||
Error* err;
|
Error* err;
|
||||||
int line = prev()->line;
|
int line = prev()->line;
|
||||||
TokenIndex op = prev()->type;
|
TokenIndex op = prev()->type;
|
||||||
check(parse_expression(self, PREC_UNARY + 1, false));
|
check(parse_expression(self, PREC_UNARY, false));
|
||||||
Expr* e = Ctx__s_popx(ctx());
|
Expr* e = Ctx__s_popx(ctx());
|
||||||
switch(op) {
|
switch(op) {
|
||||||
case TK_SUB: {
|
case TK_SUB: {
|
||||||
@ -1749,10 +1750,10 @@ static Error* consume_comp(Compiler* self, Opcode op0, Opcode op1) {
|
|||||||
bool has_cond = false;
|
bool has_cond = false;
|
||||||
check(EXPR_VARS(self)); // [expr, vars]
|
check(EXPR_VARS(self)); // [expr, vars]
|
||||||
consume(TK_IN);
|
consume(TK_IN);
|
||||||
check(parse_expression(self, PREC_TERNARY + 1, false)); // [expr, vars, iter]
|
check(parse_expression(self, PREC_TERNARY, false)); // [expr, vars, iter]
|
||||||
match_newlines();
|
match_newlines();
|
||||||
if(match(TK_IF)) {
|
if(match(TK_IF)) {
|
||||||
check(parse_expression(self, PREC_TERNARY + 1, false)); // [expr, vars, iter, cond]
|
check(parse_expression(self, PREC_TERNARY, false)); // [expr, vars, iter, cond]
|
||||||
has_cond = true;
|
has_cond = true;
|
||||||
}
|
}
|
||||||
CompExpr* ce = CompExpr__new(line, op0, op1);
|
CompExpr* ce = CompExpr__new(line, op0, op1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user