aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-08-13 03:20:01 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-08-13 03:20:01 -0400
commit9edace28e2e43fb8cb8dc64191b3ed3f5a185f99 (patch)
treec7a3008bf8dc9af5537e4cc9839f7c38a1b059ab
parent5d3ab600f1ceddd3537006b69758b2bbf2327c49 (diff)
Fix ranges
-rw-r--r--builtins/integers.c2
-rw-r--r--builtins/range.h2
-rw-r--r--compile.c5
-rw-r--r--environment.c2
4 files changed, 6 insertions, 5 deletions
diff --git a/builtins/integers.c b/builtins/integers.c
index b50d61c2..aa1ab4ae 100644
--- a/builtins/integers.c
+++ b/builtins/integers.c
@@ -360,7 +360,7 @@ public Int_t Int$random(Int_t min, Int_t max) {
}
public Range_t Int$to(Int_t from, Int_t to) {
- return (Range_t){from, to, Int$compare(&to, &from, &$Int) >= 0 ? (Int_t){.small=(1<<2)|1} : (Int_t){.small=(-1/4)|1}};
+ return (Range_t){from, to, Int$compare(&to, &from, &$Int) >= 0 ? (Int_t){.small=(1<<2)|1} : (Int_t){.small=(-1>>2)|1}};
}
public Int_t Int$from_text(CORD text) {
diff --git a/builtins/range.h b/builtins/range.h
index 9f3b25af..802c85f8 100644
--- a/builtins/range.h
+++ b/builtins/range.h
@@ -3,7 +3,7 @@
// Ranges represent numeric ranges
Range_t Range$reversed(Range_t r);
-Range_t Range$by(Range_t r, int64_t step);
+Range_t Range$by(Range_t r, Int_t step);
extern const TypeInfo Range;
diff --git a/compile.c b/compile.c
index 4be2abb5..c8edd5fb 100644
--- a/compile.c
+++ b/compile.c
@@ -850,8 +850,9 @@ CORD compile_statement(env_t *env, ast_t *ast)
return CORD_all(
"{\n"
"const Range_t range = ", range, ";\n"
- "if (range.step == 0) fail(\"This range has a 'step' of zero and will loop infinitely!\");\n"
- "for (int64_t ", value, " = range.first; range.step > 0 ? ", value, " <= range.last : ", value, " >= range.last; ", value, " += range.step) {\n"
+ "if (range.step.small == 0) fail(\"This range has a 'step' of zero and will loop infinitely!\");\n"
+ "bool negative = (Int$compare_value(range.step, I(0)) < 0);\n"
+ "for (Int_t ", value, " = range.first; negative ? Int$compare_value(", value, ", range.last) >= 0 : Int$compare_value(", value, ", range.last) <= 0 ; ", value, " = Int$plus(", value, ", range.step)) {\n"
"\t", body,
"\n}",
stop,
diff --git a/environment.c b/environment.c
index cbf35af4..57fbc6fa 100644
--- a/environment.c
+++ b/environment.c
@@ -344,7 +344,7 @@ env_t *for_scope(env_t *env, ast_t *ast)
if (for_->vars->next)
code_err(for_->vars->next->ast, "This is too many variables for this loop");
const char *var = Match(for_->vars->ast, Var)->name;
- set_binding(scope, var, new(binding_t, .type=Type(IntType, .bits=64), .code=CORD_cat("$", var)));
+ set_binding(scope, var, new(binding_t, .type=INT_TYPE, .code=CORD_cat("$", var)));
}
return scope;
}