From: Brendan Hansen Date: Fri, 25 Mar 2022 16:05:30 +0000 (-0500) Subject: renamed #code and #insert to #quote and #unquote X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=228a22583f143f97027b77679989f38fbefbaf47;p=onyx.git renamed #code and #insert to #quote and #unquote --- diff --git a/core/alloc/arena.onyx b/core/alloc/arena.onyx index 5de268c0..3428bb8c 100644 --- a/core/alloc/arena.onyx +++ b/core/alloc/arena.onyx @@ -128,7 +128,7 @@ auto :: #match { #context_scope { auto(size); - #insert body; + #unquote body; } return 0; diff --git a/core/alloc/auto_heap.onyx b/core/alloc/auto_heap.onyx index 84e54f87..fd8bc399 100644 --- a/core/alloc/auto_heap.onyx +++ b/core/alloc/auto_heap.onyx @@ -65,7 +65,7 @@ auto :: #match { #context_scope { auto(); - #insert body; + #unquote body; } return 0; diff --git a/core/builtin.onyx b/core/builtin.onyx index 3d22d7c0..abd390c9 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -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;} diff --git a/core/container/array.onyx b/core/container/array.onyx index 745b078a..af39135e 100644 --- a/core/container/array.onyx +++ b/core/container/array.onyx @@ -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);, } diff --git a/core/container/bucket_array.onyx b/core/container/bucket_array.onyx index 2786749c..f28e89f2 100644 --- a/core/container/bucket_array.onyx +++ b/core/container/bucket_array.onyx @@ -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; } } } diff --git a/core/container/iter.onyx b/core/container/iter.onyx index 6fec7997..43c52871 100644 --- a/core/container/iter.onyx +++ b/core/container/iter.onyx @@ -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; } } } diff --git a/core/container/map.onyx b/core/container/map.onyx index 8c82553e..d6dd2a50 100644 --- a/core/container/map.onyx +++ b/core/container/map.onyx @@ -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; } } diff --git a/core/net/tcp.onyx b/core/net/tcp.onyx index 3944ae56..375bdbcd 100644 --- a/core/net/tcp.onyx +++ b/core/net/tcp.onyx @@ -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; } } diff --git a/core/sync/mutex.onyx b/core/sync/mutex.onyx index 90618284..4c1a8cfc 100644 --- a/core/sync/mutex.onyx +++ b/core/sync/mutex.onyx @@ -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; } diff --git a/docs/builtins.md b/docs/builtins.md index fd6f0657..a1d3fb60 100644 --- a/docs/builtins.md +++ b/docs/builtins.md @@ -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 diff --git a/examples/18_macros.onyx b/examples/18_macros.onyx index 35cf47f8..bc5941bf 100644 --- a/examples/18_macros.onyx +++ b/examples/18_macros.onyx @@ -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 index 00000000..ea920fc4 --- /dev/null +++ b/http_test.onyx @@ -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)); +} diff --git a/src/checker.c b/src/checker.c index f5d07418..0d510a41 100644 --- a/src/checker.c +++ b/src/checker.c @@ -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)); } diff --git a/src/parser.c b/src/parser.c index 5c40a124..ca0f90cc 100644 --- a/src/parser.c +++ b/src/parser.c @@ -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; diff --git a/tests/aoc-2020/day24.onyx b/tests/aoc-2020/day24.onyx index ab94da59..2111109c 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, #code { it.alive = it.next; }); + map.update(^grid, *cell, #quote { it.alive = it.next; }); } array.clear(^cells_to_consider); diff --git a/tests/aoc-2021/day03.onyx b/tests/aoc-2021/day03.onyx index bf743ccd..db94746e 100644 --- a/tests/aoc-2021/day03.onyx +++ b/tests/aoc-2021/day03.onyx @@ -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; diff --git a/tests/aoc-2021/day15.onyx b/tests/aoc-2021/day15.onyx index 7b1e641d..c7c0ed6c 100644 --- a/tests/aoc-2021/day15.onyx +++ b/tests/aoc-2021/day15.onyx @@ -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); diff --git a/tests/bucket_array.onyx b/tests/bucket_array.onyx index bc48b02a..c449fcbf 100644 --- a/tests/bucket_array.onyx +++ b/tests/bucket_array.onyx @@ -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; }); diff --git a/tests/switch_using_equals.onyx b/tests/switch_using_equals.onyx index cd94d2c5..9eb512e9 100644 --- a/tests/switch_using_equals.onyx +++ b/tests/switch_using_equals.onyx @@ -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