aboutsummaryrefslogtreecommitdiff
path: root/compile.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-07-13 19:58:21 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-07-13 19:58:21 -0400
commit9fec3a6adb78497ff3977e2dff28b94399e512a5 (patch)
treed12a74d744f5da79fb3b23f76c76209eb2dda706 /compile.c
parent39576466a7bcc545c49a9f17b188cc307a0c9d9c (diff)
Fix for reductions over iterators like `(+) range(5, 10)`
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c8
1 files changed, 4 insertions, 4 deletions
diff --git a/compile.c b/compile.c
index cc848e80..f56604d8 100644
--- a/compile.c
+++ b/compile.c
@@ -2021,6 +2021,7 @@ CORD compile(env_t *env, ast_t *ast)
CORD code = CORD_all(
"({ // Reduction:\n",
compile_declaration(t, "reduction"), ";\n"
+ "Bool_t is_first = yes;\n"
);
env_t *scope = fresh_scope(env);
ast_t *result = FakeAST(Var, "$reduction");
@@ -2040,11 +2041,10 @@ CORD compile(env_t *env, ast_t *ast)
Text$quoted(ast->file->filename, false), (long)(reduction->iter->start - reduction->iter->file->text),
(long)(reduction->iter->end - reduction->iter->file->text)));
}
- ast_t *i = FakeAST(Var, "$i");
ast_t *item = FakeAST(Var, "$iter_value");
- set_binding(scope, "$iter_value", new(binding_t, .type=t, .code="iter_value"));
- ast_t *body = FakeAST(InlineCCode, CORD_all("reduction = $$i == 1 ? iter_value : ", compile(scope, reduction->combination), ";"));
- ast_t *loop = FakeAST(For, .vars=new(ast_list_t, .ast=i, .next=new(ast_list_t, .ast=item)), .iter=reduction->iter, .body=body, .empty=empty);
+ set_binding(scope, "$iter_value", new(binding_t, .type=t, .code="$$iter_value"));
+ ast_t *body = FakeAST(InlineCCode, CORD_all("if (is_first) {\nreduction = $$iter_value;\nis_first = no;\n} else {\nreduction = ", compile(scope, reduction->combination), ";\n}\n"));
+ ast_t *loop = FakeAST(For, .vars=new(ast_list_t, .ast=item), .iter=reduction->iter, .body=body, .empty=empty);
code = CORD_all(code, compile_statement(scope, loop), "\nreduction;})");
return code;
}