Skip to content

Commit f4150d9

Browse files
authored
Fix #596: fix unary division to produce reciprocal (#597)
1 parent 8dd5380 commit f4150d9

File tree

3 files changed

+50
-39
lines changed

3 files changed

+50
-39
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
[Squint](https://github.com/squint-cljs/squint): Light-weight ClojureScript dialect
44

5+
## v0.8.130 (2024-12-21)
6+
7+
- [#596](https://github.com/squint-cljs/squint/issues/596): fix unary division to produce reciprocal
8+
59
## v0.8.129 (2024-12-03)
610

711
- [#592](https://github.com/squint-cljs/squint/issues/592): fix `clj->js` to not process custom classes

src/squint/compiler_common.cljc

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@
6161
(drop 2 opts)]
6262
[nil (first opts) (rest opts)])]
6363
(cond->
64-
(-> (reduce (fn [template substitution]
65-
(str/replace-first template "~{}"
66-
(emit substitution (merge (assoc env :context :expr) env'))))
67-
template
68-
substitutions)
69-
(emit-return (merge env (meta expr))))
64+
(-> (reduce (fn [template substitution]
65+
(str/replace-first template "~{}"
66+
(emit substitution (merge (assoc env :context :expr) env'))))
67+
template
68+
substitutions)
69+
(emit-return (merge env (meta expr))))
7070
bool? bool-expr)))
7171

7272
(defn expr-env [env]
@@ -212,21 +212,25 @@
212212
(list operator (first args) (second args))
213213
(list* operator (rest args)))
214214
enc-env)
215-
(if (and (= '- operator)
216-
(= 1 acount))
217-
(str "-" (emit (first args) env))
218-
(-> (let [substitutions {'= "===" == "===" '!= "!=="
219-
'not= "!=="
220-
'+ "+"
221-
'bit-or "|"
222-
'bit-and "&"
223-
'js-mod "%"
224-
'js-?? "??"}]
225-
(str/join (str " " (or (substitutions operator)
226-
operator) " ")
227-
(map wrap-parens (emit-args env args))))
228-
(emit-return enc-env)
229-
(cond-> bool? (bool-expr)))))))
215+
(cond (and (= '- operator)
216+
(= 1 acount))
217+
(str "-" (emit (first args) env))
218+
(and (= '/ operator)
219+
(= 1 acount))
220+
(str "1 / " (emit (first args) env))
221+
:else
222+
(-> (let [substitutions {'= "===" == "===" '!= "!=="
223+
'not= "!=="
224+
'+ "+"
225+
'bit-or "|"
226+
'bit-and "&"
227+
'js-mod "%"
228+
'js-?? "??"}]
229+
(str/join (str " " (or (substitutions operator)
230+
operator) " ")
231+
(map wrap-parens (emit-args env args))))
232+
(emit-return enc-env)
233+
(cond-> bool? (bool-expr)))))))
230234

231235
(def core-vars (atom #{}))
232236

@@ -300,7 +304,7 @@
300304
(munged-name expr)))
301305
(some-> (maybe-core-var expr env) munge)
302306
(when (let [alias (get (:aliases current-ns) expr)]
303-
alias)
307+
alias)
304308
(str (when *repl*
305309
(str "globalThis." (munge *cljs-ns*) "."))
306310
(munged-name expr)))))
@@ -744,10 +748,10 @@
744748
true
745749
(emit-return (wrap-parens (apply str (interpose " && " (emit-args env more)))) env)))
746750

747-
(defmethod emit-special 'or [_type env [_ & more]]
748-
(if (empty? more)
749-
nil
750-
(emit-return (wrap-parens (apply str (interpose " || " (emit-args env more)))) env)))
751+
(defmethod emit-special 'or [_type env [_ & more]]
752+
(if (empty? more)
753+
nil
754+
(emit-return (wrap-parens (apply str (interpose " || " (emit-args env more)))) env)))
751755

752756
(defmethod emit-special 'while [_type env [_while test & body]]
753757
(str "while (" (emit test) ") { \n"
@@ -1190,30 +1194,30 @@ break;}" body)
11901194
(assoc :children children))))
11911195
env))
11921196
(let [ret #_(format "<%s%s>%s</%s>"
1193-
tag-name
1194-
(cc/jsx-attrs attrs env)
1195-
(let [env (expr-env env)]
1196-
(str/join "" (map #(emit % env) elts)))
1197-
tag-name)
1197+
tag-name
1198+
(cc/jsx-attrs attrs env)
1199+
(let [env (expr-env env)]
1200+
(str/join "" (map #(emit % env) elts)))
1201+
tag-name)
11981202
(str
11991203
(if (and html? fragment?)
1200-
""
1201-
(str "<"
1202-
tag-name
1203-
(jsx-attrs attrs env)
1204-
">"))
1204+
""
1205+
(str "<"
1206+
tag-name
1207+
(jsx-attrs attrs env)
1208+
">"))
12051209
(let [env (expr-env env)]
12061210
(str/join "" (map #(emit % env) elts)))
12071211
(if (and html? (or fragment?
12081212
(void-tag? tag-name)))
1209-
""
1210-
(str "</" tag-name ">")))]
1213+
""
1214+
(str "</" tag-name ">")))]
12111215
(when outer-html?
12121216
(when need-html-import
12131217
(reset! need-html-import true)))
12141218
(emit-return
12151219
(cond->> ret
1216-
outer-html?
1220+
outer-html?
12171221
(format "%s`%s`"
12181222
(if-let [t (:tag (meta expr))]
12191223
(emit t (expr-env (dissoc env :jsx :html)))

test/squint/compiler_test.cljs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,5 +2345,8 @@ new Foo();")
23452345
(is (true?
23462346
(jsv! "(= \"foo\" (clj->js \"foo\"))"))))
23472347

2348+
(deftest divide-single-arg-test
2349+
(is (= 0.5 (jsv! "(/ 2)"))))
2350+
23482351
(defn init []
23492352
(t/run-tests 'squint.compiler-test 'squint.jsx-test 'squint.string-test 'squint.html-test))

0 commit comments

Comments
 (0)