some refactor

This commit is contained in:
blueloveTH 2024-02-25 12:39:09 +08:00
parent 4af7d5c873
commit 51a3c93f69
2 changed files with 19 additions and 20 deletions

View File

@ -69,23 +69,7 @@ class Compiler {
return expr; return expr;
} }
template<typename T> void consume_comp(unique_ptr_128<CompExpr> ce, Expr_ expr);
void _consume_comp(Expr_ expr){
static_assert(std::is_base_of<CompExpr, T>::value);
unique_ptr_128<CompExpr> ce = make_expr<T>();
ce->expr = std::move(expr);
ce->vars = EXPR_VARS();
consume(TK("in"));
parse_expression(PREC_TERNARY + 1);
ce->iter = ctx()->s_expr.popx();
match_newlines_repl();
if(match(TK("if"))){
parse_expression(PREC_TERNARY + 1);
ce->cond = ctx()->s_expr.popx();
}
ctx()->s_expr.push(std::move(ce));
match_newlines_repl();
}
void exprLiteral(); void exprLiteral();
void exprLong(); void exprLong();

View File

@ -311,6 +311,21 @@ namespace pkpy{
ctx()->s_expr.push(std::move(g)); ctx()->s_expr.push(std::move(g));
} }
void Compiler::consume_comp(unique_ptr_128<CompExpr> ce, Expr_ expr){
ce->expr = std::move(expr);
ce->vars = EXPR_VARS();
consume(TK("in"));
parse_expression(PREC_TERNARY + 1);
ce->iter = ctx()->s_expr.popx();
match_newlines_repl();
if(match(TK("if"))){
parse_expression(PREC_TERNARY + 1);
ce->cond = ctx()->s_expr.popx();
}
ctx()->s_expr.push(std::move(ce));
match_newlines_repl();
}
void Compiler::exprList() { void Compiler::exprList() {
int line = prev().line; int line = prev().line;
Expr_vector items; Expr_vector items;
@ -321,7 +336,7 @@ namespace pkpy{
items.push_back(ctx()->s_expr.popx()); items.push_back(ctx()->s_expr.popx());
match_newlines_repl(); match_newlines_repl();
if(items.size()==1 && match(TK("for"))){ if(items.size()==1 && match(TK("for"))){
_consume_comp<ListCompExpr>(std::move(items[0])); consume_comp(make_expr<ListCompExpr>(), std::move(items[0]));
consume(TK("]")); consume(TK("]"));
return; return;
} }
@ -361,8 +376,8 @@ namespace pkpy{
} }
match_newlines_repl(); match_newlines_repl();
if(items.size()==1 && match(TK("for"))){ if(items.size()==1 && match(TK("for"))){
if(parsing_dict) _consume_comp<DictCompExpr>(std::move(items[0])); if(parsing_dict) consume_comp(make_expr<DictCompExpr>(), std::move(items[0]));
else _consume_comp<SetCompExpr>(std::move(items[0])); else consume_comp(make_expr<SetCompExpr>(), std::move(items[0]));
consume(TK("}")); consume(TK("}"));
return; return;
} }