renamed #code and #insert to #quote and #unquote
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 25 Mar 2022 16:05:30 +0000 (11:05 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 25 Mar 2022 16:05:30 +0000 (11:05 -0500)
19 files changed:
core/alloc/arena.onyx
core/alloc/auto_heap.onyx
core/builtin.onyx
core/container/array.onyx
core/container/bucket_array.onyx
core/container/iter.onyx
core/container/map.onyx
core/net/tcp.onyx
core/sync/mutex.onyx
docs/builtins.md
examples/18_macros.onyx
http_test.onyx [new file with mode: 0644]
src/checker.c
src/parser.c
tests/aoc-2020/day24.onyx
tests/aoc-2021/day03.onyx
tests/aoc-2021/day15.onyx
tests/bucket_array.onyx
tests/switch_using_equals.onyx

index 5de268c03da97209590d3a2d40515c211facda70..3428bb8c39bb5b029e2f33dde3bb59d131f2f153 100644 (file)
@@ -128,7 +128,7 @@ auto :: #match {
 
         #context_scope {
             auto(size); 
-            #insert body;
+            #unquote body;
         }
 
         return 0;
index 84e54f873631032783597d759a97db25bce2fc3d..fd8bc399ee80f55b7e088fd57380cb2620fc95ba 100644 (file)
@@ -65,7 +65,7 @@ auto :: #match {
 
         #context_scope {
             auto(); 
-            #insert body;
+            #unquote body;
         }
 
         return 0;
index 3d22d7c076ea437a5190867652a22079ab6423c6..abd390c97c64ac7aa6f8be7b06c67480c67cad38 100644 (file)
@@ -207,7 +207,7 @@ any :: struct {
 }
 
 
-// Represents a code block. Not constructable outside of using a '#code' directive.
+// Represents a code block. Not constructable outside of using a '#quote' directive.
 Code :: struct {_:i32;}
 
 
index 745b078a89253412a45d73e0b0161203e6d6112d..af39135e08535fa7f08bcbb944cfb1838b93cccb 100644 (file)
@@ -253,7 +253,7 @@ contains :: #match {
     },
 
     macro (arr: [] $T, $cmp: Code) -> bool {
-        for it: arr do if #insert cmp do return true;
+        for it: arr do if #unquote cmp do return true;
         return false;
     }
 }
@@ -400,7 +400,7 @@ fold :: (arr: [] $T, init: $R, f: (T, R) -> R) -> R {
 map :: #match {
     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);,
-    macro (arr: [] $T, body: Code)                     do for ^it: arr do #insert body;,
+    macro (arr: [] $T, body: Code)                     do for ^it: arr do #unquote body;,
     macro (arr: [] $T, data: $R, f: (^T, R) -> void)   do for ^it: arr do f(it, data);,
     macro (arr: [] $T, data: $R, f: (T, R) -> T)       do for ^it: arr do *it = f(*it, data);,
 }
index 2786749c8ffa92097d27c803e0e0130f5ba2e7e9..f28e89f22d53ba6b9217a8f5c09e5a67e55954d4 100644 (file)
@@ -97,7 +97,7 @@ for_each :: macro (b: Bucket_Array($T), body: Code) {
         for bucket_index: bucket.count {
             it := ^bucket.data[bucket_index];
 
-            #insert body;
+            #unquote body;
         }
     }
 }
index 6fec799732151e465fe9609f2b7d2fc00430f38d..43c52871b5ca7520494214ded84bfc597f737f95 100644 (file)
@@ -168,7 +168,7 @@ take_one :: (it: Iterator($T), no_close := false) -> (T, bool) {
     take_one :: take_one
 
     cont: bool;
-    (#insert dest), cont = take_one(it);
+    (#unquote dest), cont = take_one(it);
     return !cont;
 }
 
@@ -617,7 +617,7 @@ to_array :: (it: Iterator($T), allocator := context.allocator) -> [..] T {
         thread_function :: (__data: ^Thread_Data, $body: Code) {
             thread_data := __data.data;
             for #no_close *__data.iter {
-                #insert body;
+                #unquote body;
             }
         }
     }
index 8c82553eac4ec0c72fd7021291c7c1fd651e3695..d6dd2a5023f2d9319eff6b8930af2db17cc2ab60 100644 (file)
@@ -135,7 +135,7 @@ update :: macro (map: ^Map, key: map.Key_Type, body: Code) {
     lr := lookup_(map, key);
     if lr.entry_index >= 0 {
         it := ^map.entries[lr.entry_index].value;
-        #insert body;
+        #unquote body;
     }
 }
 
index 3944ae56be942c8b3ff8689a39a32b4c153d8fcd..375bdbcd386fcccaa7e3dec200ccfe7cc770fca8 100644 (file)
@@ -255,7 +255,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 server->get_events() do switch it.kind do #insert handler;
+        for server->get_events() do switch it.kind do #unquote handler;
     }
 }
 
index 906182845d0800700890f53645c19b2c5e5091c6..4c1a8cfcbcd42d39df48d1220a2e9fa321d476b7 100644 (file)
@@ -75,7 +75,7 @@ critical_section :: macro (m: ^Mutex, body: Code) -> i32 {
     scoped_mutex :: scoped_mutex;
     scoped_mutex(m);
 
-    #insert body;
+    #unquote body;
 
     return 0;
 }
index fd6f065744c2275ce8baedd46ec10441157b543a..a1d3fb603f3952554a34146d56b6c7c37696407e 100644 (file)
@@ -70,4 +70,4 @@ This type represents the value given by the `#callsite` expression. In practice,
 This type represents any value. It does this by using a data pointer and a type expression. Using the `builtin.type_info` package, you can introspect that type expression to retrieve data about the type, and from there reconstruct how to use the data pointer. See `conv.onyx` for how this works with `printf`.
 
 - `Code` -
-This is a dummy type that represents the type of a `#code {}` block. It is used when passing code around to macros or procedures, i.e. `f :: macro (body: Code)`
\ No newline at end of file
+This is a dummy type that represents the type of a `#unquote {}` block. It is used when passing code around to macros or procedures, i.e. `f :: macro (body: Code)`
\ No newline at end of file
index 35cf47f8ee5d68f39401e54f7a593ad95ef35752..bc5941bf39985841490eeb249ccfa0b129a20a0b 100644 (file)
@@ -73,25 +73,25 @@ main :: (args: [] cstr) {
 
     // A powerful feature of Onyx that enables macros to be very useful is code blocks.
     // Code blocks allow you to capture code and treat it as a compile-time object that can
-    // be passed around. To create a code blocks, simply put '#code' in front of the block
+    // be passed around. To create a code blocks, simply put '#quote' in front of the block
     // of code you want to capture. Notice that this uses '::' because this is a compile-time
     // value.
-    simple_code :: #code {
+    simple_code :: #quote {
         println("This is a code block!");
     }
 
-    // You can then use the '#insert' directive to place the code wherever you need it.
+    // You can then use the '#unquote' directive to place the code wherever you need it.
     // We can paste it 3 times for examples:
-    #insert simple_code;
-    #insert simple_code;
-    #insert simple_code;
+    #unquote simple_code;
+    #unquote simple_code;
+    #unquote simple_code;
 
     // Because simple_code is a compile-time value, it can be passed to procedures as so:
     triple :: ($body: Code) {
         println("Running 3 times!");
-        #insert body;
-        #insert body;
-        #insert body;
+        #unquote body;
+        #unquote body;
+        #unquote body;
     }
 
     triple(simple_code);
@@ -100,9 +100,9 @@ main :: (args: [] cstr) {
     // compile-time parameter (the '$').
     triple_macro :: macro (body: Code) {
         println("Running 3 times in a macro!");
-        #insert body;
-        #insert body;
-        #insert body;
+        #unquote body;
+        #unquote body;
+        #unquote body;
     }
 
     triple_macro(simple_code);
@@ -116,7 +116,7 @@ main :: (args: [] cstr) {
 
     cool_block :: macro (param: i32, body: Code) {
         local_variable := param * 2;
-        #insert body;
+        #unquote body;
     }
 
     cool_block(10) {
diff --git a/http_test.onyx b/http_test.onyx
new file mode 100644 (file)
index 0000000..ea920fc
--- /dev/null
@@ -0,0 +1,38 @@
+#load "core/std"
+#load "modules/http/module"
+#load "modules/json/module"
+
+use package core
+http :: package http
+
+main :: (args) => {
+    conn, err := http.connect("http://api.weatherapi.com");
+    if err != .None {
+        println(err);
+        os.exit(1);
+    }
+    defer conn->close();
+
+    w1 := alloc.heap.get_watermark();
+    f1 := alloc.heap.get_freed_size();
+
+    alloc.heap.auto() {
+        res := conn->get("/v1/current.json", .[
+            .{ "key", "59c2510355ba48d299b173546222103" },
+            .{ "q",   http.urlencode("Sioux Falls, SD") },
+        ]);
+
+        j_data := res->json();
+
+        curr := j_data.root["current"];
+        printf("It is {.1} degrees outside in {}, but it feels like {.1}.\n",
+            curr["temp_f"]->as_float(),
+            j_data.root["location"]["name"]->as_str(),
+            curr["feelslike_f"]->as_float());
+    }
+    
+    w2 := alloc.heap.get_watermark();
+    f2 := alloc.heap.get_freed_size();
+
+    printf("Leaked: {}\n", (w2 - f2) - (w1 - f1));
+}
index f5d07418c8ef8691076eb9b48deafbfcfb301b45..0d510a411e0a495cf8b7d929fe000a887977dc30 100644 (file)
@@ -392,7 +392,7 @@ CheckStatus check_switch(AstSwitch* switchnode) {
     switchnode->flags |= Ast_Flag_Has_Been_Checked;
 
     // Should the case block code be checked here?
-    // Or should this just exist to resolve macros and expand #inserts
+    // Or should this just exist to resolve macros and expand #unquotes
     // then the cases are consumed into the array or cases, THEN the blocks
     // are actually checked?
     if (switchnode->cases == NULL) {
@@ -1921,7 +1921,7 @@ CheckStatus check_insert_directive(AstDirectiveInsert** pinsert) {
     Type* code_type = type_build_from_ast(context.ast_alloc, builtin_code_type);
 
     TYPE_CHECK(&insert->code_expr, code_type) {
-        ERROR_(insert->token->pos, "#insert expected a value of type 'Code', got '%s'.",
+        ERROR_(insert->token->pos, "#unquote expected a value of type 'Code', got '%s'.",
             type_get_name(insert->code_expr->type));
     }
 
index 5c40a124c4797f101c63a019ab901864424e3ba2..ca0f90ccb1e97b112687f3f1feff655986434614 100644 (file)
@@ -648,7 +648,7 @@ static AstTyped* parse_factor(OnyxParser* parser) {
                 retval = (AstTyped *) defined;
                 break;
             }
-            else if (parse_possible_directive(parser, "code")) {
+            else if (parse_possible_directive(parser, "quote")) {
                 OnyxToken* code_token = parser->curr - 1;
 
                 AstCodeBlock* code_block = make_node(AstCodeBlock, Ast_Kind_Code_Block);
@@ -685,7 +685,7 @@ static AstTyped* parse_factor(OnyxParser* parser) {
                 retval = (AstTyped *) code_block;
                 break;
             }
-            else if (parse_possible_directive(parser, "insert")) {
+            else if (parse_possible_directive(parser, "unquote")) {
                 AstDirectiveInsert* insert = make_node(AstDirectiveInsert, Ast_Kind_Directive_Insert);
                 insert->token = parser->curr - 1;
                 insert->code_expr = parse_expression(parser, 0);
@@ -760,18 +760,6 @@ static AstTyped* parse_factor(OnyxParser* parser) {
 
                 parse_arguments(parser, ')', &call_node->args);
 
-//              This could be a cool feature where you can write:
-//
-//              foo(x, y) #{
-//                  // ...
-//              }
-//
-//              which just desugars into
-//
-//              foo(x, y, #code {
-//                  // ...
-//              })
-
                 // Wrap expressions in AstArgument
                 bh_arr_each(AstTyped *, arg, call_node->args.values) {
                     if ((*arg) == NULL) continue;
index ab94da59485dc52cfb80eb55fa1a957eef187f94..2111109cc8aca8116ef228c2b2cb35809a505cf6 100644 (file)
@@ -108,7 +108,7 @@ main :: (args: [] cstr) {
                }
 
                for ^cell: cells_to_consider {
-                       map.update(^grid, *cell, #code { it.alive = it.next; });
+                       map.update(^grid, *cell, #quote { it.alive = it.next; });
                }
 
                array.clear(^cells_to_consider);
index bf743ccd4f6c1e0ff3b81e6a8e00c4136073f22f..db94746eb9927cf3d2b3d618fa2c39d6c157f47d 100644 (file)
@@ -60,7 +60,7 @@ main :: (args) => {
                 if (it & (1 << index)) != 0 do A += 1;
             }
 
-            expected := (1 << index) if (#insert comparison) else 0;
+            expected := (1 << index) if (#unquote comparison) else 0;
 
             while i := 0; i < arr.count {
                 defer i += 1;
index 7b1e641d796096a5e140448d9bbb5d6dbdf18caa..c7c0ed6c3f3ad5fcc9980efc858de01a5025a1b6 100644 (file)
@@ -45,7 +45,7 @@ main :: (args) => {
             }
 
             attempt_add :: macro (cond: Code, dx, dy: i32) {
-                if #insert cond {
+                if #unquote cond {
                     if !tried->has(.{try.x + dx, try.y + dy}) {
                         if found := array.find_ptr(to_try.data, .{try.x + dx, try.y + dy, 0}); found != null {
                             found.cost = math.min(cell_value, found.cost);
index bc48b02af485614b17c2d021bf3da115340bbe57..c449fcbf069b0649c97230e7ad9e4632081d4ad9 100644 (file)
@@ -9,7 +9,7 @@ main :: (args: [] cstr) {
     printf("ba[10] is {}.\n", ba[10]);
 
     sum := 0;
-    bucket_array.for_each(ba, #code {
+    bucket_array.for_each(ba, #quote {
         printf("[{}] -> {}\n", bucket_index, *it);
         sum += *it;
     });
index cd94d2c5d570390e399b7292a0c514fe0fb2d5bb..9eb512e9c9a6909d3ad14e3f54111742340d433c 100644 (file)
@@ -5,7 +5,7 @@ use package core
 Vector2 :: struct { x, y: i32; }
 #operator == macro (v1: Vector2, v2: Vector2) => v1.x == v2.x && v1.y == v2.y;
 
-none_of_the_above :: #code {
+none_of_the_above :: #quote {
     case #default {
         println("Got default!");
     }
@@ -16,7 +16,7 @@ main :: (args: [] cstr) {
         switch it {
             case "Thing" do println("Got thing!");
             case "Some"  do println("Got some!");
-            #insert none_of_the_above;
+            #unquote none_of_the_above;
         }
     }
 
@@ -27,6 +27,6 @@ main :: (args: [] cstr) {
         case .{ 1, 0 } do println("1, 0");
         case .{ 1, 1 } do println("1, 1");
 
-        #insert none_of_the_above;
+        #unquote none_of_the_above;
     }
 }
\ No newline at end of file