Skip to content

Commit 58a2cc0

Browse files
committed
Merge remote-tracking branch 'origin/master' into experiment/protocols
Conflicts: src/compiler.wisp
2 parents 9ab7699 + 59cdcc2 commit 58a2cc0

File tree

6 files changed

+64
-50
lines changed

6 files changed

+64
-50
lines changed

Readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ powerful [macros] that make it the easiest way to write JavaScript.
2020

2121
# Try _Wisp_
2222

23-
You can try _wisp_ on your browser by [trying the interactive compiler](http://jeditoolkit.com/try-wisp/) or [an online REPL](http://jeditoolkit.com/interactivate-wisp) with syntax highlighting.
23+
You can try _wisp_ on your browser by [trying the interactive compiler](http://jeditoolkit.com/try-wisp/)
24+
([repo](https://github.com/Gozala/try-wisp)) or [an online REPL](http://jeditoolkit.com/interactivate-wisp)
25+
with syntax highlighting.
2426

2527
# Install
2628

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
},
4949
"dependencies": {
5050
"escodegen": "git://github.com/Constellation/escodegen.git#master",
51-
"base64-encode": "~1.0.1"
51+
"base64-encode": "~1.0.1",
52+
"commander": ">=2.2.0"
5253
}
5354
}

src/analyzer.wisp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@
625625
(install-special! :fn analyze-fn)
626626

627627
(defn parse-references
628-
"Takes part of namespace difinition and creates hash
628+
"Takes part of namespace definition and creates hash
629629
of reference forms"
630630
[forms]
631631
(reduce (fn [references form]
@@ -763,7 +763,7 @@
763763
:context - One of the following :statement, :expression, :return. That
764764
information is included in resulting nodes and is meant for
765765
writer that may output different forms based on context.
766-
:ns - Namespace of the forms being analized.
766+
:ns - Namespace of the forms being analyzed.
767767
768768
Analyzer performs all the macro & syntax expansions and transforms form
769769
into AST node of an expression. Each such node contains at least following

src/compiler.wisp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
(:require [wisp.analyzer :refer [analyze]]
33
[wisp.reader :refer [read* read push-back-reader]]
44
[wisp.string :refer [replace]]
5-
[wisp.sequence :refer [map conj cons vec first rest empty? count]]
5+
[wisp.sequence :refer [map reduce conj cons vec first rest empty? count]]
66
[wisp.runtime :refer [error? =]]
7-
[wisp.ast :refer [name symbol]]
7+
[wisp.ast :refer [name symbol pr-str]]
88

99
[wisp.backend.escodegen.generator :refer [generate]
1010
:rename {generate generate-js}]
@@ -91,9 +91,15 @@
9191
(:ast ast))))
9292
(catch error {:error error})))
9393

94+
expansion (if (identical? :expansion (:print options))
95+
(reduce (fn [result item]
96+
(str result (pr-str (.-form item)) "\n"))
97+
"" (.-ast ast)))
98+
9499
result {:source-uri source-uri
95100
:ast (:ast ast)
96-
:forms (:forms forms)}]
101+
:forms (:forms forms)
102+
:expansion expansion}]
97103
(conj options output result))))
98104

99105
(defn evaluate

src/reader.wisp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@
291291

292292
(defn read-unmatched-delimiter
293293
[rdr ch]
294-
(reader-error rdr "Unmached delimiter " ch))
294+
(reader-error rdr "Unmatched delimiter " ch))
295295

296296
(defn read-list
297297
[reader _]

src/wisp.wisp

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
(:require [fs :refer [createReadStream]]
55
[path :refer [basename dirname join resolve]]
66
[module :refer [Module]]
7+
[commander]
8+
[wisp.package :refer [version]]
79

810
[wisp.string :refer [split join upper-case replace]]
911
[wisp.sequence :refer [first second last count reduce rest
@@ -15,41 +17,12 @@
1517
[wisp.ast :refer [pr-str name]]
1618
[wisp.compiler :refer [compile]]))
1719

18-
19-
(defn flag?
20-
[param]
21-
;; HACK: Workaround for segfault #6691
22-
(identical? (subs param 0 2) (name :--)))
23-
24-
(defn flag->key
25-
[flag]
26-
(subs flag 2))
27-
28-
;; Just mungle all the `--param value` pairs into global *env* hash.
29-
(defn parse-params
30-
[params]
31-
(loop [input params
32-
output {}]
33-
(if (empty? input)
34-
output
35-
(let [name (first input)
36-
value (second input)]
37-
(if (flag? name)
38-
(if (or (nil? value) (flag? value))
39-
(recur (rest input)
40-
(assoc output (flag->key name) true))
41-
(recur (drop 2 input)
42-
(assoc output (flag->key name) value)))
43-
(recur (rest input)
44-
output))))))
45-
46-
47-
4820
(defn compile-stdin
4921
[options]
5022
(with-stream-content process.stdin
5123
compile-string
52-
options))
24+
(conj {} options)))
25+
;; (conj {:source-uri options}) causes segfault for some reason
5326

5427
(defn compile-file
5528
[path options]
@@ -61,11 +34,12 @@
6134
[source options]
6235
(let [channel (or (:print options) :code)
6336
output (compile source options)
64-
content (if (= channel :code)
65-
(:code output)
66-
(JSON.stringify (get output channel) 2 2))]
67-
(.write process.stdout (or content "nil"))
68-
(if (:error output) (throw (:error output)))))
37+
content (cond
38+
(= channel :code) (:code output)
39+
(= channel :expansion) (:expansion output)
40+
:else (JSON.stringify (get output channel) 2 2))]
41+
(.write process.stdout (or content "nil"))
42+
(if (:error output) (throw (.-error output)))))
6943

7044
(defn with-stream-content
7145
[input resume options]
@@ -82,12 +56,43 @@
8256
;; https://github.com/joyent/node/blob/master/lib/module.js#L489-493
8357
(Module._load (resolve path) null true))
8458

59+
(defmacro ->
60+
[& operations]
61+
(reduce
62+
(fn [form operation]
63+
(cons (first operation)
64+
(cons form (rest operation))))
65+
(first operations)
66+
(rest operations)))
67+
68+
(defn parse-params
69+
[params]
70+
(let [options (-> commander
71+
(.version version)
72+
(.usage "[options] <file ...>")
73+
(.option "-r, --run"
74+
"compile and execute the file (same as wisp path/to/file.wisp)")
75+
(.option "-c, --compile"
76+
"compile given file and prints to stdout")
77+
(.option "-i, --interactive"
78+
"run an interactive wisp REPL (same as wisp with no params)")
79+
(.option "--print <format>"
80+
"use custom print output `expansion`,`forms`, `ast`, `js-ast` or (default) `code`"
81+
str
82+
"code")
83+
(.option "--no-map"
84+
"disable source map generation")
85+
(.parse params))]
86+
(conj {:no-map (not (:map options))}
87+
options)))
8588

8689
(defn main
8790
[]
88-
(let [options (parse-params (drop 2 process.argv))]
89-
(cond (not process.stdin.isTTY) (compile-stdin options)
90-
(< (count process.argv) 3) (start-repl)
91-
(and (= (count process.argv) 3)
92-
(not (flag? (last process.argv)))) (run (last process.argv))
93-
(:compile options) (compile-file (:compile options) options))))
91+
(let [options (parse-params process.argv)
92+
path (aget options.args 0)]
93+
(cond options.run (run path)
94+
(not process.stdin.isTTY) (compile-stdin options)
95+
options.interactive (start-repl)
96+
options.compile (compile-file path options)
97+
path (run path)
98+
:else (start-repl))))

0 commit comments

Comments
 (0)