Skip to content

Commit

Permalink
cargo format changed files
Browse files Browse the repository at this point in the history
  • Loading branch information
zao111222333 committed Apr 3, 2024
1 parent 6c8481b commit cd9e9c1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 32 deletions.
6 changes: 4 additions & 2 deletions src/boolean_expression/_impl_boolean_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ impl Display for BooleanExpression {
Xor(l, r) => write!(f, "({} ^ {})", l, r),
Imp(l, r) => write!(f, "({} => {})", l, r),
Iff(l, r) => write!(f, "({} <=> {})", l, r),
Cond(cond, then_expr, else_expr) => write!(f, "({} ? {} : {})", cond, then_expr,else_expr),
Cond(cond, then_expr, else_expr) => {
write!(f, "({} ? {} : {})", cond, then_expr, else_expr)
}
}
}
}
Expand Down Expand Up @@ -71,7 +73,7 @@ impl BddVariableSet {
let cond = self.safe_eval_expression(cond)?;
let then_expr = self.safe_eval_expression(then_expr)?;

Check warning on line 74 in src/boolean_expression/_impl_boolean_expression.rs

View check run for this annotation

Codecov / codecov/patch

src/boolean_expression/_impl_boolean_expression.rs#L72-L74

Added lines #L72 - L74 were not covered by tests
let else_expr = self.safe_eval_expression(else_expr)?;
Some(Bdd::if_then_else(&cond,&then_expr,&else_expr))
Some(Bdd::if_then_else(&cond, &then_expr, &else_expr))

Check warning on line 76 in src/boolean_expression/_impl_boolean_expression.rs

View check run for this annotation

Codecov / codecov/patch

src/boolean_expression/_impl_boolean_expression.rs#L76

Added line #L76 was not covered by tests
}
}
}
Expand Down
65 changes: 36 additions & 29 deletions src/boolean_expression/_impl_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,36 +132,37 @@ fn iff(data: &[ExprToken]) -> Result<Box<BooleanExpression>, String> {
fn imp(data: &[ExprToken]) -> Result<Box<BooleanExpression>, String> {
let imp_token = index_of_first(data, ExprToken::Imp);
Ok(if let Some(imp_token) = imp_token {
Box::new(Imp(cond(&data[..imp_token])?, imp(&data[(imp_token + 1)..])?))
Box::new(Imp(
cond(&data[..imp_token])?,
imp(&data[(imp_token + 1)..])?,
))
} else {
cond(data)?
})
}

/// **(internal)** Recursive parsing step 3: extract `cond ? then_expr : else_expr` operators.
///
///
/// + Vaild: `(cond1 ? then_expr1 : else_expr1) + (cond2 ? then_expr2 : else_expr2)`
///
///
/// + Vaild: `(cond1 ? then_expr1 : else_expr1) + cond2 ? then_expr2 : else_expr2`
///
///
/// + Vaild: `cond1 ? then_expr1 : else_expr1 + (cond2 ? then_expr2 : else_expr2)`
///
///
/// + Invalid: `cond1 ? then_expr1 : cond2 ? then_expr2 : else_expr2`
fn cond(data: &[ExprToken]) -> Result<Box<BooleanExpression>, String> {
let question_token = index_of_first(data, ExprToken::QuestionMark);
let colon_token = index_of_first(data, ExprToken::Colon);
match (question_token,colon_token){
(None, None) => or(data),
(Some(question_token), Some(colon_token)) => Ok(Box::new(Cond(
or(&data[..question_token])?,or(&data[(question_token+1)..colon_token])?,or(&data[(colon_token + 1)..])?
))),
(None, Some(_)) => Err(format!(
"Expected `?` but only found `:`."
)),
(Some(_), None) => Err(format!(
"Expected `:` but only found `?`."
)),
}
match (question_token, colon_token) {
(None, None) => or(data),
(Some(question_token), Some(colon_token)) => Ok(Box::new(Cond(
or(&data[..question_token])?,
or(&data[(question_token + 1)..colon_token])?,
or(&data[(colon_token + 1)..])?,
))),
(None, Some(_)) => Err(format!("Expected `?` but only found `:`.")),
(Some(_), None) => Err(format!("Expected `:` but only found `?`.")),

Check warning on line 164 in src/boolean_expression/_impl_parser.rs

View check run for this annotation

Codecov / codecov/patch

src/boolean_expression/_impl_parser.rs#L163-L164

Added lines #L163 - L164 were not covered by tests
}
}

/// **(internal)** Recursive parsing step 4: extract `|` operators.
Expand Down Expand Up @@ -237,16 +238,16 @@ mod tests {
#[test]
fn parse_boolean_formula_basic() {
let inputs = vec![
"v_1+{14}", // just a variable name with fancy symbols
"!v_1", // negation
"true", // true
"false", // false
"(v_1 & v_2)", // and
"(cond ? then_expr : else_expr)", // cond
"(v_1 | v_2)", // or
"(v_1 ^ v_2)", // xor
"(v_1 => v_2)", // imp
"(v_1 <=> v_2)", // iff
"v_1+{14}", // just a variable name with fancy symbols
"!v_1", // negation
"true", // true
"false", // false
"(v_1 & v_2)", // and
"(cond ? then_expr : else_expr)", // cond
"(v_1 | v_2)", // or
"(v_1 ^ v_2)", // xor
"(v_1 => v_2)", // imp
"(v_1 <=> v_2)", // iff
];
for input in inputs {
assert_eq!(
Expand Down Expand Up @@ -291,11 +292,17 @@ mod tests {
);
assert_eq!(
"((a ? b : c) ? d : e)",
format!("{}", parse_boolean_expression("(a ? b : c) ? d : e").unwrap())
format!(
"{}",
parse_boolean_expression("(a ? b : c) ? d : e").unwrap()
)
);
assert_eq!(
"(a ? b : (c ? d : e))",
format!("{}", parse_boolean_expression("a ? b : (c ? d : e)").unwrap())
format!(
"{}",
parse_boolean_expression("a ? b : (c ? d : e)").unwrap()
)
);
}

Expand Down
6 changes: 5 additions & 1 deletion src/boolean_expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@ pub enum BooleanExpression {
Imp(Box<BooleanExpression>, Box<BooleanExpression>),
Iff(Box<BooleanExpression>, Box<BooleanExpression>),
/// cond ? then_expr : else_expr
Cond(Box<BooleanExpression>, Box<BooleanExpression>, Box<BooleanExpression>),
Cond(
Box<BooleanExpression>,
Box<BooleanExpression>,
Box<BooleanExpression>,
),
}

0 comments on commit cd9e9c1

Please sign in to comment.