From f31ab72aeaca2f144a654484affedb1207eee9cc Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Fri, 31 May 2024 23:44:11 +0200 Subject: [PATCH] feat(range): Range.subsetOf --- src/builtin/range.zig | 14 ++++++++++++++ src/obj.zig | 2 ++ 2 files changed, 16 insertions(+) diff --git a/src/builtin/range.zig b/src/builtin/range.zig index 0fe30cba..e9cb657d 100644 --- a/src/builtin/range.zig +++ b/src/builtin/range.zig @@ -71,3 +71,17 @@ pub fn invert(ctx: *obj.NativeCtx) c_int { return 1; } + +pub fn subsetOf(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.fromBoolean( + @min(rangeA.low, rangeA.high) >= @min(rangeB.low, rangeB.high) and + @max(rangeA.low, rangeA.high) <= @max(rangeB.low, rangeB.high), + ), + ); + + return 1; +} diff --git a/src/obj.zig b/src/obj.zig index f3127c99..a40ae762 100644 --- a/src/obj.zig +++ b/src/obj.zig @@ -2499,6 +2499,7 @@ pub const ObjRange = struct { .{ "toList", buzz_builtin.range.toList }, .{ "len", buzz_builtin.range.len }, .{ "invert", buzz_builtin.range.invert }, + .{ "subsetOf", buzz_builtin.range.subsetOf }, }, ); @@ -2507,6 +2508,7 @@ pub const ObjRange = struct { .{ "toList", "extern Function toList() > [int]" }, .{ "len", "extern Function len() > int" }, .{ "invert", "extern Function invert() > rg" }, + .{ "subsetOf", "extern Function subsetOf(rg other) > bool" }, }, );