anko / eslisp

un-opinionated S-expression syntax and macro system for JavaScript
ISC License
528 stars 31 forks source link

Documentation code example for “mean” macro fails with TypeError #37

Closed ktodyruik closed 8 years ago

ktodyruik commented 8 years ago

Hi,

I've been getting some errors in my macros. It's likely I'm making mistakes, so I tried an example from the documentation. I seem to be having trouble with @,

This macro:

(macro mean
 (lambda ()
  ; convert arguments to Array
  (var args ((. Array prototype slice call) arguments 0))
  (var total ((. this atom) (. args length)))
  (return `(/ (+ ,@args) ,total))))

(mean 1 2 3)

Throws and error when I compile it. Here is the error:

$ eslc test2.lisp > test2.js

/usr/local/lib/node_modules/eslisp/lib/cli.js:102
      throw err;
            ^
TypeError: Cannot read property 'type' of undefined
    at astToEstree (/usr/local/lib/node_modules/eslisp/lib/compile.js:230:14)
    at env.prototype.compile (/usr/local/lib/node_modules/eslisp/lib/env.js:106:12)
    at /usr/local/lib/node_modules/eslisp/lib/env.js:213:50
    at env.<anonymous> (/usr/local/lib/node_modules/eslisp/lib/built-in-macros.js:49:17)
    at env.<anonymous> (/usr/local/lib/node_modules/eslisp/lib/built-in-macros.js:70:10)
    at listToEstree (/usr/local/lib/node_modules/eslisp/lib/compile.js:177:27)
    at astToEstree (/usr/local/lib/node_modules/eslisp/lib/compile.js:236:25)
    at env.prototype.compile (/usr/local/lib/node_modules/eslisp/lib/env.js:106:12)
    at env.compile (/usr/local/lib/node_modules/eslisp/lib/env.js:213:50)
    at env.macro (/usr/local/lib/node_modules/eslisp/lib/built-in-macros.js:20:19)
anko commented 8 years ago

That example is out of date. Sorry! It's my fault.

Almost all of the code examples are set up to be automatically tested (with tests-ex-markdown), but that one seems to have slipped through. I'll fix that and review what other examples might be untested.

For now, test.ls contains a similar test, which definitely works:

(macro mean
 (lambda ()
  (var args
       ((. this list apply) null ((. Array prototype slice call) arguments 0)))
  (var total ((. this atom) ((. (. args values length) toString))))
  (return `(/ (+ ,@args) ,total))))
(mean 1 2 3)

That compiles to this:

(1 + (2 + 3)) / 3;

Why this is written differently now: Macros used to return arrays. Since the macro input and output formats became the same, they have returned objects with type- and value-properties, not arrays. That's why ,@ (unquote-splicing) failed for that example. Just in case, I'll make the error text clearer too.

Thanks for reporting. I'll post progress updates.