diff --git a/birdie_snapshots/function.accepted b/birdie_snapshots/function.accepted index 738e547..2c83c3a 100644 --- a/birdie_snapshots/function.accepted +++ b/birdie_snapshots/function.accepted @@ -8,3 +8,4 @@ fn add(a, b) { a + b } + diff --git a/birdie_snapshots/multiple_functions.accepted b/birdie_snapshots/multiple_functions.accepted new file mode 100644 index 0000000..cd3c32a --- /dev/null +++ b/birdie_snapshots/multiple_functions.accepted @@ -0,0 +1,16 @@ +--- +version: 1.4.1 +title: multiple_functions +file: ./test/trick_test.gleam +test_name: multiple_functions_test +--- +fn add(a, b) { + a + b +} + +fn main() { + echo add(1, 2) + Nil +} + + diff --git a/src/trick.gleam b/src/trick.gleam index 978b412..3979fae 100644 --- a/src/trick.gleam +++ b/src/trick.gleam @@ -888,7 +888,7 @@ fn definition(continue: fn() -> Expression) -> Definition { pub fn function( name: String, function: FunctionBuilder, - continue: fn() -> Definition, + continue: fn(Expression) -> Definition, ) -> Definition { use <- definition use body <- compile_statement(function.body) @@ -926,7 +926,9 @@ pub fn function( return: body.type_, ) - use rest <- compile_definition(continue()) + let function_name = return(Compiled(doc.from_string(name), type_)) + + use rest <- compile_definition(continue(function_name)) [ doc.from_string("fn "), @@ -934,7 +936,7 @@ pub fn function( parameter_list, doc.from_string(" "), body_doc, - doc.line, + doc.lines(2), rest.document, ] |> doc.concat diff --git a/test/trick_test.gleam b/test/trick_test.gleam index 0cd363d..9018640 100644 --- a/test/trick_test.gleam +++ b/test/trick_test.gleam @@ -663,9 +663,60 @@ pub fn function_test() { |> trick.expression |> trick.function_body }, - trick.empty, + fn(_) { trick.empty() }, ) |> trick.to_string |> unwrap |> birdie.snap("function") } + +pub fn multiple_functions_test() { + { + use add <- trick.function("add", { + use a <- trick.parameter("a", type_int) + use b <- trick.parameter("b", type_int) + trick.add(a, b) + |> trick.expression + |> trick.function_body + }) + + use _main <- trick.function( + "main", + trick.function_body({ + use <- trick.discard( + trick.expression(trick.echo_( + trick.call(add, [trick.int(1), trick.int(2)]), + None, + )), + ) + trick.expression(trick.nil()) + }), + ) + + trick.empty() + } + |> trick.to_string + |> unwrap + |> birdie.snap("multiple_functions") +} + +pub fn multiple_functions_type_check_correctly_test() { + let assert Error(error) = + { + use add <- trick.function("add", { + use a <- trick.parameter("a", type_int) + use b <- trick.parameter("b", type_int) + trick.add(a, b) + |> trick.expression + |> trick.function_body + }) + + trick.call(add, [trick.int(1), trick.int(2), trick.int(3)]) + |> trick.expression + |> trick.function_body + |> trick.function("main", _, fn(_main) { trick.empty() }) + } + |> trick.to_string + + assert error == trick.IncorrectNumberOfArguments(expected: 2, got: 3) +}