From bdf207a8214378e93562ab36331f032776c4164e Mon Sep 17 00:00:00 2001 From: Techatrix <19954306+Techatrix@users.noreply.github.com> Date: Sat, 14 Jan 2023 21:49:27 +0100 Subject: [PATCH] small refactor on ComptimeInterpreter --- src/ComptimeInterpreter.zig | 48 +++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/src/ComptimeInterpreter.zig b/src/ComptimeInterpreter.zig index 92aefba..567748a 100644 --- a/src/ComptimeInterpreter.zig +++ b/src/ComptimeInterpreter.zig @@ -155,7 +155,7 @@ pub fn huntItDown( namespace: NamespaceIndex, decl_name: []const u8, options: InterpretOptions, -) InterpretError!Decl { +) error{IdentifierNotFound}!Decl { _ = options; var current_namespace = namespace; @@ -168,7 +168,6 @@ pub fn huntItDown( } } - log.err("Identifier not found: {s}", .{decl_name}); return error.IdentifierNotFound; } @@ -431,13 +430,15 @@ pub fn interpret( // TODO: Floats // Logic to find identifiers in accessible scopes - const decl = interpreter.huntItDown(namespace, value, options) catch |err| { - if (err == error.IdentifierNotFound) try interpreter.recordError( - node_idx, - "undeclared_identifier", - try std.fmt.allocPrint(interpreter.allocator, "use of undeclared identifier '{s}'", .{value}), - ); - return err; + const decl = interpreter.huntItDown(namespace, value, options) catch |err| switch (err) { + error.IdentifierNotFound => |e| { + try interpreter.recordError( + node_idx, + "undeclared_identifier", + try std.fmt.allocPrint(interpreter.allocator, "use of undeclared identifier '{s}'", .{value}), + ); + return e; + }, }; return InterpretResult{ .value = Value{ @@ -456,13 +457,15 @@ pub fn interpret( const lhs_namespace = interpreter.ip.indexToKey(irv.val).getNamespace(); - var scope_sub_decl = irv.interpreter.huntItDown(lhs_namespace, rhs_str, options) catch |err| { - if (err == error.IdentifierNotFound) try interpreter.recordError( - node_idx, - "undeclared_identifier", - try std.fmt.allocPrint(interpreter.allocator, "use of undeclared identifier '{s}'", .{rhs_str}), - ); - return err; + var scope_sub_decl = irv.interpreter.huntItDown(lhs_namespace, rhs_str, options) catch |err| switch (err) { + error.IdentifierNotFound => |e| { + try interpreter.recordError( + node_idx, + "undeclared_identifier", + try std.fmt.allocPrint(interpreter.allocator, "use of undeclared identifier '{s}'", .{rhs_str}), + ); + return e; + }, }; return InterpretResult{ .value = Value{ @@ -760,13 +763,9 @@ pub fn interpret( .interpreter = interpreter, .node_idx = node_idx, .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 }; }, // TODO: Add comptime autodetection; e.g. const MyArrayList = std.ArrayList(u8) @@ -959,9 +958,9 @@ pub fn call( // TODO: type check args 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 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 }); - var buf: [1]Ast.Node.Index = undefined; - var proto = ast.fnProto(tree, func_node_idx, &buf).?; - var arg_it = proto.iterate(&tree); var arg_index: usize = 0; while (ast.nextFnParam(&arg_it)) |param| {