add an ast smith

This generates zig ASTs from `testing.Smith` and is based off the
langref's PEG.

The choice to not build the Ast while generating and instead parsing it
afterwards makes the smith more versatile by not being tied to a single
implementation at a cost of efficiency.

Additionally, a new function `boolWeighted` was added to `Smith` due to
its frequent use in `AstSmith`.
This commit is contained in:
Kendall Condon
2026-03-22 16:34:04 -04:00
parent ebca8c2dbb
commit 2aee0cd6b9
4 changed files with 2649 additions and 6 deletions
+8 -4
View File
@@ -235,7 +235,8 @@ pub const fuzz = struct {
max: u64,
weight: u64,
fn intFromValue(x: anytype) u64 {
/// `inline` to propogate comptimeness
inline fn intFromValue(x: anytype) u64 {
const T = @TypeOf(x);
return switch (@typeInfo(T)) {
.comptime_int => x,
@@ -269,11 +270,13 @@ pub const fuzz = struct {
};
}
pub fn value(T: type, x: T, weight: u64) Weight {
/// `inline` to propogate comptimeness
pub inline fn value(T: type, x: T, weight: u64) Weight {
return .{ .min = intFromValue(x), .max = intFromValue(x), .weight = weight };
}
pub fn rangeAtMost(T: type, at_least: T, at_most: T, weight: u64) Weight {
/// `inline` to propogate comptimeness
pub inline fn rangeAtMost(T: type, at_least: T, at_most: T, weight: u64) Weight {
std.debug.assert(intFromValue(at_least) <= intFromValue(at_most));
return .{
.min = intFromValue(at_least),
@@ -282,7 +285,8 @@ pub const fuzz = struct {
};
}
pub fn rangeLessThan(T: type, at_least: T, less_than: T, weight: u64) Weight {
/// `inline` to propogate comptimeness
pub inline fn rangeLessThan(T: type, at_least: T, less_than: T, weight: u64) Weight {
std.debug.assert(intFromValue(at_least) < intFromValue(less_than));
return .{
.min = intFromValue(at_least),