From d8fda0ff8c588405ac439be7124e8f7fae5a4073 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Thu, 28 Jul 2022 13:35:47 +0200 Subject: [PATCH] Fix slice indexing in example --- c_language_codegen.cpp | 2 +- examples/arrays_and_slices.kl | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/c_language_codegen.cpp b/c_language_codegen.cpp index 679485a..b0e66ba 100644 --- a/c_language_codegen.cpp +++ b/c_language_codegen.cpp @@ -306,7 +306,7 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){ if(!(type_of_var && type_of_var == type_pointer_to_char)) gen(".str"); } - else if(is_slice(node->resolved_type)){ + else if(is_slice(node->index_original_type)){ gen(".data"); } gen("["); diff --git a/examples/arrays_and_slices.kl b/examples/arrays_and_slices.kl index 2ce7daa..0a3a99a 100644 --- a/examples/arrays_and_slices.kl +++ b/examples/arrays_and_slices.kl @@ -33,7 +33,6 @@ main :: (): int // We set all variables to 1 so assert(static_array[6] == 1) - // This is how slice is defined, no [] index in between brackets // slice is array pointer + length // Other then that it works exactly like regular array @@ -41,4 +40,11 @@ main :: (): int // We can't do a compile time assert anymore assert(length_of(slice) == 8) + assert(slice[4] == 1) + // After we loop and reassign slice values + // old static_array gets changed + for slice + *it = 2 + + assert(static_array[2] == 2) \ No newline at end of file