small cleanup with macros
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 17 Aug 2021 03:07:09 +0000 (22:07 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 17 Aug 2021 03:07:09 +0000 (22:07 -0500)
CHANGELOG
core/container/array.onyx
core/container/bucket_array.onyx
core/conv.onyx
core/string/buffer.onyx
include/onyxastnodes.h
modules/json/example.onyx
modules/wasm_utils/instructions.onyx
src/onyxchecker.c
src/onyxutils.c
tests/bucket_array.onyx

index c8478d94c9c6f9d11c5d79384fc509f1a04192cc..d7217c3f46130a52204dc9bf864d5691b67af04d 100644 (file)
--- 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.
index 5e66d6e2ba3f0dd7c42dd329666cc8fa0713d40f..4115b65d58092d852b8cf8d921a6486638276043 100644 (file)
@@ -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;
index 10a22d161f0062ab90ba1e420682ec53b9fe6ddc..e11ad9aca6cb0740d5f32a3b78b06e450dc4302d 100644 (file)
@@ -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];
index 52f81f5b6945a146c737a1ed8d13237cfac71ab4..fba51cb1effc9133f7537b29d1fee5c4894eb50e 100644 (file)
@@ -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;
         }
 
index 382d9d37f6bec85f87ac38029b40f27b4726c946..75fecbf8459f8c97c53305a22e3291dd8abba411 100644 (file)
@@ -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];
index e53efbfd98271bbfef06df02dec01d34451d91d9..877cbce0bb7774ea872afbdce8ff9fd3031ffdda 100644 (file)
@@ -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;
 
index 7306cf6966ccfcf7fd41bf561b3ae86f45651afe..245583746426fbe880aed0fefd1dceedd918ddc0 100644 (file)
@@ -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
index a6bed62c47a2b4a53e583c15eedf17db3b91902a..c826c1316ec0c06e449d7d6c0c83e739a75ca409 100644 (file)
@@ -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; }
index 2704b088c549f7c77e0ce78c4fc047d4c7d2e588..a8947f421ecd18cb42b85bdb616cfcb2dbcfa3ca 100644 (file)
@@ -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;
 
index 6cac0d55ff570cfb8b50ee1157e163e2da41eec0..6a0c4bb427cabe1aee64ab749716c01f48a915fd 100644 (file)
@@ -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;
     }
index 3ab81e306032cce1131b0e0a59e10d28cf492634..02101106dbe31afa8ea7c959f714c5c1aa09584a 100644 (file)
@@ -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 {