forked from clojure/clojurescript
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out common REPL code to cljs/cljs/repl.cljs
Also get nodecljs.cljs working using repl.cljs and add example shebang.cljs which can be executed directly.
- Loading branch information
Showing
6 changed files
with
96 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,10 @@ | |||
#!./run.js nodecljs.js | |||
|
|||
(ns shebang-test) | |||
|
|||
(def dbl (fn* [x] (* 2 x))) | |||
|
|||
(println "Hello World UNIX World.") | |||
(println "(dbl 16) is:" (dbl 16)) | |||
|
|||
8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,48 @@ | |||
(ns cljs.repl | |||
(:require [cljs.analyzer :as ana] | |||
[cljs.compiler :as comp] | |||
[cljs.reader :as reader])) | |||
|
|||
(def ^:dynamic *debug* false) | |||
(def ^:dynamic *e nil) | |||
|
|||
(defn prompt [] (str *ns-sym* "=> ")) | |||
|
|||
(defn read-forms [text] | |||
;; Save and restore *ns-sym* | |||
(binding [*ns-sym* *ns-sym*] | |||
(reader/read-string (str "(do " text ")")))) | |||
|
|||
(defn eval-print [text] | |||
(try | |||
(let [env (assoc (ana/empty-env) :context :expr) | |||
forms (read-forms text) | |||
_ (when *debug* (println "READ:" (pr-str forms))) | |||
body (ana/analyze env forms) | |||
_ (when *debug* (println "ANALYZED:" (pr-str (:form body)))) | |||
res (comp/emit-str body) | |||
_ (when *debug* (println "EMITTED:" (pr-str res))) | |||
value (js/eval res)] | |||
(set! *3 *2) | |||
(set! *2 *1) | |||
(set! *1 value) | |||
(binding [*out* *rtn*] (print (pr-str value)) | |||
{:value value :js res})) | |||
(catch js/Error e | |||
(set! *e e) | |||
(binding [*out* *err*] (print (.-stack e))) | |||
{:error (.-stack e)}))) | |||
|
|||
(defn complete-form? [text] | |||
(try | |||
(reader/read-string text) | |||
true | |||
(catch js/Error e | |||
(not (re-find #"EOF while reading" (.-message e)))))) | |||
|
|||
(defn init [] | |||
;; Bootstrap an empty version of the cljs.user namespace | |||
(swap! comp/*emitted-provides* conj (symbol "cljs.user")) | |||
(.provide js/goog "cljs.user") | |||
(set! cljs.core/*ns-sym* (symbol "cljs.user"))) | |||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,37 +1,21 @@ | |||
(ns nodecljs | (ns nodecljs | ||
(:require [cljs.core] | (:require [cljs.repl :as repl])) | ||
[cljs.analyzer :as ana] | |||
[cljs.compiler :as comp] | |||
[cljs.reader :as reader])) | |||
|
|||
(defn ep [text] | |||
(let [r (reader/push-back-reader text)] | |||
(loop [str (reader/read r false :eof false)] | |||
(when (not= str :eof) | |||
(try | |||
(let [env (ana/empty-env) | |||
res (comp/emit-str (ana/analyze env str))] | |||
(js/eval res)) | |||
(catch js/Error e | |||
(println e) | |||
#_(set! *e e))) | |||
(recur (reader/read r false :eof false)))))) | |||
|
|
||
(defn -main [file & args] | (defn -main [file & args] | ||
(repl/init) | |||
|
|||
;; Setup the print function | ;; Setup the print function | ||
(set! *out* (.-write (.-stdout js/process))) | (set! *out* #(.write (.-stdout js/process) %)) | ||
(set! *err* (.-write (.-stderr js/process))) | (set! *rtn* identity) | ||
(set! *err* #(.write (.-stderr js/process) %)) | |||
(set! *print-fn* #(*out* %1)) | (set! *print-fn* #(*out* %1)) | ||
|
|
||
;; Bootstrap an empty version of the cljs.user namespace | ;(set! repl/*debug* true) | ||
(swap! cljs.compiler/*emitted-provides* conj (symbol "cljs.user")) | |||
(.provide js/goog "cljs.user") | |||
(set! cljs.core/*ns-sym* (symbol "cljs.user")) | |||
|
|||
;(set! js/env (assoc js/env :context :expr)) | |||
(let [fs (js/require "fs") | (let [fs (js/require "fs") | ||
text (.toString (.readFileSync fs file))] | text (.toString (.readFileSync fs file)) | ||
(ep text))) | res (repl/eval-print text) | ||
ret (:value res)] | |||
(.exit js/process (if (number? ret) ret 0)))) | |||
|
|
||
(set! *main-cli-fn* -main) | (set! *main-cli-fn* -main) | ||
|
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters