Skip to content

Commit

Permalink
Fix logic precedence
Browse files Browse the repository at this point in the history
Fixes #274
  • Loading branch information
borkdude authored and github-actions[bot] committed Dec 27, 2022
1 parent 3d78395 commit de40e1a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

[Squint](https://github.com/squint-cljs/squint): ClojureScript syntax to JavaScript compiler

## 0.0.7

[#274](https://github.com/squint-cljs/squint/issues/274): fix logic precedence by wrapping in parens

## 0.0.6

Add preliminary Node.js API in `node.js`
Expand Down
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
babashka/process {:mvn/version "0.1.7"}
org.babashka/cli {:mvn/version "0.4.37"}
org.babashka/sci {:mvn/version "0.6.37"}
io.github.squint-cljs/compiler-common {:git/sha "01fd1c8be7d5677dbf2502e159131b83d88d8e47"
io.github.squint-cljs/compiler-common {:git/sha "11c39efc278e83fe556c36941d9bfd27d8d5a187"
:deps/root "compiler-common"}}
:aliases
{:dev {}
Expand Down
22 changes: 11 additions & 11 deletions src/squint/compiler.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
[squint.compiler-common :as cc :refer [#?(:cljs Exception)
#?(:cljs format)
*aliases* *cljs-ns* *excluded-core-vars* *imported-vars* *public-vars* *repl*
comma-list emit emit-args emit-infix emit-repl emit-special emit-wrap escape-jsx
comma-list emit emit-args emit-infix emit-repl emit-special emit-return escape-jsx
expr-env infix-operator? prefix-unary? statement suffix-unary?]]
[squint.internal.deftype :as deftype]
[squint.internal.destructure :refer [core-let]]
Expand All @@ -28,7 +28,7 @@


(defmethod emit #?(:clj clojure.lang.Keyword :cljs Keyword) [expr env]
(-> (emit-wrap (str (pr-str (subs (str expr) 1))) env)
(-> (emit-return (str (pr-str (subs (str expr) 1))) env)
(emit-repl env)))

(def special-forms (set ['var '. 'if 'funcall 'fn 'fn* 'quote 'set!
Expand Down Expand Up @@ -100,13 +100,13 @@
(str (emit arg) operator))

(defmethod emit-special 'quote [_ env [_ form]]
(emit-wrap (emit form (expr-env (assoc env :quote true))) env))
(emit-return (emit form (expr-env (assoc env :quote true))) env))

(defmethod emit-special 'not [_ env [_ form]]
(emit-wrap (str "!" (emit form (expr-env env))) env))
(emit-return (str "!" (emit form (expr-env env))) env))

(defmethod emit-special 'js/typeof [_ env [_ form]]
(emit-wrap (str "typeof " (emit form (expr-env env))) env))
(emit-return (str "typeof " (emit form (expr-env env))) env))

(defmethod emit-special 'letfn* [_ env [_ form & body]]
(let [bindings (take-nth 2 form)
Expand All @@ -118,7 +118,7 @@
(emit let env)))

(defmethod emit-special 'quote [_ env [_ form]]
(emit-wrap (emit form (expr-env (assoc env :quote true))) env))
(emit-return (emit form (expr-env (assoc env :quote true))) env))

#_(defmethod emit-special 'let* [_type enc-env [_let bindings & body]]
(emit-let enc-env bindings body false))
Expand Down Expand Up @@ -167,7 +167,7 @@
(emit test env)
(emit then env)
(emit else env)))
(emit-wrap env))
(emit-return env))
(str (format "if (%s) {\n"
(emit test (assoc env :context :expr)))
(emit then env)
Expand Down Expand Up @@ -288,14 +288,14 @@
(symbol "")
tag-name)
tag-name (emit tag-name (expr-env (dissoc env :jsx)))]
(emit-wrap (format "<%s%s>%s</%s>"
(emit-return (format "<%s%s>%s</%s>"
tag-name
(jsx-attrs attrs env)
(let [env (expr-env env)]
(str/join " " (map #(emit % env) elts)))
tag-name)
env))
(-> (emit-wrap (format "[%s]"
(-> (emit-return (format "[%s]"
(str/join ", " (emit-args env expr))) env)
(emit-repl env))))

Expand All @@ -314,13 +314,13 @@
(emit (val pair) expr-env)))
keys (str/join ", " (map mk-pair (seq expr)))]
(escape-jsx (-> (format "({ %s })" keys)
(emit-wrap env))
(emit-return env))
env*)))

(defmethod emit #?(:clj clojure.lang.PersistentHashSet
:cljs PersistentHashSet)
[expr env]
(emit-wrap
(emit-return
(format "new Set([%s])"
(str/join ", " (emit-args (expr-env env) expr)))
env))
Expand Down
3 changes: 3 additions & 0 deletions test/squint/compiler_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,9 @@
(is (= 2 (jsv! '(do (defn foo [a b] (and a b)) (foo 1 2)))))
(is (= 1 (jsv! '(do (defn foo [a b] (or a b)) (foo 1 2))))))

(deftest logic-precedence
(is (false? (jsv! '(and (or true false) false)))))

(deftest multiple-arity-infix
(is (true? (jsv! '(> 5 4 3 2 1))))
(is (true? (jsv! '(> 5 4 3))))
Expand Down

0 comments on commit de40e1a

Please sign in to comment.