Implement vector swizzling in Kym #36
|
@ -2,6 +2,7 @@
|
|||
# Test comment.
|
||||
|
||||
pos = @vec3(10, 20, 0.3)
|
||||
coord = pos.xz
|
||||
|
||||
options = {
|
||||
.title = "Afterglow",
|
||||
|
|
|
@ -1088,6 +1088,57 @@ pub const RuntimeEnv = struct {
|
|||
|
||||
pub fn get(self: *RuntimeEnv, indexable: *RuntimeRef, index: *const RuntimeRef) RuntimeError!?*RuntimeRef {
|
||||
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(.{
|
||||
.userdata = dynamic.userdata(),
|
||||
.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) {
|
||||
.symbol => |symbol| symbol,
|
||||
else => env.raise(error.TypeMismatch, "expected symbol object")
|
||||
|
|
Loading…
Reference in New Issue