Compare commits

..

No commits in common. "8e42aa57fd40fe639863cbe13623ba160c32f177" and "429f7a52ad428c4f0d267308703b7fc48834b1e1" have entirely different histories.

3 changed files with 32 additions and 33 deletions

View File

@ -3,13 +3,10 @@ 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;
@ -20,8 +17,6 @@ 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,18 +164,12 @@ pub const RuntimeEnv = struct {
try self.compile_expression(chunk, grouped_expression.*);
},
.builtin => |builtin| {
coral.debug.assert(builtin.len != 0);
.import => {
try chunk.opcodes.push_one(.{.push_builtin = .import});
},
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");
}});
.print => {
try chunk.opcodes.push_one(.{.push_builtin = .print});
},
.local_get => |local_get| {
@ -808,9 +802,9 @@ pub const RuntimeEnv = struct {
});
pub const Options = struct {
import_access: file.Access = .null,
print: ?*const Printer = null,
print_error: ?*const Printer = null,
import_access: file.Access,
print: *const Printer,
print_error: *const Printer,
};
pub const Printer = fn (buffer: []const coral.io.Byte) void;
@ -1191,29 +1185,21 @@ pub const RuntimeEnv = struct {
}
pub fn print(self: *RuntimeEnv, buffer: []const coral.io.Byte) void {
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);
}
self.options.print(buffer);
}
pub fn raise(self: *RuntimeEnv, error_value: RuntimeError, message: []const coral.io.Byte) RuntimeError {
self.print_error(message);
self.options.print_error(message);
if (!self.frames.is_empty()) {
self.print_error("stack trace:");
self.options.print_error("stack trace:");
var remaining_frames = self.frames.values.len;
while (remaining_frames != 0) {
remaining_frames -= 1;
self.print_error(self.frames.values[remaining_frames].name);
self.options.print_error(self.frames.values[remaining_frames].name);
}
}

View File

@ -13,7 +13,8 @@ pub const Expression = union (enum) {
nil_literal,
true_literal,
false_literal,
builtin: []const coral.io.Byte,
import,
print,
number_literal: []const coral.io.Byte,
string_literal: []const coral.io.Byte,
symbol_literal: []const coral.io.Byte,
@ -353,8 +354,25 @@ fn parse_factor(self: *Self) ParseError!Expression {
.builtin => |builtin| {
self.tokenizer.skip_newlines();
coral.debug.assert(builtin.len != 0);
break: parse .{.builtin = builtin};
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");
},
.symbol_brace_left => {