Working on arrays

This commit is contained in:
Krzosa Karol
2022-06-07 21:19:06 +02:00
parent c3e071b3bb
commit b65a5f78bf
6 changed files with 48 additions and 11 deletions

View File

@@ -133,7 +133,8 @@ gen_expr(Ast_Expr *ast){
else gen("."); else gen(".");
gen_expr(node->right); gen_expr(node->right);
} }
else if(node->op == TK_ColonAssign){ // @todo: I think this needs to be a stmt else if(node->op == TK_ColonAssign){
Sym *sym = resolved_get(node); Sym *sym = resolved_get(node);
Ast_Atom *atom = (Ast_Atom *)node->left; Ast_Atom *atom = (Ast_Atom *)node->left;
assert(is_atom(atom)); assert(is_atom(atom));
@@ -210,6 +211,8 @@ gen_expr(Ast_Expr *ast){
gen("("); gen("(");
auto name_for_printf = (Ast_Atom *)node->name; auto name_for_printf = (Ast_Atom *)node->name;
For(node->exprs){ For(node->exprs){
// @special_case @todo in the future this should be replaced
// with []Any
if(intern_printf == name_for_printf->intern_val && &it == node->exprs.data){ if(intern_printf == name_for_printf->intern_val && &it == node->exprs.data){
Ast_Atom *atom = (Ast_Atom *)it->item; Ast_Atom *atom = (Ast_Atom *)it->item;
assert(atom->kind == AST_VALUE); assert(atom->kind == AST_VALUE);
@@ -231,7 +234,8 @@ gen_expr(Ast_Expr *ast){
function void function void
gen_line(Ast *node){ gen_line(Ast *node){
genln("#line %d", node->pos->line+1); if(emit_line_directives)
genln("#line %d", node->pos->line+1);
} }
function void function void
@@ -254,7 +258,8 @@ gen_ast(Ast *ast){
switch(ast->kind){ switch(ast->kind){
CASE(PACKAGE, Package){ CASE(PACKAGE, Package){
genln("#line 0 \"%s\"", node->name.str); if(emit_line_directives)
genln("#line 0 \"%s\"", node->name.str);
For(node->ordered) { For(node->ordered) {
gen_line(it); gen_line(it);
genln(""); genln("");
@@ -290,6 +295,8 @@ gen_ast(Ast *ast){
gen_simple_decl(sym->type, {}); gen_simple_decl(sym->type, {});
gen("){}"); gen("){}");
gen("}"); gen("}");
} else { // Default zero
gen(" = {}");
} }
gen(";"); gen(";");
BREAK(); BREAK();

View File

@@ -80,7 +80,7 @@ Expr:
[ ] - Arrays with size passed [ ] - Arrays with size passed
[ ] - Switch [ ] - Switch
[ ] - Values inited to 0 by default [ ] - Values inited to 0 by default
[ ] - Emitting #line [ ] - Add c string
[ ] - Comma notation when declaring variables thing1, thing2: S32 [ ] - Comma notation when declaring variables thing1, thing2: S32
[ ] - Array of inferred size [ ] - Array of inferred size
@@ -111,6 +111,8 @@ Expr:
[ ] - Conditional compilation #if [ ] - Conditional compilation #if
@donzo @donzo
[x] - Emitting #line
[x] - Making sure debugger works
[x] - We need ++ -- operators [x] - We need ++ -- operators
[x] - Some way to call foreign functions [x] - Some way to call foreign functions
[x] - We are parsing wrong here: (t.str=(&string_to_lex.str)[i]); [x] - We are parsing wrong here: (t.str=(&string_to_lex.str)[i]);
@@ -178,7 +180,7 @@ int main(int argument_count, char **arguments){
emit_line_directives = false;
if(argument_count > 1){ if(argument_count > 1){
Scratch scratch; Scratch scratch;
String name = string_fmt(scratch, "%s.kl", arguments[1]); String name = string_fmt(scratch, "%s.kl", arguments[1]);

View File

@@ -83,7 +83,30 @@ for_loops :: ()
Custom_Data :: struct Custom_Data :: struct
thing: S32 thing: S32
get_element_item :: (array: []Custom_Data, index: S64): Custom_Data
if index < array.len
return array[index]
else
return Custom_Data()
get_element_pointer :: (array: []Custom_Data, index: S64): *Custom_Data
if index < array.len
return &array[index]
else
return 0
get_slice_len :: (array: []Custom_Data): S64
return array.len
array_test :: () array_test :: ()
thing := []Custom_Data(Custom_Data(1), Custom_Data(2)) thing := []Custom_Data(Custom_Data(1), Custom_Data(2))
reference := thing reference := thing
length := reference.len length := reference.len
item := get_element_item(thing, 1)
pointer := get_element_pointer(thing, 0)
len := get_slice_len(thing)
with_size: [2]Custom_Data
// get_slice_len(with_size)

View File

@@ -710,9 +710,7 @@ resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_res
if(item){ if(item){
item->flags = set_flag(item->flags, AST_ITEM_INCLUDED); item->flags = set_flag(item->flags, AST_ITEM_INCLUDED);
Operand expr = resolve_expr(item->item); Operand expr = resolve_expr(item->item);
expr.value = convert_untyped_to_typed(node->pos, expr.value, resolved); make_sure_value_is_compatible_with_type(node->pos, &expr, resolved, TYPE_AND_EXPR_REQUIRED);
if(expr.type != resolved) type_error(item->pos, resolved, expr.type, "Type is not matching function definition");
items.add(item); items.add(item);
} }
else{ else{

View File

@@ -292,7 +292,8 @@ type_array(Ast_Resolved_Type *base, B32 size_present, S64 size){
size = ARRAY_SIZE_INFERRED; size = ARRAY_SIZE_INFERRED;
} }
U64 hash = hash_mix(hash_ptr(base), hash_u64(size)); U64 hash_base = hash_ptr(base);
U64 hash = hash_mix(hash_base, hash_u64(size));
Ast_Resolved_Type *result = (Ast_Resolved_Type *)map_get(&pctx->type_map, hash); Ast_Resolved_Type *result = (Ast_Resolved_Type *)map_get(&pctx->type_map, hash);
if(result){ if(result){
assert(result->kind == TYPE_ARRAY); assert(result->kind == TYPE_ARRAY);
@@ -304,6 +305,7 @@ type_array(Ast_Resolved_Type *base, B32 size_present, S64 size){
result = type_new(pctx->perm, TYPE_ARRAY, pointer_size, pointer_align); result = type_new(pctx->perm, TYPE_ARRAY, pointer_size, pointer_align);
result->arr.base = base; result->arr.base = base;
result->arr.size = size; result->arr.size = size;
result->arr.inferred_size_hash = hash_mix(hash_base, hash_u64(ARRAY_SIZE_INFERRED));
map_insert(&pctx->type_map, hash, result); map_insert(&pctx->type_map, hash, result);
return result; return result;
} }

View File

@@ -100,6 +100,11 @@ struct Ast_Resolved_Type{
struct{ struct{
Ast_Resolved_Type *base; Ast_Resolved_Type *base;
S64 size; S64 size;
// @note: if you have array with size "[32]"
// you still want to pass that array into
// a function that expects an array of size "[]"
// so we want also should check this
U64 inferred_size_hash;
}arr; }arr;
struct{ struct{
Array<Ast_Resolved_Member> members; Array<Ast_Resolved_Member> members;