Replace System Objects with Builtins and Syscalls #33

Merged
kayomn merged 3 commits from kym-builtins into main 2023-08-13 02:35:26 +02:00
2 changed files with 13 additions and 25 deletions
Showing only changes of commit 8e42aa57fd - Show all commits

View File

@ -164,12 +164,18 @@ pub const RuntimeEnv = struct {
try self.compile_expression(chunk, grouped_expression.*); try self.compile_expression(chunk, grouped_expression.*);
}, },
.import => { .builtin => |builtin| {
try chunk.opcodes.push_one(.{.push_builtin = .import}); coral.debug.assert(builtin.len != 0);
},
.print => { const decoded_builtin = @as(?Builtin, switch (builtin[0]) {
try chunk.opcodes.push_one(.{.push_builtin = .print}); '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| { .local_get => |local_get| {

View File

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