From 6459fc42bd5d3df382cdf6aafeeddec928bc41af Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Mon, 3 Oct 2022 14:35:45 -0500 Subject: [PATCH] removed `#with` in interfaces; use T.{} --- compiler/include/astnodes.h | 6 ------ compiler/src/checker.c | 10 ---------- compiler/src/parser.c | 13 ------------- core/container/array.onyx | 10 ++++++++++ core/hash.onyx | 1 + 5 files changed, 11 insertions(+), 29 deletions(-) diff --git a/compiler/include/astnodes.h b/compiler/include/astnodes.h index 246d4dc1..4ac685b2 100644 --- a/compiler/include/astnodes.h +++ b/compiler/include/astnodes.h @@ -1101,18 +1101,12 @@ typedef struct InterfaceConstraint { b32 invert_condition: 1; } InterfaceConstraint; -typedef struct InterfaceVariable { - OnyxToken *symbol; - AstType *type; -} InterfaceVariable; - struct AstInterface { AstTyped_base; char *name; bh_arr(InterfaceParam) params; bh_arr(InterfaceConstraint) exprs; - bh_arr(InterfaceVariable) vars; }; typedef enum ConstraintPhase { diff --git a/compiler/src/checker.c b/compiler/src/checker.c index cc0c9adb..0854670c 100644 --- a/compiler/src/checker.c +++ b/compiler/src/checker.c @@ -2969,16 +2969,6 @@ CheckStatus check_constraint(AstConstraint *constraint) { symbol_introduce(constraint->scope, ip->type_token, (AstNode *) type_alias); } - fori (i, 0, bh_arr_length(constraint->interface->vars)) { - InterfaceVariable *iv = &constraint->interface->vars[i]; - - AstTyped *sentinel = onyx_ast_node_new(context.ast_alloc, sizeof(AstTyped), Ast_Kind_Constraint_Sentinel); - sentinel->token = iv->symbol; - sentinel->type_node = iv->type; - - symbol_introduce(constraint->scope, iv->symbol, (AstNode *) sentinel); - } - assert(constraint->entity); constraint->entity->scope = constraint->scope; diff --git a/compiler/src/parser.c b/compiler/src/parser.c index 912312e9..246c9582 100644 --- a/compiler/src/parser.c +++ b/compiler/src/parser.c @@ -2177,24 +2177,11 @@ static AstInterface* parse_interface(OnyxParser* parser) { } bh_arr_new(global_heap_allocator, interface->exprs, 2); - bh_arr_new(global_heap_allocator, interface->vars, 2); expect_token(parser, '{'); while (!consume_token_if_next(parser, '}')) { if (parser->hit_unexpected_token) return interface; - if (parse_possible_directive(parser, "with")) { - InterfaceVariable v; - v.symbol = expect_token(parser, Token_Type_Symbol); - expect_token(parser, ':'); - - v.type = parse_type(parser); - expect_token(parser, ';'); - - bh_arr_push(interface->vars, v); - continue; - } - InterfaceConstraint ic = {0}; if (parse_possible_directive(parser, "not")) { ic.invert_condition = 1; diff --git a/core/container/array.onyx b/core/container/array.onyx index 41e92972..3d5f3169 100644 --- a/core/container/array.onyx +++ b/core/container/array.onyx @@ -442,12 +442,22 @@ unique :: (arr: ^[] $T) { } +fold :: #match #local {} + +#overload fold :: (arr: [] $T, init: $R, f: (T, R) -> R) -> R { val := init; for it: arr do val = f(it, val); return val; } +#overload +fold :: macro (arr: [] $T, init: $R, body: Code) -> R { + acc := init; + for it: arr do acc = #unquote body; + return acc; +} + map :: #match #locked { macro (arr: [] $T, f: (^T) -> void) do for ^it: arr do f(it);, macro (arr: [] $T, f: (T) -> T) do for ^it: arr do *it = f(*it);, diff --git a/core/hash.onyx b/core/hash.onyx index 8935dda7..364ef6ae 100644 --- a/core/hash.onyx +++ b/core/hash.onyx @@ -21,6 +21,7 @@ Hashable :: interface (t: $T) { { to_u32(t) } -> u32; } +#local HasHashMethod :: interface (t: $T) { { t->hash() } -> u32; } -- 2.25.1