diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-07-20 17:20:50 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-07-20 17:20:50 -0400 |
| commit | 5b0a841db6f42a1ce840472f5ccbaef3699823f3 (patch) | |
| tree | d61bf169b76c84380dc15494edfe4c21119889c8 | |
| parent | 279cd231437461c59ad39340e002cc3390ec5558 (diff) | |
Fix edge case with iterating over array:from(-999)
| -rw-r--r-- | builtins/tomo.h | 1 | ||||
| -rw-r--r-- | compile.c | 3 |
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" @@ -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, ")"); |
