@@ -4,10 +4,10 @@ mod options;
44mod parser;
55mod roll;
66
7+ use crate :: interpreter:: Ast ;
78pub use crate :: parser:: * ;
89pub use crate :: roll:: * ;
910pub use rand_core;
10- use crate :: interpreter:: Ast ;
1111use std:: collections:: HashMap ;
1212
1313pub fn inplace_interp ( s : & str , advanced : bool ) -> String {
@@ -20,7 +20,6 @@ pub fn inplace_interp(s: &str, advanced: bool) -> String {
2020 Ok ( i) => i,
2121 Err ( e) => {
2222 panic ! ( "{}" , e) ;
23-
2423 }
2524 } ;
2625
@@ -34,35 +33,54 @@ pub fn inplace_interp(s: &str, advanced: bool) -> String {
3433 map. insert ( pos, roll) ;
3534 }
3635
37- let res = replace_rolls ( copy, & map, |roll| {
38- format ! ( "{:?}" , roll. vals)
39- } ) ;
36+ let res = replace_rolls ( copy, & map, |roll| format ! ( "{:?}" , roll. vals) ) ;
4037 format ! ( "{} = {} = {}" , s, res, total)
4138}
4239
43- fn replace_rolls ( ast : Ast , lookup : & HashMap < u64 , Roll > , func : fn ( & Roll ) -> String ) -> Ast {
40+ fn replace_rolls ( ast : Ast , lookup : & HashMap < u64 , Roll > , func : fn ( & Roll ) -> String ) -> Ast {
4441 return match ast {
45- Ast :: Add ( l, r) => Ast :: Add ( Box :: from ( replace_rolls ( * l, lookup, func) ) , Box :: from ( replace_rolls ( * r, lookup, func) ) ) ,
46- Ast :: Sub ( l, r) => Ast :: Sub ( Box :: from ( replace_rolls ( * l, lookup, func) ) , Box :: from ( replace_rolls ( * r, lookup, func) ) ) ,
47- Ast :: Mul ( l, r) => Ast :: Mul ( Box :: from ( replace_rolls ( * l, lookup, func) ) , Box :: from ( replace_rolls ( * r, lookup, func) ) ) ,
48- Ast :: Div ( l, r) => Ast :: Div ( Box :: from ( replace_rolls ( * l, lookup, func) ) , Box :: from ( replace_rolls ( * r, lookup, func) ) ) ,
49- Ast :: Mod ( l, r) => Ast :: Mod ( Box :: from ( replace_rolls ( * l, lookup, func) ) , Box :: from ( replace_rolls ( * r, lookup, func) ) ) ,
50- Ast :: IDiv ( l, r) => Ast :: IDiv ( Box :: from ( replace_rolls ( * l, lookup, func) ) , Box :: from ( replace_rolls ( * r, lookup, func) ) ) ,
51- Ast :: Power ( l, r) => Ast :: Power ( Box :: from ( replace_rolls ( * l, lookup, func) ) , Box :: from ( replace_rolls ( * r, lookup, func) ) ) ,
42+ Ast :: Add ( l, r) => Ast :: Add (
43+ Box :: from ( replace_rolls ( * l, lookup, func) ) ,
44+ Box :: from ( replace_rolls ( * r, lookup, func) ) ,
45+ ) ,
46+ Ast :: Sub ( l, r) => Ast :: Sub (
47+ Box :: from ( replace_rolls ( * l, lookup, func) ) ,
48+ Box :: from ( replace_rolls ( * r, lookup, func) ) ,
49+ ) ,
50+ Ast :: Mul ( l, r) => Ast :: Mul (
51+ Box :: from ( replace_rolls ( * l, lookup, func) ) ,
52+ Box :: from ( replace_rolls ( * r, lookup, func) ) ,
53+ ) ,
54+ Ast :: Div ( l, r) => Ast :: Div (
55+ Box :: from ( replace_rolls ( * l, lookup, func) ) ,
56+ Box :: from ( replace_rolls ( * r, lookup, func) ) ,
57+ ) ,
58+ Ast :: Mod ( l, r) => Ast :: Mod (
59+ Box :: from ( replace_rolls ( * l, lookup, func) ) ,
60+ Box :: from ( replace_rolls ( * r, lookup, func) ) ,
61+ ) ,
62+ Ast :: IDiv ( l, r) => Ast :: IDiv (
63+ Box :: from ( replace_rolls ( * l, lookup, func) ) ,
64+ Box :: from ( replace_rolls ( * r, lookup, func) ) ,
65+ ) ,
66+ Ast :: Power ( l, r) => Ast :: Power (
67+ Box :: from ( replace_rolls ( * l, lookup, func) ) ,
68+ Box :: from ( replace_rolls ( * r, lookup, func) ) ,
69+ ) ,
5270 Ast :: Minus ( l) => Ast :: Minus ( Box :: from ( replace_rolls ( * l, lookup, func) ) ) ,
5371 Ast :: Dice ( _, _, _, pos) => {
5472 let roll = lookup. get ( & pos) . unwrap ( ) ;
5573 Ast :: Const ( func ( roll) )
56- } ,
57- x@ Ast :: Const ( _) => x
58- }
74+ }
75+ x @ Ast :: Const ( _) => x,
76+ } ;
5977}
6078
6179#[ cfg( test) ]
6280mod test {
81+ use super :: * ;
6382 use crate :: parser:: Parser ;
6483 use bnf:: Grammar ;
65- use super :: * ;
6684
6785 const GRAMMAR : & str = include_str ! ( "../../grammar.bnf" ) ;
6886
@@ -92,6 +110,6 @@ mod test {
92110
93111 #[ test]
94112 fn test_inplace ( ) {
95- println ! ( "{}" , inplace_interp( "4d8 + 2d8" ) ) ;
113+ println ! ( "{}" , inplace_interp( "4d8 + 2d8" , false ) ) ;
96114 }
97115}
0 commit comments