From c045c54309dfaa7bb11f97dc3f36c595736eca3b Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 5 Aug 2024 14:40:28 -0400 Subject: Add a Range datatype with creation methods like `5:to(10)` and modification methods like `range:by(2)` or `range:reversed()` --- compile.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'compile.c') diff --git a/compile.c b/compile.c index 9f91161c..a141302b 100644 --- a/compile.c +++ b/compile.c @@ -817,6 +817,22 @@ CORD compile_statement(env_t *env, ast_t *ast) body = CORD_all(body, "\n", loop_ctx.skip_label, ": continue;"); CORD stop = loop_ctx.stop_label ? CORD_all("\n", loop_ctx.stop_label, ":;") : CORD_EMPTY; + if (iter_t == RANGE_TYPE) { + CORD value = for_->vars ? compile(env, for_->vars->ast) : "i"; + CORD range = compile(env, for_->iter); + if (for_->empty) + code_err(ast, "Ranges are never empty, they always contain at least their starting element"); + 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" + "\t", body, + "\n}", + stop, + "\n}"); + } + switch (iter_t->tag) { case ArrayType: { type_t *item_t = Match(iter_t, ArrayType)->item_type; -- cgit v1.2.3