45 lines
1.5 KiB
Rust
45 lines
1.5 KiB
Rust
mod tests;
|
|
|
|
use crate::ast::{ Token, Node, add_unary_node, add_binary_node };
|
|
|
|
pub fn parse_formula(formula: &str) -> Node<bool> {
|
|
let mut stack = vec![];
|
|
for c in formula.chars() {
|
|
match c {
|
|
'0' => stack.push(Node::Leaf(false)),
|
|
'1' => stack.push(Node::Leaf(true)),
|
|
'!' => add_unary_node(&mut stack, Token::Negation),
|
|
'&' => add_binary_node(&mut stack, Token::Conjunction),
|
|
'|' => add_binary_node(&mut stack, Token::Disjunction),
|
|
'^' => add_binary_node(&mut stack, Token::ExclusiveDisjunction),
|
|
'>' => add_binary_node(&mut stack, Token::MaterialCondition),
|
|
'=' => add_binary_node(&mut stack, Token::LogicalEquivalence),
|
|
_ => panic!("Error: {} is not a valid character", c)
|
|
}
|
|
}
|
|
stack.pop().unwrap()
|
|
}
|
|
|
|
fn compute(operator: Token, lhs: bool, rhs: bool) -> bool {
|
|
match operator {
|
|
Token::Negation => !lhs,
|
|
Token::Conjunction => lhs & rhs,
|
|
Token::Disjunction => lhs | rhs,
|
|
Token::ExclusiveDisjunction => lhs ^ rhs,
|
|
Token::MaterialCondition => !(lhs && !rhs),
|
|
Token::LogicalEquivalence => lhs == rhs
|
|
}
|
|
}
|
|
|
|
fn evaluate(tree: Node<bool>) -> bool {
|
|
match tree {
|
|
Node::Unary { operator, operand } => compute(operator, evaluate(*operand), false),
|
|
Node::Binary { operator, lhs, rhs } => compute(operator, evaluate(*lhs), evaluate(*rhs)),
|
|
Node::Leaf(b) => b,
|
|
}
|
|
}
|
|
|
|
pub fn eval_formula(formula: &str) -> bool {
|
|
let tree = parse_formula(formula);
|
|
evaluate(tree)
|
|
} |