small refactor on ComptimeInterpreter

This commit is contained in:
Techatrix 2023-01-14 21:49:27 +01:00
parent d49979d002
commit bdf207a821

View File

@ -155,7 +155,7 @@ pub fn huntItDown(
namespace: NamespaceIndex, namespace: NamespaceIndex,
decl_name: []const u8, decl_name: []const u8,
options: InterpretOptions, options: InterpretOptions,
) InterpretError!Decl { ) error{IdentifierNotFound}!Decl {
_ = options; _ = options;
var current_namespace = namespace; var current_namespace = namespace;
@ -168,7 +168,6 @@ pub fn huntItDown(
} }
} }
log.err("Identifier not found: {s}", .{decl_name});
return error.IdentifierNotFound; return error.IdentifierNotFound;
} }
@ -431,13 +430,15 @@ pub fn interpret(
// TODO: Floats // TODO: Floats
// Logic to find identifiers in accessible scopes // Logic to find identifiers in accessible scopes
const decl = interpreter.huntItDown(namespace, value, options) catch |err| { const decl = interpreter.huntItDown(namespace, value, options) catch |err| switch (err) {
if (err == error.IdentifierNotFound) try interpreter.recordError( error.IdentifierNotFound => |e| {
node_idx, try interpreter.recordError(
"undeclared_identifier", node_idx,
try std.fmt.allocPrint(interpreter.allocator, "use of undeclared identifier '{s}'", .{value}), "undeclared_identifier",
); try std.fmt.allocPrint(interpreter.allocator, "use of undeclared identifier '{s}'", .{value}),
return err; );
return e;
},
}; };
return InterpretResult{ .value = Value{ return InterpretResult{ .value = Value{
@ -456,13 +457,15 @@ pub fn interpret(
const lhs_namespace = interpreter.ip.indexToKey(irv.val).getNamespace(); const lhs_namespace = interpreter.ip.indexToKey(irv.val).getNamespace();
var scope_sub_decl = irv.interpreter.huntItDown(lhs_namespace, rhs_str, options) catch |err| { var scope_sub_decl = irv.interpreter.huntItDown(lhs_namespace, rhs_str, options) catch |err| switch (err) {
if (err == error.IdentifierNotFound) try interpreter.recordError( error.IdentifierNotFound => |e| {
node_idx, try interpreter.recordError(
"undeclared_identifier", node_idx,
try std.fmt.allocPrint(interpreter.allocator, "use of undeclared identifier '{s}'", .{rhs_str}), "undeclared_identifier",
); try std.fmt.allocPrint(interpreter.allocator, "use of undeclared identifier '{s}'", .{rhs_str}),
return err; );
return e;
},
}; };
return InterpretResult{ .value = Value{ return InterpretResult{ .value = Value{
@ -760,13 +763,9 @@ pub fn interpret(
.interpreter = interpreter, .interpreter = interpreter,
.node_idx = node_idx, .node_idx = node_idx,
.ty = string_literal_type, .ty = string_literal_type,
.val = try interpreter.ip.get(interpreter.allocator, IPKey{ .bytes = str }), // TODO .val = try interpreter.ip.get(interpreter.allocator, IPKey{ .bytes = str }),
}; };
// TODO: Add type casting, sentinel
// TODO: Should this be a `*const [len:0]u8`?
// try val.value_data.slice_ptr.append(interpreter.allocator, .{ .unsigned_int = 0 });
return InterpretResult{ .value = val }; return InterpretResult{ .value = val };
}, },
// TODO: Add comptime autodetection; e.g. const MyArrayList = std.ArrayList(u8) // TODO: Add comptime autodetection; e.g. const MyArrayList = std.ArrayList(u8)
@ -959,9 +958,9 @@ pub fn call(
// TODO: type check args // TODO: type check args
const tree = interpreter.getHandle().tree; const tree = interpreter.getHandle().tree;
const tags = tree.nodes.items(.tag);
if (tags[func_node_idx] != .fn_decl) return error.CriticalAstFailure; var buf: [1]Ast.Node.Index = undefined;
var proto = ast.fnProto(tree, func_node_idx, &buf) orelse return error.CriticalAstFailure;
// TODO: Make argument namespace to evaluate arguments in // TODO: Make argument namespace to evaluate arguments in
try interpreter.namespaces.append(interpreter.allocator, .{ try interpreter.namespaces.append(interpreter.allocator, .{
@ -973,9 +972,6 @@ pub fn call(
const type_type = try interpreter.ip.get(interpreter.allocator, IPKey{ .simple = .type }); const type_type = try interpreter.ip.get(interpreter.allocator, IPKey{ .simple = .type });
var buf: [1]Ast.Node.Index = undefined;
var proto = ast.fnProto(tree, func_node_idx, &buf).?;
var arg_it = proto.iterate(&tree); var arg_it = proto.iterate(&tree);
var arg_index: usize = 0; var arg_index: usize = 0;
while (ast.nextFnParam(&arg_it)) |param| { while (ast.nextFnParam(&arg_it)) |param| {