File tree Expand file tree Collapse file tree 4 files changed +77
-0
lines changed Expand file tree Collapse file tree 4 files changed +77
-0
lines changed Original file line number Diff line number Diff line change @@ -74,6 +74,8 @@ impl Environment {
74
74
let subtract_fn = rust_core:: SubtractFn { } ;
75
75
let multiply_fn = rust_core:: MultiplyFn { } ;
76
76
let divide_fn = rust_core:: DivideFn { } ;
77
+ let rand_fn = rust_core:: RandFn { } ;
78
+ let rand_int_fn = rust_core:: RandIntFn { } ;
77
79
let str_fn = rust_core:: StrFn { } ;
78
80
let do_fn = rust_core:: DoFn { } ;
79
81
let nth_fn = rust_core:: NthFn { } ;
@@ -105,6 +107,8 @@ impl Environment {
105
107
environment. insert ( Symbol :: intern ( "-" ) , subtract_fn. to_rc_value ( ) ) ;
106
108
environment. insert ( Symbol :: intern ( "*" ) , multiply_fn. to_rc_value ( ) ) ;
107
109
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 ( ) ) ;
108
112
environment. insert ( Symbol :: intern ( "let" ) , let_macro. to_rc_value ( ) ) ;
109
113
environment. insert ( Symbol :: intern ( "str" ) , str_fn. to_rc_value ( ) ) ;
110
114
environment. insert ( Symbol :: intern ( "quote" ) , quote_macro. to_rc_value ( ) ) ;
Original file line number Diff line number Diff line change @@ -22,6 +22,12 @@ pub use self::_divide_::*;
22
22
pub ( crate ) mod _multiply_;
23
23
pub use self :: _multiply_:: * ;
24
24
25
+ pub ( crate ) mod rand;
26
+ pub use self :: rand:: * ;
27
+
28
+ pub ( crate ) mod rand_int;
29
+ pub use self :: rand_int:: * ;
30
+
25
31
// string
26
32
pub ( crate ) mod str;
27
33
pub use self :: str:: * ;
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 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
+ }
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