@@ -19,17 +19,25 @@ impl Source {
1919 Expr :: String ( string) => self . eval_string_literal ( string) ,
2020 Expr :: Array ( items) => Box :: pin ( self . eval_array_literal ( items, context) ) . await ,
2121 Expr :: Map ( pairs) => Box :: pin ( self . eval_map_literal ( pairs, context) ) . await ,
22- Expr :: Index ( value, index) => Box :: pin ( self . eval_index_expr ( value, index, context) ) . await ,
22+ Expr :: Index ( value, index) => {
23+ Box :: pin ( self . eval_index_expr ( value, index, context) ) . await
24+ }
2325 Expr :: Field ( map, field) => Box :: pin ( self . eval_field_expr ( map, field, context) ) . await ,
2426 Expr :: Ident ( ident) => self . eval_ident_expr ( ident, context) ,
2527 Expr :: Let ( name, expr) => Box :: pin ( self . eval_let_expr ( name, expr, context) ) . await ,
26- Expr :: Unary ( token, right) => Box :: pin ( self . eval_unary_expr ( token, right, context) ) . await ,
27- Expr :: Binary ( token, left, right) => Box :: pin ( self . eval_binary_expr ( token, left, right, context) ) . await ,
28+ Expr :: Unary ( token, right) => {
29+ Box :: pin ( self . eval_unary_expr ( token, right, context) ) . await
30+ }
31+ Expr :: Binary ( token, left, right) => {
32+ Box :: pin ( self . eval_binary_expr ( token, left, right, context) ) . await
33+ }
2834 Expr :: Paren ( expr) => Box :: pin ( self . eval_expr ( expr, context) ) . await ,
2935 Expr :: If ( condition, consequence, alternative) => {
3036 Box :: pin ( self . eval_if_expr ( condition, consequence, alternative, context) ) . await
3137 }
32- Expr :: Call ( name, arguments) => Box :: pin ( self . eval_call_expr ( name, arguments, context) ) . await ,
38+ Expr :: Call ( name, arguments) => {
39+ Box :: pin ( self . eval_call_expr ( name, arguments, context) ) . await
40+ }
3341 Expr :: Send ( name) => Box :: pin ( self . eval_send_expr ( name, context) ) . await ,
3442 }
3543 }
@@ -50,11 +58,19 @@ impl Source {
5058 Ok ( Value :: String ( string. to_owned ( ) ) )
5159 }
5260
53- async fn eval_array_literal ( & self , items : & [ Expr ] , context : & mut Context ) -> Result < Value , String > {
61+ async fn eval_array_literal (
62+ & self ,
63+ items : & [ Expr ] ,
64+ context : & mut Context ,
65+ ) -> Result < Value , String > {
5466 Ok ( Value :: Array ( self . eval_list ( items, context) . await ?) )
5567 }
5668
57- async fn eval_map_literal ( & self , pairs : & Vec < ( Expr , Expr ) > , context : & mut Context ) -> Result < Value , String > {
69+ async fn eval_map_literal (
70+ & self ,
71+ pairs : & Vec < ( Expr , Expr ) > ,
72+ context : & mut Context ,
73+ ) -> Result < Value , String > {
5874 let mut map = HashMap :: new ( ) ;
5975 for ( key, value) in pairs {
6076 let key = self . eval_expr ( key, context) . await ?;
@@ -64,7 +80,12 @@ impl Source {
6480 Ok ( Value :: Map ( map) )
6581 }
6682
67- async fn eval_index_expr ( & self , value : & Expr , index : & Expr , context : & mut Context ) -> Result < Value , String > {
83+ async fn eval_index_expr (
84+ & self ,
85+ value : & Expr ,
86+ index : & Expr ,
87+ context : & mut Context ,
88+ ) -> Result < Value , String > {
6889 // TODO enhance indent expr get variable use reference
6990 let value = self . eval_expr ( value, context) . await ?;
7091 let index = self . eval_expr ( index, context) . await ?;
@@ -89,7 +110,12 @@ impl Source {
89110 }
90111 }
91112
92- async fn eval_field_expr ( & self , map : & Expr , field : & String , context : & mut Context ) -> Result < Value , String > {
113+ async fn eval_field_expr (
114+ & self ,
115+ map : & Expr ,
116+ field : & String ,
117+ context : & mut Context ,
118+ ) -> Result < Value , String > {
93119 // TODO enhance indent expr get variable use reference
94120 match self . eval_expr ( map, context) . await ? {
95121 Value :: Map ( mut pairs) => {
@@ -110,16 +136,28 @@ impl Source {
110136 }
111137 }
112138
113- async fn eval_let_expr ( & self , name : & String , expr : & Expr , context : & mut Context ) -> Result < Value , String > {
139+ async fn eval_let_expr (
140+ & self ,
141+ name : & String ,
142+ expr : & Expr ,
143+ context : & mut Context ,
144+ ) -> Result < Value , String > {
114145 let value = self . eval_expr ( expr, context) . await ?;
115146 context. set ( name. to_owned ( ) , value. to_owned ( ) ) ;
116147 Ok ( value)
117148 }
118149
119- async fn eval_unary_expr ( & self , token : & Token , right : & Expr , context : & mut Context ) -> Result < Value , String > {
150+ async fn eval_unary_expr (
151+ & self ,
152+ token : & Token ,
153+ right : & Expr ,
154+ context : & mut Context ,
155+ ) -> Result < Value , String > {
120156 let right = self . eval_expr ( right, context) . await ?;
121157 match ( token. kind , right) {
122- ( Kind :: Not , Value :: Boolean ( false ) ) | ( Kind :: Not , Value :: Null ) => Ok ( Value :: Boolean ( true ) ) ,
158+ ( Kind :: Not , Value :: Boolean ( false ) ) | ( Kind :: Not , Value :: Null ) => {
159+ Ok ( Value :: Boolean ( true ) )
160+ }
123161 ( Kind :: Not , Value :: Integer ( integer) ) => Ok ( Value :: Integer ( !integer) ) ,
124162 ( Kind :: Not , _) => Ok ( Value :: Boolean ( false ) ) ,
125163 ( Kind :: Sub , Value :: Integer ( integer) ) => Ok ( Value :: Integer ( -integer) ) ,
@@ -128,18 +166,44 @@ impl Source {
128166 }
129167 }
130168
131- async fn eval_binary_expr ( & self , token : & Token , left : & Expr , right : & Expr , context : & mut Context ) -> Result < Value , String > {
169+ async fn eval_binary_expr (
170+ & self ,
171+ token : & Token ,
172+ left : & Expr ,
173+ right : & Expr ,
174+ context : & mut Context ,
175+ ) -> Result < Value , String > {
132176 match token. kind {
133- Kind :: Add => self . eval_expr ( left, context) . await ? + self . eval_expr ( right, context) . await ?,
134- Kind :: Sub => self . eval_expr ( left, context) . await ? - self . eval_expr ( right, context) . await ?,
135- Kind :: Mul => self . eval_expr ( left, context) . await ? * self . eval_expr ( right, context) . await ?,
136- Kind :: Div => self . eval_expr ( left, context) . await ? / self . eval_expr ( right, context) . await ?,
137- Kind :: Rem => self . eval_expr ( left, context) . await ? % self . eval_expr ( right, context) . await ?,
138- Kind :: Bx => self . eval_expr ( left, context) . await ? ^ self . eval_expr ( right, context) . await ?,
139- Kind :: Bo => self . eval_expr ( left, context) . await ? | self . eval_expr ( right, context) . await ?,
140- Kind :: Ba => self . eval_expr ( left, context) . await ? & self . eval_expr ( right, context) . await ?,
141- Kind :: Sl => self . eval_expr ( left, context) . await ? << self . eval_expr ( right, context) . await ?,
142- Kind :: Sr => self . eval_expr ( left, context) . await ? >> self . eval_expr ( right, context) . await ?,
177+ Kind :: Add => {
178+ self . eval_expr ( left, context) . await ? + self . eval_expr ( right, context) . await ?
179+ }
180+ Kind :: Sub => {
181+ self . eval_expr ( left, context) . await ? - self . eval_expr ( right, context) . await ?
182+ }
183+ Kind :: Mul => {
184+ self . eval_expr ( left, context) . await ? * self . eval_expr ( right, context) . await ?
185+ }
186+ Kind :: Div => {
187+ self . eval_expr ( left, context) . await ? / self . eval_expr ( right, context) . await ?
188+ }
189+ Kind :: Rem => {
190+ self . eval_expr ( left, context) . await ? % self . eval_expr ( right, context) . await ?
191+ }
192+ Kind :: Bx => {
193+ self . eval_expr ( left, context) . await ? ^ self . eval_expr ( right, context) . await ?
194+ }
195+ Kind :: Bo => {
196+ self . eval_expr ( left, context) . await ? | self . eval_expr ( right, context) . await ?
197+ }
198+ Kind :: Ba => {
199+ self . eval_expr ( left, context) . await ? & self . eval_expr ( right, context) . await ?
200+ }
201+ Kind :: Sl => {
202+ self . eval_expr ( left, context) . await ? << self . eval_expr ( right, context) . await ?
203+ }
204+ Kind :: Sr => {
205+ self . eval_expr ( left, context) . await ? >> self . eval_expr ( right, context) . await ?
206+ }
143207 Kind :: Lo => match self . eval_expr ( left, context) . await ? {
144208 Value :: Boolean ( false ) | Value :: Null => self . eval_expr ( right, context) . await ,
145209 left => Ok ( left) ,
@@ -184,7 +248,12 @@ impl Source {
184248 }
185249 }
186250
187- async fn eval_call_expr ( & self , name : & str , arguments : & [ Expr ] , context : & mut Context ) -> Result < Value , String > {
251+ async fn eval_call_expr (
252+ & self ,
253+ name : & str ,
254+ arguments : & [ Expr ] ,
255+ context : & mut Context ,
256+ ) -> Result < Value , String > {
188257 let arguments = self . eval_list ( arguments, context) . await ?;
189258 match self . function ( name) {
190259 Some ( ( params, body) ) => {
@@ -223,8 +292,14 @@ impl Source {
223292 for assert in exprs {
224293 if let Expr :: Binary ( token, left, right) = assert {
225294 let expr = format ! ( "{left} {token} {right}" ) ;
226- let left = self . eval_expr ( left, & mut local) . await . unwrap_or ( Value :: Null ) ;
227- let right = self . eval_expr ( right, & mut local) . await . unwrap_or ( Value :: Null ) ;
295+ let left = self
296+ . eval_expr ( left, & mut local)
297+ . await
298+ . unwrap_or ( Value :: Null ) ;
299+ let right = self
300+ . eval_expr ( right, & mut local)
301+ . await
302+ . unwrap_or ( Value :: Null ) ;
228303 if let Some ( result) = match token. kind {
229304 Kind :: Lt => Some ( left < right) ,
230305 Kind :: Gt => Some ( left > right) ,
@@ -345,14 +420,20 @@ mod tests {
345420 ( "5.0 / 2.0 * 2.0 + 1.0 - 0.5" , Value :: Float ( 5.5 ) ) ,
346421 ( "5.0 * (0.2 + 1.0)" , Value :: Float ( 6.0 ) ) ,
347422 ( "0.5 + 0.5 + 0.5 + 0.5 - 1.0" , Value :: Float ( 1.0 ) ) ,
348- ( "0.2 * 0.2 * 0.2 * 0.2 * 0.2" , Value :: Float ( 0.00032000000000000013 ) ) ,
423+ (
424+ "0.2 * 0.2 * 0.2 * 0.2 * 0.2" ,
425+ Value :: Float ( 0.00032000000000000013 ) ,
426+ ) ,
349427 ( "0.5 * 2.2 + 1.1" , Value :: Float ( 2.2 ) ) ,
350428 ( "0.5 + 0.2 * 10.0" , Value :: Float ( 2.5 ) ) ,
351429 ( "0.5 * (2.0 + 10.0)" , Value :: Float ( 6.0 ) ) ,
352430 ( "-0.5" , Value :: Float ( -0.5 ) ) ,
353431 ( "-1.0" , Value :: Float ( -1.0 ) ) ,
354432 ( "-5.0 + 10.0 + -5.0" , Value :: Float ( 0.0 ) ) ,
355- ( "(0.5 + 1.5 * 0.2 + 1.5 / 3.0) * 2.0 + -1.0" , Value :: Float ( 1.6 ) ) ,
433+ (
434+ "(0.5 + 1.5 * 0.2 + 1.5 / 3.0) * 2.0 + -1.0" ,
435+ Value :: Float ( 1.6 ) ,
436+ ) ,
356437 ] ;
357438 run_eval_tests ( tests) . await ;
358439 }
@@ -395,9 +476,18 @@ mod tests {
395476 #[ tokio:: test]
396477 async fn test_string_literal ( ) {
397478 let tests = vec ! [
398- ( r#""hello world""# , Value :: String ( String :: from( "hello world" ) ) ) ,
399- ( r#""hello" + " world""# , Value :: String ( String :: from( "hello world" ) ) ) ,
400- ( r#""hello"+" world"+"!""# , Value :: String ( String :: from( "hello world!" ) ) ) ,
479+ (
480+ r#""hello world""# ,
481+ Value :: String ( String :: from( "hello world" ) ) ,
482+ ) ,
483+ (
484+ r#""hello" + " world""# ,
485+ Value :: String ( String :: from( "hello world" ) ) ,
486+ ) ,
487+ (
488+ r#""hello"+" world"+"!""# ,
489+ Value :: String ( String :: from( "hello world!" ) ) ,
490+ ) ,
401491 ] ;
402492 run_eval_tests ( tests) . await ;
403493 }
@@ -439,11 +529,19 @@ mod tests {
439529 ( "[]" , Value :: Array ( vec![ ] ) ) ,
440530 (
441531 "[1, 2, 3]" ,
442- Value :: Array ( vec![ Value :: Integer ( 1 ) , Value :: Integer ( 2 ) , Value :: Integer ( 3 ) ] ) ,
532+ Value :: Array ( vec![
533+ Value :: Integer ( 1 ) ,
534+ Value :: Integer ( 2 ) ,
535+ Value :: Integer ( 3 ) ,
536+ ] ) ,
443537 ) ,
444538 (
445539 "[1 + 2, 3 - 4, 5 * 6]" ,
446- Value :: Array ( vec![ Value :: Integer ( 3 ) , Value :: Integer ( -1 ) , Value :: Integer ( 30 ) ] ) ,
540+ Value :: Array ( vec![
541+ Value :: Integer ( 3 ) ,
542+ Value :: Integer ( -1 ) ,
543+ Value :: Integer ( 30 ) ,
544+ ] ) ,
447545 ) ,
448546 ] ;
449547 run_eval_tests ( tests) . await ;
@@ -499,10 +597,16 @@ mod tests {
499597 let tests = vec ! [
500598 ( "let one = 1; one" , Value :: Integer ( 1 ) ) ,
501599 ( "let one = 1; let two = 2; one + two" , Value :: Integer ( 3 ) ) ,
502- ( "let one = 1; let two = one + one; one + two" , Value :: Integer ( 3 ) ) ,
600+ (
601+ "let one = 1; let two = one + one; one + two" ,
602+ Value :: Integer ( 3 ) ,
603+ ) ,
503604 ( "let one = 1; one;" , Value :: Integer ( 1 ) ) ,
504605 ( "let one = 1; let two = 2; one + two;" , Value :: Integer ( 3 ) ) ,
505- ( "let one = 1; let two = one + one; one + two;" , Value :: Integer ( 3 ) ) ,
606+ (
607+ "let one = 1; let two = one + one; one + two;" ,
608+ Value :: Integer ( 3 ) ,
609+ ) ,
506610 ( "let one = 1;let one = 2;" , Value :: Integer ( 2 ) ) ,
507611 ( "let one = 1;let one = 2;one" , Value :: Integer ( 2 ) ) ,
508612 ( "let one = 1;let two = 2;let one = 3;one" , Value :: Integer ( 3 ) ) ,
@@ -522,7 +626,10 @@ mod tests {
522626 ( "if (1 > 2) { 10 } else { 20 }" , Value :: Integer ( 20 ) ) ,
523627 ( "if (1 > 2) { 10 }" , Value :: Null ) ,
524628 ( "if (false) { 10 }" , Value :: Null ) ,
525- ( "if ((if (false) { 10 })) { 10 } else { 20 }" , Value :: Integer ( 20 ) ) ,
629+ (
630+ "if ((if (false) { 10 })) { 10 } else { 20 }" ,
631+ Value :: Integer ( 20 ) ,
632+ ) ,
526633 ( "if (true) {} else { 10 }" , Value :: Null ) ,
527634 ( "if (true) { 1; 2 } else { 3 }" , Value :: Integer ( 2 ) ) ,
528635 ] ;
@@ -548,8 +655,14 @@ mod tests {
548655 // ("fn identity(x) { return x; }; identity(5);", Value::Integer(5)),
549656 ( "fn double(x) { x * 2; }; double(5);" , Value :: Integer ( 10 ) ) ,
550657 ( "fn add(x, y) { x + y; }; add(5, 5);" , Value :: Integer ( 10 ) ) ,
551- ( "fn add(x, y) { x + y; }; add(5 + 5, add(5, 5));" , Value :: Integer ( 20 ) ) ,
552- ( "fn one(x) { x + 1; } fn two(x) { one(one(x)); }; two(0);" , Value :: Integer ( 2 ) ) ,
658+ (
659+ "fn add(x, y) { x + y; }; add(5 + 5, add(5, 5));" ,
660+ Value :: Integer ( 20 ) ,
661+ ) ,
662+ (
663+ "fn one(x) { x + 1; } fn two(x) { one(one(x)); }; two(0);" ,
664+ Value :: Integer ( 2 ) ,
665+ ) ,
553666 ( "fn x(x) { x; }; x(5)" , Value :: Integer ( 5 ) ) ,
554667 ( "fn len(x) { x; }; len(10)" , Value :: Integer ( 10 ) ) ,
555668 ] ;
0 commit comments