File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ( ) ) ;
Original file line number Diff line number Diff line change @@ -25,6 +25,8 @@ pub use self::_multiply_::*;
2525pub ( crate ) mod rand;
2626pub use self :: rand:: * ;
2727
28+ pub ( crate ) mod rand_int;
29+ pub use self :: rand_int:: * ;
2830
2931// string
3032pub ( crate ) mod str;
Original file line number Diff line number Diff line change 11use crate :: ifn:: IFn ;
22use crate :: value:: { Value , ToValue } ;
33use std:: rc:: Rc ;
4- use rand:: { random , thread_rng, Rng } ;
4+ use rand:: { thread_rng, Rng } ;
55use crate :: error_message;
6- use std:: any:: Any ;
76
87/// (rand) or (rand n)
98///
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