Working on slices, arrays and any types

This commit is contained in:
Krzosa Karol
2022-06-19 20:42:45 +02:00
parent 1a0354213e
commit 367562f0a8
3 changed files with 30 additions and 4 deletions

View File

@@ -288,9 +288,13 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){
CASE(INDEX, Index){ CASE(INDEX, Index){
gen("("); gen("(");
gen_expr(node->expr); 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)) if(!(type_of_var && type_of_var == type_pointer_to_char))
gen(".str"); gen(".str");
}
else if(is_slice(node->resolved_type)){
gen(".data");
}
gen("["); gen("[");
gen_expr(node->index); gen_expr(node->index);
gen("]"); gen("]");
@@ -370,14 +374,26 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){
gen(")"); gen(")");
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){ For(node->exprs){
if(is_struct(node->resolved_type)) if(is_struct(node->resolved_type))
gen(".%Q = ", it->resolved_name); gen(".%Q = ", it->resolved_name);
else if(is_array(node->resolved_type)) else if(is_array(node->resolved_type))
gen("[%d] = ", (int)it->resolved_index); gen("[%d] = ", (int)it->resolved_index);
gen_expr(it->item, it->resolved_type); gen_expr(it->item, it->resolved_type);
if(!node->exprs.is_last(&it)) gen(", "); if(!node->exprs.is_last(&it)) gen(", ");
} }
if(is_slice(node->resolved_type)) gen("}");
gen("}"); gen("}");
BREAK(); 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, "void"_s , type_void);
insert_builtin_into_scope(p, "Bool"_s , type_bool); insert_builtin_into_scope(p, "Bool"_s , type_bool);
insert_builtin_into_scope(p, "String"_s, type_string); 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); insert_builtin_into_scope(p, "Type"_s, type_type);
} }

View File

@@ -1,15 +1,26 @@
main :: (argc: int, argv: **char): int main :: (argc: int, argv: **char): int
test_arrays()
test_any() test_any()
test_type() 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 :: () test_any :: ()
some_int_value := 10 some_int_value := 10
thing: Any = some_int_value thing: Any = some_int_value
other_any: Any = thing other_any: Any = thing
imp_any := thing imp_any := thing
assert(thing.type != Any) assert(thing.type != Any)
any_array := []Any{some_int_value, thing}
Some_Struct :: struct Some_Struct :: struct
thing: int thing: int

View File

@@ -746,7 +746,7 @@ resolve_compound_array(Ast_Call *node, Ast_Type *type){
it->resolved_index = default_counter; 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); make_sure_value_is_compatible_with_type(it->pos, &item, item_type, TYPE_AND_EXPR_REQUIRED);
it->resolved_type = item_type; it->resolved_type = item_type;
} }
@@ -800,7 +800,7 @@ resolve_compound_struct(Ast_Call *node, Ast_Type *type){
} }
assert(item_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); make_sure_value_is_compatible_with_type(it->pos, &item, item_type, TYPE_AND_EXPR_REQUIRED);
it->resolved_type = item_type; it->resolved_type = item_type;