From: Brendan Hansen Date: Fri, 15 Oct 2021 03:00:31 +0000 (-0500) Subject: random bugfixes and changes X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=c11e524636d823572abbdba5319d8233d6d46c0e;p=onyx.git random bugfixes and changes --- diff --git a/bin/onyx b/bin/onyx index 6fa13f91..4cea0fe1 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/core/container/bucket_array.onyx b/core/container/bucket_array.onyx index e11ad9ac..6947ff72 100644 --- a/core/container/bucket_array.onyx +++ b/core/container/bucket_array.onyx @@ -45,6 +45,11 @@ clear :: (use b: ^Bucket_Array($T)) { array.clear(^buckets); } +#operator [] macro (b: Bucket_Array($T), idx: i32) -> T { + get :: get + return get(^b, idx); +} + get :: (use b: ^Bucket_Array($T), idx: i32) -> T { bucket_index := idx / elements_per_bucket; elem_index := idx % elements_per_bucket; diff --git a/core/random.onyx b/core/random.onyx index bd8ba8b3..0bc3e50e 100644 --- a/core/random.onyx +++ b/core/random.onyx @@ -21,3 +21,7 @@ between :: (lo: i32, hi: i32) -> i32 do return int () % (hi + 1 - lo) + lo; float :: (lo := 0.0f, hi := 1.0f) -> f32 { return (cast(f32) (int() % (1 << 20)) / cast(f32) (1 << 20)) * (hi - lo) + lo; } + +choice :: (a: [] $T) -> T { + return a[between(0, a.count - 1)]; +} \ No newline at end of file diff --git a/include/astnodes.h b/include/astnodes.h index c6da2856..20966dd9 100644 --- a/include/astnodes.h +++ b/include/astnodes.h @@ -1419,7 +1419,7 @@ void report_unable_to_match_overload(AstCall* call); void expand_macro(AstCall** pcall, AstFunction* template); AstFunction* macro_resolve_header(AstMacro* macro, Arguments* args, OnyxToken* callsite); -AstStructType* polymorphic_struct_lookup(AstPolyStructType* ps_type, bh_arr(AstPolySolution) slns, OnyxFilePos pos); +Type* polymorphic_struct_lookup(AstPolyStructType* ps_type, bh_arr(AstPolySolution) slns, OnyxFilePos pos); // NOTE: Useful inlined functions static inline b32 is_lval(AstNode* node) { diff --git a/misc/onyx.sublime-syntax b/misc/onyx.sublime-syntax index d07244e5..1dd09072 100644 --- a/misc/onyx.sublime-syntax +++ b/misc/onyx.sublime-syntax @@ -9,6 +9,9 @@ contexts: main: # Strings begin and end with quotes, and use backslashes as an escape # character + - match: '"""' + scope: punctuation.definition.string.begin.onyx + push: triple_quoted_string - match: '"' scope: punctuation.definition.string.begin.onyx push: double_quoted_string @@ -97,6 +100,12 @@ contexts: scope: punctuation.definition.string.end.onyx pop: true + triple_quoted_string: + - meta_scope: string.quoted.triple.onyx + - match: '"""' + scope: punctuation.definition.string.end.onyx + pop: true + line_comment: - meta_scope: comment.line.onyx - match: $ diff --git a/src/polymorph.c b/src/polymorph.c index 335eec9d..cd479e9e 100644 --- a/src/polymorph.c +++ b/src/polymorph.c @@ -814,7 +814,7 @@ char* build_poly_struct_name(AstPolyStructType* ps_type, Type* cs_type) { return bh_aprintf(global_heap_allocator, "%s", name_buf); } -AstStructType* polymorphic_struct_lookup(AstPolyStructType* ps_type, bh_arr(AstPolySolution) slns, OnyxFilePos pos) { +Type* polymorphic_struct_lookup(AstPolyStructType* ps_type, bh_arr(AstPolySolution) slns, OnyxFilePos pos) { // @Cleanup assert(ps_type->scope != NULL); @@ -890,7 +890,7 @@ AstStructType* polymorphic_struct_lookup(AstPolyStructType* ps_type, bh_arr(AstP if (cs_type->Struct.poly_sln == NULL) cs_type->Struct.poly_sln = bh_arr_copy(global_heap_allocator, slns); if (cs_type->Struct.name == NULL) cs_type->Struct.name = build_poly_struct_name(ps_type, cs_type); - return concrete_struct; + return cs_type; } Scope* sln_scope = scope_create(context.ast_alloc, ps_type->scope, ps_type->token->pos); diff --git a/src/types.c b/src/types.c index 58e3dc08..dcb3d041 100644 --- a/src/types.c +++ b/src/types.c @@ -550,16 +550,15 @@ Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) { } } - AstStructType* concrete = polymorphic_struct_lookup(ps_type, slns, pc_type->token->pos); + Type* concrete = polymorphic_struct_lookup(ps_type, slns, pc_type->token->pos); // This should be copied in the previous function. // CLEANUP: Maybe don't copy it and just use this one since it is allocated on the heap? bh_arr_free(slns); if (!concrete) return NULL; - Type* struct_type = type_build_from_ast(alloc, (AstType *) concrete); - struct_type->Struct.constructed_from = (AstType *) ps_type; - return struct_type; + concrete->Struct.constructed_from = (AstType *) ps_type; + return concrete; } case Ast_Kind_Type_Compound: { diff --git a/src/utils.c b/src/utils.c index 52fbb2cf..ef91b57c 100644 --- a/src/utils.c +++ b/src/utils.c @@ -229,6 +229,11 @@ all_types_peeled_off: AstStructType* stype = ((AstPolyStructType *) node)->base_struct; return symbol_raw_resolve(stype->scope, symbol); } + + case Ast_Kind_Poly_Call_Type: { + AstNode* callee = (AstNode *) ((AstPolyCallType *) node)->callee; + return try_symbol_raw_resolve_from_node(callee, symbol); + } } return NULL; diff --git a/tests/bucket_array b/tests/bucket_array index 0daca9e2..2f8f1c0b 100644 --- a/tests/bucket_array +++ b/tests/bucket_array @@ -1,3 +1,4 @@ +ba[10] is 10. [0] -> 0 [1] -> 1 [2] -> 2 diff --git a/tests/bucket_array.onyx b/tests/bucket_array.onyx index d94ae518..9307dca9 100644 --- a/tests/bucket_array.onyx +++ b/tests/bucket_array.onyx @@ -6,6 +6,8 @@ main :: (args: [] cstr) { ba := bucket_array.make(i32, 4); for i: 24 do ba << i; + printf("ba[10] is {}.\n", ba[10]); + sum := 0; bucket_array.for_each(ba, #code { printf("[{}] -> {}\n", bucket_index, *it);