Compare commits

..

2 Commits

Author SHA1 Message Date
kayomn 8e42aa57fd Move builtin decoding responsibility to compiler
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details
2023-08-13 01:34:32 +01:00
kayomn 9a0a594345 Make Kym env options optional 2023-08-13 01:27:13 +01:00
3 changed files with 33 additions and 32 deletions

View File

@ -3,10 +3,13 @@ const coral = @import("coral");
const ext = @import("./ext.zig");
pub const Access = union (enum) {
null,
sandboxed_path: *const Path,
pub fn open_readable(self: Access, readable_path: Path) ?*Readable {
switch (self) {
.null => return null,
.sandboxed_path => |sandboxed_path| {
const readable_path_string = sandboxed_path.joined(readable_path).to_string() orelse return null;
@ -17,6 +20,8 @@ pub const Access = union (enum) {
pub fn query(self: Access, path: Path) ?Info {
switch (self) {
.null => return null,
.sandboxed_path => |sandboxed_path| {
const path_string = sandboxed_path.joined(path).to_string() orelse return null;
const rw_ops = ext.SDL_RWFromFile(path_string, "rb") orelse return null;

View File

@ -164,12 +164,18 @@ pub const RuntimeEnv = struct {
try self.compile_expression(chunk, grouped_expression.*);
},
.import => {
try chunk.opcodes.push_one(.{.push_builtin = .import});
},
.builtin => |builtin| {
coral.debug.assert(builtin.len != 0);
.print => {
try chunk.opcodes.push_one(.{.push_builtin = .print});
const decoded_builtin = @as(?Builtin, switch (builtin[0]) {
'i' => if (coral.io.ends_with(builtin, "mport")) .import else null,
'p' => if (coral.io.ends_with(builtin, "rint")) .print else null,
else => null,
});
try chunk.opcodes.push_one(.{.push_builtin = decoded_builtin orelse {
return chunk.env.raise(error.BadSyntax, "unknown builtin");
}});
},
.local_get => |local_get| {
@ -802,9 +808,9 @@ pub const RuntimeEnv = struct {
});
pub const Options = struct {
import_access: file.Access,
print: *const Printer,
print_error: *const Printer,
import_access: file.Access = .null,
print: ?*const Printer = null,
print_error: ?*const Printer = null,
};
pub const Printer = fn (buffer: []const coral.io.Byte) void;
@ -1185,21 +1191,29 @@ pub const RuntimeEnv = struct {
}
pub fn print(self: *RuntimeEnv, buffer: []const coral.io.Byte) void {
self.options.print(buffer);
if (self.options.print) |bound_print| {
bound_print(buffer);
}
}
pub fn print_error(self: *RuntimeEnv, buffer: []const coral.io.Byte) void {
if (self.options.print_error) |bound_print_error| {
bound_print_error(buffer);
}
}
pub fn raise(self: *RuntimeEnv, error_value: RuntimeError, message: []const coral.io.Byte) RuntimeError {
self.options.print_error(message);
self.print_error(message);
if (!self.frames.is_empty()) {
self.options.print_error("stack trace:");
self.print_error("stack trace:");
var remaining_frames = self.frames.values.len;
while (remaining_frames != 0) {
remaining_frames -= 1;
self.options.print_error(self.frames.values[remaining_frames].name);
self.print_error(self.frames.values[remaining_frames].name);
}
}

View File

@ -13,8 +13,7 @@ pub const Expression = union (enum) {
nil_literal,
true_literal,
false_literal,
import,
print,
builtin: []const coral.io.Byte,
number_literal: []const coral.io.Byte,
string_literal: []const coral.io.Byte,
symbol_literal: []const coral.io.Byte,
@ -354,25 +353,8 @@ fn parse_factor(self: *Self) ParseError!Expression {
.builtin => |builtin| {
self.tokenizer.skip_newlines();
coral.debug.assert(builtin.len != 0);
switch (builtin[0]) {
'i' => {
if (coral.io.ends_with(builtin, "mport")) {
break: parse .import;
}
},
'p' => {
if (coral.io.ends_with(builtin, "rint")) {
break: parse .print;
}
},
else => {},
}
return self.report("unknown builtin");
break: parse .{.builtin = builtin};
},
.symbol_brace_left => {