From 76dcf0a4ae98a27e4aaa8766737e7bb929e0826b Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Wed, 29 Jan 2025 14:52:53 +0100 Subject: [PATCH] feat(range): range.contains --- src/builtin/range.zig | 14 ++++++++++++++ src/obj.zig | 3 +++ 2 files changed, 17 insertions(+) diff --git a/src/builtin/range.zig b/src/builtin/range.zig index 42210689..4d230ba3 100644 --- a/src/builtin/range.zig +++ b/src/builtin/range.zig @@ -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; +} diff --git a/src/obj.zig b/src/obj.zig index 3c35361d..50d7f52d 100644 --- a/src/obj.zig +++ b/src/obj.zig @@ -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{ @@ -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( @@ -3116,6 +3118,7 @@ pub const ObjRange = struct { .{ "subsetOf", 5 }, .{ "toList", 6 }, .{ "union", 7 }, + .{ "contains", 8 }, }, );