Skip to content

Commit

Permalink
feat(range): rg.intersect
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed May 31, 2024
1 parent f31ab72 commit d8282d3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +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`

## 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 @@ -85,3 +85,29 @@ pub fn subsetOf(ctx: *obj.NativeCtx) c_int {

return 1;
}

pub fn intersect(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 = @max(
@min(rangeB.low, rangeB.high),
@min(rangeA.low, rangeA.high),
),
.low = @min(
@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 @@ -2500,6 +2500,7 @@ pub const ObjRange = struct {
.{ "len", buzz_builtin.range.len },
.{ "invert", buzz_builtin.range.invert },
.{ "subsetOf", buzz_builtin.range.subsetOf },
.{ "intersect", buzz_builtin.range.intersect },
},
);

Expand All @@ -2509,6 +2510,7 @@ pub const ObjRange = struct {
.{ "len", "extern Function len() > int" },
.{ "invert", "extern Function invert() > rg" },
.{ "subsetOf", "extern Function subsetOf(rg other) > bool" },
.{ "intersect", "extern Function intersect(rg other) > rg" },
},
);

Expand Down

0 comments on commit d8282d3

Please sign in to comment.