Skip to content

Commit 4c4ed5f

Browse files
authored
Merge pull request #38 from erkkikeranen/feature/rand
rand and rand-int
2 parents b129bc6 + 4147290 commit 4c4ed5f

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

src/environment.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ impl Environment {
7474
let subtract_fn = rust_core::SubtractFn {};
7575
let multiply_fn = rust_core::MultiplyFn {};
7676
let divide_fn = rust_core::DivideFn {};
77+
let rand_fn = rust_core::RandFn {};
78+
let rand_int_fn = rust_core::RandIntFn {};
7779
let str_fn = rust_core::StrFn {};
7880
let do_fn = rust_core::DoFn {};
7981
let nth_fn = rust_core::NthFn {};
@@ -105,6 +107,8 @@ impl Environment {
105107
environment.insert(Symbol::intern("-"), subtract_fn.to_rc_value());
106108
environment.insert(Symbol::intern("*"), multiply_fn.to_rc_value());
107109
environment.insert(Symbol::intern("_slash_"), divide_fn.to_rc_value());
110+
environment.insert(Symbol::intern("rand"), rand_fn.to_rc_value());
111+
environment.insert(Symbol::intern("rand-int"), rand_int_fn.to_rc_value());
108112
environment.insert(Symbol::intern("let"), let_macro.to_rc_value());
109113
environment.insert(Symbol::intern("str"), str_fn.to_rc_value());
110114
environment.insert(Symbol::intern("quote"), quote_macro.to_rc_value());

src/rust_core.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ pub use self::_divide_::*;
2222
pub(crate) mod _multiply_;
2323
pub use self::_multiply_::*;
2424

25+
pub(crate) mod rand;
26+
pub use self::rand::*;
27+
28+
pub(crate) mod rand_int;
29+
pub use self::rand_int::*;
30+
2531
// string
2632
pub(crate) mod str;
2733
pub use self::str::*;

src/rust_core/rand.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 RandFn {}
11+
impl ToValue for RandFn {
12+
fn to_value(&self) -> Value {
13+
Value::IFn(Rc::new(self.clone()))
14+
}
15+
}
16+
impl IFn for RandFn {
17+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
18+
match args.len() {
19+
0 => Value::F64(thread_rng().gen()),
20+
1 => {
21+
let arg = args.get(0).unwrap().to_value();
22+
match arg {
23+
Value::I32(i_) => Value::F64(thread_rng().gen_range(0.0, i_ as f64)),
24+
Value::F64(f_) => Value::F64(thread_rng().gen_range(0.0, f_)),
25+
_ => Value::Condition(format!( // TODO: what error message should be returned regarding using typetags?
26+
"Type mismatch; Expecting: (i32 | i64 | f32 | f64), Found: {}",
27+
arg.type_tag()
28+
))
29+
}
30+
},
31+
_ => error_message::wrong_varg_count(&[0, 1], args.len())
32+
}
33+
}
34+
}

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)