blueloveTH 2 лет назад
Родитель
Сommit
51a3c93f69
2 измененных файлов с 19 добавлено и 20 удалено
  1. 1 17
      include/pocketpy/compiler.h
  2. 18 3
      src/compiler.cpp

+ 1 - 17
include/pocketpy/compiler.h

@@ -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();

+ 18 - 3
src/compiler.cpp

@@ -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;
             }