From 919d32e714290a98073bb8253b26203bda1927ef Mon Sep 17 00:00:00 2001 From: kayomn Date: Sun, 13 Aug 2023 02:03:43 +0100 Subject: [PATCH] Add vector primitive object to Kym --- debug/app.ona | 2 ++ source/coral/coral.zig | 2 ++ source/coral/io.zig | 26 +++++++++++++++++++------- source/coral/lina.zig | 12 ++++++++++++ source/ona/kym.zig | 17 ++++++++++++++++- 5 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 source/coral/lina.zig diff --git a/debug/app.ona b/debug/app.ona index 8a50cd9..4950301 100644 --- a/debug/app.ona +++ b/debug/app.ona @@ -1,6 +1,8 @@ # Test comment. +pos = [10, 20, 0.3] + options = { .title = "Afterglow", .width = 1280, diff --git a/source/coral/coral.zig b/source/coral/coral.zig index e1c021b..1cd5221 100644 --- a/source/coral/coral.zig +++ b/source/coral/coral.zig @@ -5,6 +5,8 @@ pub const debug = @import("./debug.zig"); pub const io = @import("./io.zig"); +pub const lina = @import("./lina.zig"); + pub const list = @import("./list.zig"); pub const map = @import("./map.zig"); diff --git a/source/coral/io.zig b/source/coral/io.zig index 39025ee..1e6a897 100644 --- a/source/coral/io.zig +++ b/source/coral/io.zig @@ -350,15 +350,23 @@ pub fn find_first(haystack: []const Byte, needle: Byte) ?usize { return null; } -var null_context = @as(usize, 0); +pub fn jenkins_hash(comptime int: std.builtin.Type.Int, bytes: []const Byte) math.Int(int) { + var hash = @as(math.Int(int), 0); -pub const null_writer = Writer.bind(usize, &null_context, struct { - fn write(context: *usize, buffer: []const u8) ?usize { - debug.assert(context.* == 0); + for (bytes) |byte| { + hash +%= byte; + hash +%= (hash << 10); + hash ^= (hash >> 6); + } - return buffer.len; - } -}.write); + hash +%= (hash << 3); + hash ^= (hash >> 11); + hash +%= (hash << 15); + + return hash; +} + +pub const null_writer = Writer.from(write_null); pub fn slice_sentineled(comptime sen: anytype, ptr: [*:sen]const @TypeOf(sen)) [:sen]const @TypeOf(sen) { var len = @as(usize, 0); @@ -374,6 +382,10 @@ pub fn tag_of(comptime value: anytype) Tag(@TypeOf(value)) { return @as(Tag(@TypeOf(value)), value); } +pub fn write_null(buffer: []const u8) ?usize { + return buffer.len; +} + pub fn zero(target: []Byte) void { for (target) |*t| t.* = 0; } diff --git a/source/coral/lina.zig b/source/coral/lina.zig new file mode 100644 index 0000000..4ca347e --- /dev/null +++ b/source/coral/lina.zig @@ -0,0 +1,12 @@ + +pub const Vector3 = struct { + x: f32, + y: f32, + z: f32, + + pub fn equals(self: Vector3, other: Vector3) bool { + return self.x == other.x and self.y == other.y and self.z == other.z; + } + + pub const zero = Vector3{.x = 0, .y = 0, .z = 0}; +}; diff --git a/source/ona/kym.zig b/source/ona/kym.zig index 920ca6f..46e45f8 100644 --- a/source/ona/kym.zig +++ b/source/ona/kym.zig @@ -991,7 +991,7 @@ pub const RuntimeEnv = struct { if (object.ref_count == 0) { switch (object.payload) { - .false, .true, .float, .fixed, .symbol, .syscall => {}, + .false, .true, .float, .fixed, .symbol, .vector, .syscall => {}, .string => |string| { coral.debug.assert(string.len >= 0); @@ -1190,6 +1190,13 @@ pub const RuntimeEnv = struct { }); } + pub fn new_vector(self: *RuntimeEnv, x: f32, y: f32, z: f32) RuntimeError!*RuntimeRef { + return RuntimeRef.allocate(self.allocator, .{ + .ref_count = 1, + .payload = .{.vector = .{x, y, z}}, + }); + } + pub fn print(self: *RuntimeEnv, buffer: []const coral.io.Byte) void { if (self.options.print) |bound_print| { bound_print(buffer); @@ -1287,6 +1294,7 @@ pub const RuntimeRef = opaque { float: Float, fixed: Fixed, symbol: [*:0]const coral.io.Byte, + vector: coral.lina.Vector3, syscall: *const Syscall, string: struct { @@ -1357,6 +1365,11 @@ pub const RuntimeRef = opaque { else => false, }, + .vector => |self_vector| switch (other.object().payload) { + .vector => |other_vector| self_vector.equals(other_vector), + else => false, + }, + .syscall => |self_syscall| switch (other.object().payload) { .syscall => |other_syscall| self_syscall == other_syscall, else => false, @@ -1384,6 +1397,7 @@ pub const RuntimeRef = opaque { .float => |float| @bitCast(float), .fixed => |fixed| @intCast(@as(u32, @bitCast(fixed))), .symbol => |symbol| @intFromPtr(symbol), + .vector => |vector| coral.io.jenkins_hash(@typeInfo(usize).Int, coral.io.bytes_of(&vector)), .syscall => |syscall| @intFromPtr(syscall), .string => |string| coral.io.djb2_hash(@typeInfo(usize).Int, string.unpack()), .dynamic => |dynamic| @intFromPtr(dynamic.typeinfo()) ^ @intFromPtr(dynamic.userdata().ptr), @@ -1404,6 +1418,7 @@ pub const RuntimeRef = opaque { .float => |float| float != 0, .fixed => |fixed| fixed != 0, .symbol => true, + .vector => |vector| !vector.equals(coral.lina.Vector3.zero), .syscall => true, .string => |string| string.len != 0, .dynamic => true,