mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-04-27 19:09:47 +03:00
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:
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user