diff --git a/ccodegen.cpp b/ccodegen.cpp index 8465423..9f007cd 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -288,9 +288,13 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){ CASE(INDEX, Index){ gen("("); gen_expr(node->expr); - if(is_string(node->resolved_type)) + if(is_string(node->resolved_type)){ if(!(type_of_var && type_of_var == type_pointer_to_char)) gen(".str"); + } + else if(is_slice(node->resolved_type)){ + gen(".data"); + } gen("["); gen_expr(node->index); gen("]"); @@ -370,14 +374,26 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){ gen(")"); gen("{"); + if(is_slice(node->resolved_type)) { + gen(".len = %d, ", node->exprs.len); + gen(".data = ("); + gen_simple_decl(node->resolved_type->base); + gen("[]"); + gen(")"); + gen("{"); + } + For(node->exprs){ if(is_struct(node->resolved_type)) gen(".%Q = ", it->resolved_name); else if(is_array(node->resolved_type)) gen("[%d] = ", (int)it->resolved_index); gen_expr(it->item, it->resolved_type); + if(!node->exprs.is_last(&it)) gen(", "); } + + if(is_slice(node->resolved_type)) gen("}"); gen("}"); BREAK(); } @@ -699,7 +715,6 @@ insert_builtin_types_into_scope(Ast_Scope *p){ insert_builtin_into_scope(p, "void"_s , type_void); insert_builtin_into_scope(p, "Bool"_s , type_bool); insert_builtin_into_scope(p, "String"_s, type_string); - // insert_builtin_into_scope(p, "Any"_s, type_any); insert_builtin_into_scope(p, "Type"_s, type_type); } diff --git a/programs/any.kl b/programs/any.kl index b517f35..e487e32 100644 --- a/programs/any.kl +++ b/programs/any.kl @@ -1,15 +1,26 @@ main :: (argc: int, argv: **char): int + test_arrays() test_any() test_type() +test_arrays :: () + array1 := []S64{1,2,3,4,5} + array2: []S64 = {0,9,8} + array4 := [][]S64{[]S64{5,6},[]S64{4,3},[]S64{2,3}} + array5 := [][]S64{{5,6},{4,3},{2,3}} + for i := 0, i < length_of(array1), i+=1 + assert(i+1 == array1[i]) + array6: []*S64 = {&array1[0]} + test_any :: () some_int_value := 10 thing: Any = some_int_value other_any: Any = thing imp_any := thing assert(thing.type != Any) + any_array := []Any{some_int_value, thing} Some_Struct :: struct thing: int diff --git a/typechecking.cpp b/typechecking.cpp index 2c40530..a5dee3e 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -746,7 +746,7 @@ resolve_compound_array(Ast_Call *node, Ast_Type *type){ it->resolved_index = default_counter; } - Operand item = resolve_expr(it->item, AST_CANT_BE_NULL); + Operand item = resolve_expr(it->item, AST_CANT_BE_NULL, item_type); make_sure_value_is_compatible_with_type(it->pos, &item, item_type, TYPE_AND_EXPR_REQUIRED); it->resolved_type = item_type; } @@ -800,7 +800,7 @@ resolve_compound_struct(Ast_Call *node, Ast_Type *type){ } assert(item_type); - Operand item = resolve_expr(it->item, AST_CANT_BE_NULL); + Operand item = resolve_expr(it->item, AST_CANT_BE_NULL, item_type); make_sure_value_is_compatible_with_type(it->pos, &item, item_type, TYPE_AND_EXPR_REQUIRED); it->resolved_type = item_type;