Skip to content

Commit 4147290

Browse files
committed
rand-int functionality
1 parent 2c9acbf commit 4147290

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

src/environment.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ impl Environment {
7575
let multiply_fn = rust_core::MultiplyFn {};
7676
let divide_fn = rust_core::DivideFn {};
7777
let rand_fn = rust_core::RandFn {};
78+
let rand_int_fn = rust_core::RandIntFn {};
7879
let str_fn = rust_core::StrFn {};
7980
let do_fn = rust_core::DoFn {};
8081
let nth_fn = rust_core::NthFn {};
@@ -107,6 +108,7 @@ impl Environment {
107108
environment.insert(Symbol::intern("*"), multiply_fn.to_rc_value());
108109
environment.insert(Symbol::intern("_slash_"), divide_fn.to_rc_value());
109110
environment.insert(Symbol::intern("rand"), rand_fn.to_rc_value());
111+
environment.insert(Symbol::intern("rand-int"), rand_int_fn.to_rc_value());
110112
environment.insert(Symbol::intern("let"), let_macro.to_rc_value());
111113
environment.insert(Symbol::intern("str"), str_fn.to_rc_value());
112114
environment.insert(Symbol::intern("quote"), quote_macro.to_rc_value());

src/rust_core.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ pub use self::_multiply_::*;
2525
pub(crate) mod rand;
2626
pub use self::rand::*;
2727

28+
pub(crate) mod rand_int;
29+
pub use self::rand_int::*;
2830

2931
// string
3032
pub(crate) mod str;

src/rust_core/rand.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use crate::ifn::IFn;
22
use crate::value::{Value, ToValue};
33
use std::rc::Rc;
4-
use rand::{random, thread_rng, Rng};
4+
use rand::{thread_rng, Rng};
55
use crate::error_message;
6-
use std::any::Any;
76

87
/// (rand) or (rand n)
98
///

src/rust_core/rand_int.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use crate::ifn::IFn;
2+
use crate::value::{Value, ToValue};
3+
use std::rc::Rc;
4+
use rand::{thread_rng, Rng};
5+
use crate::error_message;
6+
7+
/// (rand) or (rand n)
8+
///
9+
#[derive(Debug, Clone)]
10+
pub struct RandIntFn {}
11+
impl ToValue for RandIntFn {
12+
fn to_value(&self) -> Value {
13+
Value::IFn(Rc::new(self.clone()))
14+
}
15+
}
16+
impl IFn for RandIntFn {
17+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
18+
match args.len() {
19+
1 => {
20+
let arg = args.get(0).unwrap().to_value();
21+
match arg {
22+
Value::I32(i_) => Value::I32(thread_rng().gen_range(0, i_)),
23+
Value::F64(f_) => Value::I32(thread_rng().gen_range(0, f_ as i32)),
24+
_ => Value::Condition(format!( // TODO: what error message should be returned regarding using typetags?
25+
"Type mismatch; Expecting: (i32 | i64 | f32 | f64), Found: {}",
26+
arg.type_tag()
27+
))
28+
}
29+
},
30+
_ => error_message::wrong_arg_count(1, args.len())
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)