From: Brendan Hansen Date: Mon, 7 Nov 2022 04:49:57 +0000 (-0600) Subject: slowly making the core libraries more consistent X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=2eb41cd16661f8a068558e029c6512fa8292ce16;p=onyx.git slowly making the core libraries more consistent --- diff --git a/core/builtin.onyx b/core/builtin.onyx index 6f8b6a07..892e7117 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -176,6 +176,10 @@ cfree :: (ptr: rawptr) do raw_free(context.allocator, ptr); } } + new_temp :: macro (T: type_expr) => { + return new(T, allocator=context.temp_allocator); + } + make :: #match { macro ($T: type_expr, allocator := context.allocator) => { return __make_overload(cast(^T) null, allocator=allocator); @@ -186,6 +190,10 @@ cfree :: (ptr: rawptr) do raw_free(context.allocator, ptr); }, } + make_temp :: macro (T: type_expr) => { + return make(T, allocator=context.temp_allocator); + } + // // This is a rather unique way of using the type matching system // to select an overload. What is desired here is that when you say: diff --git a/core/container/iter.onyx b/core/container/iter.onyx index b9684fbd..4141f1ff 100644 --- a/core/container/iter.onyx +++ b/core/container/iter.onyx @@ -455,33 +455,18 @@ enumerate :: (it: Iterator($T), start_index: i32 = 0) -> Iterator(Enumeration_Va #overload as_iterator :: from_array -from_array :: (arr: [] $T) -> Iterator(^T) { - Context :: struct (T: type_expr) { - data: ^T; - count: u32; - current: u32; - } +from_array :: (arr: [] $T) => generator( + ^.{ data = arr.data, count = arr.count, current = 0 }, - c := make(Context(T), allocator=context.temp_allocator); - c.data = arr.data; - c.count = arr.count; - c.current = 0; - - next :: (use _: ^Context($T)) -> (^T, bool) { + (use ctx: ^$T) -> (typeof ctx.data, bool) { if current < count { defer current += 1; return ^data[current], true; - - } else { - return null, false; } - } - return .{ - data = c, - next = #solidify next { T = T }, - }; -} + return null, false; + } +); #overload as_iterator :: (x: ^[..] $T) -> Iterator(T) { diff --git a/core/container/list.onyx b/core/container/list.onyx index fb1a4c95..59db247b 100644 --- a/core/container/list.onyx +++ b/core/container/list.onyx @@ -13,6 +13,9 @@ List :: struct (Elem_Type: type_expr) { last: ^ListElem(Elem_Type) = null; // "Method" like things +} + +#inject List { free :: free push_end :: push_end push_begin :: push_begin @@ -23,7 +26,7 @@ List :: struct (Elem_Type: type_expr) { contains :: contains fold :: fold map :: map - get_iterator :: get_iterator + as_iter :: as_iter } make :: ($T: type_expr, allocator := context.allocator) -> List(T) { @@ -145,8 +148,8 @@ map :: #match #local {} } } -#match core.iter.as_iterator get_iterator -get_iterator :: (list: ^List($T)) -> Iterator(T) { +#match core.iter.as_iterator as_iter +as_iter :: (list: ^List($T)) -> Iterator(T) { iterator_next :: (list_iter: ^ListIterator($T)) -> (T, bool) { if list_iter.current == null do return .{}, false; @@ -158,13 +161,12 @@ get_iterator :: (list: ^List($T)) -> Iterator(T) { current: ^ListElem(T); } - list_iterator := new(ListIterator(T)); + list_iterator := new_temp(ListIterator(T)); list_iterator.current = list.first; return .{ data = list_iterator, next = #solidify iterator_next { T = T }, - close = cfree, }; } diff --git a/core/container/map.onyx b/core/container/map.onyx index 7e7ddceb..c5986139 100644 --- a/core/container/map.onyx +++ b/core/container/map.onyx @@ -34,10 +34,6 @@ Map :: struct (Key_Type: type_expr, Value_Type: type_expr) where ValidKey(Key_Ty } #inject Map { - // - // These need to have aliases because some of them like - // 'delete', collide with the global 'delete', which - // causes it to map to the wrong function. init :: init has :: has get :: get diff --git a/core/container/set.onyx b/core/container/set.onyx index 8bf9d3e6..066733fe 100644 --- a/core/container/set.onyx +++ b/core/container/set.onyx @@ -20,7 +20,9 @@ Set :: struct (Elem_Type: type_expr) where SetValue(Elem_Type) { hash : u32; value : T; } +} +#inject Set { init :: init free :: free has :: has @@ -30,7 +32,7 @@ Set :: struct (Elem_Type: type_expr) where SetValue(Elem_Type) { remove :: remove clear :: clear empty :: empty - iterator :: iterator + as_iter :: as_iter } make :: ($T: type_expr, default := T.{}, allocator := context.allocator) -> Set(T) { @@ -116,33 +118,20 @@ empty :: (use set: ^Set) -> bool { return entries.count == 0; } -#match core.iter.as_iterator iterator -iterator :: (set: ^Set($T)) -> Iterator(T) { - Context :: struct (T: type_expr) { - set: ^Set(T); - position: i32; - } - - context := new(Context(T)); - context.set = set; - context.position = 0; +#overload core.iter.as_iterator as_iter - next :: (use context: ^Context($T)) -> (T, bool) { - if position < set.entries.count { - defer position += 1; - return set.entries[position].value, true; +as_iter :: (s: ^Set) => + core.iter.generator( + ^.{ s = s, i = 0 }, - } else { - return .{}, false; - } - } + (ctx) => { + if ctx.i >= ctx.s.entries.count { + return (typeof ^ctx.s.entries.data.value).{}, false; + } - return .{ - data = context, - next = #solidify next { T = T }, - close = cfree, - }; -} + defer ctx.i += 1; + return ^ctx.s.entries.data[ctx.i].value, true; + }); // // Private symbols diff --git a/tests/aoc-2021/day12.onyx b/tests/aoc-2021/day12.onyx index 516e6114..2837716a 100644 --- a/tests/aoc-2021/day12.onyx +++ b/tests/aoc-2021/day12.onyx @@ -41,16 +41,16 @@ main :: (args) => { node_stack << .{ "start", 0, false }; children_of :: (edges: ^$T, name: str) -> Iterator(str) { - #persist NAME_HACK: str; - NAME_HACK = name; + name_copy := make_temp(str); + *name_copy = name; return iter.concat( iter.as_iterator(edges) - |> iter.filter((x) => x.a == NAME_HACK) + |> iter.filter(name_copy, (x, n) => x.a == *n) |> iter.map((x) => x.b), iter.as_iterator(edges) - |> iter.filter((x) => x.b == NAME_HACK) + |> iter.filter(name_copy, (x, n) => x.b == *n) |> iter.map((x) => x.a) ); } @@ -62,7 +62,7 @@ main :: (args) => { edge_map: Map(str, [] str); for v: iter.as_iterator(^verticies) { - edge_map[v] = children_of(^edges, v) |> iter.to_array(); + edge_map[*v] = children_of(^edges, *v) |> iter.to_array(); } paths_count := 0; diff --git a/tests/aoc-2021/day18.onyx b/tests/aoc-2021/day18.onyx index afd60668..77a8a1f1 100644 --- a/tests/aoc-2021/day18.onyx +++ b/tests/aoc-2021/day18.onyx @@ -30,7 +30,7 @@ SnailNum :: struct { } clone :: (n: ^SnailNum) -> ^SnailNum { - if n == null do return null; + if !n do return null; new_num := SnailNum.make(); new_num->set_left(SnailNum.clone(n.left)); @@ -42,8 +42,8 @@ SnailNum :: struct { } add :: (a, b: ^SnailNum) => { - if a == null do return b; - if b == null do return a; + if !a do return b; + if !b do return a; new_root := SnailNum.make(); new_root->set_left(a); @@ -87,8 +87,8 @@ SnailNum :: struct { pleft := n->number_to_left(); pright := n->number_to_right(); - if pleft != null do *pleft += left_val; - if pright != null do *pright += right_val; + if pleft do *pleft += left_val; + if pright do *pright += right_val; left_val = 0; right_val = 0; @@ -97,7 +97,7 @@ SnailNum :: struct { } reduce_splits :: (use n: ^SnailNum) -> (reduced_something: bool) { - if left != null { + if left { if left->reduce_splits() { return true; } @@ -109,7 +109,7 @@ SnailNum :: struct { return true; } - if right != null { + if right { if right->reduce_splits() { return true; } @@ -125,34 +125,34 @@ SnailNum :: struct { split_number :: (n: u32) -> (u32, u32) { h := n / 2; - return h, h + (0 if n % 2 == 0 else 1); + return h, h + (n % 2); } } set_left :: (parent, new_left: ^SnailNum) { parent.left_val = 0; parent.left = new_left; - if new_left != null do new_left.parent = parent; + if new_left do new_left.parent = parent; } set_right :: (parent, new_right: ^SnailNum) { parent.right_val = 0; parent.right = new_right; - if new_right != null do new_right.parent = parent; + if new_right do new_right.parent = parent; } number_to_left :: (n: ^SnailNum) -> ^u32 { - while n.parent != null && n.parent.left == n { + while n.parent && n.parent.left == n { n = n.parent; } - if n.parent == null do return null; + if !n.parent do return null; - if n.parent.left == null do return ^n.parent.left_val; + if !n.parent.left do return ^n.parent.left_val; n = n.parent.left; - while n.right != null { + while n.right { n = n.right; } @@ -160,17 +160,17 @@ SnailNum :: struct { } number_to_right :: (n: ^SnailNum) -> ^u32 { - while n.parent != null && n.parent.right == n { + while n.parent && n.parent.right == n { n = n.parent; } - if n.parent == null do return null; + if !n.parent do return null; - if n.parent.right == null do return ^n.parent.right_val; + if !n.parent.right do return ^n.parent.right_val; n = n.parent.right; - while n.left != null { + while n.left { n = n.left; } @@ -178,7 +178,7 @@ SnailNum :: struct { } magnitude :: (use n: ^SnailNum) => { - if n == null { + if !n { return 0; } @@ -210,16 +210,16 @@ SnailNum :: struct { } format :: (output: ^conv.Format_Output, s: ^conv.Format, use n: ^SnailNum) { - if left == null && right == null { + if !left && !right { conv.format(output, "[{},{}]", left_val, right_val); } - elseif left == null && right != null { + elseif !left && right { conv.format(output, "[{},{*}]", left_val, right); } - elseif left != null && right == null { + elseif left && !right { conv.format(output, "[{*},{}]", left, right_val); } - elseif left != null && right != null { + elseif left && right { conv.format(output, "[{*},{*}]", left, right); } } diff --git a/tests/interfaces.onyx b/tests/interfaces.onyx index 897f903a..6e51366e 100644 --- a/tests/interfaces.onyx +++ b/tests/interfaces.onyx @@ -65,7 +65,7 @@ Complex :: struct { #operator + (c1, c2: Complex) => Complex.{ c1.x + c2.x, c1.y + c2.y }; #operator * (c1, c2: Complex) => Complex.{ c1.x * c2.x - c1.y * c2.y, c1.x * c2.y + c1.y * c2.x }; -consume :: macro (x: $T) -> #auto where iter.Iterable(T) { +consume :: macro (x: $T/iter.Iterable) => { // This is a weird limitation of the Onyx type system. // In order to be able tell what the iterator produces, you // have to pattern match it in a procedure parameter. This means diff --git a/tests/lazy_iterators.onyx b/tests/lazy_iterators.onyx index 2363483a..ae30bd3e 100644 --- a/tests/lazy_iterators.onyx +++ b/tests/lazy_iterators.onyx @@ -3,34 +3,22 @@ use package core count_iterator :: (lo: $T, hi: T, step: T = 1) -> Iterator(T) { - next :: (ci: ^CountIterator($T)) -> (T, bool) { - if ci.current > ci.high do return 0, false; - - defer ci.current += ci.step; - return ci.current, true; - } - - close :: (data: rawptr) { - println("Closing the count iterator..."); - cfree(data); - } - - CountIterator :: struct (T:type_expr) { - low, high, step: T; - current: T; - } - - count_iterator := new(CountIterator(T)); - count_iterator.low = lo; - count_iterator.high = hi; - count_iterator.step = step; - count_iterator.current = lo; - - return .{ - data = count_iterator, - next = #solidify next {T=T}, - close = close, - }; + return iter.generator( + ^.{ low = lo, high = hi, step = step, current = lo }, + + (ctx) => { + if ctx.current <= ctx.high { + defer ctx.current += ctx.step; + return ctx.current, true; + } + + return 0, false; + }, + + (ctx) => { + println("Closing the count iterator..."); + } + ); } main :: (args: [] cstr) {