diff --git a/src/trick.gleam b/src/trick.gleam index 6237087..74c6f38 100644 --- a/src/trick.gleam +++ b/src/trick.gleam @@ -201,3 +201,30 @@ pub fn greater_than_or_equal_float( ) -> Expression(Float) { binary_operator(left, ">=.", right) } + +fn unary_operator(operator: String, value: Expression(a)) -> Expression(a) { + [doc.from_string(operator), value.document] |> doc.concat |> Expression +} + +pub fn negate_int(value: Expression(Int)) -> Expression(Int) { + unary_operator("-", value) +} + +pub fn negate_bool(value: Expression(Bool)) -> Expression(Bool) { + unary_operator("!", value) +} +// TODO: +// BitString +// Block +// Call +// Case +// Echo +// FieldAccess +// Float +// Fn +// FnCapture +// Pipes +// RecordUpdate +// Tuple +// TupleIndex +// Variable diff --git a/test/trick_test.gleam b/test/trick_test.gleam index d543292..979ad6b 100644 --- a/test/trick_test.gleam +++ b/test/trick_test.gleam @@ -130,3 +130,11 @@ pub fn binary_operator_test() { |> trick.expression_to_string == "False != False" } + +pub fn unary_operator_test() { + assert 10 |> trick.int |> trick.negate_int |> trick.expression_to_string + == "-10" + + assert False |> trick.bool |> trick.negate_bool |> trick.expression_to_string + == "!False" +}