wisp-lang / wisp

A little Clojure-like LISP in JavaScript
https://gozala.github.io/wisp/
Other
982 stars 68 forks source link

Support for Generators #67

Open thehydroimpulse opened 10 years ago

thehydroimpulse commented 10 years ago

What do you think of supporting harmony generators?

I'd send in a PR if you'd like.

Gozala commented 10 years ago

Sure, I'd love a pull request. Note that currently generators will actually work in spidermonkey:

(fn []
  (yield 1)
  (yield 2))

This will output:

function() {
  yield(1);
  return yield(2);
}

I'd suggest to add support via metadata so that syntax sugar can be just a macro:

(defmacro generator
  [& forms]
  (with-meta (cons 'fn forms) {:generator true}))

(generator []
  (yield 1)
  (yield 2))

And then modify writer to such that it will set :generator to (:generator (meta form))

Although the problem is I don't think escodegen used to generate JS form AST supports generators yet, so that will require fixing too.

thehydroimpulse commented 10 years ago

@Gozala Awesome. Right now Node also supports generators under the --harmony or --harmony-generator flag (>=0.11.4). Hopefully it'll become stable soon, which then the flags will be on by default.

Gozala commented 10 years ago

@TheHydroImpulse Forgot to put link to a writer I was intending to, updated my comment to include it now.