Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another bunch of fixes and Clojure stuff #165

Merged
merged 18 commits into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion browserify.wisp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns browserify (:require [browserify]))

(-> "engine/browser backend/javascript/writer analyzer ast compiler expander reader repl runtime sequence string"
(-> "engine/browser backend/javascript/writer analyzer ast compiler expander reader runtime sequence string"
(.split " ")
(.reduce (fn [bundler k] (.require bundler (str "./" k) {:expose (str "wisp/" k)}))
(browserify "./engine/browser.js"))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@
"base64-encode": "~1.0.1",
"commander": ">=2.2.0",
"escodegen": "git://github.com/Constellation/escodegen.git#41fbbe5058849b5e082542c5cfce76c2d67792e6",
"minify": "2.0.13"
"minify": "3.0.5"
}
}
3 changes: 2 additions & 1 deletion src/ast.wisp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns wisp.ast
(:require [wisp.sequence :refer [list? sequential? first second count
last map vec repeat]]
identity-set? last map vec repeat]]
[wisp.string :refer [split join]]
[wisp.runtime :refer [nil? vector? number? string? boolean?
object? date? re-pattern? dictionary?
Expand Down Expand Up @@ -184,6 +184,7 @@
(str key " " value)))
x))
"}")
(identity-set? x) (str "#{" (join " " (map #(pr-str % (inc offset)) (vec x))) "}")
(sequential? x) (str "(" (join " " (map #(pr-str % (inc offset))
(vec x))) ")")
(re-pattern? x) (str "#\"" (join "\\/" (split (.-source x) "/")) "\"")
Expand Down
9 changes: 4 additions & 5 deletions src/engine/browser.wisp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@

(defn __main__ [ev]
; hoist wisp builtins into the global window context
(.map [_wisp_runtime _wisp_sequence _wisp_string]
(fn [f]
(.map (.keys Object f)
(fn [k]
(set! (get window k) (get f k))))))
(.forEach [_wisp_string _wisp_sequence _wisp_runtime]
(fn [f]
(.forEach (.keys Object f)
#(set! (get window %) (get f %)))))
;(console.log "running __main__")
; find all the script tags on the page
(let [scripts (document.getElementsByTagName "script")]
Expand Down
56 changes: 49 additions & 7 deletions src/expander.wisp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns wisp.expander
"wisp syntax and macro expander module"
(:require [wisp.ast :refer [meta with-meta symbol? keyword?
quote? symbol namespace name
quote? symbol namespace name gensym
unquote? unquote-splicing?]]
[wisp.sequence :refer [list? list conj partition seq
empty? map vec every? concat
Expand Down Expand Up @@ -264,14 +264,24 @@
"Thread first macro"
[& operations]
(reduce
(fn [form operation]
(cons (first operation)
(cons form (rest operation))))
(first operations)
(map #(if (list? %) % `(~%))
(rest operations))))
(fn [form operation]
(cons (first operation)
(cons form (rest operation))))
(first operations)
(map #(if (list? %) % `(~%))
(rest operations))))
(install-macro! :-> expand-thread-first)

(defn expand-thread-last

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no test for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't found a single test for existing macros, so I didn't add any for new ones. Though I guess it wouldn't hurt adding tests for wisp.expander

"Thread last macro"
[& operations]
(reduce
(fn [form operation] (concat operation [form]))
(first operations)
(map #(if (list? %) % `(~%))
(rest operations))))
(install-macro! :->> expand-thread-last)

(defn expand-cond
"Takes a set of test/expr pairs. It evaluates each test one at a
time. If a test returns logical true, cond evaluates and returns
Expand Down Expand Up @@ -335,3 +345,35 @@
[& body]
`(.call lazy-seq nil false (fn [] ~@body)))
(install-macro :lazy-seq expand-lazy-seq)


(defn expand-when
"Evaluates test. If logical true, evaluates body in an implicit do."
[test & body]
`(if ~test (do ~@body)))
(install-macro :when expand-when)

(defn expand-when-not
"Evaluates test. If logical false, evaluates body in an implicit do."
[test & body]
`(when (not ~test) ~@body))
(install-macro :when-not expand-when-not)


(defn expand-if-let
"bindings => binding-form test
body => [then else]
If test is true, evaluates then with binding-form bound to the value of
test, if not, yields else."
[bindings then else*]
(let [name (first bindings), test (second bindings), sym (gensym)]
LeXofLeviafan marked this conversation as resolved.
Show resolved Hide resolved
`(let [~sym ~test]
(if ~sym (let [~name ~sym] ~then) ~else*))))
LeXofLeviafan marked this conversation as resolved.
Show resolved Hide resolved
(install-macro :if-let expand-if-let)

(defn expand-when-let
"bindings => binding-form test
When test is true, evaluates body with binding-form bound to the value of test."
[bindings & body]
`(if-let ~bindings (do ~@body)))
(install-macro :when-let expand-when-let)
16 changes: 12 additions & 4 deletions src/reader.wisp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
as wisp data structures"
(:require [wisp.sequence :refer [list list? count empty? first second third
rest map vec cons conj rest concat last
butlast sort lazy-seq reduce]]
butlast sort reduce identity-set]]
[wisp.runtime :refer [odd? dictionary keys nil? inc dec vector? string?
number? boolean? object? dictionary? re-pattern
re-matches re-find str subs char vals =]]
Expand Down Expand Up @@ -325,7 +325,7 @@
(defn read-set
[reader _]
(let [form (read-delimited-list "}" reader true)]
(with-meta (concat ['set] form) (meta form))))
(with-meta (concat ['identity-set] form) (meta form))))

(defn read-number
[reader initch]
Expand Down Expand Up @@ -608,10 +608,18 @@
(reader-error
nil "Queue literal expects a vector for its elements.")))

(defn ^:private read-date
[date]
(if (string? date)
`(Date. ~date)
(reader-error
nil "Date literal expects a string as its representation.")))


(def **tag-table**
(dictionary :uuid read-uuid
:queue read-queue))
(dictionary :uuid read-uuid
:queue read-queue
:inst read-date))

(defn maybe-read-tagged-type
[reader initch]
Expand Down
16 changes: 11 additions & 5 deletions src/runtime.wisp
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,9 @@
(defn int
"Coerce to int by stripping decimal places."
[x]
(if (number? x)
(if (>= x 0)
(.floor Math x)
(.floor Math x))
(.charCodeAt x 0)))
(cond (number? x) (.floor Math x)
(string? x) (.charCodeAt x 0) ; not like in Clojure
:else 0)) ; like in Clojure

(defn subs
"Returns the substring of s beginning at start inclusive, and ending
Expand All @@ -276,6 +274,13 @@
(identical? (Number x) (Number y))))


(defn- ^boolean identity-set-equal?
[x y]
(and (instance? Set x)
(instance? Set y)
(identical? x.size y.size)
(.every (Array.from x) #(y.has %))))

(defn- ^boolean dictionary-equal?
[x y]
(and (object? x)
Expand Down Expand Up @@ -323,6 +328,7 @@
(.toString y)))
(number? x) (and (number? y) (identical? (.valueOf x)
(.valueOf y)))
(instance? Set x) (identity-set-equal? x y)
(fn? x) false
(boolean? x) false
(date? x) (date-equal? x y)
Expand Down
Loading