louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runÉchoué#94 · main · 15 min 28 s
0Étoiles
0Forks
40Issues
| 1 | /// The generators the fuzzer draws its choices from: a copy of the ones |
| 2 | /// it used from the `gen` package (now in louise-lang/packages), kept |
| 3 | /// byte for byte so that a seed still generates the same program. The |
| 4 | /// fuzzer runs in `cargo test` (crates/louise_codegen_c/tests/fuzz.rs), |
| 5 | /// which resolves packages offline: it depends on no package. |
| 6 | |
| 7 | import std/list |
| 8 | import std/random.{type random} |
| 9 | import std/string |
| 10 | |
| 11 | /// An Int in `low..=high`; panics if `high < low`. |
| 12 | pub fn int :: ~low: Int ~high: Int -> Int / <random> |
| 13 | { |
| 14 | random.range low high |
| 15 | } |
| 16 | |
| 17 | pub fn bool :: -> Bool / <random> |
| 18 | { |
| 19 | random.int 2 == 1 |
| 20 | } |
| 21 | |
| 22 | /// One of `xs`; panics if there is none. |
| 23 | pub fn pick :: xs: List<a> -> a / <random> |
| 24 | { |
| 25 | match list.drop xs (random.int (list.length xs)) |
| 26 | { [x, ..] -> x, [] -> panic "gen.pick: nothing to pick from" } |
| 27 | } |
| 28 | |
| 29 | /// One of `weighted`'s values, each chosen in proportion to its weight; |
| 30 | /// panics unless the weights are positive. To choose between generators, |
| 31 | /// choose a value and `match` on it: a generator stored in a list would |
| 32 | /// not get its caller's `random` (handlers are lexical, ADR 0010). |
| 33 | pub fn weighted :: choices: List<(Int, a)> -> a / <random> |
| 34 | { |
| 35 | let total = list.fold choices 0 fn sum c { sum + c.0 } |
| 36 | if total < 1 |
| 37 | { |
| 38 | panic "gen.weighted: the weights must be positive" |
| 39 | } |
| 40 | weighed choices (random.int total) |
| 41 | } |
| 42 | |
| 43 | fn weighed :: choices: List<(Int, a)> at: Int -> a |
| 44 | { |
| 45 | match choices |
| 46 | { [ (weight, value) |
| 47 | , ..rest |
| 48 | ] -> if at < weight { value } else { weighed rest (at - weight) } |
| 49 | , [] -> panic "gen.weighted: the weights must be positive" |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /// `min` to `max` values of `item`. |
| 54 | fn list :: ~min: Int ~max: Int item: fn() -> a / <random> -> List<a> / <random> |
| 55 | { |
| 56 | let count = random.range min max |
| 57 | list.map (list.range 1 count) fn _ { item () } |
| 58 | } |
| 59 | |
| 60 | /// `min` to `max` characters of `alphabet`. |
| 61 | pub fn string :: ~alphabet: String ~min: Int ~max: Int -> String / <random> |
| 62 | { |
| 63 | let chars = string.chars alphabet |
| 64 | string.concat (list ~min ~max fn { pick chars }) |
| 65 | } |
| 66 | |
| 67 | pub fn lower :: -> String |
| 68 | { |
| 69 | "abcdefghijklmnopqrstuvwxyz" |
| 70 | } |