Skip to content

Commit

Permalink
Add support for dot-namespace qualification.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsbw committed Jan 31, 2024
1 parent b76f6bd commit 0986ef5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion project.clj
@@ -1,4 +1,4 @@
(defproject rm-hull/infix "0.4.2"
(defproject rm-hull/infix "0.4.3"
:description "A small Clojure library for expressing LISP expressions as infix rather than prefix notation"
:url "https://github.com/rm-hull/infix"
:license {
Expand Down
6 changes: 3 additions & 3 deletions src/infix/grammar.clj
Expand Up @@ -38,7 +38,7 @@
;
; function ::= envref expression | envref "(" <empty> | expression { "," expression } ")".
; ternary ::= expression "?" expression ":" expression.
; envref ::= letter | "_" { letter | digit | "_" }.
; envref ::= letter | "_" { letter | digit | "_" | "." }.
; var ::= envref.
; boolean :: = "true" | "false"
; number ::= integer | decimal | rational | binary | hex
Expand All @@ -56,7 +56,7 @@
; TODO: allow unicode/utf8 characters
(def letter (from-re #"[a-zA-Z]"))

(def alpha-num (any-of letter digit (match "_")))
(def alpha-num (any-of letter digit (match "_") (match ".")))

(def digits
(m/do*
Expand All @@ -65,7 +65,7 @@

(def envref
(m/do*
(fst <- (any-of letter (match "_")))
(fst <- (any-of letter (match "_") (match ".")))
(rst <- (token (many alpha-num)))
(m/return (let [kw (keyword (strip-location (cons fst rst)))]
(fn [env]
Expand Down
8 changes: 5 additions & 3 deletions test/infix/grammar_test.clj
Expand Up @@ -37,10 +37,11 @@
(<= (Math/abs (- x y)) (* scale epsilon)))))

(deftest check-var
(let [env {:x 32 :something_else 19}]
(let [env {:x 32 :something_else 19 :dot.var 608}]
(is (thrown? ParseException (parse-all var "54")))
(is (= 32 ((parse-all var "x") env)))
(is (= 19 ((parse-all var "something_else") env)))
(is (= 608 ((parse-all var "dot.var") env)))
(is (thrown? IllegalStateException ((parse-all var "fred") env)))))

(deftest check-integer
Expand Down Expand Up @@ -110,12 +111,13 @@
(is (thrown? clojure.lang.ArityException ((parse-all function "sqrt(7, 5)") env)))))

(deftest check-expression
(let [env (merge base-env {:t 0.324 :x_y_z 3 :_a 4})]
(let [env (merge base-env {:t 0.324 :x_y_z 3 :_a 4 :_a.b 2})]
(is (float= 1.4176457261295824 ((parse-all expression "sin(2 * t) + 3 * cos(4 * t)") env)))
(is (thrown? IllegalStateException ((parse-all expression "3 + 4") {})))
(is (= 43 ((parse-all expression "3 + 5 * 8") env)))
(is (= 64 ((parse-all expression "(3 + 5) * 8") env)))
(is (= 10 ((parse-all expression "x_y_z * 2 + _a") env)))))
(is (= 10 ((parse-all expression "x_y_z * 2 + _a") env)))
(is (= 8 ((parse-all expression "x_y_z * 2 + _a / _a.b") env)))))

(deftest check-ternary-op
(let [env (merge base-env {:t 150 :x 10 :y 20})]
Expand Down

0 comments on commit 0986ef5

Please sign in to comment.