Skip to content

Commit

Permalink
Compile empty lists to null instead of nothing
Browse files Browse the repository at this point in the history
This breaks API for how empty lists are compiled.  They used to stand
for no code whatsoever, and now compile to a `null` identifier.

Rationale: `()` compiling to nothing (as it has since
2b47f4b), is pretty useless, given that
there is already a way to make nothing by writing nothing.

The for loop macro was relying on being able to compile () into nothing,
but the fix was pretty simple.

Resolves anko#32.
  • Loading branch information
anko committed Jan 19, 2016
1 parent fdc1ca9 commit 3c74426
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
12 changes: 9 additions & 3 deletions src/built-in-macros.ls
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ function-type = (type) -> (params, ...rest) ->
params : params
body : optionally-implicit-block-statement this, rest

compile-unless-empty-list = (compile, ast) ->
if ast.type isnt \list
throw Error "Unexpected argument AST; expected list"
if ast.values.length then compile ast
else null

contents =
\+ : n-ary-expr \+
\- : n-ary-expr \-
Expand Down Expand Up @@ -236,9 +242,9 @@ contents =

\for : (init, test, update, ...body) ->
type : \ForStatement
init : @compile init
test : @compile test
update : @compile update
init : compile-unless-empty-list @compile, init
test : compile-unless-empty-list @compile, test
update : compile-unless-empty-list @compile, update
body : optionally-implicit-block-statement this, body

\forin : (left, right, ...body) ->
Expand Down
7 changes: 6 additions & 1 deletion src/compile.ls
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ list-to-self-producer = (env, { values }) ->

list-to-estree = (env, { values }:ast, options={}) ->

return null if values.length is 0
if values.length is 0
return do
type : \Literal
value : null
raw : \null
loc : ast.location

[ head, ...rest ] = values

Expand Down
14 changes: 7 additions & 7 deletions test.ls
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ test "variable declaration and assignment" ->

test "empty statement" ->
esl "()"
..`@equals` ""
..`@equals` "null;"

test "break statement" ->
esl "(while true (break))"
Expand Down Expand Up @@ -564,14 +564,14 @@ test "simple unquoting macro" ->
..`@equals` "three();"

test "empty-list-returning macro" ->
esl "(macro nothing (lambda () (return '())))
(nothing)"
..`@equals` ""
esl "(macro shouldbenull (lambda () (return '())))
(shouldbenull)"
..`@equals` "null;"

test "empty-list-returning macro (using quasiquote)" ->
esl "(macro nothing (lambda () (return `())))
(nothing)"
..`@equals` ""
esl "(macro shouldbenull (lambda () (return `())))
(shouldbenull)"
..`@equals` "null;"

test "nothing-returning macro" ->
esl "(macro nothing (lambda () (return undefined)))
Expand Down

0 comments on commit 3c74426

Please sign in to comment.