diff --git a/src/builtin/range.zig b/src/builtin/range.zig index a934bb6d..ba168637 100644 --- a/src/builtin/range.zig +++ b/src/builtin/range.zig @@ -111,3 +111,29 @@ pub fn intersect(ctx: *obj.NativeCtx) c_int { return 1; } + +pub fn @"union"(ctx: *obj.NativeCtx) c_int { + const rangeA = ctx.vm.peek(1).obj().access(obj.ObjRange, .Range, ctx.vm.gc).?; + const rangeB = ctx.vm.peek(0).obj().access(obj.ObjRange, .Range, ctx.vm.gc).?; + + ctx.vm.push( + Value.fromObj((ctx.vm.gc.allocateObject( + obj.ObjRange, + obj.ObjRange{ + .high = @min( + @min(rangeB.low, rangeB.high), + @min(rangeA.low, rangeA.high), + ), + .low = @max( + @max(rangeB.low, rangeB.high), + @max(rangeA.low, rangeA.high), + ), + }, + ) catch { + ctx.vm.panic("Out of memory"); + unreachable; + }).toObj()), + ); + + return 1; +} diff --git a/src/obj.zig b/src/obj.zig index 6ce4695b..839e4353 100644 --- a/src/obj.zig +++ b/src/obj.zig @@ -2501,6 +2501,7 @@ pub const ObjRange = struct { .{ "invert", buzz_builtin.range.invert }, .{ "subsetOf", buzz_builtin.range.subsetOf }, .{ "intersect", buzz_builtin.range.intersect }, + .{ "union", buzz_builtin.range.@"union" }, }, ); @@ -2511,6 +2512,7 @@ pub const ObjRange = struct { .{ "invert", "extern Function invert() > rg" }, .{ "subsetOf", "extern Function subsetOf(rg other) > bool" }, .{ "intersect", "extern Function intersect(rg other) > rg" }, + .{ "union", "extern Function union(rg other) > rg" }, }, );