From 6a2d3eada303efc2154d6a83fd3dcd61e836e37c Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sat, 3 Jun 2023 00:22:39 -0500 Subject: [PATCH] changed: removed `#quote` in favor of parameter syntax `[] {}` --- compiler/include/astnodes.h | 3 + compiler/src/checker.c | 49 +++++++++++++ compiler/src/clone.c | 10 +++ compiler/src/parser.c | 88 ++++++++++++++---------- compiler/src/symres.c | 6 ++ core/alloc/arena.onyx | 2 +- core/container/array.onyx | 5 +- core/container/bucket_array.onyx | 2 +- core/container/iter.onyx | 20 ++++-- core/container/map.onyx | 4 +- core/container/optional.onyx | 12 +++- core/container/result.onyx | 4 +- core/container/slice.onyx | 57 ++++++++++----- core/conv/format.onyx | 2 +- core/encoding/csv.onyx | 6 +- core/encoding/ini.onyx | 2 +- core/encoding/json/encoder.onyx | 12 ++-- core/encoding/utf8.onyx | 2 +- core/net/tcp.onyx | 4 +- core/os/file.onyx | 2 +- core/random/random.onyx | 6 +- core/runtime/info/helper.onyx | 4 +- core/runtime/info/proc_tags.onyx | 2 +- core/runtime/platform/wasi/wasi_env.onyx | 2 +- core/string/string.onyx | 2 +- core/time/date.onyx | 1 - examples/18_macros.onyx | 4 +- interpreter/src/wasm/type.c | 2 +- scripts/onyx-pkg.onyx | 2 +- tests/aoc-2020/day24.onyx | 2 +- tests/aoc-2021/day03.onyx | 4 +- tests/aoc-2021/day15.onyx | 8 +-- tests/bucket_array.onyx | 6 +- tests/switch_using_equals.onyx | 4 +- 34 files changed, 231 insertions(+), 110 deletions(-) diff --git a/compiler/include/astnodes.h b/compiler/include/astnodes.h index e1453471..8bd054c1 100644 --- a/compiler/include/astnodes.h +++ b/compiler/include/astnodes.h @@ -814,6 +814,7 @@ struct AstBlock { Scope *scope; Scope *binding_scope; + Scope *quoted_block_capture_scope; BlockRule rules; u32 statement_idx; @@ -1518,12 +1519,14 @@ struct AstCodeBlock { AstTyped_base; AstNode *code; + bh_arr(OnyxToken *) binding_symbols; }; struct AstDirectiveInsert { AstTyped_base; AstTyped *code_expr; + bh_arr(AstTyped *) binding_exprs; }; struct AstDirectiveInit { diff --git a/compiler/src/checker.c b/compiler/src/checker.c index 2abf59de..a5fb1168 100644 --- a/compiler/src/checker.c +++ b/compiler/src/checker.c @@ -2455,6 +2455,10 @@ CheckStatus check_insert_directive(AstDirectiveInsert** pinsert) { YIELD(insert->token->pos, "Waiting for resolution to code expression type."); } + bh_arr_each(AstTyped *, pexpr, insert->binding_exprs) { + CHECK(expression, pexpr); + } + Type* code_type = type_build_from_ast(context.ast_alloc, builtin_code_type); TYPE_CHECK(&insert->code_expr, code_type) { @@ -2469,8 +2473,53 @@ CheckStatus check_insert_directive(AstDirectiveInsert** pinsert) { ERROR(insert->token->pos, "Expected compile-time known expression of type 'Code'."); } + u32 bound_symbol_count = bh_arr_length(code_block->binding_symbols); + u32 bound_expr_count = bh_arr_length(insert->binding_exprs); + if (bound_symbol_count > bound_expr_count) { + onyx_report_error(insert->token->pos, Error_Critical, + "Expected at least %d argument%s to unquote code block, only got %d.", + bound_symbol_count, bh_num_plural(bound_symbol_count), bound_expr_count); + ERROR(code_block->token->pos, "Here is the code block being unquoted."); + } + AstNode* cloned_block = ast_clone(context.ast_alloc, code_block->code); cloned_block->next = insert->next; + + if (bound_expr_count > 0) { + Scope **scope = NULL; + + if (cloned_block->kind == Ast_Kind_Block) { + scope = &((AstBlock *) cloned_block)->quoted_block_capture_scope; + + } else if (bound_symbol_count > 0) { + AstReturn* return_node = onyx_ast_node_new(context.ast_alloc, sizeof(AstReturn), Ast_Kind_Return); + return_node->token = cloned_block->token; + return_node->expr = (AstTyped *) cloned_block; + + AstBlock* body_block = onyx_ast_node_new(context.ast_alloc, sizeof(AstBlock), Ast_Kind_Block); + body_block->token = cloned_block->token; + body_block->body = (AstNode *) return_node; + scope = &((AstBlock *) body_block)->quoted_block_capture_scope; + + AstDoBlock* doblock = (AstDoBlock *) onyx_ast_node_new(context.ast_alloc, sizeof(AstDoBlock), Ast_Kind_Do_Block); + doblock->token = cloned_block->token; + doblock->block = body_block; + doblock->type = &type_auto_return; + doblock->next = cloned_block->next; + + cloned_block = (AstNode *) doblock; + } + + if (bound_symbol_count > 0) { + assert(scope); + *scope = scope_create(context.ast_alloc, NULL, code_block->token->pos); + + fori (i, 0, bound_symbol_count) { + symbol_introduce(*scope, code_block->binding_symbols[i], (AstNode *) insert->binding_exprs[i]); + } + } + } + *(AstNode **) pinsert = cloned_block; insert->flags |= Ast_Flag_Has_Been_Checked; diff --git a/compiler/src/clone.c b/compiler/src/clone.c index 20897a40..449899b0 100644 --- a/compiler/src/clone.c +++ b/compiler/src/clone.c @@ -272,6 +272,7 @@ AstNode* ast_clone(bh_allocator a, void* n) { case Ast_Kind_Block: ((AstBlock *) nn)->body = ast_clone_list(a, ((AstBlock *) node)->body); + ((AstBlock *) nn)->quoted_block_capture_scope = NULL; break; case Ast_Kind_Defer: @@ -646,6 +647,15 @@ AstNode* ast_clone(bh_allocator a, void* n) { case Ast_Kind_Directive_Insert: C(AstDirectiveInsert, code_expr); + + AstDirectiveInsert* id = (AstDirectiveInsert *) nn; + AstDirectiveInsert* is = (AstDirectiveInsert *) node; + id->binding_exprs = NULL; + bh_arr_new(global_heap_allocator, id->binding_exprs, bh_arr_length(is->binding_exprs)); + + bh_arr_each(AstTyped *, expr, is->binding_exprs) { + bh_arr_push(id->binding_exprs, (AstTyped *) ast_clone(a, (AstNode *) *expr)); + } break; case Ast_Kind_Directive_Defined: diff --git a/compiler/src/parser.c b/compiler/src/parser.c index 441d0dad..98e39149 100644 --- a/compiler/src/parser.c +++ b/compiler/src/parser.c @@ -614,8 +614,44 @@ static AstTyped* parse_factor(OnyxParser* parser) { break; } - case '?': case '[': { + // HACK CLEANUP + // :LinearTokenDependent + OnyxToken *matching = find_matching_paren(parser->curr); + if (matching->type == ']' && + ((matching + 1)->type == '{' || (matching + 1)->type == '(')) { + AstCodeBlock* code_block = make_node(AstCodeBlock, Ast_Kind_Code_Block); + code_block->token = expect_token(parser, '['); + + assert(builtin_code_type != NULL); + code_block->type_node = builtin_code_type; + + bh_arr_new(global_heap_allocator, code_block->binding_symbols, 4); + while (!consume_token_if_next(parser, ']')) { + if (parser->hit_unexpected_token) return (AstTyped *) code_block; + + OnyxToken *symbol = expect_token(parser, Token_Type_Symbol); + bh_arr_push(code_block->binding_symbols, symbol); + + if (parser->curr->type != ']') + expect_token(parser, ','); + } + + if (parser->curr->type == '{') { + code_block->code = (AstNode *) parse_block(parser, 1, NULL); + ((AstBlock *) code_block->code)->rules = Block_Rule_Code_Block; + } else { + code_block->code = (AstNode *) parse_expression(parser, 1); + } + + retval = (AstTyped *) code_block; + break; + } + + // :fallthrough + } + + case '?': { AstType *type = parse_type(parser); retval = (AstTyped *) type; break; @@ -768,43 +804,6 @@ static AstTyped* parse_factor(OnyxParser* parser) { retval = (AstTyped *) defined; break; } - else if (parse_possible_directive(parser, "quote")) { - OnyxToken* code_token = parser->curr - 1; - - AstCodeBlock* code_block = make_node(AstCodeBlock, Ast_Kind_Code_Block); - code_block->token = code_token; - - assert(builtin_code_type != NULL); - code_block->type_node = builtin_code_type; - - if (parser->curr->type == '{') { - code_block->code = (AstNode *) parse_block(parser, 1, NULL); - ((AstBlock *) code_block->code)->rules = Block_Rule_Code_Block; - - } else { - code_block->code = (AstNode *) parse_expression(parser, 0); - } - - retval = (AstTyped *) code_block; - break; - } - else if (next_tokens_are(parser, 2, '#', '(')) { - OnyxToken* code_token = expect_token(parser, '#'); - expect_token(parser, '('); - - AstCodeBlock* code_block = make_node(AstCodeBlock, Ast_Kind_Code_Block); - code_block->token = code_token; - - assert(builtin_code_type != NULL); - code_block->type_node = builtin_code_type; - - code_block->code = (AstNode *) parse_expression(parser, 0); - - expect_token(parser, ')'); - - retval = (AstTyped *) code_block; - break; - } else if (parse_possible_directive(parser, "unquote")) { AstDirectiveInsert* insert = make_node(AstDirectiveInsert, Ast_Kind_Directive_Insert); insert->token = parser->curr - 1; @@ -820,6 +819,19 @@ static AstTyped* parse_factor(OnyxParser* parser) { insert->code_expr = parse_expression(parser, 0); parser->parse_calls = 1; + if (consume_token_if_next(parser, '(')) { + bh_arr_new(global_heap_allocator, insert->binding_exprs, 4); + while (!consume_token_if_next(parser, ')')) { + if (parser->hit_unexpected_token) break; + + AstTyped *expr = parse_expression(parser, 0); + bh_arr_push(insert->binding_exprs, expr); + + if (parser->curr->type != ')') + expect_token(parser, ','); + } + } + retval = (AstTyped *) insert; break; } diff --git a/compiler/src/symres.c b/compiler/src/symres.c index 527b9be1..385e7743 100644 --- a/compiler/src/symres.c +++ b/compiler/src/symres.c @@ -916,6 +916,9 @@ static SymresStatus symres_directive_defined(AstDirectiveDefined** pdefined) { static SymresStatus symres_directive_insert(AstDirectiveInsert* insert) { SYMRES(expression, &insert->code_expr); + bh_arr_each(AstTyped *, pexpr, insert->binding_exprs) { + SYMRES(expression, pexpr); + } return Symres_Success; } @@ -1003,6 +1006,9 @@ static SymresStatus symres_block(AstBlock* block) { if (block->binding_scope != NULL) scope_include(current_scope, block->binding_scope, block->token->pos); + if (block->quoted_block_capture_scope != NULL) + scope_include(current_scope, block->quoted_block_capture_scope, block->token->pos); + if (block->body) { AstNode** start = &block->body; fori (i, 0, block->statement_idx) { diff --git a/core/alloc/arena.onyx b/core/alloc/arena.onyx index 61ea8681..7e736b1b 100644 --- a/core/alloc/arena.onyx +++ b/core/alloc/arena.onyx @@ -156,7 +156,7 @@ get_allocated_bytes :: (arena: &Arena) -> u32 { } auto :: #match { - macro (size := 32 * 1024, $dest: Code = #(context.allocator)) { + macro (size := 32 * 1024, $dest: Code = [](context.allocator)) { use core.alloc {arena, heap_allocator} a := arena.make(heap_allocator, size); diff --git a/core/container/array.onyx b/core/container/array.onyx index be2ad6e2..8d0bca8f 100644 --- a/core/container/array.onyx +++ b/core/container/array.onyx @@ -300,7 +300,7 @@ concat :: (arr: &[..] $T, other: Iterator(T)) { Use `it` to refer to the current element being tested. arr := array.make(.[ 1, 2, 3, 4, 5 ]); - array.filter(&arr, #(it % 2 == 0)); + array.filter(&arr, [v](v % 2 == 0)); println(arr); // 2, 4 """ filter :: macro (arr: &[..] $T, body: Code) { @@ -310,7 +310,7 @@ filter :: macro (arr: &[..] $T, body: Code) { defer i += 1; it := arr[i]; - if !(#unquote body) do move += 1; + if !(#unquote body(it)) do move += 1; if move != 0 do arr.data[i] = arr.data[i + move]; } @@ -354,6 +354,7 @@ fill_range :: slice.fill_range to_list :: slice.to_list find :: slice.find find_ptr :: slice.find_ptr +find_opt :: slice.find_opt first :: slice.first count_where :: slice.count_where windows :: slice.windows diff --git a/core/container/bucket_array.onyx b/core/container/bucket_array.onyx index e7eadb86..8df7e882 100644 --- a/core/container/bucket_array.onyx +++ b/core/container/bucket_array.onyx @@ -99,7 +99,7 @@ for_each :: macro (b: Bucket_Array($T), body: Code) { for bucket_index: bucket.count { it := &bucket.data[bucket_index]; - #unquote body; + #unquote body(it); } } } diff --git a/core/container/iter.onyx b/core/container/iter.onyx index 07c477d1..d7a78478 100644 --- a/core/container/iter.onyx +++ b/core/container/iter.onyx @@ -529,7 +529,7 @@ take_one :: (it: Iterator($T), no_close := false) -> (T, bool) { value: i32; iterator: Iterator(i32) = ...; - if #(value) << iterator { + if [](value) << iterator { ...iterater closed... } """ @@ -649,13 +649,13 @@ generic_dynamic_array_as_iter :: (x: &[..] $T, $access: Code, $return_type: type #overload as_iter :: macro (x: &[..] $T) => { G :: generic_dynamic_array_as_iter - return G(x, #(arr.data[current]), Iterator(T)); + return G(x, [](arr.data[current]), Iterator(T)); } #overload as_iter :: macro (x: &[..] $T, by_pointer: bool) => { G :: generic_dynamic_array_as_iter - return G(x, #(&arr.data[current]), Iterator(&T)); + return G(x, [](&arr.data[current]), Iterator(&T)); } @@ -773,6 +773,14 @@ to_map :: (it: Iterator(Pair($K, $V)), allocator := context.allocator) -> Map(K, return m; } +#doc """ + Collects elements into an array, or a map, depending on if the + iterator produces a Pair(K, V) or not. +""" +collect :: #match { + to_array +} + #doc """ Produces an iterator that first yields all values from the @@ -832,7 +840,7 @@ prod :: (x: $I1/Iterable, y_iter: Iterator($Y)) => { Python: results = [it * 2 for it in [1, 2, 3, 4, 5]] Onyx: - results := iter.comp(u32.[1, 2, 3, 4, 5], #(it * 2)); + results := iter.comp(u32.[1, 2, 3, 4, 5], [it](it * 2)); """ comp :: #match #local {} @@ -843,7 +851,7 @@ comp :: macro (i: Iterator(&$V), value: Code) => { for __it: i { it := *__it; - a << (#unquote value); + a << (#unquote value(it)); } return a; } @@ -853,7 +861,7 @@ comp :: macro (i: Iterator($V), value: Code) => { it: V; a := make([..] typeof #unquote value); - for i do a << (#unquote value); + for i do a << (#unquote value(it)); return a; } diff --git a/core/container/map.onyx b/core/container/map.onyx index afaece43..aa3b6d41 100644 --- a/core/container/map.onyx +++ b/core/container/map.onyx @@ -213,7 +213,7 @@ to the value. *it += 10; } or: - m->update("test", #(*it += 10)); + m->update("test", [v](*v += 10)); """ update :: macro (map: ^Map, key: map.Key_Type, body: Code) { lookup_ :: lookup @@ -221,7 +221,7 @@ update :: macro (map: ^Map, key: map.Key_Type, body: Code) { if lr.entry_index >= 0 { it := &map.entries[lr.entry_index].value; - #unquote body; + #unquote body(it); } } diff --git a/core/container/optional.onyx b/core/container/optional.onyx index 6acb33e5..fa7bad94 100644 --- a/core/container/optional.onyx +++ b/core/container/optional.onyx @@ -146,6 +146,15 @@ use core } } + with :: macro (o: ?$T, body: Code) { + switch o { + case .None ---; + case .Some => it { + #unquote body(it); + } + } + } + #doc """ Creates a scope that the `?` operator on an Optional type can return to, instead of returning from the enclosing function. @@ -173,7 +182,7 @@ use core // ... }); """ - try :: macro (body: Code, catch: Code = #quote {}) -> bool { + try :: macro (body: Code, catch: Code = []{}) -> bool { // // Using a 'do'-expression block to introduce a new // 'return' location. This way, when the code in the `body` @@ -227,6 +236,7 @@ use core #operator ?? macro (opt: ?$T, catch: Code) -> T { switch value := opt; value { case .Some => v do return v; + case #default --- } #unquote catch; diff --git a/core/container/result.onyx b/core/container/result.onyx index 2ed9c51d..edd59589 100644 --- a/core/container/result.onyx +++ b/core/container/result.onyx @@ -175,8 +175,8 @@ Result :: union (Ok_Type: type_expr, Err_Type: type_expr) { catch :: macro (r: Result($T, $E), on_err: Code) -> T { switch res := r; res { case .Ok => v do return v; - case .Err { - #unquote on_err; + case .Err => err { + #unquote on_err(err); } } } diff --git a/core/container/slice.onyx b/core/container/slice.onyx index e5fed638..a54385f2 100644 --- a/core/container/slice.onyx +++ b/core/container/slice.onyx @@ -166,7 +166,7 @@ contains :: #match #locked { }, macro (arr: [] $T, $cmp: Code) -> bool { - for it: arr do if #unquote cmp do return true; + for it: arr do if #unquote cmp(it) do return true; return false; } } @@ -371,7 +371,7 @@ unique :: (arr: &[] $T) { // OR - slice.fold(arr, 0, #(it + acc)) |> println(); + slice.fold(arr, 0, [it](it + acc)) |> println(); """ fold :: #match #local {} @@ -385,7 +385,7 @@ fold :: (arr: [] $T, init: $R, f: (T, R) -> R) -> R { #overload fold :: macro (arr: [] $T, init: $R, body: Code) -> R { acc := init; - for it: arr do acc = #unquote body; + for it: arr do acc = #unquote body(it, acc); return acc; } @@ -395,12 +395,12 @@ fold :: macro (arr: [] $T, init: $R, body: Code) -> R { every :: #match #local {} #overload -every :: macro (arr: [] $T, predicate: (T) -> bool) => #this_package.every(arr, #(predicate(it))); +every :: macro (arr: [] $T, predicate: (T) -> bool) => #this_package.every(arr, [it](predicate(it))); #overload every :: macro (arr: [] $T, predicate_body: Code) -> bool { for arr { - if !(#unquote predicate_body) do return false; + if !(#unquote predicate_body(it)) do return false; } return true; } @@ -411,12 +411,12 @@ every :: macro (arr: [] $T, predicate_body: Code) -> bool { some :: #match #local {} #overload -some :: macro (arr: [] $T, predicate: (T) -> bool) => #this_package.some(arr, #(predicate(it))); +some :: macro (arr: [] $T, predicate: (T) -> bool) => #this_package.some(arr, [it](predicate(it))); #overload some :: macro (arr: [] $T/type_is_struct, predicate_body: Code) -> bool { for & arr { - if #unquote predicate_body do return true; + if #unquote predicate_body(it) do return true; } return false; } @@ -424,7 +424,7 @@ some :: macro (arr: [] $T/type_is_struct, predicate_body: Code) -> bool { #overload some :: macro (arr: [] $T, predicate_body: Code) -> bool { for arr { - if #unquote predicate_body do return true; + if #unquote predicate_body(it) do return true; } return false; } @@ -481,7 +481,7 @@ find :: (arr: [] $T, value: T) -> i32 { find :: macro (arr: [] $T/type_is_struct, pred: Code) -> i32 { for i: arr.count { it := &arr[i]; - if #unquote pred do return i; + if #unquote pred(it) do return i; } return -1; @@ -491,7 +491,7 @@ find :: macro (arr: [] $T/type_is_struct, pred: Code) -> i32 { find :: macro (arr: [] $T, pred: Code) -> i32 { for i: arr.count { it := arr[i]; - if #unquote pred do return i; + if #unquote pred(it) do return i; } return -1; @@ -510,15 +510,38 @@ find_ptr :: (arr: [] $T, value: T) -> &T { return null; } +#doc """ + +""" +find_opt :: #match { + macro (arr: [] $T/type_is_struct, cond: Code) -> ? T { + for& it: arr { + if #unquote cond(it) { + return *it; + } + } + return .{}; + }, + + macro (arr: [] $T, cond: Code) -> ? T { + for it: arr { + if #unquote cond(it) { + return it; + } + } + return .{}; + }, +} + first :: #match #locked { macro (arr: [] $T, predicate: (T) -> bool) -> &T { first :: first - return first(arr, #(predicate(it))); + return first(arr, [it](predicate(it))); }, macro (arr: [] $T/type_is_struct, predicate_body: Code) -> &T { for & arr { - if #unquote predicate_body do return it; + if #unquote predicate_body(it) do return it; } return null; @@ -530,7 +553,7 @@ first :: #match #locked { // structure. for &it_ptr: arr { it := *it_ptr; - if #unquote predicate_body do return it_ptr; + if #unquote predicate_body(it) do return it_ptr; } return null; @@ -543,13 +566,13 @@ first :: #match #locked { count_where :: #match #local {} #overload -count_where :: macro (arr: [] $T, predicate: (T) -> bool) => #this_package.count_where(arr, #(predicate(it))); +count_where :: macro (arr: [] $T, predicate: (T) -> bool) => #this_package.count_where(arr, [it](predicate(it))); #overload count_where :: macro (arr: [] $T, predicate_body: Code) -> u32 { count: u32 = 0; for arr { - if #unquote predicate_body do count += 1; + if #unquote predicate_body(it) do count += 1; } return count; } @@ -618,7 +641,7 @@ chunks :: (arr: [] $T, width: i32) -> Iterator([] T) { """ greatest :: macro (arr: [] $T) -> (i32, T) { fold_idx_elem :: fold_idx_elem - return fold_idx_elem(arr, #(*A > *B)); + return fold_idx_elem(arr, [](*A > *B)); } #doc """ @@ -626,5 +649,5 @@ greatest :: macro (arr: [] $T) -> (i32, T) { """ least :: macro (arr: [] $T) -> (i32, T) { fold_idx_elem :: fold_idx_elem - return fold_idx_elem(arr, #(*A < *B)); + return fold_idx_elem(arr, [](*A < *B)); } diff --git a/core/conv/format.onyx b/core/conv/format.onyx index eb15333b..ce9aa448 100644 --- a/core/conv/format.onyx +++ b/core/conv/format.onyx @@ -729,7 +729,7 @@ format_any :: (output: &Format_Output, formatting: &Format, v: any) { tag_value := *cast(&u32, v.data); - variant := array.first(u.variants, #(it.tag_value == tag_value)); + variant := array.first(u.variants, [x](x.tag_value == tag_value)); if !variant { output->write("unknown_variant"); diff --git a/core/encoding/csv.onyx b/core/encoding/csv.onyx index 915f1578..3e82d45b 100644 --- a/core/encoding/csv.onyx +++ b/core/encoding/csv.onyx @@ -93,8 +93,8 @@ CSV_Column :: struct { |> string.strip_trailing_whitespace(); for header: string.split_iter(header_line, #char ",") { - member := array.first(output_type_info.members, #(do { - if tag := array.first(it.tags, #(it.type == CSV_Column)); tag { + member := array.first(output_type_info.members, [](do { + if tag := array.first(it.tags, [t](t.type == CSV_Column)); tag { return any_as(*tag, CSV_Column).name == header; } @@ -144,7 +144,7 @@ CSV_Column :: struct { for &member: output_type_info.members { if !#first do io.write(writer, ","); - if tag := array.first(member.tags, #(it.type == CSV_Column)); tag { + if tag := array.first(member.tags, [t](t.type == CSV_Column)); tag { io.write(writer, any_as(*tag, CSV_Column).name); } else { io.write(writer, member.name); diff --git a/core/encoding/ini.onyx b/core/encoding/ini.onyx index 56576c8a..1fe9e8c1 100644 --- a/core/encoding/ini.onyx +++ b/core/encoding/ini.onyx @@ -91,7 +91,7 @@ parse_ini_file_inner :: (r: &io.Reader, output_ptr: any) -> (IniParseResult, Ini output.type = (cast(&info.Type_Info_Pointer) t).to; } - alloc.arena.auto(dest=#(context.temp_allocator)); + alloc.arena.auto(dest=[](context.temp_allocator)); active_item_ptr := null; active_item_type := void; diff --git a/core/encoding/json/encoder.onyx b/core/encoding/json/encoder.onyx index 25f57081..60baaf1b 100644 --- a/core/encoding/json/encoder.onyx +++ b/core/encoding/json/encoder.onyx @@ -211,11 +211,11 @@ encode :: (w: ^io.Writer, data: any) -> Encoding_Error { for ^member: s.members { key := member.name; - if tag := array.first(member.tags, #(it.type == Custom_Key)); tag != null { + if tag := array.first(member.tags, [t](t.type == Custom_Key)); tag != null { key = (cast(^Custom_Key) tag.data).key; } - if tag := array.first(member.tags, #(it.type == type_expr)); tag != null { + if tag := array.first(member.tags, [t](t.type == type_expr)); tag != null { if *cast(^type_expr, tag.data) == Ignore { continue; } @@ -326,11 +326,11 @@ from_any :: (type: type_expr, in: rawptr, allocator := context.allocator) -> Val for^ member: s_info.members { key := member.name; - if tag := array.first(member.tags, #(it.type == Custom_Key)); tag != null { + if tag := array.first(member.tags, [t](t.type == Custom_Key)); tag != null { key = (cast(^Custom_Key) tag.data).key; } - if tag := array.first(member.tags, #(it.type == type_expr)); tag != null { + if tag := array.first(member.tags, [t](t.type == type_expr)); tag != null { if *cast(^type_expr, tag.data) == Ignore { continue; } @@ -424,11 +424,11 @@ as_any :: (value: Value, type: type_expr, out: rawptr) { for^ member: s_info.members { key := member.name; - if tag := array.first(member.tags, #(it.type == Custom_Key)); tag != null { + if tag := array.first(member.tags, [t](t.type == Custom_Key)); tag != null { key = (cast(^Custom_Key) tag.data).key; } - if tag := array.first(member.tags, #(it.type == type_expr)); tag != null { + if tag := array.first(member.tags, [t](t.type == type_expr)); tag != null { if *cast(^type_expr, tag.data) == Ignore { continue; } diff --git a/core/encoding/utf8.onyx b/core/encoding/utf8.onyx index 3c1117d2..a8045092 100644 --- a/core/encoding/utf8.onyx +++ b/core/encoding/utf8.onyx @@ -102,7 +102,7 @@ full_rune :: (buffer: [] u8) -> bool { // be a valid rune. if buffer.length < len do return false; - return array.every(buffer[1 .. buffer.length], #(it & 0xC0 == 0x80)); + return array.every(buffer[1 .. buffer.length], [c](c & 0xC0 == 0x80)); } rune_count :: (s: str) -> i32 { diff --git a/core/net/tcp.onyx b/core/net/tcp.onyx index 66d35e9e..f57281f3 100644 --- a/core/net/tcp.onyx +++ b/core/net/tcp.onyx @@ -286,7 +286,7 @@ tcp_server_pulse :: (use server: &TCP_Server) -> bool { } } - client_count = array.count_where(clients, #(it != null)); + client_count = array.count_where(clients, [v](v != null)); return server.alive; } @@ -308,7 +308,7 @@ tcp_server_broadcast :: (use server: &TCP_Server, data: [] u8, except: &TCP_Serv tcp_server_handle_events :: macro (server: &TCP_Server, handler: Code) { while server->pulse() { for iter.as_iter(&server.connection) { - switch it.kind do #unquote handler; + switch it.kind do #unquote handler(it); } } } diff --git a/core/os/file.onyx b/core/os/file.onyx index c0e694e4..c2c791f8 100644 --- a/core/os/file.onyx +++ b/core/os/file.onyx @@ -154,7 +154,7 @@ file_logger_open :: (filename: str, allocator := context.allocator) -> Result(&F file_logger.file = new(File, allocator); - *file_logger.file = open(filename, mode=.Append)->catch(#quote { + *file_logger.file = open(filename, mode=.Append)->catch([] { raw_free(allocator, file_logger.file); raw_free(allocator, file_logger); diff --git a/core/random/random.onyx b/core/random/random.onyx index c8bbe7d3..2423fb32 100644 --- a/core/random/random.onyx +++ b/core/random/random.onyx @@ -118,16 +118,16 @@ string :: (bytes_long: u32, alpha_numeric := false, allocator := context.allocat RANDOM_INCREMENT :: cast(i64) 11 #if #defined(runtime.platform.__random_get) { - __initial_value :: #(do { + __initial_value :: [](do { v: u64; runtime.platform.__random_get(.{ ~~&v, sizeof typeof v }); return v; }) } else { #if #defined(core.os.time) { - __initial_value :: #(core.os.time()) + __initial_value :: [](core.os.time()) } else { - __initial_value :: #(8675309) + __initial_value :: [](8675309) } } } diff --git a/core/runtime/info/helper.onyx b/core/runtime/info/helper.onyx index b7c921de..d97bbf21 100644 --- a/core/runtime/info/helper.onyx +++ b/core/runtime/info/helper.onyx @@ -321,7 +321,7 @@ get_struct_method :: (type: type_expr, method_name: str) -> &any { info := cast(&Type_Info_Struct) get_type_info(type); if info.kind != .Struct do return null; - method := core.array.first(info.methods, #(it.name == method_name)); + method := core.array.first(info.methods, [x](x.name == method_name)); if method != null do return &method.func; return null; } @@ -349,6 +349,6 @@ for_all_types :: macro (body: Code) { type_info := runtime.info.type_table[it]; type_idx : type_expr = ~~ it; - #unquote body; + #unquote body(type_info, type_idx); } } diff --git a/core/runtime/info/proc_tags.onyx b/core/runtime/info/proc_tags.onyx index 6472e135..42dfc491 100644 --- a/core/runtime/info/proc_tags.onyx +++ b/core/runtime/info/proc_tags.onyx @@ -35,7 +35,7 @@ get_procedures_with_tag :: ($tag_type: type_expr) -> [] GPWT_Result(tag_type) { results := make([..] GPWT_Result(tag_type)); for proc: tagged_procedures { - if tag := array.first(proc.tags, #(it.type == tag_type)); tag != null { + if tag := array.first(proc.tags, [v](v.type == tag_type)); tag != null { array.push(&results, .{ func = proc.func, type = proc.type, diff --git a/core/runtime/platform/wasi/wasi_env.onyx b/core/runtime/platform/wasi/wasi_env.onyx index df1b33fd..3ff2d07f 100644 --- a/core/runtime/platform/wasi/wasi_env.onyx +++ b/core/runtime/platform/wasi/wasi_env.onyx @@ -48,7 +48,7 @@ __get_all_env :: () -> [] Pair(str, str) { __get_env :: (key: str) -> ? str { __lookup_all_envs(); - return slice.first(all_env, #(it.first == key)) + return slice.first(all_env, [v](v.first == key)) |> Optional.from_ptr() |> Optional.transform(x => x.second); } diff --git a/core/string/string.onyx b/core/string/string.onyx index 81736c81..a1b89a40 100644 --- a/core/string/string.onyx +++ b/core/string/string.onyx @@ -171,7 +171,7 @@ contains :: (s: str, substr: str) -> bool { join :: (strs: [] str, sep: str, allocator := context.allocator) -> str { if strs.count == 0 do return ""; - len_sum := array.fold(strs, 0, #(acc + it.length)); + len_sum := array.fold(strs, 0, [v, acc](acc + v.length)); out := make(str, len_sum + (strs.count - 1) * sep.count); i := 0; diff --git a/core/time/date.onyx b/core/time/date.onyx index eeb21b3e..e84a90d5 100644 --- a/core/time/date.onyx +++ b/core/time/date.onyx @@ -1,7 +1,6 @@ package core.time use core {package, *} -use runtime Date :: struct { year: i32; diff --git a/examples/18_macros.onyx b/examples/18_macros.onyx index 1361d6d5..39a55e73 100644 --- a/examples/18_macros.onyx +++ b/examples/18_macros.onyx @@ -108,8 +108,8 @@ main :: (args: [] cstr) { triple_macro(simple_code); // As another small shorthand, if a code block only contains one expression (like the code block above), - // then you can write by wrapping the expression in #(...). - triple_macro(#(println("Short code block!"))); + // then you can write by wrapping the expression in [](...). + triple_macro([](println("Short code block!"))); // As yet another small piece of syntactic sugar, you can pass a code block to a macro or procedure in // the following way: diff --git a/interpreter/src/wasm/type.c b/interpreter/src/wasm/type.c index 8bc6298f..fc6e3ae4 100644 --- a/interpreter/src/wasm/type.c +++ b/interpreter/src/wasm/type.c @@ -157,7 +157,7 @@ WASM_DECLARE_VEC_IMPL(memorytype, *) // wasm_externtype_t -const wasm_externkind_t wasm_externtype_kind(const wasm_externtype_t *externtype) { +wasm_externkind_t wasm_externtype_kind(const wasm_externtype_t *externtype) { return externtype->kind; } diff --git a/scripts/onyx-pkg.onyx b/scripts/onyx-pkg.onyx index ddedbc00..6d2c1e4b 100644 --- a/scripts/onyx-pkg.onyx +++ b/scripts/onyx-pkg.onyx @@ -58,7 +58,7 @@ main :: (args: [] cstr) { command_procedures := runtime.info.get_procedures_with_tag(Command); defer delete(&command_procedures); - command_tag := array.first(command_procedures, #(it.tag.command == command)); + command_tag := array.first(command_procedures, [p](p.tag.command == command)); if !command_tag { run_help_command(arguments); return; diff --git a/tests/aoc-2020/day24.onyx b/tests/aoc-2020/day24.onyx index 903e11a4..9d183c7c 100644 --- a/tests/aoc-2020/day24.onyx +++ b/tests/aoc-2020/day24.onyx @@ -108,7 +108,7 @@ main :: (args: [] cstr) { } for &cell: cells_to_consider { - map.update(&grid, *cell, #quote { it.alive = it.next; }); + map.update(&grid, *cell, [v]{ v.alive = v.next; }); } array.clear(&cells_to_consider); diff --git a/tests/aoc-2021/day03.onyx b/tests/aoc-2021/day03.onyx index d9ccdd57..e922a5f8 100644 --- a/tests/aoc-2021/day03.onyx +++ b/tests/aoc-2021/day03.onyx @@ -73,8 +73,8 @@ main :: (args) => { } for iter.as_iter(range.{ BITS - 1, 0, -1 }) { - filter_array(oxygen_array, it, #(A >= B)); - filter_array(co2_array, it, #(A < B)); + filter_array(oxygen_array, it, [](A >= B)); + filter_array(co2_array, it, [](A < B)); } printf("Part 2: {}\n", oxygen_array[0] * co2_array[0]); diff --git a/tests/aoc-2021/day15.onyx b/tests/aoc-2021/day15.onyx index d1b08461..e9ff322f 100644 --- a/tests/aoc-2021/day15.onyx +++ b/tests/aoc-2021/day15.onyx @@ -56,10 +56,10 @@ main :: (args) => { } } - attempt_add(#(try.x > 0), -1, 0); - attempt_add(#(try.x < width * 5 - 1), 1, 0); - attempt_add(#(try.y > 0), 0, -1); - attempt_add(#(try.y < height * 5 - 1), 0, 1); + attempt_add([](try.x > 0), -1, 0); + attempt_add([](try.x < width * 5 - 1), 1, 0); + attempt_add([](try.y > 0), 0, -1); + attempt_add([](try.y < height * 5 - 1), 0, 1); } printf("Part 2: {}\n", minimum); diff --git a/tests/bucket_array.onyx b/tests/bucket_array.onyx index d19715fb..8a78e2e3 100644 --- a/tests/bucket_array.onyx +++ b/tests/bucket_array.onyx @@ -9,9 +9,9 @@ main :: (args: [] cstr) { printf("ba[10] is {}.\n", ba[10]); sum := 0; - bucket_array.for_each(ba, #quote { - printf("[{}] -> {}\n", bucket_index, *it); - sum += *it; + bucket_array.for_each(ba, [value] { + printf("[{}] -> {}\n", bucket_index, *value); + sum += *value; }); for it: bucket_array.as_iter(&ba) { diff --git a/tests/switch_using_equals.onyx b/tests/switch_using_equals.onyx index 9ed4e1a0..e0fd8051 100644 --- a/tests/switch_using_equals.onyx +++ b/tests/switch_using_equals.onyx @@ -5,7 +5,7 @@ use core {*} Vector2 :: struct { x, y: i32; } #operator == macro (v1: Vector2, v2: Vector2) => v1.x == v2.x && v1.y == v2.y; -none_of_the_above :: #quote { +none_of_the_above :: [] { case #default { println("Got default!"); } @@ -29,4 +29,4 @@ main :: (args: [] cstr) { #unquote none_of_the_above; } -} \ No newline at end of file +} -- 2.25.1