add formatting to recordError

This commit is contained in:
Techatrix 2023-01-20 19:55:26 +01:00
parent ea7fc74483
commit 861e2ffb05

View File

@ -40,10 +40,16 @@ pub const InterpreterError = struct {
}; };
/// `message` must be allocated with interpreter allocator /// `message` must be allocated with interpreter allocator
pub fn recordError(interpreter: *ComptimeInterpreter, node_idx: Ast.Node.Index, code: []const u8, message: []const u8) error{OutOfMemory}!void { pub fn recordError(
interpreter: *ComptimeInterpreter,
node_idx: Ast.Node.Index,
code: []const u8,
comptime fmt: []const u8,
args: anytype,
) error{OutOfMemory}!void {
try interpreter.errors.put(interpreter.allocator, node_idx, .{ try interpreter.errors.put(interpreter.allocator, node_idx, .{
.code = code, .code = code,
.message = message, .message = try std.fmt.allocPrint(interpreter.allocator, fmt, args),
}); });
} }
@ -244,7 +250,8 @@ pub fn interpret(
try interpreter.recordError( try interpreter.recordError(
container_field.ast.type_expr, container_field.ast.type_expr,
"expected_type", "expected_type",
try std.fmt.allocPrint(interpreter.allocator, "expected type 'type', found '{}'", .{init_type_value.ty.fmtType(interpreter.ip)}), "expected type 'type', found '{}'",
.{init_type_value.ty.fmtType(interpreter.ip)},
); );
continue; continue;
} }
@ -429,7 +436,8 @@ pub fn interpret(
try interpreter.recordError( try interpreter.recordError(
node_idx, node_idx,
"undeclared_identifier", "undeclared_identifier",
try std.fmt.allocPrint(interpreter.allocator, "use of undeclared identifier '{s}'", .{value}), "use of undeclared identifier '{s}'",
.{value},
); );
return e; return e;
}, },
@ -456,7 +464,8 @@ pub fn interpret(
try interpreter.recordError( try interpreter.recordError(
node_idx, node_idx,
"undeclared_identifier", "undeclared_identifier",
try std.fmt.allocPrint(interpreter.allocator, "use of undeclared identifier '{s}'", .{rhs_str}), "`{}` has no member '{s}'",
.{ irv.ty.fmtType(interpreter.ip), rhs_str },
); );
return e; return e;
}, },
@ -620,14 +629,15 @@ pub fn interpret(
if (index != params.len - 1) if (index != params.len - 1)
try writer.writeAll(", "); try writer.writeAll(", ");
} }
try interpreter.recordError(node_idx, "compile_log", try final.toOwnedSlice()); try interpreter.recordError(node_idx, "compile_log", "{s}", .{try final.toOwnedSlice()});
return InterpretResult{ .nothing = {} }; return InterpretResult{ .nothing = {} };
} }
if (std.mem.eql(u8, call_name, "@compileError")) { if (std.mem.eql(u8, call_name, "@compileError")) {
// TODO: Add message if (params.len != 0) return error.InvalidBuiltin;
try interpreter.recordError(node_idx, "compile_error", try std.fmt.allocPrint(interpreter.allocator, "compile error", .{})); const message = offsets.nodeToSlice(tree, params[0]);
try interpreter.recordError(node_idx, "compile_error", "{s}", .{message});
return InterpretResult{ .@"return" = {} }; return InterpretResult{ .@"return" = {} };
} }
@ -865,7 +875,12 @@ pub fn interpret(
const value = try result.getValue(); const value = try result.getValue();
if (value.ty != bool_type) { if (value.ty != bool_type) {
try interpreter.recordError(node_idx, "invalid_deref", try std.fmt.allocPrint(interpreter.allocator, "expected type `bool` but got `{}`", .{value.ty.fmtType(interpreter.ip)})); try interpreter.recordError(
node_idx,
"invalid_deref",
"expected type `bool` but got `{}`",
.{value.ty.fmtType(interpreter.ip)},
);
return error.InvalidOperation; return error.InvalidOperation;
} }
@ -912,7 +927,7 @@ pub fn interpret(
const type_key = interpreter.ip.indexToKey(value.ty); const type_key = interpreter.ip.indexToKey(value.ty);
if (type_key != .pointer_type) { if (type_key != .pointer_type) {
try interpreter.recordError(node_idx, "invalid_deref", try std.fmt.allocPrint(interpreter.allocator, "cannot deference non-pointer", .{})); try interpreter.recordError(node_idx, "invalid_deref", "cannot deference non-pointer", .{});
return error.InvalidOperation; return error.InvalidOperation;
} }
@ -973,7 +988,8 @@ pub fn call(
try interpreter.recordError( try interpreter.recordError(
param.type_expr, param.type_expr,
"expected_type", "expected_type",
std.fmt.allocPrint(interpreter.allocator, "expected type 'type', found '{}'", .{tex.ty.fmtType(interpreter.ip)}) catch return error.CriticalAstFailure, "expected type 'type', found '{}'",
.{tex.ty.fmtType(interpreter.ip)},
); );
return error.InvalidCast; return error.InvalidCast;
} }