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
3 changed files with 9 additions and 12 deletions
Showing only changes of commit 00631e66a5 - Show all commits

View File

@ -308,11 +308,7 @@ pub const RuntimeEnv = struct {
}; };
} }
const chunk_name = try self.new_string(file_name); break: make_chunk try Chunk.make(self, file_name, &root.environment);
defer self.discard(chunk_name);
break: make_chunk try Chunk.make(self, chunk_name, &root.environment);
}; };
defer chunk.free(self); defer chunk.free(self);

View File

@ -576,9 +576,9 @@ pub fn free(self: *Self, env: *kym.RuntimeEnv) void {
self.bindings = &.{}; self.bindings = &.{};
} }
pub fn make(env: *kym.RuntimeEnv, name: *const kym.RuntimeRef, environment: *const tree.Environment) kym.RuntimeError!Self { pub fn make(env: *kym.RuntimeEnv, name: []const coral.io.Byte, environment: *const tree.Environment) kym.RuntimeError!Self {
var chunk = Self{ var chunk = Self{
.name = name.acquire(), .name = try env.new_symbol(name),
.opcodes = OpcodeList.make(env.allocator), .opcodes = OpcodeList.make(env.allocator),
.constants = ConstList.make(env.allocator), .constants = ConstList.make(env.allocator),
.bindings = &.{}, .bindings = &.{},
@ -634,6 +634,7 @@ pub const typeinfo = &kym.Typeinfo{
.name = "lambda", .name = "lambda",
.destruct = typeinfo_destruct, .destruct = typeinfo_destruct,
.call = typeinfo_call, .call = typeinfo_call,
.to_string = typeinfo_to_string,
}; };
fn typeinfo_call(env: *kym.RuntimeEnv, userdata: []coral.io.Byte, frame: kym.Frame) kym.RuntimeError!?*kym.RuntimeRef { fn typeinfo_call(env: *kym.RuntimeEnv, userdata: []coral.io.Byte, frame: kym.Frame) kym.RuntimeError!?*kym.RuntimeRef {
@ -653,3 +654,7 @@ fn typeinfo_call(env: *kym.RuntimeEnv, userdata: []coral.io.Byte, frame: kym.Fra
fn typeinfo_destruct(env: *kym.RuntimeEnv, userdata: []coral.io.Byte) void { fn typeinfo_destruct(env: *kym.RuntimeEnv, userdata: []coral.io.Byte) void {
@as(*Self, @ptrCast(@alignCast(userdata))).free(env); @as(*Self, @ptrCast(@alignCast(userdata))).free(env);
} }
fn typeinfo_to_string(env: *kym.RuntimeEnv, userdata: []coral.io.Byte) coral.io.AllocationError!*kym.RuntimeRef {
return env.to_string(@as(*Self, @ptrCast(@alignCast(userdata))).name);
}

View File

@ -86,11 +86,7 @@ fn compile_expression(self: Self, environment: *const tree.Environment, expressi
}, },
.lambda_construct => |lambda_construct| { .lambda_construct => |lambda_construct| {
const anonymous_chunk_name = try self.env.new_symbol(name orelse "<lambda>"); var chunk = try Chunk.make(self.env, name orelse "<lambda>", lambda_construct.environment);
defer self.env.discard(anonymous_chunk_name);
var chunk = try Chunk.make(self.env, anonymous_chunk_name, lambda_construct.environment);
errdefer chunk.free(self.env); errdefer chunk.free(self.env);