From 5b0a841db6f42a1ce840472f5ccbaef3699823f3 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 20 Jul 2024 17:20:50 -0400 Subject: Fix edge case with iterating over array:from(-999) --- builtins/tomo.h | 1 + compile.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) 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 #include #include +#include #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, ")"); -- cgit v1.2.3