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.*);
},
.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| {

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