Add vector primitive object to Kym

This commit is contained in:
kayomn 2023-08-13 02:03:43 +01:00
parent 3fecb795e9
commit 919d32e714
5 changed files with 51 additions and 8 deletions

View File

@ -1,6 +1,8 @@
# Test comment. # Test comment.
pos = [10, 20, 0.3]
options = { options = {
.title = "Afterglow", .title = "Afterglow",
.width = 1280, .width = 1280,

View File

@ -5,6 +5,8 @@ pub const debug = @import("./debug.zig");
pub const io = @import("./io.zig"); pub const io = @import("./io.zig");
pub const lina = @import("./lina.zig");
pub const list = @import("./list.zig"); pub const list = @import("./list.zig");
pub const map = @import("./map.zig"); pub const map = @import("./map.zig");

View File

@ -350,15 +350,23 @@ pub fn find_first(haystack: []const Byte, needle: Byte) ?usize {
return null; 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 { for (bytes) |byte| {
fn write(context: *usize, buffer: []const u8) ?usize { hash +%= byte;
debug.assert(context.* == 0); 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) { pub fn slice_sentineled(comptime sen: anytype, ptr: [*:sen]const @TypeOf(sen)) [:sen]const @TypeOf(sen) {
var len = @as(usize, 0); 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); return @as(Tag(@TypeOf(value)), value);
} }
pub fn write_null(buffer: []const u8) ?usize {
return buffer.len;
}
pub fn zero(target: []Byte) void { pub fn zero(target: []Byte) void {
for (target) |*t| t.* = 0; for (target) |*t| t.* = 0;
} }

12
source/coral/lina.zig Normal file
View File

@ -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};
};

View File

@ -991,7 +991,7 @@ pub const RuntimeEnv = struct {
if (object.ref_count == 0) { if (object.ref_count == 0) {
switch (object.payload) { switch (object.payload) {
.false, .true, .float, .fixed, .symbol, .syscall => {}, .false, .true, .float, .fixed, .symbol, .vector, .syscall => {},
.string => |string| { .string => |string| {
coral.debug.assert(string.len >= 0); 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 { pub fn print(self: *RuntimeEnv, buffer: []const coral.io.Byte) void {
if (self.options.print) |bound_print| { if (self.options.print) |bound_print| {
bound_print(buffer); bound_print(buffer);
@ -1287,6 +1294,7 @@ pub const RuntimeRef = opaque {
float: Float, float: Float,
fixed: Fixed, fixed: Fixed,
symbol: [*:0]const coral.io.Byte, symbol: [*:0]const coral.io.Byte,
vector: coral.lina.Vector3,
syscall: *const Syscall, syscall: *const Syscall,
string: struct { string: struct {
@ -1357,6 +1365,11 @@ pub const RuntimeRef = opaque {
else => false, 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 => |self_syscall| switch (other.object().payload) {
.syscall => |other_syscall| self_syscall == other_syscall, .syscall => |other_syscall| self_syscall == other_syscall,
else => false, else => false,
@ -1384,6 +1397,7 @@ pub const RuntimeRef = opaque {
.float => |float| @bitCast(float), .float => |float| @bitCast(float),
.fixed => |fixed| @intCast(@as(u32, @bitCast(fixed))), .fixed => |fixed| @intCast(@as(u32, @bitCast(fixed))),
.symbol => |symbol| @intFromPtr(symbol), .symbol => |symbol| @intFromPtr(symbol),
.vector => |vector| coral.io.jenkins_hash(@typeInfo(usize).Int, coral.io.bytes_of(&vector)),
.syscall => |syscall| @intFromPtr(syscall), .syscall => |syscall| @intFromPtr(syscall),
.string => |string| coral.io.djb2_hash(@typeInfo(usize).Int, string.unpack()), .string => |string| coral.io.djb2_hash(@typeInfo(usize).Int, string.unpack()),
.dynamic => |dynamic| @intFromPtr(dynamic.typeinfo()) ^ @intFromPtr(dynamic.userdata().ptr), .dynamic => |dynamic| @intFromPtr(dynamic.typeinfo()) ^ @intFromPtr(dynamic.userdata().ptr),
@ -1404,6 +1418,7 @@ pub const RuntimeRef = opaque {
.float => |float| float != 0, .float => |float| float != 0,
.fixed => |fixed| fixed != 0, .fixed => |fixed| fixed != 0,
.symbol => true, .symbol => true,
.vector => |vector| !vector.equals(coral.lina.Vector3.zero),
.syscall => true, .syscall => true,
.string => |string| string.len != 0, .string => |string| string.len != 0,
.dynamic => true, .dynamic => true,