Skip to content

Commit

Permalink
Let return take 0 arguments
Browse files Browse the repository at this point in the history
Fixes #53.

Also added an argument number sanity-check to the `return` macro's
implementation, and an additional test for it firing.
  • Loading branch information
anko committed Jul 30, 2017
1 parent c483d59 commit 859e19e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/built-in-macros.ls
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,11 @@ contents =
body : body

\return : (arg) ->
if &length not in [ 0 1 ]
throw Error "Expected `return` macro to get 0 or 1 arguments, but got \
#{&length}"
type : \ReturnStatement
argument : @compile arg
argument : if arg then @compile arg else null

\. : do

Expand Down
8 changes: 8 additions & 0 deletions test.ls
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ test "return statement" ->
esl "(lambda () (return \"hello there\"))"
..`@equals` "(function () {\n return 'hello there';\n});"

test "empty return statement" ->
esl "(lambda () (return))"
..`@equals` "(function () {\n return;\n});"

test "return with too many args throws" ->
(-> esl "(lambda () (return 1 2))")
..`@throws` Error

test "member expression" ->
esl "(. console log)"
..`@equals` "console.log;"
Expand Down

0 comments on commit 859e19e

Please sign in to comment.