Skip to content

Commit a3525a4

Browse files
committed
Add standalone and lib compilation options to the compiler
1 parent 5ff260c commit a3525a4

File tree

4 files changed

+99
-78
lines changed

4 files changed

+99
-78
lines changed

bin/cljslua.clj

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(require '[cljs.lua.repl :as repl])
22
(require '[cljs.lua.config :as conf])
33
(require '[cljs.lua.compile :as comp])
4+
(require '[cljs.analyzer :as ana])
45

56
(def commands {"compile" comp/-main
67
"repl" repl/-main})
@@ -9,9 +10,11 @@
910
(for [arg args]
1011
(if (.startsWith arg ":") (keyword (subs arg 1)) arg)))
1112

12-
(let [args (keywordize-args *command-line-args*)
13-
cmd-func (commands (first args))]
14-
(conf/load-config)
15-
(if cmd-func
16-
(apply cmd-func (rest args))
17-
(println "Unknown command : " (first args))))
13+
(ana/with-core-macros "/cljs/lua/core"
14+
(let [args (keywordize-args *command-line-args*)
15+
cmd-func (commands (first args))]
16+
(conf/load-config)
17+
(comp/compile-core)
18+
(if cmd-func
19+
(apply cmd-func (rest args))
20+
(println "Unknown command : " (first args)))))

src/cljs/lua/common.clj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@
4040
(str (get-cljs-dir) file-sep "libs"))
4141

4242
(defn init-dirs []
43-
(.mkdirs (io/file (common-libs-path))))
43+
(.mkdirs (io/file (common-libs-path))))
44+
45+
(def core-lib-path (str (common-libs-path) file-sep "core.cljlib"))

src/cljs/lua/compile.clj

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
(def ^:dynamic *cljs-files*)
1818

19+
(defn emit-lib-header [ns]
20+
(println "-- CLJS/LUA " ns))
21+
1922
(defn files-in
2023
"Return a sequence of all files with given extension in the given directory."
2124
[ext dir]
@@ -81,8 +84,8 @@
8184
;; Adding builtins
8285
(if-not no-deps (println (slurp (io/resource "builtins.lua"))))
8386
;; Adding core.cljs
84-
(if-not no-deps (compile-seq com/core-forms-seq))
85-
(if as-lib (println "-- CLJS/LUA " (second (nsd :form))))
87+
(if-not no-deps (println (slurp com/core-lib-path)))
88+
(if as-lib (emit-lib-header (second (nsd :form))))
8689
;; Compile main file and deps
8790
((if (and nsd (not no-deps)) compile-root-file compile-file) file optmap)))
8891

@@ -104,12 +107,22 @@
104107
(io/writer o)))
105108

106109
(defn -main [src-file & {:keys [out-file as-lib] :as optmap}]
107-
(ana/with-core-macros "/cljs/lua/core"
108110
(binding [ana/*cljs-ns* 'cljs.user
109111
ana/*cljs-static-fns* true
110112
comp/*ns-emit-require* false]
111113
(let [src-file (io/file src-file)]
112114
(if (.isDirectory src-file)
113115
(println "Input must be a cljsc file !")
114116
(binding [*out* (mk-out-file src-file optmap)]
115-
(-compile src-file (if as-lib (assoc optmap :no-deps true) optmap))))))))
117+
(-compile src-file (if as-lib (assoc optmap :no-deps true) optmap)))))))
118+
119+
(defn compile-core []
120+
(let [core-lib (io/file com/core-lib-path)]
121+
(when-not (.exists core-lib)
122+
(println "Compiling core ...")
123+
(binding [ana/*cljs-ns* 'cljs.user
124+
ana/*cljs-static-fns* true
125+
comp/*ns-emit-require* false
126+
*out* (io/writer core-lib)]
127+
(emit-lib-header 'cljs.core)
128+
(compile-seq com/core-forms-seq)))))

src/cljs/lua/repl.clj

Lines changed: 70 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -50,76 +50,79 @@
5050
(.waitFor (.exec (Runtime/getRuntime) (str "mkfifo " pipe-path)))
5151
(File. pipe-path)))
5252

53-
(defn -main [args]
54-
(ana/with-core-macros "/cljs/lua/core"
55-
(println "Cljs/Lua repl")
56-
(binding [ana/*cljs-ns* 'cljs.user
57-
ana/*cljs-static-fns* true
58-
*repl-verbose* false
59-
*repl-exec* true
60-
*lua-interp* (conf/get :repl :lua-runtime)]
61-
(let [;; Lua subprocess
62-
pb (ProcessBuilder. [*lua-interp* "cljs/exec_server.lua"])
63-
lua-process (.start pb)
64-
65-
;; Read lua stdout
66-
rdr (io/reader (.getInputStream lua-process))
53+
(defn -main [& args]
54+
(println "Cljs/Lua repl")
55+
(binding [ana/*cljs-ns* 'cljs.user
56+
ana/*cljs-static-fns* true
57+
*repl-verbose* false
58+
*repl-exec* true
59+
*lua-interp* (conf/get :repl :lua-runtime)]
60+
(let [;; Lua subprocess
61+
pb (ProcessBuilder. [*lua-interp* "cljs/exec_server.lua"])
62+
lua-process (.start pb)
63+
64+
;; Read lua stdout
65+
rdr (io/reader (.getInputStream lua-process))
6766

68-
;; Named pipes to communicate with lua subproc
69-
pipe-in (create-named-pipe "ltj")
70-
pipe-out (create-named-pipe "jtl")
71-
pipe-rdr (future (io/reader pipe-in))
72-
pipe-wr (future (io/writer pipe-out))
67+
;; Named pipes to communicate with lua subproc
68+
pipe-in (create-named-pipe "ltj")
69+
pipe-out (create-named-pipe "jtl")
70+
pipe-rdr (future (io/reader pipe-in))
71+
pipe-wr (future (io/writer pipe-out))
7372

74-
;; Function to analyze a form, emit lua code,
75-
;; pass it to the lua subproc, and get back the result
76-
eval-form (fn [env form]
77-
(let [lua-code (with-out-str (comp/emit (ana/analyze env form)))]
78-
(when *repl-verbose*
79-
(println "---- LUA CODE ----")
80-
(println lua-code))
81-
(when *repl-exec*
82-
(binding [*out* @pipe-wr]
83-
(println (json/json-str {:action :exec :body lua-code})))
84-
(let [resp (json/read-json (.readLine @pipe-rdr))]
85-
(if (= (:status resp) "OK")
86-
(when *repl-show-result* (println (:body resp)))
87-
(do
88-
(println "ERROR : " (:body resp))
89-
(when *error-fatal?*
90-
(println lua-code)
91-
)))))))]
73+
;; Function to analyze a form, emit lua code,
74+
;; pass it to the lua subproc, and get back the result
75+
eval-form (fn [env form]
76+
(let [lua-code (with-out-str (comp/emit (ana/analyze env form)))]
77+
(when *repl-verbose*
78+
(println "---- LUA CODE ----")
79+
(println lua-code))
80+
(when *repl-exec*
81+
(binding [*out* @pipe-wr]
82+
(println (json/json-str {:action :exec :body lua-code})))
83+
(let [resp (json/read-json (.readLine @pipe-rdr))]
84+
(if (= (:status resp) "OK")
85+
(when *repl-show-result* (println (:body resp)))
86+
(do
87+
(println "ERROR : " (:body resp))
88+
(when *error-fatal?*
89+
(println lua-code)
90+
)))))))]
9291

93-
;; Redirect everything from subprocess stdout to own stdout
94-
(.start (Thread. (fn [] (while true (let [l (.readLine rdr)] (when l (println l)))))))
92+
;; Redirect everything from subprocess stdout to own stdout
93+
(.start (Thread. (fn [] (while true (let [l (.readLine rdr)] (when l (println l)))))))
9594

96-
(try (do
97-
(.exitValue lua-process)
98-
(println "Lua subprocess has exited prematurely, verify you have lua installed, and required libraries : lua-json and lua-bitops")
99-
(System/exit 0))
100-
(catch Exception e))
95+
(try (do
96+
(.exitValue lua-process)
97+
(println "Lua subprocess has exited prematurely, verify you have lua installed, and required libraries : lua-json and lua-bitops")
98+
(System/exit 0))
99+
(catch Exception e))
101100

102-
;; Send it the two pipes names
103-
(binding [*out* (PrintWriter. (.getOutputStream lua-process))]
104-
(println (.getCanonicalPath pipe-in))
105-
(println (.getCanonicalPath pipe-out)))
101+
;; Send it the two pipes names
102+
(binding [*out* (PrintWriter. (.getOutputStream lua-process))]
103+
(println (.getCanonicalPath pipe-in))
104+
(println (.getCanonicalPath pipe-out)))
106105

107-
;; Eval core.cljs forms
108-
(binding [*repl-verbose* false
109-
*repl-show-result* false
110-
*error-fatal?* true]
111-
(eval-core-forms eval-form -1))
112-
113-
;; Eval common ns forms
114-
(eval-form (nenv) '(ns cljs.user))
115-
116-
;; Repl loop
117-
(while true
118-
(.print System/out (str ana/*cljs-ns* " > "))
119-
(.flush (System/out))
120-
(let [env (nenv)
121-
form (read)
122-
special-fn? (and (seq? form) (contains? special-fns-set (first form)))]
123-
(if special-fn?
124-
(println (apply (special-fns (first form)) eval-form (rest form)))
125-
(eval-form env form))))))))
106+
;; Eval core.cljs forms
107+
(binding [*repl-verbose* false
108+
*repl-show-result* false
109+
*error-fatal?* true]
110+
(eval-core-forms eval-form -1))
111+
112+
;; Eval common ns forms
113+
(eval-form (nenv) '(ns cljs.user))
114+
115+
;; Repl loop
116+
(while true
117+
(.print System/out (str ana/*cljs-ns* " > "))
118+
(.flush (System/out))
119+
(let [env (nenv)
120+
form (read)
121+
special-fn? (and (seq? form) (contains? special-fns-set (first form)))]
122+
(if special-fn?
123+
(println (apply (special-fns (first form)) eval-form (rest form)))
124+
(try (eval-form env form)
125+
(catch Exception e
126+
(.printStackTrace e))
127+
(catch AssertionError a
128+
(.printStackTrace a)))))))))

0 commit comments

Comments
 (0)