txlyre 23 hours ago
parent
commit
4dd508c975
1 changed files with 11 additions and 1 deletions
  1. 11 1
      qic.c

+ 11 - 1
qic.c

@@ -1184,6 +1184,7 @@ node_t *parse_primary(list_t *tokens, size_t *pos) {
     list_t *a = parse_sequence(tokens, pos, T_RSB);
 
     node_t *loop = NULL;
+    node_t *cond = NULL;
 
     if (MATCH(FOR)) {
       if (MATCH(LSB)) {
@@ -1210,12 +1211,15 @@ node_t *parse_primary(list_t *tokens, size_t *pos) {
 
         loop = NODE2t(FOROF, parse_expr(tokens, pos), NODEL(LIST, a), t);
       }
+
+      if (MATCH(IF))
+        cond = parse_expr(tokens, pos);
     }
 
     EXPECT(RSB, "]");
 
     if (loop)
-      return NODE1(LISTGEN, loop);
+      return NODE2(LISTGEN, loop, cond);
 
     return NODEL(LIST, a);
   } else if (MATCH(LCB)) {
@@ -3696,6 +3700,12 @@ void compile_node(buffer_t *gbuf, buffer_t *buf, list_t *ctx, table_t *ltab, sta
       buffer_t *bbuf = buffer_new();
 
       for (size_t i = 0; i < node->a->b->l->length; i++) {
+        if (node->b) {
+          buffer_fmt(bbuf, "if (_qi_truthy(state, ");
+          compile_node(gbuf, bbuf, ctx, ltab, lstk, sstk, lbl, node->b);
+          buffer_fmt(bbuf, "))");
+        }
+
         buffer_fmt(bbuf, "qi_list_push(list, ");
         compile_node(gbuf, bbuf, ctx, ltab, lstk, sstk, lbl, node->a->b->l->data[i]);
         buffer_fmt(bbuf, ");\n");