Implement vector swizzling in Kym #36

Merged
kayomn merged 1 commits from kym-vector-swizzling into main 2023-08-20 03:37:55 +02:00
2 changed files with 53 additions and 1 deletions
Showing only changes of commit d53bf188cf - Show all commits

View File

@ -2,6 +2,7 @@
# Test comment. # Test comment.
pos = @vec3(10, 20, 0.3) pos = @vec3(10, 20, 0.3)
coord = pos.xz
options = { options = {
.title = "Afterglow", .title = "Afterglow",

View File

@ -1088,6 +1088,57 @@ pub const RuntimeEnv = struct {
pub fn get(self: *RuntimeEnv, indexable: *RuntimeRef, index: *const RuntimeRef) RuntimeError!?*RuntimeRef { pub fn get(self: *RuntimeEnv, indexable: *RuntimeRef, index: *const RuntimeRef) RuntimeError!?*RuntimeRef {
return switch (indexable.object().payload) { return switch (indexable.object().payload) {
.vector2 => |vector2| swizzle: {
const swizzle_symbol = try self.unbox_symbol(index);
var swizzle_buffer = [_]f32{0} ** 3;
var swizzle_count = @as(usize, 0);
while (true) : (swizzle_count += 1) {
if (swizzle_count > swizzle_buffer.len) {
return null;
}
swizzle_buffer[swizzle_count] = switch (swizzle_symbol[swizzle_count]) {
0 => break: swizzle switch (swizzle_count) {
1 => self.new_float(swizzle_buffer[0]),
2 => self.new_vector2(swizzle_buffer[0], swizzle_buffer[1]),
3 => self.new_vector3(swizzle_buffer[0], swizzle_buffer[1], swizzle_buffer[2]),
else => unreachable,
},
'x' => vector2[0],
'y' => vector2[1],
else => return null,
};
}
},
.vector3 => |vector3| swizzle: {
const swizzle_symbol = try self.unbox_symbol(index);
var swizzle_buffer = [_]f32{0} ** 3;
var swizzle_count = @as(usize, 0);
while (true) : (swizzle_count += 1) {
if (swizzle_count > swizzle_buffer.len) {
return null;
}
swizzle_buffer[swizzle_count] = switch (swizzle_symbol[swizzle_count]) {
0 => break: swizzle switch (swizzle_count) {
1 => self.new_float(swizzle_buffer[0]),
2 => self.new_vector2(swizzle_buffer[0], swizzle_buffer[1]),
3 => self.new_vector3(swizzle_buffer[0], swizzle_buffer[1], swizzle_buffer[2]),
else => unreachable,
},
'x' => vector3[0],
'y' => vector3[1],
'z' => vector3[2],
else => return null,
};
}
},
.dynamic => |dynamic| dynamic.typeinfo().get(.{ .dynamic => |dynamic| dynamic.typeinfo().get(.{
.userdata = dynamic.userdata(), .userdata = dynamic.userdata(),
.env = self, .env = self,
@ -1298,7 +1349,7 @@ pub const RuntimeEnv = struct {
}; };
} }
pub fn unbox_symbol(env: *RuntimeEnv, value: *RuntimeRef) RuntimeError![*:0]const coral.io.Byte { pub fn unbox_symbol(env: *RuntimeEnv, value: *const RuntimeRef) RuntimeError![*:0]const coral.io.Byte {
return switch (value.object().payload) { return switch (value.object().payload) {
.symbol => |symbol| symbol, .symbol => |symbol| symbol,
else => env.raise(error.TypeMismatch, "expected symbol object") else => env.raise(error.TypeMismatch, "expected symbol object")