Skip to content

Commit

Permalink
feat(range): rg.union
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Jun 1, 2024
1 parent 1918240 commit 9d58c7a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Added
- Enum can now have `rg`, `ud`, `void`, `pat` has value type
- Object can have `const` properties (https://github.com/buzz-language/buzz/issues/13). A object with only `const` properties is considered itself `const`. Although we don't do anything yet with that concept. https://github.com/buzz-language/buzz/issues/114 is the objective but it requires being able to build objects and instances at compile time which is not yet possible.
- `rg.subsetOf`, `rg.intersect`
- `rg.subsetOf`, `rg.intersect`, `rg.union`

## Modified

Expand Down
26 changes: 26 additions & 0 deletions src/builtin/range.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/obj.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
},
);

Expand All @@ -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" },
},
);

Expand Down

0 comments on commit 9d58c7a

Please sign in to comment.