Skip to content

Commit ddca7b0

Browse files
committed
feat(range): rg.union
1 parent 1918240 commit ddca7b0

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/builtin/range.zig

+26
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,29 @@ pub fn intersect(ctx: *obj.NativeCtx) c_int {
111111

112112
return 1;
113113
}
114+
115+
pub fn @"union"(ctx: *obj.NativeCtx) c_int {
116+
const rangeA = ctx.vm.peek(1).obj().access(obj.ObjRange, .Range, ctx.vm.gc).?;
117+
const rangeB = ctx.vm.peek(0).obj().access(obj.ObjRange, .Range, ctx.vm.gc).?;
118+
119+
ctx.vm.push(
120+
Value.fromObj((ctx.vm.gc.allocateObject(
121+
obj.ObjRange,
122+
obj.ObjRange{
123+
.high = @min(
124+
@min(rangeB.low, rangeB.high),
125+
@min(rangeA.low, rangeA.high),
126+
),
127+
.low = @max(
128+
@max(rangeB.low, rangeB.high),
129+
@max(rangeA.low, rangeA.high),
130+
),
131+
},
132+
) catch {
133+
ctx.vm.panic("Out of memory");
134+
unreachable;
135+
}).toObj()),
136+
);
137+
138+
return 1;
139+
}

src/obj.zig

+2
Original file line numberDiff line numberDiff line change
@@ -2501,6 +2501,7 @@ pub const ObjRange = struct {
25012501
.{ "invert", buzz_builtin.range.invert },
25022502
.{ "subsetOf", buzz_builtin.range.subsetOf },
25032503
.{ "intersect", buzz_builtin.range.intersect },
2504+
.{ "union", buzz_builtin.range.@"union" },
25042505
},
25052506
);
25062507

@@ -2511,6 +2512,7 @@ pub const ObjRange = struct {
25112512
.{ "invert", "extern Function invert() > rg" },
25122513
.{ "subsetOf", "extern Function subsetOf(rg other) > bool" },
25132514
.{ "intersect", "extern Function intersect(rg other) > rg" },
2515+
.{ "union", "extern Function union(rg other) > rg" },
25142516
},
25152517
);
25162518

0 commit comments

Comments
 (0)