/// The generators the fuzzer draws its choices from: a copy of the ones /// it used from the `gen` package (now in louise-lang/packages), kept /// byte for byte so that a seed still generates the same program. The /// fuzzer runs in `cargo test` (crates/louise_codegen_c/tests/fuzz.rs), /// which resolves packages offline: it depends on no package. import std/list import std/random.{type random} import std/string /// An Int in `low..=high`; panics if `high < low`. pub fn int :: ~low: Int ~high: Int -> Int / { random.range low high } pub fn bool :: -> Bool / { random.int 2 == 1 } /// One of `xs`; panics if there is none. pub fn pick :: xs: List -> a / { match list.drop xs (random.int (list.length xs)) { [x, ..] -> x, [] -> panic "gen.pick: nothing to pick from" } } /// One of `weighted`'s values, each chosen in proportion to its weight; /// panics unless the weights are positive. To choose between generators, /// choose a value and `match` on it: a generator stored in a list would /// not get its caller's `random` (handlers are lexical, ADR 0010). pub fn weighted :: choices: List<(Int, a)> -> a / { let total = list.fold choices 0 fn sum c { sum + c.0 } if total < 1 { panic "gen.weighted: the weights must be positive" } weighed choices (random.int total) } fn weighed :: choices: List<(Int, a)> at: Int -> a { match choices { [ (weight, value) , ..rest ] -> if at < weight { value } else { weighed rest (at - weight) } , [] -> panic "gen.weighted: the weights must be positive" } } /// `min` to `max` values of `item`. fn list :: ~min: Int ~max: Int item: fn() -> a / -> List / { let count = random.range min max list.map (list.range 1 count) fn _ { item () } } /// `min` to `max` characters of `alphabet`. pub fn string :: ~alphabet: String ~min: Int ~max: Int -> String / { let chars = string.chars alphabet string.concat (list ~min ~max fn { pick chars }) } pub fn lower :: -> String { "abcdefghijklmnopqrstuvwxyz" }