From 4950af8efd40ab57db5116e48d446ce2ab9e21b5 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Mon, 16 Aug 2021 22:07:09 -0500 Subject: [PATCH] small cleanup with macros --- CHANGELOG | 1 + core/container/array.onyx | 2 +- core/container/bucket_array.onyx | 4 +++- core/conv.onyx | 12 ++++++++---- core/string/buffer.onyx | 2 +- include/onyxastnodes.h | 7 +++---- modules/json/example.onyx | 2 +- modules/wasm_utils/instructions.onyx | 4 ++-- src/onyxchecker.c | 14 +------------- src/onyxutils.c | 4 +++- tests/bucket_array.onyx | 5 +---- 11 files changed, 25 insertions(+), 32 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index c8478d94..d7217c3f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -8,6 +8,7 @@ are going to be changed and added in the future, but first I want to get this be version out there in a packaged format. Additions: +* Macros and paste-able code blocks * overloaded procedures can have polymorphic and overloaded procedures as overloads. This makes them MUCH more powerful. * types are not required on struct members that have default values. This is still very experimental so there are some noticable bugs with it. You can always specify the type explicitly if you have issues. diff --git a/core/container/array.onyx b/core/container/array.onyx index 5e66d6e2..4115b65d 100644 --- a/core/container/array.onyx +++ b/core/container/array.onyx @@ -75,7 +75,7 @@ push :: (arr: ^[..] $T, x: T) -> bool { } // Semi-useful shortcut for adding something to an array. -#operator << macro (arr: [..] $T, v: T) do array.push(^arr, v); +#operator << macro (arr: [..] $T, v: T) do (package core.array).push(^arr, v); insert :: (arr: ^[..] $T, idx: u32, x: T) -> bool { if !ensure_capacity(arr, arr.count + 1) do return false; diff --git a/core/container/bucket_array.onyx b/core/container/bucket_array.onyx index 10a22d16..e11ad9ac 100644 --- a/core/container/bucket_array.onyx +++ b/core/container/bucket_array.onyx @@ -73,13 +73,15 @@ push :: (use b: ^Bucket_Array($T), elem: T) -> bool { } } +#operator << macro (b: Bucket_Array($T), elem: T) do (package core.bucket_array).push(^b, elem); + pop :: (use b: ^Bucket_Array($T)) { last_bucket := ^buckets[buckets.count - 1]; last_bucket.count -= 1; } // Give you a pointer to the bucket data via 'it'. -for_each :: macro (b: Bucket_Array($T), $body: Code) { +for_each :: macro (b: Bucket_Array($T), body: Code) { for ^bucket: b.buckets { for bucket_index: bucket.count { it := ^bucket.data[bucket_index]; diff --git a/core/conv.onyx b/core/conv.onyx index 52f81f5b..fba51cb1 100644 --- a/core/conv.onyx +++ b/core/conv.onyx @@ -333,6 +333,14 @@ str_format_va :: (format: str, buffer: [] u8, va: [] any) -> str { formatting.minimum_width = ~~digits; } + case #char "}" { + arg := va[vararg_index]; + vararg_index += 1; + print_any(^output, formatting, arg); + + break break; + } + case #default do break break; } } @@ -345,10 +353,6 @@ str_format_va :: (format: str, buffer: [] u8, va: [] any) -> str { continue; } - arg := va[vararg_index]; - vararg_index += 1; - print_any(^output, formatting, arg); - continue; } diff --git a/core/string/buffer.onyx b/core/string/buffer.onyx index 382d9d37..75fecbf8 100644 --- a/core/string/buffer.onyx +++ b/core/string/buffer.onyx @@ -48,7 +48,7 @@ buffer_insert :: (use buffer: ^String_Buffer, position: i32, ch: u8) -> bool { } buffer_append :: (use buffer: ^String_Buffer, end: str) -> bool { - if count + end.count == capacity do return false; + if count + end.count > capacity do return false; for i: end.count { data[i + count] = end[i]; diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index e53efbfd..877cbce0 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -485,11 +485,10 @@ typedef enum VarArgKind { typedef enum BlockRule { Block_Rule_New_Scope = BH_BIT(1), - Block_Rule_New_Binding_Scope = BH_BIT(2), // Unused - Block_Rule_Clear_Defer = BH_BIT(3), + Block_Rule_Clear_Defer = BH_BIT(2), - Block_Rule_Normal = Block_Rule_New_Scope | Block_Rule_New_Binding_Scope | Block_Rule_Clear_Defer, - Block_Rule_Macro = Block_Rule_New_Scope | Block_Rule_New_Binding_Scope, + Block_Rule_Normal = Block_Rule_New_Scope | Block_Rule_Clear_Defer, + Block_Rule_Macro = Block_Rule_New_Scope, Block_Rule_Code_Block = Block_Rule_New_Scope, } BlockRule; diff --git a/modules/json/example.onyx b/modules/json/example.onyx index 7306cf69..24558374 100644 --- a/modules/json/example.onyx +++ b/modules/json/example.onyx @@ -52,7 +52,7 @@ Vector2 :: struct { } #add_match json.encode, (w: ^io.Writer, v: Vector2) -> json.Encoding_Error { - io.write_format(w, "{\"x\":%f,\"y\":%f}", v.x, v.y); + io.write_format(w, "{{\"x\":{},\"y\":{}}}", v.x, v.y); return .None; } \ No newline at end of file diff --git a/modules/wasm_utils/instructions.onyx b/modules/wasm_utils/instructions.onyx index a6bed62c..c826c131 100644 --- a/modules/wasm_utils/instructions.onyx +++ b/modules/wasm_utils/instructions.onyx @@ -298,8 +298,8 @@ parse_instruction :: (reader: ^io.Reader, binary: ^WasmBinary, code_offset := 0, ib := io.read_byte(reader); switch cast(u32) ib { - case 0x00 do instr.code = .unreachable; - case 0x01 do instr.code = .nop; + case 0x00 { instr.code = .unreachable; } + case 0x01 { instr.code = .nop; } case 0x02 { instr.code = .block; block_depth += 1; parse_after = .Block_Type; } case 0x03 { instr.code = .loop; block_depth += 1; parse_after = .Block_Type; } case 0x04 { instr.code = .if_; block_depth += 1; parse_after = .Block_Type; } diff --git a/src/onyxchecker.c b/src/onyxchecker.c index 2704b088..a8947f42 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -1805,19 +1805,7 @@ CheckStatus check_insert_directive(AstDirectiveInsert** pinsert) { AstNode* cloned_block = ast_clone(context.ast_alloc, code_block->code); cloned_block->next = insert->next; - - /*if (cloned_block->kind == Ast_Kind_Block) { - AstNode* next = insert->next; - insert->next = (AstNode *) ((AstBlock *) cloned_block)->body; - - AstNode* last_stmt = insert->next; - while (last_stmt->next != NULL) last_stmt = last_stmt->next; - last_stmt->next = next; - - } else { - */ - *(AstNode **) pinsert = cloned_block; - //} + *(AstNode **) pinsert = cloned_block; insert->flags |= Ast_Flag_Has_Been_Checked; diff --git a/src/onyxutils.c b/src/onyxutils.c index 6cac0d55..6a0c4bb4 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -1236,8 +1236,10 @@ void expand_macro(AstCall** pcall, AstFunction* template) { AstPolyProc* pp = (AstPolyProc *) macro->body; bh_table_each_start(AstSolidifiedFunction, pp->concrete_funcs); - if (value.func == template) + if (value.func == template) { scope_include(argument_scope, value.poly_scope, call->token->pos); + break; + } bh_table_each_end; } diff --git a/tests/bucket_array.onyx b/tests/bucket_array.onyx index 3ab81e30..02101106 100644 --- a/tests/bucket_array.onyx +++ b/tests/bucket_array.onyx @@ -3,11 +3,8 @@ use package core main :: (args: [] cstr) { - ba := bucket_array.make(i32, 4); - for i: 24 { - bucket_array.push(^ba, i); - } + for i: 24 do ba << i; sum := 0; bucket_array.for_each(ba, #code { -- 2.25.1