wisp-lang / wisp

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

wisp -c is blocking? #126

Open danieltanfh95 opened 9 years ago

danieltanfh95 commented 9 years ago

I wrote the following code to try compile all the wisp files in a folder. However, there seems to be nothing coming out of stdout of, "wisp -c {file}". The program just freezes there waiting for an output that never came.

(ns tangle.index
  (:require 
    [ramda :as r]
    [fs]
    [child_process :as proc]))

;;Make a curried function that accepts a call-back and a single argument
(defmacro defnode-data [name func extras]
  `(def ~name 
    (r.curry
      (fn [call-back argument]
        (~func argument ~@extras
          (fn [err data]
            (do
              (if err (throw err))
              (call-back data))))))))

(defmacro defnode-void [name func]
  `(def ~name 
    (r.curry
      (fn [argument extras]
        (~func argument extras
          (fn [err]
            (do
              (console.log "start")
              (if err (throw err)))))))))

(defnode-data run-args proc.exec)
(defnode-data get-file fs.readFile [:utf8])
(defnode-void write-to fs.writeFile)

(def config {
  :src-path "src"
  :lib-path "lib"
  })

(get-file console.log "src/index.wisp") ;This runs fine.
(run-args (write-to "index2.js") "wisp -h") ;This runs fine too.
(run-args console.log "wisp -c src/index.wisp") ;This blocks any output completely. 
robjens commented 8 years ago

Ouch. I would be very careful with intra-process communication and side-effects.

Is it a necessary implementation step? Imho this kind of tackiness together with macro's of all things, would be a last, last, last resort means to work with (wisp) source.

Why not just do:

(ns foo
     (:require fs [wisp.compiler :refer [compile]]))

(print (:code (compile (.to-string (fs/read-file-sync "src/index.wisp" :utf8))
                     {:no-map true})))

Note that I've just used the sync version from fs. I hate callbacks and event loops, and rarely need to resort to them. Even in Node.js... Although the programming style is useful to know and understand (why/when/how), its a dreadful mess, impossible to reason about and largely complects the entire exception chain in my opinion. Although wisp pays no special attention to the programming style (as it should), this doesn't mean one can't craft perfectly fine asynchronous expressions in wisp. Personally, still, I prefer to use bluebird for promises in those cases. Much better control, less deep nesting of expressions, much easier to track what goes wrong.

That having said, there is some general consensus, at least in the Clojure community, that macro's should really be a last resort if there is no way to tackle a problem with regular (higher order) functions.

However looking at your skills, I'd say you should/would know that and this is a required computation step in your procedure... in which case, nothing said ^^