From 151cbfd706fb6769eda08c4789b9ed6838cde2e5 Mon Sep 17 00:00:00 2001 From: Dmitry Atamanov Date: Sat, 9 Apr 2022 23:09:35 +0500 Subject: [PATCH] Semi-implemented foreach for vectors (#423) Implement foreach for vectors --- src/compiler/llvm_codegen_expr.c | 1 + test/test_suite/statements/foreach_common.c3t | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 8ea83f77b..a44744637 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -2200,6 +2200,7 @@ void llvm_emit_len_for_expr(GenContext *c, BEValue *be_value, BEValue *expr_to_l } break; case TYPE_ARRAY: + case TYPE_VECTOR: llvm_value_set(be_value, llvm_const_int(c, type_usize, expr_to_len->type->array.len), type_usize); break; default: diff --git a/test/test_suite/statements/foreach_common.c3t b/test/test_suite/statements/foreach_common.c3t index 8f56f0ab2..0befa0987 100644 --- a/test/test_suite/statements/foreach_common.c3t +++ b/test/test_suite/statements/foreach_common.c3t @@ -6,6 +6,7 @@ extern fn void printf(char*, ...); fn void main() { float[3] foo = { 2, 4.5, 8 }; + float[<3>] foo2 = { 2, 4.5, 8 }; foreach (a : foo) { printf("Value: %f\n", a); @@ -31,6 +32,23 @@ fn void main() { printf("Value3: %f\n", a); } + + foreach (a : foo2) + { + printf("Value: %f\n", a); + } + foreach (i, a : foo2) + { + printf("Value[%d]: %f\n", i, a); + } + foreach (char i, double a : foo2) + { + printf("Value2[%d]: %f\n", i, a); + } + foreach (double a : foo2) + { + printf("Value3: %f\n", a); + } } /* #expect: test.ll