Skip to content

Commit

Permalink
fix: resolve lambda infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
sor4chi committed Jun 30, 2024
1 parent 3d8dcc0 commit 90be682
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/bin/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn main() {

match result {
Node::String(s) => println!("{}", s),
Node::Integer(n) => println!("{}", n),
_ => panic!("Unexpected result: {:?}", result),
}
}
12 changes: 11 additions & 1 deletion src/icfp/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Evaluator {
Node::String(_) => node.clone(),
Node::Boolean(_) => node.clone(),
Node::Variable(_) => node.clone(),
Node::Lambda(arity, body) => Node::Lambda(*arity, Box::new(self.evaluate_node(body))),
Node::Lambda(arity, body) => Node::Lambda(*arity, body.clone()),
Node::UnaryOperator(operator, operand) => {
self.evaluate_unary_operator(operator, *operand.clone())
}
Expand Down Expand Up @@ -180,6 +180,16 @@ impl Evaluator {
let new_right = self.replace_variable(right, variables);
Node::BinaryOperator(operator.clone(), Box::new(new_left), Box::new(new_right))
}
Node::If(condition, then_branch, else_branch) => {
let new_condition = self.replace_variable(condition, variables);
let new_then_branch = self.replace_variable(then_branch, variables);
let new_else_branch = self.replace_variable(else_branch, variables);
Node::If(
Box::new(new_condition),
Box::new(new_then_branch),
Box::new(new_else_branch),
)
}
_ => panic!("Unsupported node: {:?}", node),
}
}
Expand Down

0 comments on commit 90be682

Please sign in to comment.