builtins/
rand.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use rand::distributions::{Alphanumeric, Distribution};
use rand::Rng;
use std::iter;
use unicode_segmentation::UnicodeSegmentation;

use bridge_adapters::add_builtin;
use compile_state::state::SloshVm;
use rand::rngs::ThreadRng;
use slvm::{from_i56, VMError, VMResult, Value};
use std::borrow::Cow;

fn builtin_random(_vm: &mut SloshVm, registers: &[Value]) -> VMResult<Value> {
    let mut rng = rand::thread_rng();
    let mut args = registers.iter();
    if let (Some(next_arg), None) = (args.next(), args.next()) {
        match next_arg {
            Value::Int(i) => {
                let i: i64 = from_i56(i);
                match i {
                    positive if positive > 0 => Ok(rng.gen_range(0..i).into()),
                    _ => Err(VMError::new("rand", "Expected positive number")),
                }
            }
            Value::Float(f) => {
                let f: f64 = (*f).into();
                match f {
                    positive if positive > 0.0 => Ok(rng.gen_range(0.0..f).into()),
                    _ => Err(VMError::new("rand", "Expected positive number")),
                }
            }
            _ => Err(VMError::new(
                "rand",
                "Expected positive number, float or int",
            )),
        }
    } else {
        Err(VMError::new(
            "rand",
            "Expected positive number, float or int",
        ))
    }
}

fn builtin_get_random_str(vm: &mut SloshVm, registers: &[Value]) -> VMResult<Value> {
    let mut args = registers.iter();
    if let (Some(Value::Int(i)), Some(arg2)) = (args.next(), args.next()) {
        let i: i64 = from_i56(i);
        match i {
            positive if positive > 0 => get_random_str(vm, *arg2, positive as u64),
            _ => Err(VMError::new("rand", "Expected positive length")),
        }
    } else {
        Err(VMError::new(
            "rand",
            "Expected two arguments, length and charset",
        ))
    }
}

pub fn rand_alphanumeric_str(len: u64, rng: &mut ThreadRng) -> Cow<'static, str> {
    iter::repeat(())
        .map(|()| rng.sample(Alphanumeric))
        .map(char::from)
        .take(len as usize)
        .collect()
}

fn get_random_str(vm: &mut SloshVm, arg: Value, len: u64) -> VMResult<Value> {
    let mut rng = rand::thread_rng();
    match arg {
        Value::Keyword(i) => {
            let sym = vm.get_interned(i);
            match sym {
                "ascii" => Ok(vm.alloc_string(
                    iter::repeat(())
                        .map(|()| rng.sample(Ascii))
                        .map(char::from)
                        .take(len as usize)
                        .collect(),
                )),
                "alnum" => Ok(vm.alloc_string(rand_alphanumeric_str(len, &mut rng).to_string())),
                "hex" => Ok(vm.alloc_string(
                    iter::repeat(())
                        .map(|()| rng.sample(Hex))
                        .map(char::from)
                        .take(len as usize)
                        .collect(),
                )),
                _ => Err(VMError::new("rand", format!("Unknown symbol :{}", sym))),
            }
        }
        Value::String(h) => {
            let string = vm.get_string(h);
            let upg = UserProvidedGraphemes::new(string);
            Ok(vm.alloc_string(
                iter::repeat(())
                    .map(|()| rng.sample(&upg))
                    .take(len as usize)
                    .collect(),
            ))
        }
        Value::StringConst(i) => {
            let string = vm.get_interned(i);
            let upg = UserProvidedGraphemes::new(string);
            Ok(vm.alloc_string(
                iter::repeat(())
                    .map(|()| rng.sample(&upg))
                    .take(len as usize)
                    .collect(),
            ))
        }
        _ => Err(VMError::new(
            "rand",
            "Second argument must be keyword or string",
        )),
    }
}

#[derive(Debug)]
struct Ascii;

impl Distribution<u8> for Ascii {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u8 {
        const RANGE: u32 = 26 + 26 + 10 + 32;
        const ASCII_PRINTABLE_CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
                abcdefghijklmnopqrstuvwxyz\
                0123456789\
                !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
        // We can pick from 94 characters. This is so close to a power of 2, 128,
        // that we can do better than `Uniform`. Use a simple bitshift and
        // rejection sampling. We do not use a bitmask, because for small RNGs
        // the most significant bits are usually of higher quality.
        loop {
            let var = rng.next_u32() >> (32 - 7);
            if var < RANGE {
                return ASCII_PRINTABLE_CHARSET[var as usize];
            }
        }
    }
}

#[derive(Debug)]
struct Hex;

impl Distribution<u8> for Hex {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u8 {
        const RANGE: u32 = 16;
        const HEX_CHARSET: &[u8] = b"abcdef0123456789";
        // We can pick from 16 characters. This is a power of 2. Use a
        // simple bitshift and rejection sampling. We do not use a bitmask,
        // because for small RNGs/ the most significant bits are usually
        // of higher quality.
        loop {
            let var = rng.next_u32() >> (32 - 4);
            if var < RANGE {
                return HEX_CHARSET[var as usize];
            }
        }
    }
}

#[derive(Debug)]
struct UserProvidedGraphemes {
    sample_space: Vec<String>,
    len: usize,
}

impl UserProvidedGraphemes {
    pub fn new(s: &str) -> UserProvidedGraphemes {
        let mut sample_space: Vec<String> = Vec::new();
        for cluster in UnicodeSegmentation::graphemes(s, true) {
            sample_space.push(cluster.to_string());
        }
        let len = sample_space.len();
        UserProvidedGraphemes { sample_space, len }
    }
}

impl Distribution<String> for UserProvidedGraphemes {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> String {
        self.sample_space
            .get(rng.gen_range(0..self.len))
            .unwrap()
            .to_owned()
    }
}

fn builtin_probool(_vm: &mut SloshVm, registers: &[Value]) -> VMResult<Value> {
    let tup: Option<(u32, u32)> = match (registers.first(), registers.get(1), registers.get(2)) {
        (None, None, None) => Some((1, 2)),
        (Some(Value::Int(first)), Some(Value::Int(second)), None) => {
            let i: i64 = from_i56(first);
            let j: i64 = from_i56(second);
            if i >= 0 && i < u32::MAX as i64 && j >= 0 && j < u32::MAX as i64 {
                Some((i as u32, j as u32))
            } else {
                None
            }
        }
        _ => None,
    };
    match tup {
        None => Err(VMError::new("rand", "Expected zero or two positive ints")),
        Some((_, 0)) => Err(VMError::new("rand", "Denominator can not be zero")),
        Some((i, j)) => match i / j {
            improper if improper > 1 => Ok(Value::True),
            _ => match rand::thread_rng().gen_ratio(i, j) {
                true => Ok(Value::True),
                false => Ok(Value::False),
            },
        },
    }
}

pub fn add_rand_builtins(env: &mut SloshVm) {
    add_builtin(
        env,
        "probool",
        builtin_probool,
        "Usage: (probool), (probool numerator denominator)

PRObability of a BOOLean.

If no arguments are given, returns #t 1/2 of the time, otherwise takes two
integers, numerator and denominator, and returns #t numerator/denominator of the
time. Throws an error if denominator is 0. If (>= (/ numerator denominator) 1)
probool always returns true. If numerator is 0 probool always returns false.

Section: random

Example:
(def val0 (probool))
(test::assert-true (or (= #t val0) (= nil val0)))
(def val1 (probool 17 42))
(test::assert-true (or (= #t val1) (= nil val1)))
(test::assert-true (probool 1 1))
(test::assert-false (probool 0 42))
(test::assert-error-msg (probool 0 0) :rand \"Denominator can not be zero\")
(test::assert-error-msg (probool 0 0 0) :rand \"Expected zero or two positive ints\")
",
    );

    add_builtin(
        env,
        "random-str",
        builtin_get_random_str,
        "Usage: (random-str str-length [char-set])

Takes a positive integer, str-length, and one of :hex, :ascii, :alnum, or
a string. Returns random string of provided str-length composed of second argument,
:hex results in random hex string, :ascii results in random string of all printable
ascii characters, :alnum results in random string of all alphanumeric characters,
and providing a string results in a random string composed by sampling input.

Section: random

Example:
(test::assert-error-msg (random-str) :rand \"Expected two arguments, length and charset\")
(test::assert-error-msg (random-str 10) :rand \"Expected two arguments, length and charset\")
(test::assert-error-msg (random-str -1 :hex) :rand \"Expected positive length\")
(test::assert-error-msg (random-str 10 1) :rand \"Second argument must be keyword or string\")
(test::assert-error-msg (random-str 1 :hexy) :rand \"Unknown symbol :hexy\")
(test::assert-equal 10 (len (random-str 10 :hex)))
(test::assert-true (str-contains (random-str 42 \"\u{2699}\") \"\u{2699}\"))
(test::assert-equal 19 (len (random-str 19 :ascii)))
(test::assert-equal 91 (len (random-str 91 :alnum)))
",
    );

    add_builtin(
        env,
        "random",
        builtin_random,
        "Usage: (random), (random limit)

Returns non-negative number less than limit and of the same type as limit.

Section: random

Example:
(def rand-int (random 100))
(test::assert-true (and (>= rand-int 0) (< rand-int 100)))
(def rand-float (random 1.0))
(test::assert-true (and (>= rand-float 0.0) (< rand-float 1.0)))
(test::assert-error-msg (random -1) :rand \"Expected positive number\")
(test::assert-error-msg (random 1 2) :rand \"Expected positive number, float or int\")
",
    );
}