aboutsummaryrefslogtreecommitdiff
path: root/compile.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-07-20 17:20:50 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-07-20 17:20:50 -0400
commit5b0a841db6f42a1ce840472f5ccbaef3699823f3 (patch)
treed61bf169b76c84380dc15494edfe4c21119889c8 /compile.c
parent279cd231437461c59ad39340e002cc3390ec5558 (diff)
Fix edge case with iterating over array:from(-999)
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c3
1 files changed, 2 insertions, 1 deletions
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, ")");