File tree Expand file tree Collapse file tree 4 files changed +38
-2
lines changed Expand file tree Collapse file tree 4 files changed +38
-2
lines changed Original file line number Diff line number Diff line change @@ -75,6 +75,7 @@ impl Environment {
75
75
let multiply_fn = rust_core:: MultiplyFn { } ;
76
76
let divide_fn = rust_core:: DivideFn { } ;
77
77
let rand_fn = rust_core:: RandFn { } ;
78
+ let rand_int_fn = rust_core:: RandIntFn { } ;
78
79
let str_fn = rust_core:: StrFn { } ;
79
80
let do_fn = rust_core:: DoFn { } ;
80
81
let nth_fn = rust_core:: NthFn { } ;
@@ -107,6 +108,7 @@ impl Environment {
107
108
environment. insert ( Symbol :: intern ( "*" ) , multiply_fn. to_rc_value ( ) ) ;
108
109
environment. insert ( Symbol :: intern ( "_slash_" ) , divide_fn. to_rc_value ( ) ) ;
109
110
environment. insert ( Symbol :: intern ( "rand" ) , rand_fn. to_rc_value ( ) ) ;
111
+ environment. insert ( Symbol :: intern ( "rand-int" ) , rand_int_fn. to_rc_value ( ) ) ;
110
112
environment. insert ( Symbol :: intern ( "let" ) , let_macro. to_rc_value ( ) ) ;
111
113
environment. insert ( Symbol :: intern ( "str" ) , str_fn. to_rc_value ( ) ) ;
112
114
environment. insert ( Symbol :: intern ( "quote" ) , quote_macro. to_rc_value ( ) ) ;
Original file line number Diff line number Diff line change @@ -25,6 +25,8 @@ pub use self::_multiply_::*;
25
25
pub ( crate ) mod rand;
26
26
pub use self :: rand:: * ;
27
27
28
+ pub ( crate ) mod rand_int;
29
+ pub use self :: rand_int:: * ;
28
30
29
31
// string
30
32
pub ( crate ) mod str;
Original file line number Diff line number Diff line change 1
1
use crate :: ifn:: IFn ;
2
2
use crate :: value:: { Value , ToValue } ;
3
3
use std:: rc:: Rc ;
4
- use rand:: { random , thread_rng, Rng } ;
4
+ use rand:: { thread_rng, Rng } ;
5
5
use crate :: error_message;
6
- use std:: any:: Any ;
7
6
8
7
/// (rand) or (rand n)
9
8
///
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments