Skip to content

Commit

Permalink
feat(range): range.contains
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Jan 29, 2025
1 parent ff41859 commit 76dcf0a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/builtin/range.zig
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,17 @@ pub fn low(ctx: *obj.NativeCtx) c_int {

return 1;
}

pub fn contains(ctx: *obj.NativeCtx) c_int {
const range = ctx.vm.peek(1).obj().access(obj.ObjRange, .Range, ctx.vm.gc).?;
const value = ctx.vm.peek(0).integer();

ctx.vm.push(
Value.fromBoolean(
(range.high >= range.low and value >= range.low and value < range.high) or
(range.low >= range.high and value >= range.high and value < range.low),
),
);

return 1;
}
3 changes: 3 additions & 0 deletions src/obj.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3093,6 +3093,7 @@ pub const ObjRange = struct {
buzz_builtin.range.subsetOf,
buzz_builtin.range.toList,
buzz_builtin.range.@"union",
buzz_builtin.range.contains,
};

const members_typedef = [_][]const u8{
Expand All @@ -3104,6 +3105,7 @@ pub const ObjRange = struct {
"extern fun subsetOf(other: rg) > bool",
"extern fun toList() > [int]",
"extern fun union(other: rg) > rg",
"extern fun contains(value: int) > bool",
};

pub const members_name = std.StaticStringMap(usize).initComptime(
Expand All @@ -3116,6 +3118,7 @@ pub const ObjRange = struct {
.{ "subsetOf", 5 },
.{ "toList", 6 },
.{ "union", 7 },
.{ "contains", 8 },
},
);

Expand Down

0 comments on commit 76dcf0a

Please sign in to comment.