mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 19:40:18 +00:00
some refactor
This commit is contained in:
parent
4af7d5c873
commit
51a3c93f69
@ -69,23 +69,7 @@ class Compiler {
|
||||
return expr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
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 consume_comp(unique_ptr_128<CompExpr> ce, Expr_ expr);
|
||||
|
||||
void exprLiteral();
|
||||
void exprLong();
|
||||
|
@ -311,6 +311,21 @@ namespace pkpy{
|
||||
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() {
|
||||
int line = prev().line;
|
||||
Expr_vector items;
|
||||
@ -321,7 +336,7 @@ namespace pkpy{
|
||||
items.push_back(ctx()->s_expr.popx());
|
||||
match_newlines_repl();
|
||||
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("]"));
|
||||
return;
|
||||
}
|
||||
@ -361,8 +376,8 @@ namespace pkpy{
|
||||
}
|
||||
match_newlines_repl();
|
||||
if(items.size()==1 && match(TK("for"))){
|
||||
if(parsing_dict) _consume_comp<DictCompExpr>(std::move(items[0]));
|
||||
else _consume_comp<SetCompExpr>(std::move(items[0]));
|
||||
if(parsing_dict) consume_comp(make_expr<DictCompExpr>(), std::move(items[0]));
|
||||
else consume_comp(make_expr<SetCompExpr>(), std::move(items[0]));
|
||||
consume(TK("}"));
|
||||
return;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user