From: Brendan Hansen Date: Sun, 30 Aug 2020 16:11:10 +0000 (-0500) Subject: added '#add_overload' directive X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=2e2071fd44001f151da0928a292e688097712ec9;p=onyx.git added '#add_overload' directive --- diff --git a/core/builtin.onyx b/core/builtin.onyx index 694a0744..7f1dc555 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -67,13 +67,13 @@ array_ensure_capacity :: proc (arr: ^[..] $T, cap: u32) { arr.data = cresize(arr.data, sizeof T * arr.capacity); } -array_add :: proc (arr: ^[..] $T, x: T) { +array_push :: proc (arr: ^[..] $T, x: T) { array_ensure_capacity(arr, arr.count + 1); arr.data[arr.count] = x; arr.count += 1; } -array_add_at :: proc (arr: ^[..] $T, x: T, idx: u32) { +array_insert :: proc (arr: ^[..] $T, idx: u32, x: T) { array_ensure_capacity(arr, arr.count + 1); arr.count += 1; @@ -96,7 +96,7 @@ array_remove :: proc (arr: ^[..] $T, elem: T) { arr.count -= move; } -array_remove_at :: proc (arr: ^[..] $T, idx: u32) { +array_delete :: proc (arr: ^[..] $T, idx: u32) { if idx >= arr.count do return; for i: idx, arr.count - 1 { diff --git a/core/stdio.onyx b/core/stdio.onyx index 9402b756..b6afabe5 100644 --- a/core/stdio.onyx +++ b/core/stdio.onyx @@ -13,6 +13,7 @@ print_cstring :: proc (s: cstring) do string_builder_append(^cout_state.sb, s); print_i64 :: proc (n: i64, base := 10l) do string_builder_append(^cout_state.sb, n, base); print_i32 :: proc (n: i32, base := 10) do string_builder_append(^cout_state.sb, cast(i64) n, cast(u64) base); print_bool :: proc (b: bool) do string_builder_append(^cout_state.sb, b); +print_ptr :: proc (p: rawptr) do string_builder_append(^cout_state.sb, cast(i64) p, 16l); print :: proc #overloaded { print_string, @@ -20,6 +21,7 @@ print :: proc #overloaded { print_i64, print_i32, print_bool, + print_ptr, } print_flush :: proc { diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 7d310690..ca2d678c 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -464,6 +464,10 @@ struct AstFunction { AstBlock *body; bh_arr(AstLocal *) locals; + // NOTE: used by the #add_overload directive. Initially set to a symbol, + // then resolved to an overloaded function. + AstNode *overloaded_function; + union { // NOTE: Used when a function is exported with a specific name OnyxToken* exported_name; diff --git a/onyx b/onyx index fb29383a..41dca389 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/poly_test.onyx b/progs/poly_test.onyx index 58b542c7..0e430e35 100644 --- a/progs/poly_test.onyx +++ b/progs/poly_test.onyx @@ -43,25 +43,20 @@ print_arr :: proc (arr: $T) { print("\n"); } -print_vec_arr :: proc (arr: [..] Vec3) { - for i: 0, arr.count { - print("Vec3("); - print(arr[i].x); - print(", "); - print(arr[i].y); - print(", "); - print(arr[i].z); - print(")\n"); - } - - print("\n"); +print_vec :: proc #add_overload print (v: Vec3) { + print("Vec3("); + print(v.x); + print(", "); + print(v.y); + print(", "); + print(v.z); + print(")"); } // This demonstrates that we have something similar to static 'duck' typing. get_count :: proc (x: $T) -> u32 do return x.count; - SOA :: struct { a : [..] i32; b : [..] i64; @@ -86,8 +81,8 @@ main :: proc (args: [] cstring) { print_arr_details(^s.b); for i: 0, 100 { - array_add(^s.a, 5 * i); - array_add(^s.b, 3l * cast(i64) i); + array_push(^s.a, 5 * i); + array_push(^s.b, 3l * cast(i64) i); } print_arr(^s.a); @@ -110,14 +105,14 @@ main2 :: proc (args: [] cstring) { print_arr_details(^iarr); - array_add(^iarr, 1234); + array_push(^iarr, 1234); - for i: 0, 12 do array_add(^iarr, i % 5); + for i: 0, 12 do array_push(^iarr, i % 5); print_arr_details(^iarr); print_arr(^iarr); - array_remove_at(^iarr, 4); + array_delete(^iarr, 4); print_arr_details(^iarr); print_arr(^iarr); @@ -128,7 +123,7 @@ main2 :: proc (args: [] cstring) { print_arr_details(^iarr); print_arr(^iarr); - array_add_at(^iarr, 5678, 2); + array_insert(^iarr, 2, 5678); print_arr_details(^iarr); print_arr(^iarr); @@ -142,7 +137,7 @@ main2 :: proc (args: [] cstring) { defer array_free(^barr); for i: 0, 500 { - array_add(^barr, cast(u64) (3 * i * i + 4 * i - 2)); + array_push(^barr, cast(u64) (3 * i * i + 4 * i - 2)); } print_arr_details(^barr); @@ -161,17 +156,17 @@ main2 :: proc (args: [] cstring) { defer array_free(^varr); for i: 0, 20 { - array_add(^varr, Vec3.{ + array_push(^varr, Vec3.{ x = i, y = i * i, z = i * i * i, }); } - array_add(^varr, Vec3.{ 4, 2, 3 }); + array_push(^varr, Vec3.{ 4, 2, 3 }); print_arr_details(^varr); - print_vec_arr(varr); + print_arr(varr); print(get_count(iarr)); print("\n"); diff --git a/src/onyxparser.c b/src/onyxparser.c index 223ccbd0..738c9064 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -1466,7 +1466,17 @@ static AstFunction* parse_function_definition(OnyxParser* parser) { return (AstFunction *) ofunc; } - if (parse_possible_directive(parser, "intrinsic")) { + if (parse_possible_directive(parser, "add_overload")) { + if (func_def->overloaded_function != NULL) { + onyx_report_error(parser->curr->pos, "cannot have multiple #add_overload directives on a single procedure."); + } else { + AstNode* sym_node = make_node(AstNode, Ast_Kind_Symbol); + sym_node->token = expect_token(parser, Token_Type_Symbol); + func_def->overloaded_function = sym_node; + } + } + + else if (parse_possible_directive(parser, "intrinsic")) { func_def->flags |= Ast_Flag_Intrinsic; if (parser->curr->type == Token_Type_Literal_String) { diff --git a/src/onyxsymres.c b/src/onyxsymres.c index a91641ad..e048f544 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -465,6 +465,20 @@ void symres_function(AstFunction* func) { } } + if (func->overloaded_function != NULL) { + symres_expression((AstTyped **) &func->overloaded_function); + if (func->overloaded_function == NULL) return; // NOTE: Error message will already be generated + + if (func->overloaded_function->kind != Ast_Kind_Overloaded_Function) { + onyx_report_error(func->token->pos, "#add_overload directive did not resolve to an overloaded function."); + return; + + } else { + AstOverloadedFunction* ofunc = (AstOverloadedFunction *) func->overloaded_function; + bh_arr_push(ofunc->overloads, (AstTyped *) func); + } + } + func->return_type = symres_type(func->return_type); scope_enter(func->scope);