diff --git a/crates/aiken-lang/src/format.rs b/crates/aiken-lang/src/format.rs index 338175e1..e345875c 100644 --- a/crates/aiken-lang/src/format.rs +++ b/crates/aiken-lang/src/format.rs @@ -775,11 +775,8 @@ impl<'comments> Formatter<'comments> { UntypedExpr::Fn { fn_style: FnStyle::BinOp(op), - body, .. - } => { - unimplemented!() - } + } => op.to_doc(), UntypedExpr::Fn { fn_style: FnStyle::Plain, @@ -1070,7 +1067,9 @@ impl<'comments> Formatter<'comments> { let right = self.expr(right); self.operator_side(left, precedence, left_precedence) + .append(" ") .append(name) + .append(" ") .append(self.operator_side(right, precedence, right_precedence - 1)) } @@ -1726,19 +1725,19 @@ impl<'a> Documentable<'a> for &'a UnqualifiedImport { impl<'a> Documentable<'a> for &'a BinOp { fn to_doc(self) -> Document<'a> { match self { - BinOp::And => " && ", - BinOp::Or => " || ", - BinOp::LtInt => " < ", - BinOp::LtEqInt => " <= ", - BinOp::Eq => " == ", - BinOp::NotEq => " != ", - BinOp::GtEqInt => " >= ", - BinOp::GtInt => " > ", - BinOp::AddInt => " + ", - BinOp::SubInt => " - ", - BinOp::MultInt => " * ", - BinOp::DivInt => " / ", - BinOp::ModInt => " % ", + BinOp::And => "&&", + BinOp::Or => "||", + BinOp::LtInt => "<", + BinOp::LtEqInt => "<=", + BinOp::Eq => "==", + BinOp::NotEq => "!=", + BinOp::GtEqInt => ">=", + BinOp::GtInt => ">", + BinOp::AddInt => "+", + BinOp::SubInt => "-", + BinOp::MultInt => "*", + BinOp::DivInt => "/", + BinOp::ModInt => "%", } .to_doc() } diff --git a/crates/aiken-lang/src/tests/format.rs b/crates/aiken-lang/src/tests/format.rs index 9154e290..79ca88d4 100644 --- a/crates/aiken-lang/src/tests/format.rs +++ b/crates/aiken-lang/src/tests/format.rs @@ -864,3 +864,26 @@ fn hex_and_numeric_underscore() { assert_fmt(src, src); } + +#[test] +fn first_class_binop() { + let src = indoc! { r#" + fn foo() { + compare_with(a, >, b) + compare_with(a, >=, b) + compare_with(a, <, b) + compare_with(a, <=, b) + compare_with(a, ==, b) + compare_with(a, !=, b) + combine_with(a, &&, b) + combine_with(a, ||, b) + compute_with(a, +, b) + compute_with(a, -, b) + compute_with(a, /, b) + compute_with(a, *, b) + compute_with(a, %, b) + } + "#}; + + assert_fmt(src, src); +}