Compare commits

..

No commits in common. "8137f2b474608880b3e3211c1f6e8ca750fdbd6c" and "1ea115b277bb833bf3d7df48a38b8351e27a7366" have entirely different histories.

6 changed files with 280 additions and 314 deletions

View File

@ -17,17 +17,25 @@ pub const Manifest = struct {
defer env.discard(manifest_ref);
const title_ref = try kym.get_field(env, manifest_ref, "title");
defer env.discard(title_ref);
const title_string = switch (env.unbox(title_ref)) {
.string => |string| string,
else => "",
};
const width = @as(u16, get: {
const ref = try kym.get_field(env, manifest_ref, "width");
defer env.discard(ref);
if (env.unbox(ref).expect_number()) |number| {
// TODO: Add safety-checks to int cast.
break: get @intFromFloat(number);
}
break: get self.width;
break: get switch (env.unbox(ref)) {
.number => |number| @intFromFloat(number),
else => self.width,
};
});
const height = @as(u16, get: {
@ -35,12 +43,11 @@ pub const Manifest = struct {
defer env.discard(ref);
if (env.unbox(ref).expect_number()) |number| {
// TODO: Add safety-checks to int cast.
break: get @intFromFloat(number);
}
break: get self.height;
break: get switch (env.unbox(ref)) {
.number => |number| @intFromFloat(number),
else => self.height,
};
});
const tick_rate = @as(f32, get: {
@ -48,20 +55,13 @@ pub const Manifest = struct {
defer env.discard(ref);
if (env.unbox(ref).expect_number()) |number| {
// TODO: Add safety-checks to int cast.
break: get @floatCast(number);
}
break: get self.tick_rate;
break: get switch (env.unbox(ref)) {
.number => |number| @floatCast(number),
else => self.tick_rate,
};
});
{
const title_ref = try kym.get_field(env, manifest_ref, "title");
defer env.discard(title_ref);
const title_string = env.unbox(title_ref).expect_string() orelse "";
const limited_title_len = coral.math.min(title_string.len, self.title.len);
coral.io.copy(&self.title, title_string[0 .. limited_title_len]);

View File

@ -156,15 +156,15 @@ pub const allocator = coral.io.Allocator.bind(Context, &context, .{
pub fn trace_leaks() void {
switch (builtin.mode) {
.Debug, .ReleaseSafe => {
var current_node = context.head;
var current_allocation_info = context.allocation_info_head;
while (current_node) |node| : (current_node = node.next) {
while (current_allocation_info) |allocation_info| : (current_allocation_info = allocation_info.next_info) {
std.debug.print("{d} byte leak at 0x{x} detected:\n", .{
node.size,
@intFromPtr(node) + @sizeOf(AllocationNode),
allocation_info.size,
@as(usize, allocation_info) + @sizeOf(AllocationNode),
});
node.trace.dump();
allocation_info.trace.dump();
}
},

View File

@ -10,48 +10,17 @@ const file = @import("./file.zig");
const tokens = @import("./kym/tokens.zig");
pub const Any = union (enum) {
nil,
boolean: bool,
number: Float,
string: []const coral.io.Byte,
dynamic: *DynamicObject,
pub const Args = []const ?*const RuntimeRef;
pub fn expect_dynamic(self: Any) ?*DynamicObject {
return switch (self) {
.dynamic => |dynamic| dynamic,
else => null,
};
}
pub fn expect_number(self: Any) ?Float {
return switch (self) {
.number => |number| number,
else => null,
};
}
pub fn expect_string(self: Any) ?[]const coral.io.Byte {
return switch (self) {
.string => |string| string,
else => null,
};
}
pub const CallContext = struct {
env: *RuntimeEnv,
caller: ?*const RuntimeRef,
userdata: []coral.io.Byte,
};
pub const Caller = coral.io.Generator(RuntimeError!?*RuntimeRef, *RuntimeEnv);
pub const DynamicObject = struct {
userdata: []coral.io.Byte,
typeinfo: *const Typeinfo,
pub fn as_caller(self: *DynamicObject) Caller {
return Caller.bind(DynamicObject, self, DynamicObject.call);
}
fn call(self: *DynamicObject, env: *RuntimeEnv) RuntimeError!?*RuntimeRef {
return self.typeinfo.call(env);
}
};
pub const ErrorHandler = coral.io.Generator(void, ErrorInfo);
@ -85,7 +54,7 @@ pub const RuntimeEnv = struct {
const FrameStack = coral.list.Stack(Frame);
const SyscallerTable = coral.map.StringTable(Caller);
const SyscallerTable = coral.map.StringTable(Syscaller);
const RefStack = coral.list.Stack(?*RuntimeRef);
@ -103,7 +72,7 @@ pub const RuntimeEnv = struct {
pub const Syscall = struct {
name: []const coral.io.Byte,
caller: Caller,
caller: Syscaller,
};
pub fn acquire(self: *RuntimeEnv, ref: ?*const RuntimeRef) ?*RuntimeRef {
@ -125,39 +94,6 @@ pub const RuntimeEnv = struct {
}
}
pub fn call(
self: *RuntimeEnv,
name: []const coral.io.Byte,
arg_count: u8,
caller: Caller,
) RuntimeError!?*RuntimeRef {
try self.frames.push_one(.{
.name = name,
.arg_count = arg_count,
.locals_top = self.local_refs.values.len,
});
defer {
const frame = self.frames.pop();
coral.debug.assert(frame != null);
{
var pops_remaining = (self.local_refs.values.len - frame.?.locals_top) + frame.?.arg_count;
while (pops_remaining != 0) : (pops_remaining -= 1) {
const local = self.local_refs.pop();
coral.debug.assert(local != null);
self.discard(local.?);
}
}
}
return caller.invoke(self);
}
pub fn discard(self: *RuntimeEnv, ref: ?*RuntimeRef) void {
const key = @intFromPtr(ref orelse return);
var ref_data = self.ref_values.remove(key) orelse unreachable;
@ -169,13 +105,7 @@ pub const RuntimeEnv = struct {
if (ref_data.ref_count == 0) {
switch (ref_data.object) {
.string => |string| self.allocator.deallocate(string),
.dynamic => |dynamic| {
dynamic.typeinfo.clean(self, dynamic.userdata);
self.allocator.deallocate(dynamic.userdata);
self.allocator.deallocate(dynamic);
},
.dynamic => |virtual| self.allocator.deallocate(virtual),
else => {},
}
} else {
@ -213,55 +143,70 @@ pub const RuntimeEnv = struct {
};
}
var chunk = Chunk.make(self);
var exe = Chunk.make(self);
defer chunk.free();
defer exe.free();
try chunk.compile_ast(ast);
try exe.compile_ast(ast);
return self.call(name, 0, chunk.as_caller());
return try exe.execute(name);
}
pub fn frame_pop(self: *RuntimeEnv) void {
const frame = self.frames.pop();
coral.debug.assert(frame != null);
coral.debug.assert(self.local_refs.drop((self.local_refs.values.len - frame.?.locals_top) + frame.?.arg_count));
}
pub fn frame_push(self: *RuntimeEnv, frame_name: []const coral.io.Byte, arg_count: u8) RuntimeError!void {
try self.frames.push_one(.{
.name = frame_name,
.arg_count = arg_count,
.locals_top = self.local_refs.values.len,
});
}
pub fn free(self: *RuntimeEnv) void {
for (self.local_refs.values) |ref| {
self.discard(ref);
}
self.frames.free();
self.syscallers.free();
self.local_refs.free();
self.ref_values.free();
}
pub fn get_local(self: *RuntimeEnv, local: u8) RuntimeError!?*RuntimeRef {
if (local >= self.local_refs.values.len) {
return self.raise(error.IllegalState, "out of bounds local get");
pub fn get_arg(self: *RuntimeEnv, index: usize) RuntimeError!?*const RuntimeRef {
const frame = self.frames.peek() orelse return self.raise(error.IllegalState, "stack underflow");
if (index >= frame.arg_count) {
return null;
}
return self.local_refs.values[frame.locals_top - (1 + index)];
}
pub fn local_get(self: *RuntimeEnv, local: u8) ?*RuntimeRef {
return self.local_refs.values[local];
}
pub fn pop_local(self: *RuntimeEnv) RuntimeError!?*RuntimeRef {
return self.local_refs.pop() orelse self.raise(error.IllegalState, "stack underflow");
pub fn local_pop(self: *RuntimeEnv) ?*RuntimeRef {
const ref = self.local_refs.pop();
coral.debug.assert(ref != null);
return ref.?;
}
pub fn push_boolean(self: *RuntimeEnv, boolean: bool) RuntimeError!void {
return self.local_refs.push_one(try self.new_boolean(boolean));
}
pub fn push_number(self: *RuntimeEnv, number: Float) RuntimeError!void {
return self.local_refs.push_one(try self.new_number(number));
}
pub fn push_ref(self: *RuntimeEnv, ref: ?*RuntimeRef) RuntimeError!void {
pub fn local_push_ref(self: *RuntimeEnv, ref: ?*RuntimeRef) RuntimeError!void {
return self.local_refs.push_one(self.acquire(ref));
}
pub fn set_local(self: *RuntimeEnv, local: u8, value: ?*RuntimeRef) RuntimeError!void {
if (local >= self.local_refs.values.len) {
return self.raise(error.IllegalState, "out of bounds local set");
pub fn local_push_boolean(self: *RuntimeEnv, boolean: bool) RuntimeError!void {
return self.local_refs.push_one(try self.new_boolean(boolean));
}
pub fn local_push_number(self: *RuntimeEnv, number: Float) RuntimeError!void {
return self.local_refs.push_one(try self.new_number(number));
}
pub fn local_set(self: *RuntimeEnv, local: u8, value: ?*RuntimeRef) void {
self.local_refs.values[local] = self.acquire(value);
}
@ -332,13 +277,12 @@ pub const RuntimeEnv = struct {
return error_value;
}
pub fn syscaller(self: *RuntimeEnv, name: []const coral.io.Byte) RuntimeError!Caller {
pub fn syscaller(self: *RuntimeEnv, name: []const coral.io.Byte) RuntimeError!Syscaller {
return self.syscallers.lookup(name) orelse self.raise(error.BadOperation, "attempt to call undefined syscall");
}
pub fn unbox(self: *RuntimeEnv, ref: ?*const RuntimeRef) Any {
if (ref) |live_ref| {
const ref_data = self.ref_values.lookup(@intFromPtr(live_ref));
pub fn unbox(self: *RuntimeEnv, ref: ?*const RuntimeRef) Unboxed {
const ref_data = self.ref_values.lookup(@intFromPtr(ref orelse return .nil));
coral.debug.assert(ref_data != null);
@ -351,17 +295,32 @@ pub const RuntimeEnv = struct {
};
}
return .nil;
pub fn unbox_dynamic(self: *RuntimeEnv, ref: ?*const RuntimeRef) RuntimeError![]const coral.io.Byte {
if (ref) |live_ref| {
const ref_data = self.ref_values.lookup(@intFromPtr(live_ref));
coral.debug.assert(ref_data != null);
if (ref_data.?.object == .dynamic) {
return ref_data.?.object.dynamic;
}
}
pub fn view_arg(self: *RuntimeEnv, index: usize) RuntimeError!?*const RuntimeRef {
const frame = self.frames.peek() orelse return self.raise(error.IllegalState, "stack underflow");
if (index >= frame.arg_count) {
return null;
return self.raise(error.TypeMismatch, "expected dynamic");
}
return self.local_refs.values[frame.locals_top - (1 + index)];
pub fn unbox_string(self: *RuntimeEnv, ref: ?*const RuntimeRef) RuntimeError![]const coral.io.Byte {
if (ref) |live_ref| {
const ref_data = self.ref_values.lookup(@intFromPtr(live_ref));
coral.debug.assert(ref_data != null);
if (ref_data.?.object == .string) {
return ref_data.?.object.string;
}
}
return self.raise(error.TypeMismatch, "expected string");
}
};
@ -370,10 +329,13 @@ pub const RuntimeError = coral.io.AllocationError || error {
TypeMismatch,
BadOperation,
BadSyntax,
Assertion,
};
pub const RuntimeRef = opaque {};
pub const Syscaller = coral.io.Generator(RuntimeError!?*RuntimeRef, *RuntimeEnv);
pub const TestContext = struct {
env: *RuntimeEnv,
userdata: []const coral.io.Byte,
@ -382,18 +344,18 @@ pub const TestContext = struct {
pub const Typeinfo = struct {
name: []const coral.io.Byte,
call: *const fn (env: *RuntimeEnv) RuntimeError!?*RuntimeRef = default_call,
clean: *const fn (env: *RuntimeEnv, userdata: []coral.io.Byte) void = default_clean,
call: *const fn (context: CallContext) RuntimeError!?*RuntimeRef = default_call,
clean: *const fn (userdata: []coral.io.Byte) void = default_clean,
get: *const fn (context: IndexContext) RuntimeError!?*RuntimeRef = default_get,
set: *const fn (context: IndexContext, value: ?*const RuntimeRef) RuntimeError!void = default_set,
test_difference: *const fn (context: TestContext) RuntimeError!Float = default_test_difference,
test_equality: *const fn (context: TestContext) RuntimeError!bool = default_test_equality,
fn default_call(env: *RuntimeEnv) RuntimeError!?*RuntimeRef {
return env.raise(error.TypeMismatch, "object is not callable");
fn default_call(context: CallContext) RuntimeError!?*RuntimeRef {
return context.env.raise(error.TypeMismatch, "object is not callable");
}
fn default_clean(_: *RuntimeEnv, _: []coral.io.Byte) void {
fn default_clean(_: []coral.io.Byte) void {
// Nothing to clean by default.
}
@ -417,11 +379,32 @@ pub const Typeinfo = struct {
}
};
pub const Unboxed = union (enum) {
nil,
boolean: bool,
number: Float,
string: []const coral.io.Byte,
dynamic: *const DynamicObject,
pub fn expect_dynamic(self: Unboxed, env: *RuntimeEnv) RuntimeError!*const DynamicObject {
return switch (self) {
.dynamic => |dynamic| dynamic,
else => env.raise(error.TypeMismatch, "expected dynamic"),
};
}
};
pub fn assert(env: *RuntimeEnv, condition: bool, message: []const coral.io.Byte) RuntimeError!void {
if (!condition) {
return env.raise(error.Assertion, message);
}
}
pub fn call(
env: *RuntimeEnv,
caller_ref: ?*const RuntimeRef,
callable_ref: ?*const RuntimeRef,
arg_refs: []const ?*const RuntimeRef,
arg_refs: Args,
) RuntimeError!?*RuntimeRef {
for (arg_refs) |arg_ref| {
try env.local_push_ref(arg_ref);
@ -445,7 +428,7 @@ pub fn get(
indexable_ref: ?*const RuntimeRef,
index_ref: ?*const RuntimeRef,
) RuntimeError!?*RuntimeRef {
const dynamic = try unbox_dynamic(env, indexable_ref);
const dynamic = try env.unbox(indexable_ref).expect_dynamic(env);
return dynamic.typeinfo.get(.{
.userdata = dynamic.userdata,
@ -454,11 +437,7 @@ pub fn get(
});
}
pub fn get_field(
env: *RuntimeEnv,
indexable_ref: ?*const RuntimeRef,
field_name: []const coral.io.Byte,
) RuntimeError!?*RuntimeRef {
pub fn get_field(env: *RuntimeEnv, indexable_ref: ?*const RuntimeRef, field_name: []const coral.io.Byte) RuntimeError!?*RuntimeRef {
const field_name_ref = try env.new_string(field_name);
defer env.discard(field_name_ref);
@ -472,7 +451,7 @@ pub fn set(
index_ref: ?*const RuntimeRef,
value_ref: ?*const RuntimeRef,
) RuntimeError!void {
const dynamic = try unbox_dynamic(env, indexable_ref);
const dynamic = try env.unbox(indexable_ref).expect_dynamic(env);
return dynamic.typeinfo.set(.{
.userdata = dynamic.userdata,
@ -481,12 +460,7 @@ pub fn set(
}, value_ref);
}
pub fn set_field(
env: *RuntimeEnv,
indexable_ref: ?*const RuntimeRef,
field_name: []const coral.io.Byte,
value_ref: ?*const RuntimeRef,
) RuntimeError!void {
pub fn set_field(env: *RuntimeEnv, indexable_ref: ?*const RuntimeRef, field_name: []const coral.io.Byte, value_ref: ?*const RuntimeRef) RuntimeError!void {
const field_name_ref = try env.new_string(field_name);
defer env.discard(field_name_ref);
@ -494,7 +468,13 @@ pub fn set_field(
return set(env, indexable_ref, field_name_ref, value_ref);
}
pub const new_table = Table.new;
pub fn new_table(env: *RuntimeEnv) RuntimeError!?*RuntimeRef {
var table = Table.make(env);
errdefer table.free();
return try env.new_dynamic(coral.io.bytes_of(&table), &Table.typeinfo);
}
pub fn test_difference(env: *RuntimeEnv, lhs_ref: ?*const RuntimeRef, rhs_ref: ?*const RuntimeRef) RuntimeError!Float {
return switch (env.unbox(lhs_ref)) {
@ -545,11 +525,3 @@ pub fn test_equality(env: *RuntimeEnv, lhs_ref: ?*const RuntimeRef, rhs_ref: ?*c
}),
};
}
pub fn unbox_dynamic(env: *RuntimeEnv, ref: ?*const RuntimeRef) RuntimeError!*DynamicObject {
return env.unbox(ref).expect_dynamic() orelse env.raise(error.TypeMismatch, "expected dynamic object");
}
pub fn unbox_string(env: *RuntimeEnv, ref: ?*const RuntimeRef) RuntimeError![]const coral.io.Byte {
return env.unbox(ref).expect_string() orelse env.raise(error.TypeMismatch, "expected string object");
}

View File

@ -206,11 +206,9 @@ pub fn append_opcode(self: *Self, opcode: Opcode) kym.RuntimeError!void {
return self.opcodes.push_one(opcode);
}
pub fn as_caller(self: *Self) kym.Caller {
return kym.Caller.bind(Self, self, execute);
}
pub fn compile_ast(self: *Self, ast: Ast) kym.RuntimeError!void {
self.free();
try self.constant_refs.grow(coral.math.max_int(@typeInfo(u16).Int));
var compiler = AstCompiler{.chunk = self};
@ -255,239 +253,250 @@ pub fn declare_constant_string(self: *Self, constant: []const coral.io.Byte) kym
return @intCast(tail);
}
fn execute(self: *Self, env: *kym.RuntimeEnv) kym.RuntimeError!?*kym.RuntimeRef {
pub fn execute(self: *Self, name: []const coral.io.Byte) kym.RuntimeError!?*kym.RuntimeRef {
try self.env.frame_push(name, 0);
defer self.env.frame_pop();
for (self.opcodes.values) |opcode| {
switch (opcode) {
.pop => env.discard(try env.pop_local()),
.push_nil => try env.push_ref(null),
.push_true => try env.push_boolean(true),
.push_false => try env.push_boolean(false),
.push_const => |constant| try env.push_ref(self.constant_refs.values[constant]),
.pop => self.env.discard(self.env.local_pop()),
.push_nil => try self.env.local_push_ref(null),
.push_true => try self.env.local_push_boolean(true),
.push_false => try self.env.local_push_boolean(false),
.push_const => |constant| try self.env.local_push_ref(self.constant_refs.values[constant]),
.push_table => |field_count| {
const table_ref = try kym.new_table(env);
const table_ref = try kym.new_table(self.env);
defer env.discard(table_ref);
defer self.env.discard(table_ref);
{
const dynamic = try kym.unbox_dynamic(env, table_ref);
const dynamic = try self.env.unbox(table_ref).expect_dynamic(self.env);
var popped = @as(usize, 0);
while (popped < field_count) : (popped += 1) {
const index_ref = try env.pop_local();
defer env.discard(index_ref);
const value_ref = try env.pop_local();
defer env.discard(value_ref);
const index_ref = self.env.local_pop();
const value_ref = self.env.local_pop();
try dynamic.typeinfo.set(.{
.userdata = dynamic.userdata,
.env = env,
.env = self.env,
.index_ref = index_ref,
}, value_ref);
}
}
try env.push_ref(table_ref);
try self.env.local_push_ref(table_ref);
},
.push_local => |local| {
const ref = try env.get_local(local);
const ref = self.env.local_get(local);
defer env.discard(ref);
defer self.env.discard(ref);
try env.push_ref(ref);
try self.env.local_push_ref(ref);
},
.set_local => |local| {
const ref = try env.pop_local();
const ref = self.env.local_pop();
defer env.discard(ref);
defer self.env.discard(ref);
try env.set_local(local, ref);
self.env.local_set(local, ref);
},
.call => |arg_count| {
const result_ref = call: {
const callable_ref = try env.pop_local();
const callable_ref = self.env.local_pop();
defer env.discard(callable_ref);
defer self.env.discard(callable_ref);
break: call try env.call("", arg_count, (try kym.unbox_dynamic(env, callable_ref)).as_caller());
};
try self.env.frame_push("", arg_count);
defer env.discard(result_ref);
defer self.env.frame_pop();
try env.push_ref(result_ref);
const dynamic = try self.env.unbox(callable_ref).expect_dynamic(self.env);
const result_ref = try dynamic.typeinfo.call(.{
.env = self.env,
.caller = null,
.userdata = dynamic.userdata,
});
defer self.env.discard(result_ref);
try self.env.local_push_ref(result_ref);
},
.syscall => |arg_count| {
const result_ref = call: {
const identifier_ref = try env.pop_local();
const identifier_ref = self.env.local_pop();
defer env.discard(identifier_ref);
defer self.env.discard(identifier_ref);
const identifier = try kym.unbox_string(env, identifier_ref);
const identifier = try self.env.unbox_string(identifier_ref);
break: call try env.call(identifier, arg_count, try env.syscaller(identifier));
};
try self.env.frame_push(identifier, arg_count);
defer env.discard(result_ref);
errdefer self.env.frame_pop();
try env.push_ref(result_ref);
const result_ref = try (try self.env.syscaller(identifier)).invoke(self.env);
defer self.env.discard(result_ref);
self.env.frame_pop();
try self.env.local_push_ref(result_ref);
},
.neg => try env.push_number(switch (env.unbox(try env.pop_local())) {
.neg => try self.env.local_push_number(switch (self.env.unbox(self.env.local_pop())) {
.number => |number| -number,
else => return env.raise(error.TypeMismatch, "object is not scalar negatable"),
else => return self.env.raise(error.TypeMismatch, "object is not scalar negatable"),
}),
.not => try env.push_boolean(switch (env.unbox(try env.pop_local())) {
.not => try self.env.local_push_boolean(switch (self.env.unbox(self.env.local_pop())) {
.boolean => |boolean| !boolean,
else => return env.raise(error.TypeMismatch, "object is not boolean negatable"),
else => return self.env.raise(error.TypeMismatch, "object is not boolean negatable"),
}),
.add => {
const rhs_ref = try env.pop_local();
const rhs_ref = self.env.local_pop();
defer env.discard(rhs_ref);
defer self.env.discard(rhs_ref);
const lhs_ref = try env.pop_local();
const lhs_ref = self.env.local_pop();
defer env.discard(lhs_ref);
defer self.env.discard(lhs_ref);
try env.push_ref(try switch (env.unbox(lhs_ref)) {
.number => |lhs_number| switch (env.unbox(rhs_ref)) {
.number => |rhs_number| env.new_number(lhs_number + rhs_number),
else => return env.raise(error.TypeMismatch, "right-hand object is not addable"),
try self.env.local_push_ref(switch (self.env.unbox(lhs_ref)) {
.number => |lhs_number| switch (self.env.unbox(rhs_ref)) {
.number => |rhs_number| try self.env.new_number(lhs_number + rhs_number),
else => return self.env.raise(error.TypeMismatch, "right-hand object is not addable"),
},
else => return env.raise(error.TypeMismatch, "left-hand object is not addable"),
else => return self.env.raise(error.TypeMismatch, "left-hand object is not addable"),
});
},
.sub => {
const rhs_ref = try env.pop_local();
const rhs_ref = self.env.local_pop();
defer env.discard(rhs_ref);
defer self.env.discard(rhs_ref);
const lhs_ref = try env.pop_local();
const lhs_ref = self.env.local_pop();
defer env.discard(lhs_ref);
defer self.env.discard(lhs_ref);
try env.push_ref(try switch (env.unbox(lhs_ref)) {
.number => |lhs_number| switch (env.unbox(rhs_ref)) {
.number => |rhs_number| env.new_number(lhs_number - rhs_number),
else => return env.raise(error.TypeMismatch, "right-hand object is not subtractable"),
try self.env.local_push_ref(switch (self.env.unbox(lhs_ref)) {
.number => |lhs_number| switch (self.env.unbox(rhs_ref)) {
.number => |rhs_number| try self.env.new_number(lhs_number - rhs_number),
else => return self.env.raise(error.TypeMismatch, "right-hand object is not subtractable"),
},
else => return env.raise(error.TypeMismatch, "left-hand object is not subtractable"),
else => return self.env.raise(error.TypeMismatch, "left-hand object is not subtractable"),
});
},
.mul => {
const rhs_ref = try env.pop_local();
const rhs_ref = self.env.local_pop();
defer env.discard(rhs_ref);
defer self.env.discard(rhs_ref);
const lhs_ref = try env.pop_local();
const lhs_ref = self.env.local_pop();
defer env.discard(lhs_ref);
defer self.env.discard(lhs_ref);
try env.push_ref(try switch (env.unbox(lhs_ref)) {
.number => |lhs_number| switch (env.unbox(rhs_ref)) {
.number => |rhs_number| env.new_number(lhs_number * rhs_number),
else => return env.raise(error.TypeMismatch, "right-hand object is not multiplyable"),
try self.env.local_push_ref(switch (self.env.unbox(lhs_ref)) {
.number => |lhs_number| switch (self.env.unbox(rhs_ref)) {
.number => |rhs_number| try self.env.new_number(lhs_number * rhs_number),
else => return self.env.raise(error.TypeMismatch, "right-hand object is not multiplyable"),
},
else => return env.raise(error.TypeMismatch, "left-hand object is not multiplyable"),
else => return self.env.raise(error.TypeMismatch, "left-hand object is not multiplyable"),
});
},
.div => {
const rhs_ref = try env.pop_local();
const rhs_ref = self.env.local_pop();
defer env.discard(rhs_ref);
defer self.env.discard(rhs_ref);
const lhs_ref = try env.pop_local();
const lhs_ref = self.env.local_pop();
defer env.discard(lhs_ref);
defer self.env.discard(lhs_ref);
try env.push_ref(try switch (env.unbox(lhs_ref)) {
.number => |lhs_number| switch (env.unbox(rhs_ref)) {
.number => |rhs_number| env.new_number(lhs_number / rhs_number),
else => return env.raise(error.TypeMismatch, "right-hand object is not divisable"),
try self.env.local_push_ref(switch (self.env.unbox(lhs_ref)) {
.number => |lhs_number| switch (self.env.unbox(rhs_ref)) {
.number => |rhs_number| try self.env.new_number(lhs_number / rhs_number),
else => return self.env.raise(error.TypeMismatch, "right-hand object is not divisable"),
},
else => return env.raise(error.TypeMismatch, "left-hand object is not divisable"),
else => return self.env.raise(error.TypeMismatch, "left-hand object is not divisable"),
});
},
.eql => {
const rhs_ref = try env.pop_local();
const rhs_ref = self.env.local_pop();
defer env.discard(rhs_ref);
defer self.env.discard(rhs_ref);
const lhs_ref = try env.pop_local();
const lhs_ref = self.env.local_pop();
defer env.discard(lhs_ref);
defer self.env.discard(lhs_ref);
try env.push_boolean(try kym.test_equality(env, lhs_ref, rhs_ref));
try self.env.local_push_boolean(try kym.test_equality(self.env, lhs_ref, rhs_ref));
},
.cgt => {
const rhs_ref = try env.pop_local();
const rhs_ref = self.env.local_pop();
defer env.discard(rhs_ref);
defer self.env.discard(rhs_ref);
const lhs_ref = try env.pop_local();
const lhs_ref = self.env.local_pop();
defer env.discard(lhs_ref);
defer self.env.discard(lhs_ref);
try env.push_boolean(try kym.test_difference(env, lhs_ref, rhs_ref) > 0);
try self.env.local_push_boolean(try kym.test_difference(self.env, lhs_ref, rhs_ref) > 0);
},
.clt => {
const rhs_ref = try env.pop_local();
const rhs_ref = self.env.local_pop();
defer env.discard(rhs_ref);
defer self.env.discard(rhs_ref);
const lhs_ref = try env.pop_local();
const lhs_ref = self.env.local_pop();
defer env.discard(lhs_ref);
defer self.env.discard(lhs_ref);
try env.push_boolean(try kym.test_difference(env, lhs_ref, rhs_ref) < 0);
try self.env.local_push_boolean(try kym.test_difference(self.env, lhs_ref, rhs_ref) < 0);
},
.cge => {
const rhs_ref = try env.pop_local();
const rhs_ref = self.env.local_pop();
defer env.discard(rhs_ref);
defer self.env.discard(rhs_ref);
const lhs_ref = try env.pop_local();
const lhs_ref = self.env.local_pop();
defer env.discard(lhs_ref);
defer self.env.discard(lhs_ref);
try env.push_boolean(try kym.test_difference(env, lhs_ref, rhs_ref) >= 0);
try self.env.local_push_boolean(try kym.test_difference(self.env, lhs_ref, rhs_ref) >= 0);
},
.cle => {
const rhs_ref = try env.pop_local();
const rhs_ref = self.env.local_pop();
defer env.discard(rhs_ref);
defer self.env.discard(rhs_ref);
const lhs_ref = try env.pop_local();
const lhs_ref = self.env.local_pop();
defer env.discard(lhs_ref);
defer self.env.discard(lhs_ref);
try env.push_boolean(try kym.test_difference(env, lhs_ref, rhs_ref) <= 0);
try self.env.local_push_boolean(try kym.test_difference(self.env, lhs_ref, rhs_ref) <= 0);
},
}
}
return env.pop_local();
return self.env.local_pop();
}
pub fn free(self: *Self) void {

View File

@ -11,16 +11,14 @@ const FieldTable = coral.map.StringTable(struct {
const Self = @This();
pub fn new(env: *kym.RuntimeEnv) kym.RuntimeError!?*kym.RuntimeRef {
var self = Self{
pub fn free(self: *Self) void {
self.fields.free();
}
pub fn make(env: *kym.RuntimeEnv) Self {
return .{
.fields = FieldTable.make(env.allocator),
};
errdefer {
self.fields.free();
}
return try env.new_dynamic(coral.io.bytes_of(&self), &typeinfo);
}
pub const typeinfo = kym.Typeinfo{
@ -30,19 +28,8 @@ pub const typeinfo = kym.Typeinfo{
.set = typeinfo_set,
};
fn typeinfo_clean(env: *kym.RuntimeEnv, userdata: []coral.io.Byte) void {
const table = @as(*Self, @ptrCast(@alignCast(userdata.ptr)));
{
var field_iterable = table.fields.as_iterable();
while (field_iterable.next()) |entry| {
env.discard(entry.value.key_ref);
env.discard(entry.value.value_ref);
}
}
table.fields.free();
fn typeinfo_clean(userdata: []u8) void {
@as(*Self, @ptrCast(@alignCast(userdata.ptr))).free();
}
fn typeinfo_get(context: kym.IndexContext) kym.RuntimeError!?*kym.RuntimeRef {

View File

@ -21,19 +21,19 @@ fn kym_handle_errors(info: kym.ErrorInfo) void {
}
fn kym_log_info(env: *kym.RuntimeEnv) kym.RuntimeError!?*kym.RuntimeRef {
app.log_info(try kym.unbox_string(env, try env.view_arg(0)));
app.log_info(try env.unbox_string(try env.get_arg(0)));
return null;
}
fn kym_log_warn(env: *kym.RuntimeEnv) kym.RuntimeError!?*kym.RuntimeRef {
app.log_warn(try kym.unbox_string(env, try env.view_arg(0)));
app.log_warn(try env.unbox_string(try env.get_arg(0)));
return null;
}
fn kym_log_fail(env: *kym.RuntimeEnv) kym.RuntimeError!?*kym.RuntimeRef {
app.log_fail(try kym.unbox_string(env, try env.view_arg(0)));
app.log_fail(try env.unbox_string(try env.get_arg(0)));
return null;
}
@ -43,8 +43,6 @@ fn last_sdl_error() [:0]const u8 {
}
pub fn run_app(file_access: file.Access) void {
defer heap.trace_leaks();
if (ext.SDL_Init(ext.SDL_INIT_EVERYTHING) != 0) {
return app.log_fail(last_sdl_error());
}
@ -60,11 +58,11 @@ pub fn run_app(file_access: file.Access) void {
script_env.bind_syscalls(&.{
.{
.name = "log_info",
.caller = kym.Caller.from(kym_log_info),
.caller = kym.Syscaller.from(kym_log_info),
},
.{
.name = "log_fail",
.caller = kym.Caller.from(kym_log_fail),
.caller = kym.Syscaller.from(kym_log_fail),
},
}) catch {
return app.log_fail("failed to initialize script runtime");