Skip to content

Commit edc65bd

Browse files
committed
chore(ci): fix clippy logging warnings
1 parent bcc3bb9 commit edc65bd

File tree

1 file changed

+79
-51
lines changed

1 file changed

+79
-51
lines changed

src/lambda_vm.rs

Lines changed: 79 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::lambda_parse::{parse_lambda, LambdaExpression};
2+
use colored::*;
3+
use log::{debug, trace};
24
use std::rc::Rc;
35
use uuid::Uuid;
4-
use log::{debug, trace};
5-
use colored::*;
66

77
pub struct VM {
88
max_iterations: usize,
@@ -26,12 +26,12 @@ impl VM {
2626
let next = self.eval_recursive(&current, 0);
2727
if *next == *current {
2828
debug!("{}", "Evaluation complete:".green().bold());
29-
debug!(" {}", next.to_string().green());
29+
debug!(" {}", format!("{next}").green());
3030
return next;
3131
}
3232
current = next;
3333
if i % 1000 == 0 {
34-
debug!("Iteration {}: {}", i, current.to_string());
34+
debug!("Iteration {}: {}", i, current);
3535
}
3636
}
3737
debug!("{}", "Reached maximum iterations".yellow().bold());
@@ -46,26 +46,26 @@ impl VM {
4646

4747
let result = match expr {
4848
LambdaExpression::Variable(_) => {
49-
debug!("{}Variable: {}", " ".repeat(depth), expr.to_string());
49+
debug!("{}Variable: {}", " ".repeat(depth), expr);
5050
Rc::new(expr.clone())
5151
}
5252
LambdaExpression::Number(_) => {
53-
debug!("{}Number: {}", " ".repeat(depth), expr.to_string());
53+
debug!("{}Number: {}", " ".repeat(depth), expr);
5454
Rc::new(expr.clone())
5555
}
5656
LambdaExpression::Boolean(_) => {
57-
debug!("{}Boolean: {}", " ".repeat(depth), expr.to_string());
57+
debug!("{}Boolean: {}", " ".repeat(depth), expr);
5858
Rc::new(expr.clone())
5959
}
6060
LambdaExpression::Abstraction { parameter, body } => {
61-
debug!("{}Abstraction: {}", " ".repeat(depth), expr.to_string());
61+
debug!("{}Abstraction: {}", " ".repeat(depth), expr);
6262
Rc::new(LambdaExpression::Abstraction {
6363
parameter: parameter.clone(),
6464
body: self.eval_recursive(body, depth + 1),
6565
})
6666
}
6767
LambdaExpression::Application { function, argument } => {
68-
debug!("{}Application: {}", " ".repeat(depth), expr.to_string());
68+
debug!("{}Application: {}", " ".repeat(depth), expr);
6969
let eval_func = self.eval_recursive(function, depth + 1);
7070
let eval_arg = self.eval_recursive(argument, depth + 1);
7171
match &*eval_func {
@@ -80,49 +80,64 @@ impl VM {
8080
}
8181
}
8282
LambdaExpression::IsZero(inner) => {
83-
debug!("{}IsZero: {}", " ".repeat(depth), expr.to_string());
83+
debug!("{}IsZero: {}", " ".repeat(depth), expr);
8484
let eval_inner = self.eval_recursive(inner, depth + 1);
8585
match &*eval_inner {
86-
LambdaExpression::Abstraction { parameter: _f, body } => {
86+
LambdaExpression::Abstraction {
87+
parameter: _f,
88+
body,
89+
} => {
8790
match &**body {
88-
LambdaExpression::Abstraction { parameter: x, body: inner_body } => {
91+
LambdaExpression::Abstraction {
92+
parameter: x,
93+
body: inner_body,
94+
} => {
8995
if **inner_body == LambdaExpression::Variable(x.clone()) {
9096
trace!("{}IsZero: it is zero", " ".repeat(depth));
91-
Rc::new(parse_lambda("λx. λy. x").unwrap()) // true
97+
Rc::new(parse_lambda("λx. λy. x").unwrap()) // true
9298
} else {
9399
trace!("{}IsZero: it is not zero", " ".repeat(depth));
94-
Rc::new(parse_lambda("λx. λy. y").unwrap()) // false
100+
Rc::new(parse_lambda("λx. λy. y").unwrap()) // false
95101
}
96-
},
102+
}
97103
_ => Rc::new(LambdaExpression::IsZero(eval_inner)),
98104
}
99-
},
105+
}
100106
_ => Rc::new(LambdaExpression::IsZero(eval_inner)),
101107
}
102-
},
108+
}
103109
LambdaExpression::IfThenElse(condition, then_expr, else_expr) => {
104110
let eval_condition = self.eval_recursive(condition, depth + 1);
105111
match eval_condition.to_church_bool() {
106112
Some(true) => {
107-
debug!("{}Condition is true, evaluating then_expr", " ".repeat(depth));
113+
debug!(
114+
"{}Condition is true, evaluating then_expr",
115+
" ".repeat(depth)
116+
);
108117
self.eval_recursive(then_expr, depth + 1)
109-
},
118+
}
110119
Some(false) => {
111-
debug!("{}Condition is false, evaluating else_expr", " ".repeat(depth));
120+
debug!(
121+
"{}Condition is false, evaluating else_expr",
122+
" ".repeat(depth)
123+
);
112124
self.eval_recursive(else_expr, depth + 1)
113-
},
125+
}
114126
None => {
115-
debug!("{}Condition is not a Church boolean, returning unevaluated IfThenElse", " ".repeat(depth));
127+
debug!(
128+
"{}Condition is not a Church boolean, returning unevaluated IfThenElse",
129+
" ".repeat(depth)
130+
);
116131
Rc::new(LambdaExpression::IfThenElse(
117132
eval_condition.clone(),
118133
then_expr.clone(),
119134
else_expr.clone(),
120135
))
121-
},
136+
}
122137
}
123-
},
138+
}
124139
LambdaExpression::Pred(inner) => {
125-
debug!("{}Pred: {}", " ".repeat(depth), expr.to_string());
140+
debug!("{}Pred: {}", " ".repeat(depth), expr);
126141
let eval_inner = self.eval_recursive(inner, depth + 1);
127142
match &*eval_inner {
128143
LambdaExpression::Abstraction { .. } => {
@@ -139,7 +154,7 @@ impl VM {
139154
}
140155
}
141156
LambdaExpression::Succ(inner) => {
142-
debug!("{}Succ: {}", " ".repeat(depth), expr.to_string());
157+
debug!("{}Succ: {}", " ".repeat(depth), expr);
143158
let eval_inner = self.eval_recursive(inner, depth + 1);
144159
match &*eval_inner {
145160
LambdaExpression::Abstraction { .. } => {
@@ -179,7 +194,7 @@ impl VM {
179194
}
180195
}
181196
LambdaExpression::And(left, right) => {
182-
debug!("{}And: {}", " ".repeat(depth), expr.to_string());
197+
debug!("{}And: {}", " ".repeat(depth), expr);
183198
let eval_left = self.eval_recursive(left, depth + 1);
184199
let eval_right = self.eval_recursive(right, depth + 1);
185200
let true_expr = parse_lambda("λx. λy. x").unwrap();
@@ -202,7 +217,7 @@ impl VM {
202217
}
203218
}
204219
LambdaExpression::Not(inner) => {
205-
debug!("{}Not: {}", " ".repeat(depth), expr.to_string());
220+
debug!("{}Not: {}", " ".repeat(depth), expr);
206221
let eval_inner = self.eval_recursive(inner, depth + 1);
207222
let true_expr = parse_lambda("λx. λy. x").unwrap();
208223
let false_expr = parse_lambda("λx. λy. y").unwrap();
@@ -213,21 +228,21 @@ impl VM {
213228
}
214229
}
215230
LambdaExpression::Pair(first, second) => {
216-
debug!("{}Pair: {}", " ".repeat(depth), expr.to_string());
231+
debug!("{}Pair: {}", " ".repeat(depth), expr);
217232
let eval_first = self.eval_recursive(first, depth + 1);
218233
let eval_second = self.eval_recursive(second, depth + 1);
219234
Rc::new(LambdaExpression::Pair(eval_first, eval_second))
220235
}
221236
LambdaExpression::First(pair) => {
222-
debug!("{}First: {}", " ".repeat(depth), expr.to_string());
237+
debug!("{}First: {}", " ".repeat(depth), expr);
223238
let eval_pair = self.eval_recursive(pair, depth + 1);
224239
match &*eval_pair {
225240
LambdaExpression::Pair(first, _) => first.clone(),
226241
_ => Rc::new(LambdaExpression::First(eval_pair)),
227242
}
228243
}
229244
LambdaExpression::Second(pair) => {
230-
debug!("{}Second: {}", " ".repeat(depth), expr.to_string());
245+
debug!("{}Second: {}", " ".repeat(depth), expr);
231246
let eval_pair = self.eval_recursive(pair, depth + 1);
232247
match &*eval_pair {
233248
LambdaExpression::Pair(_, second) => second.clone(),
@@ -237,7 +252,7 @@ impl VM {
237252
// not allow Y combinator nested
238253
// TODO
239254
LambdaExpression::YCombinator(f) => {
240-
debug!("{}YCombinator: {}", " ".repeat(depth), expr.to_string());
255+
debug!("{}YCombinator: {}", " ".repeat(depth), expr);
241256
self.eval_y_combinator(f, depth)
242257
}
243258
};
@@ -421,15 +436,19 @@ pub fn church_encode(n: u64) -> LambdaExpression {
421436
pub fn church_decode(expr: &LambdaExpression) -> Result<u64, String> {
422437
fn count_applications(expr: &LambdaExpression) -> u64 {
423438
match expr {
424-
LambdaExpression::Application { function: _, argument } => {
425-
1 + count_applications(argument)
426-
}
439+
LambdaExpression::Application {
440+
function: _,
441+
argument,
442+
} => 1 + count_applications(argument),
427443
_ => 0,
428444
}
429445
}
430446

431447
match expr {
432-
LambdaExpression::Abstraction { parameter: _f, body } => match &**body {
448+
LambdaExpression::Abstraction {
449+
parameter: _f,
450+
body,
451+
} => match &**body {
433452
LambdaExpression::Abstraction {
434453
parameter: _x,
435454
body: inner_body,
@@ -480,13 +499,17 @@ mod tests {
480499
// check expr === value return boolean
481500
fn is_church_numeral(expr: &LambdaExpression, value: u64) -> bool {
482501
match expr {
483-
LambdaExpression::Abstraction { parameter: _f, body } => match &**body {
484-
LambdaExpression::Abstraction { parameter: _x, body: _inner_body } => {
485-
church_decode(expr).map_or(false, |n| n == value)
486-
},
487-
_ => false
502+
LambdaExpression::Abstraction {
503+
parameter: _f,
504+
body,
505+
} => match &**body {
506+
LambdaExpression::Abstraction {
507+
parameter: _x,
508+
body: _inner_body,
509+
} => church_decode(expr).map_or(false, |n| n == value),
510+
_ => false,
488511
},
489-
_ => false
512+
_ => false,
490513
}
491514
}
492515

@@ -854,13 +877,13 @@ mod tests {
854877
let vm = VM::new();
855878

856879
let test_cases = vec![
857-
("is_zero 0", parse_lambda("λx. λy. x").unwrap()), // true
858-
("is_zero 1", parse_lambda("λx. λy. y").unwrap()), // false
859-
("is_zero (pred 1)", parse_lambda("λx. λy. x").unwrap()), // true
880+
("is_zero 0", parse_lambda("λx. λy. x").unwrap()), // true
881+
("is_zero 1", parse_lambda("λx. λy. y").unwrap()), // false
882+
("is_zero (pred 1)", parse_lambda("λx. λy. x").unwrap()), // true
860883
("2 * 3", church_encode(6)),
861884
("0 * 5", church_encode(0)),
862-
("is_zero (3 * 0)", parse_lambda("λx. λy. x").unwrap()), // true
863-
("is_zero (2 * 3)", parse_lambda("λx. λy. y").unwrap()), // false
885+
("is_zero (3 * 0)", parse_lambda("λx. λy. x").unwrap()), // true
886+
("is_zero (2 * 3)", parse_lambda("λx. λy. y").unwrap()), // false
864887
];
865888

866889
for (input, expected) in test_cases {
@@ -968,8 +991,13 @@ mod tests {
968991
match church_decode(&result) {
969992
Ok(decoded) => {
970993
println!("Decoded result: {}", decoded);
971-
assert_eq!(decoded, if n == 0 { 1 } else { n }, "Factorial of {} failed", n);
972-
},
994+
assert_eq!(
995+
decoded,
996+
if n == 0 { 1 } else { n },
997+
"Factorial of {} failed",
998+
n
999+
);
1000+
}
9731001
Err(e) => panic!("Failed to decode result: {}", e),
9741002
}
9751003
}
@@ -1013,8 +1041,8 @@ mod tests {
10131041
Ok(decoded) => {
10141042
println!("Decoded result: {}", decoded);
10151043
assert_eq!(decoded, 1, "Fibonacci of {} failed", n);
1016-
},
1044+
}
10171045
Err(e) => panic!("Failed to decode result: {}", e),
10181046
}
10191047
}
1020-
}
1048+
}

0 commit comments

Comments
 (0)