aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--builtins/tomo.h1
-rw-r--r--compile.c3
2 files changed, 3 insertions, 1 deletions
diff --git a/builtins/tomo.h b/builtins/tomo.h
index 17439b59..482c8d2d 100644
--- a/builtins/tomo.h
+++ b/builtins/tomo.h
@@ -7,6 +7,7 @@
#include <gc/cord.h>
#include <stdbool.h>
#include <stdint.h>
+#include <sys/param.h>
#include "array.h"
#include "bool.h"
diff --git a/compile.c b/compile.c
index 441ace29..dc6e3d78 100644
--- a/compile.c
+++ b/compile.c
@@ -841,7 +841,8 @@ CORD compile_statement(env_t *env, ast_t *ast)
array = Match(for_->iter, MethodCall)->self;
array_code = is_idempotent(array) ? compile(env, array) : "arr";
CORD first = compile_arguments(env, for_->iter, new(arg_t, .type=Type(IntType, .bits=64), .name="last"), Match(for_->iter, MethodCall)->args);
- for_code = CORD_all("for (int64_t ", index, " = ", first, "; ", index, " <= ", array_code, ".length; ++", index, ")");
+ for_code = CORD_all("for (int64_t first = ", first, ", ", index, " = MAX(1, first < 1 ? ", array_code, ".length + first + 1 : first", "); ",
+ index, " <= ", array_code, ".length; ++", index, ")");
} else {
array_code = is_idempotent(array) ? compile(env, array) : "arr";
for_code = CORD_all("for (int64_t ", index, " = 1; ", index, " <= ", array_code, ".length; ++", index, ")");