Improve comptime evaluator

This commit is contained in:
Richard Feldman 2025-12-06 11:46:51 -05:00
parent e3e9b2b135
commit c2d313dece
No known key found for this signature in database
2 changed files with 69 additions and 2 deletions

View file

@ -1514,8 +1514,12 @@ pub const ComptimeEvaluator = struct {
try self.reportProblem(expect_info.message, expect_info.region, .expect_failed);
},
.error_eval => |error_info| {
const error_name = @errorName(error_info.err);
try self.reportProblem(error_name, error_info.region, .error_eval);
// Provide user-friendly messages for specific errors
const error_message = switch (error_info.err) {
error.DivisionByZero => "Division by zero",
else => @errorName(error_info.err),
};
try self.reportProblem(error_message, error_info.region, .error_eval);
},
}
}

View file

@ -1763,3 +1763,66 @@ test "comptime eval - to_str on unbound number literal" {
// Flex var defaults to Dec; Dec.to_str is provided by builtins
try testing.expectEqual(@as(usize, 0), result.problems.len());
}
// --- Division by zero tests ---
test "comptime eval - division by zero produces error" {
const src =
\\x = 5 // 0
;
var result = try parseCheckAndEvalModule(src);
defer cleanupEvalModule(&result);
const summary = try result.evaluator.evalAll();
// Should evaluate 1 declaration with no crashes (it's an error, not a crash)
try testing.expectEqual(@as(u32, 1), summary.evaluated);
try testing.expectEqual(@as(u32, 0), summary.crashed);
// Should have 1 problem reported (division by zero)
try testing.expect(result.problems.len() >= 1);
try testing.expect(errorContains(result.problems, "Division by zero"));
}
test "comptime eval - division by zero in expression" {
const src =
\\a = 10
\\b = 0
\\c = a // b
;
var result = try parseCheckAndEvalModule(src);
defer cleanupEvalModule(&result);
const summary = try result.evaluator.evalAll();
// Should evaluate 3 declarations, c will cause an error
try testing.expectEqual(@as(u32, 3), summary.evaluated);
// Should have 1 problem reported (division by zero)
try testing.expect(result.problems.len() >= 1);
try testing.expect(errorContains(result.problems, "Division by zero"));
}
test "comptime eval - modulo by zero produces error" {
const src =
\\x = 10 % 0
;
var result = try parseCheckAndEvalModule(src);
defer cleanupEvalModule(&result);
const summary = try result.evaluator.evalAll();
// Should evaluate 1 declaration
try testing.expectEqual(@as(u32, 1), summary.evaluated);
// Should have 1 problem reported (division by zero for modulo)
try testing.expect(result.problems.len() >= 1);
try testing.expect(errorContains(result.problems, "Division by zero"));
}
// Note: "division by zero does not halt other defs" test is skipped because
// the interpreter state after an eval error may not allow continuing evaluation
// of subsequent definitions that share the same evaluation context.