Move builtin decoding responsibility to compiler
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
kayomn 2023-08-13 01:34:32 +01:00
parent 9a0a594345
commit 8e42aa57fd
2 changed files with 13 additions and 25 deletions

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 => {