From: Brendan Hansen Date: Fri, 8 Jan 2021 23:19:30 +0000 (-0600) Subject: various little bug fixes around the code X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=e36f73215f7a00c675f2fe063ed1fc041a66b1e1;p=onyx.git various little bug fixes around the code --- diff --git a/bin/onyx b/bin/onyx index ab0104e3..797a3f47 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/onyx.exe b/onyx.exe index 4f9b9084..14ae3406 100644 Binary files a/onyx.exe and b/onyx.exe differ diff --git a/progs/odin_example.onyx b/progs/odin_example.onyx index 8791accb..42cfc561 100644 --- a/progs/odin_example.onyx +++ b/progs/odin_example.onyx @@ -69,9 +69,9 @@ f :: proc () -> [5] [2] u32 #export "IAMTHEFUNCTION" { return mem; } -compress :: proc (arr: [5] $T, f : proc (T, T) -> T) -> T { +compress :: proc (arr: [$N] $T, f : proc (T, T) -> T) -> T { val := arr[0]; - for i: 1 .. 5 do val = f(val, arr[i]); + for i: 1..N do val = f(val, arr[i]); return val; } @@ -86,7 +86,6 @@ Vec2 :: struct { x: i32; y: i32; } Entity :: struct { use pos: Vec2; } array_literal_optim :: proc () #export { - // This needs to be optimized bar : [5] u32; bar = u32.[ 1, 2, 3, 4, 5 ]; bar[2] = 1234; diff --git a/src/onyxchecker.c b/src/onyxchecker.c index 54f074b2..77c8ae8f 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -1308,7 +1308,12 @@ CheckStatus check_align_of(AstAlignOf* ao) { CheckStatus check_expression(AstTyped** pexpr) { AstTyped* expr = *pexpr; if (expr->kind > Ast_Kind_Type_Start && expr->kind < Ast_Kind_Type_End) { - onyx_report_error(expr->token->pos, "Type used as part of an expression."); + if (expr->token) { + onyx_report_error(expr->token->pos, "Type used as part of an expression."); + } + else { + onyx_report_error((OnyxFilePos) { 0 }, "Type used as part of an expression somewhere in the program."); + } return Check_Error; } diff --git a/src/onyxlex.c b/src/onyxlex.c index cf1d286b..c88a31a4 100644 --- a/src/onyxlex.c +++ b/src/onyxlex.c @@ -254,7 +254,8 @@ whitespace_skipped: if (*tokenizer->curr == '.') hit_decimal = 1; u32 len = 1; - while (char_is_num(*(tokenizer->curr + 1)) || (!hit_decimal && *(tokenizer->curr + 1) == '.')) { + while (char_is_num(*(tokenizer->curr + 1)) + || (!hit_decimal && *(tokenizer->curr + 1) == '.' && *(tokenizer->curr + 2) != '.')) { len++; INCREMENT_CURR_TOKEN(tokenizer); diff --git a/src/onyxutils.c b/src/onyxutils.c index 1813e98a..47a3407a 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -428,6 +428,27 @@ void promote_numlit_to_larger(AstNumLit* num) { } } +static void insert_poly_slns_into_scope(Scope* scope, bh_arr(AstPolySolution) slns) { + bh_arr_each(AstPolySolution, sln, slns) { + AstNode *node = NULL; + + switch (sln->kind) { + case PSK_Type: + node = onyx_ast_node_new(semstate.node_allocator, sizeof(AstTypeRawAlias), Ast_Kind_Type_Raw_Alias); + ((AstTypeRawAlias *) node)->token = sln->poly_sym->token; + ((AstTypeRawAlias *) node)->to = sln->type; + break; + + case PSK_Value: + // CLEANUP: Maybe clone this? + node = (AstNode *) sln->value; + break; + } + + symbol_introduce(scope, sln->poly_sym->token, node); + } +} + typedef struct PolySolveResult { PolySolutionKind kind; union { @@ -730,23 +751,7 @@ AstFunction* polymorphic_proc_solidify(AstPolyProc* pp, bh_arr(AstPolySolution) } Scope* poly_scope = scope_create(semstate.node_allocator, pp->poly_scope, pos); - bh_arr_each(AstPolySolution, sln, slns) { - AstNode *node = NULL; - - switch (sln->kind) { - case PSK_Type: - node = onyx_ast_node_new(semstate.node_allocator, sizeof(AstTypeRawAlias), Ast_Kind_Type_Raw_Alias); - ((AstTypeRawAlias *) node)->to = sln->type; - break; - - case PSK_Value: - // CLEANUP: Maybe clone this? - node = (AstNode *) sln->value; - break; - } - - symbol_introduce(poly_scope, sln->poly_sym->token, node); - } + insert_poly_slns_into_scope(poly_scope, slns); AstFunction* func = (AstFunction *) ast_clone(semstate.node_allocator, pp->base_func); bh_table_put(AstFunction *, pp->concrete_funcs, unique_key, func); @@ -936,26 +941,10 @@ AstStructType* polymorphic_struct_lookup(AstPolyStructType* ps_type, bh_arr(AstP } scope_clear(ps_type->scope); - - bh_arr_each(AstPolySolution, sln, slns) { - AstNode *node = NULL; - - switch (sln->kind) { - case PSK_Type: - node = onyx_ast_node_new(semstate.node_allocator, sizeof(AstTypeRawAlias), Ast_Kind_Type_Raw_Alias); - ((AstTypeRawAlias *) node)->to = sln->type; - break; - - case PSK_Value: - // CLEANUP: Maybe clone this? - node = (AstNode *) sln->value; - break; - } - - symbol_introduce(ps_type->scope, sln->poly_sym->token, node); - } + insert_poly_slns_into_scope(ps_type->scope, slns); AstStructType* concrete_struct = (AstStructType *) ast_clone(semstate.node_allocator, ps_type->base_struct); + bh_table_put(AstStructType *, ps_type->concrete_structs, unique_key, concrete_struct); Entity struct_entity = { .state = Entity_State_Resolve_Symbols, @@ -980,8 +969,6 @@ AstStructType* polymorphic_struct_lookup(AstPolyStructType* ps_type, bh_arr(AstP return NULL; } - bh_table_put(AstStructType *, ps_type->concrete_structs, unique_key, concrete_struct); - Type* cs_type = type_build_from_ast(semstate.node_allocator, (AstType *) concrete_struct); cs_type->Struct.poly_sln = NULL; bh_arr_new(global_heap_allocator, cs_type->Struct.poly_sln, bh_arr_length(slns)); diff --git a/tests/poly_structs_with_values b/tests/poly_structs_with_values index 3d8fae79..72e3b797 100644 --- a/tests/poly_structs_with_values +++ b/tests/poly_structs_with_values @@ -4,3 +4,4 @@ 12345 1234 Hello World! +Hello World! diff --git a/tests/poly_structs_with_values.onyx b/tests/poly_structs_with_values.onyx index 38eb53ec..7ca5541b 100644 --- a/tests/poly_structs_with_values.onyx +++ b/tests/poly_structs_with_values.onyx @@ -3,7 +3,6 @@ use package core main :: proc (args: [] cstr) { - NewPolyStruct :: struct (T: type_expr, N: i32) { x : [N] T; y : [N] f32; @@ -16,6 +15,10 @@ main :: proc (args: [] cstr) { for x: nps.x do println(x); + + + + SimpleWithDefault :: struct (T: type_expr, default: str) { x : T; str_member : str = default; @@ -24,4 +27,7 @@ main :: proc (args: [] cstr) { swd := .{ x = 1234 }; println(swd.x); println(swd.str_member); + poly_match(swd); + + poly_match :: proc (swd: SimpleWithDefault($T, $D)) do println(D); } \ No newline at end of file