anko / eslisp

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

Issue using atoms with macros #55

Closed ghost closed 5 years ago

ghost commented 5 years ago

I'm writing a macro to convert a hex atom into a decimal literal since eslisp doesn't seem to like hex literals

Current macro code looks like this:

(= (. module exports)
    (lambda (num)
    (return ((. this list)
        ((. this atom) "parseInt")
        ((. this string) (. num value))
        ((. this string) "16")))))

And the compiled JS looks like this:

module.exports = function (num) {
    return this.list(this.atom('parseInt'), this.string(num.value), this.string('16'));
};

Calling it with (macro test (require "./mac.js")) (test 'ff) returns the following error:

TypeError: Cannot read property 'type' of undefined
    at astToEstree (/home/taylor/.npm-global/lib/node_modules/eslisp/lib/compile.js:244:15)
    at /home/taylor/.npm-global/lib/node_modules/eslisp/lib/compile.js:275:51
    at Array.map (<anonymous>)
    at listToEstree (/home/taylor/.npm-global/lib/node_modules/eslisp/lib/compile.js:238:23)
    at astToEstree (/home/taylor/.npm-global/lib/node_modules/eslisp/lib/compile.js:250:25)
    at /home/taylor/.npm-global/lib/node_modules/eslisp/lib/compile.js:218:16
    at listToEstree (/home/taylor/.npm-global/lib/node_modules/eslisp/lib/compile.js:234:6)
    at astToEstree (/home/taylor/.npm-global/lib/node_modules/eslisp/lib/compile.js:250:25)
    at /home/taylor/.npm-global/lib/node_modules/eslisp/lib/translate.js:36:14

Am I doing anything wrong, or is this some weird issue with eslisp?

ghost commented 5 years ago

Realized I could do this with a function, too, and it still had the same issue

ghost commented 5 years ago

I ended up just precomputing the value with

(= (. module exports) (lambda (num)
     (return ((. this atom) (parseInt (. num value) "16")))))

and it works fine, my bad!