Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export ast-to-estree? #52

Open
newhouseb opened this issue May 21, 2017 · 1 comment
Open

Export ast-to-estree? #52

newhouseb opened this issue May 21, 2017 · 1 comment
Labels

Comments

@newhouseb
Copy link

newhouseb commented May 21, 2017

Hi,

I'd like to use eslisp to evaluate eslisp, so that I can do things like:

(eval (quote (+ 1 2 3)))

eval, of course, takes Javascript so I'd like a way to transform the estree (as returned by quote) to javascript. Looking at https://github.com/anko/eslisp/blob/master/src/index.ls, it appears that we might be able to export ast-to-estree such that something like

(eval (ast-to-estree (quote (+ 1 2 3))))

could work (edit: not quite, the AST used internally seems to be different from what quote returns). Another option would be to make compileOnce, etc default to this behavior if the input was an estree instead of a string.

What do you think?

Thanks,
Ben

@anko
Copy link
Owner

anko commented May 21, 2017

Trying to isolate the root issue—correct me if I'm wrong:

You want to evaluate (+ 1 2 3) at compile-time, such that its output (6) is inserted into the output JavaScript as a numeric constant.

At the moment, you could do that with this.evaluate inside a macro:

; Define a new macro that compiles & evaluates its input, and outputs the result
(macro precompute
       (lambda (input)
        (var output ((. this evaluate) input))
        (return ((. this atom) output))))

; Call it
(precompute (+ 1 2 3))

The output is:

6;

The only sparse documentation I've written for this sort of thing so far is here. It's buried behind a few links from the main readme, because my thoughts on it haven't fully come together yet—sorry about that. It has various unit tests though.

To return array and object literals from macros, you currently have to manually construct the appropriate estree objects and return those. Macros should probably expose a generic this.valueToEstree. This isn't documented anywhere, because again I'm not convinced I've fully figured it out yet.

Eventually, I want to get rid of as much of this AST-format juggling as possible, and document properly what's going on. It's actually not very complex, it's just liable to change a lot.

Does some of that answer some of your questions? 😅

@anko anko added the question label Jul 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants