Improve Kym Run-TIme Errors #45

Merged
kayomn merged 5 commits from better-kym-runtime-errors into main 2023-11-06 00:47:37 +01:00
2 changed files with 21 additions and 21 deletions
Showing only changes of commit fd15903260 - Show all commits

View File

@ -1,7 +1,7 @@
let test_param = "monkey wrench" let test_param = "monkey wrench"
let printer = lambda (pfx): let printer = lambda (pfx, anotha):
@print(test_param) @print(test_param)
return lambda (msg): return lambda (msg):

View File

@ -11,7 +11,7 @@ const tokens = @import("./kym/tokens.zig");
const tree = @import("./kym/tree.zig"); const tree = @import("./kym/tree.zig");
pub const Frame = struct { pub const Frame = struct {
name_stringable: *RuntimeRef, callable: *RuntimeRef,
arg_count: u8, arg_count: u8,
locals_top: usize, locals_top: usize,
@ -381,14 +381,14 @@ pub const RuntimeEnv = struct {
}; };
} }
pub fn new_boolean(self: *RuntimeEnv, value: bool) RuntimeError!*RuntimeRef { pub fn new_boolean(self: *RuntimeEnv, value: bool) coral.io.AllocationError!*RuntimeRef {
return RuntimeRef.allocate(self.allocator, .{ return RuntimeRef.allocate(self.allocator, .{
.ref_count = 1, .ref_count = 1,
.payload = if (value) .true else .false, .payload = if (value) .true else .false,
}); });
} }
pub fn new_boxed(self: *RuntimeEnv, value: ?*const RuntimeRef) RuntimeError!*RuntimeRef { pub fn new_boxed(self: *RuntimeEnv, value: ?*const RuntimeRef) coral.io.AllocationError!*RuntimeRef {
return RuntimeRef.allocate(self.allocator, .{ return RuntimeRef.allocate(self.allocator, .{
.ref_count = 1, .ref_count = 1,
.payload = .{.boxed = if (value) |ref| ref.acquire() else null}, .payload = .{.boxed = if (value) |ref| ref.acquire() else null},
@ -399,7 +399,7 @@ pub const RuntimeEnv = struct {
self: *RuntimeEnv, self: *RuntimeEnv,
userdata: []const coral.io.Byte, userdata: []const coral.io.Byte,
typeinfo: *const Typeinfo, typeinfo: *const Typeinfo,
) RuntimeError!*RuntimeRef { ) coral.io.AllocationError!*RuntimeRef {
coral.debug.assert(userdata.len == typeinfo.size); coral.debug.assert(userdata.len == typeinfo.size);
const dynamic = try self.allocator.reallocate(null, @sizeOf(usize) + typeinfo.size); const dynamic = try self.allocator.reallocate(null, @sizeOf(usize) + typeinfo.size);
@ -415,21 +415,21 @@ pub const RuntimeEnv = struct {
}); });
} }
pub fn new_fixed(self: *RuntimeEnv, value: Fixed) RuntimeError!*RuntimeRef { pub fn new_fixed(self: *RuntimeEnv, value: Fixed) coral.io.AllocationError!*RuntimeRef {
return RuntimeRef.allocate(self.allocator, .{ return RuntimeRef.allocate(self.allocator, .{
.ref_count = 1, .ref_count = 1,
.payload = .{.fixed = value}, .payload = .{.fixed = value},
}); });
} }
pub fn new_float(self: *RuntimeEnv, value: Float) RuntimeError!*RuntimeRef { pub fn new_float(self: *RuntimeEnv, value: Float) coral.io.AllocationError!*RuntimeRef {
return RuntimeRef.allocate(self.allocator, .{ return RuntimeRef.allocate(self.allocator, .{
.ref_count = 1, .ref_count = 1,
.payload = .{.float = value}, .payload = .{.float = value},
}); });
} }
pub fn new_string(self: *RuntimeEnv, value: []const coral.io.Byte) RuntimeError!*RuntimeRef { pub fn new_string(self: *RuntimeEnv, value: []const coral.io.Byte) coral.io.AllocationError!*RuntimeRef {
if (value.len > coral.math.max_int(@typeInfo(Fixed).Int)) { if (value.len > coral.math.max_int(@typeInfo(Fixed).Int)) {
return error.OutOfMemory; return error.OutOfMemory;
} }
@ -450,7 +450,7 @@ pub const RuntimeEnv = struct {
}); });
} }
pub fn new_symbol(self: *RuntimeEnv, value: []const coral.io.Byte) RuntimeError!*RuntimeRef { pub fn new_symbol(self: *RuntimeEnv, value: []const coral.io.Byte) coral.io.AllocationError!*RuntimeRef {
return RuntimeRef.allocate(self.allocator, .{ return RuntimeRef.allocate(self.allocator, .{
.ref_count = 1, .ref_count = 1,
@ -468,14 +468,14 @@ pub const RuntimeEnv = struct {
}); });
} }
pub fn new_syscall(self: *RuntimeEnv, value: *const Syscall) RuntimeError!*RuntimeRef { pub fn new_syscall(self: *RuntimeEnv, value: *const Syscall) coral.io.AllocationError!*RuntimeRef {
return RuntimeRef.allocate(self.allocator, .{ return RuntimeRef.allocate(self.allocator, .{
.ref_count = 1, .ref_count = 1,
.payload = .{.syscall = value}, .payload = .{.syscall = value},
}); });
} }
pub fn new_table(self: *RuntimeEnv) RuntimeError!*RuntimeRef { pub fn new_table(self: *RuntimeEnv) coral.io.AllocationError!*RuntimeRef {
var table = Table.make(self); var table = Table.make(self);
errdefer table.free(self); errdefer table.free(self);
@ -483,14 +483,14 @@ pub const RuntimeEnv = struct {
return try self.new_dynamic(coral.io.bytes_of(&table), Table.typeinfo); return try self.new_dynamic(coral.io.bytes_of(&table), Table.typeinfo);
} }
pub fn new_vector2(self: *RuntimeEnv, x: f32, y: f32) RuntimeError!*RuntimeRef { pub fn new_vector2(self: *RuntimeEnv, x: f32, y: f32) coral.io.AllocationError!*RuntimeRef {
return RuntimeRef.allocate(self.allocator, .{ return RuntimeRef.allocate(self.allocator, .{
.ref_count = 1, .ref_count = 1,
.payload = .{.vector2 = .{x, y}}, .payload = .{.vector2 = .{x, y}},
}); });
} }
pub fn new_vector3(self: *RuntimeEnv, x: f32, y: f32, z: f32) RuntimeError!*RuntimeRef { pub fn new_vector3(self: *RuntimeEnv, x: f32, y: f32, z: f32) coral.io.AllocationError!*RuntimeRef {
return RuntimeRef.allocate(self.allocator, .{ return RuntimeRef.allocate(self.allocator, .{
.ref_count = 1, .ref_count = 1,
.payload = .{.vector3 = .{x, y, z}}, .payload = .{.vector3 = .{x, y, z}},
@ -513,7 +513,7 @@ pub const RuntimeEnv = struct {
const popped_frame = self.frames.pop(); const popped_frame = self.frames.pop();
coral.debug.assert(popped_frame != null); coral.debug.assert(popped_frame != null);
self.discard(popped_frame.?.name_stringable); self.discard(popped_frame.?.callable);
var locals_to_pop = self.locals.values.len - popped_frame.?.locals_top; var locals_to_pop = self.locals.values.len - popped_frame.?.locals_top;
@ -534,9 +534,9 @@ pub const RuntimeEnv = struct {
return self.locals.pop() orelse self.raise(error.IllegalState, "stack underflow", .{}); return self.locals.pop() orelse self.raise(error.IllegalState, "stack underflow", .{});
} }
pub fn push_frame(self: *RuntimeEnv, name_stringable: *RuntimeRef, arg_count: u8) RuntimeError!Frame { pub fn push_frame(self: *RuntimeEnv, callable: *const RuntimeRef, arg_count: u8) RuntimeError!Frame {
const frame = Frame{ const frame = Frame{
.name_stringable = name_stringable.acquire(), .callable = callable.acquire(),
.arg_count = arg_count, .arg_count = arg_count,
.locals_top = self.locals.values.len - arg_count, .locals_top = self.locals.values.len - arg_count,
}; };
@ -563,7 +563,7 @@ pub const RuntimeEnv = struct {
while (remaining_frames != 0) { while (remaining_frames != 0) {
remaining_frames -= 1; remaining_frames -= 1;
const callable_string = try self.to_string(self.frames.values[remaining_frames].name_stringable); const callable_string = try self.to_string(self.frames.values[remaining_frames].callable);
defer self.discard(callable_string); defer self.discard(callable_string);
@ -623,7 +623,7 @@ pub const RuntimeEnv = struct {
}; };
} }
pub fn to_string(self: *RuntimeEnv, value: *const RuntimeRef) RuntimeError!*RuntimeRef { pub fn to_string(self: *RuntimeEnv, value: *const RuntimeRef) coral.io.AllocationError!*RuntimeRef {
const decimal_format = coral.utf8.DecimalFormat.default; const decimal_format = coral.utf8.DecimalFormat.default;
return switch (value.object().payload) { return switch (value.object().payload) {
@ -957,7 +957,7 @@ pub const Typeinfo = struct {
name: []const coral.io.Byte, name: []const coral.io.Byte,
size: usize, size: usize,
destruct: ?*const fn (env: *RuntimeEnv, userdata: []coral.io.Byte) void = null, destruct: ?*const fn (env: *RuntimeEnv, userdata: []coral.io.Byte) void = null,
to_string: *const fn (env: *RuntimeEnv, userdata: []coral.io.Byte) RuntimeError!*RuntimeRef = default_to_string, to_string: *const fn (env: *RuntimeEnv, userdata: []coral.io.Byte) coral.io.AllocationError!*RuntimeRef = default_to_string,
call: *const fn (env: *RuntimeEnv, userdata: []coral.io.Byte, frame: Frame) RuntimeError!?*RuntimeRef = default_call, call: *const fn (env: *RuntimeEnv, userdata: []coral.io.Byte, frame: Frame) RuntimeError!?*RuntimeRef = default_call,
get: *const fn (env: *RuntimeEnv, userdata: []coral.io.Byte, index: *const RuntimeRef) RuntimeError!?*RuntimeRef = default_get, get: *const fn (env: *RuntimeEnv, userdata: []coral.io.Byte, index: *const RuntimeRef) RuntimeError!?*RuntimeRef = default_get,
set: *const fn (env: *RuntimeEnv, userdata: []coral.io.Byte, value: *const RuntimeRef, value: ?*const RuntimeRef) RuntimeError!void = default_set, set: *const fn (env: *RuntimeEnv, userdata: []coral.io.Byte, value: *const RuntimeRef, value: ?*const RuntimeRef) RuntimeError!void = default_set,
@ -974,8 +974,8 @@ pub const Typeinfo = struct {
return env.raise(error.BadOperation, "this dynamic object is not set-indexable", .{}); return env.raise(error.BadOperation, "this dynamic object is not set-indexable", .{});
} }
fn default_to_string(env: *RuntimeEnv, _: []coral.io.Byte) RuntimeError!*RuntimeRef { fn default_to_string(env: *RuntimeEnv, _: []coral.io.Byte) coral.io.AllocationError!*RuntimeRef {
return env.raise(error.BadOperation, "this dynamic object is not stringable", .{}); return env.new_string("{}");
} }
}; };