Skip to content

Add support for ratio #37

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

Merged
merged 1 commit into from
Mar 13, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 15 additions & 1 deletion parseclj-lex.el
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,20 @@ S goes through three transformations:
((eq first-char ?o) (string-to-number (substring c 2) 8))
(t first-char))))

(defun parseclj-lex--number-value (number-str)
"Parse the NUMBER-STR to an Elisp number."
(let ((ratio (split-string number-str "/")))
(if (= 2 (length ratio))
(let ((numerator (string-to-number (car ratio)))
(denominator (string-to-number (cadr ratio))))
(/ numerator (float denominator)))
(string-to-number number-str))))

(defun parseclj-lex--leaf-token-value (token)
"Parse the given leaf TOKEN to an Emacs Lisp value."
(let ((token-type (parseclj-lex-token-type token)))
(cond
((eq :number token-type) (string-to-number (alist-get :form token)))
((eq :number token-type) (parseclj-lex--number-value (alist-get :form token)))
((eq :nil token-type) nil)
((eq :true token-type) t)
((eq :false token-type) nil)
Expand Down Expand Up @@ -254,6 +263,11 @@ S goes through three transformations:
(when (eq (char-after (point)) ?N)
(right-char))

;; clojure.lang.Ratio
(when (eq (char-after (point)) ?/)
(right-char)
(parseclj-lex-skip-number))

(let ((char (char-after (point))))
(if (and char (or (and (<= ?a char) (<= char ?z))
(and (<= ?A char) (<= char ?Z))
Expand Down
7 changes: 7 additions & 0 deletions test/parseclj-lex-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@
(:form . "0xff00AA")
(:pos . 1)))))

(with-temp-buffer
(insert "12/34")
(goto-char 1)
(should (equal (parseclj-lex-next) '((:token-type . :number)
(:form . "12/34")
(:pos . 1)))))

(with-temp-buffer
(insert "#?(:clj 1 :cljs 2)")
(goto-char 1)
Expand Down
12 changes: 12 additions & 0 deletions test/parseclj-test-data.el
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@
(:form . ":foo-bar")
(:value . :foo-bar))))))

"ratio"
(parseclj-alist
:tags '(:edn-roundtrip)
:source "12/34"
:edn '(0.35294117647058826)
:ast '((:node-type . :root)
(:position . 1)
(:children ((:node-type . :number)
(:position . 1)
(:form . "12/34")
(:value . 0.35294117647058826)))))

"vector"
(parseclj-alist
:tags '(:edn-roundtrip)
Expand Down
10 changes: 10 additions & 0 deletions test/parseclj-unparse-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@
(:form . ":foo-bar")
(:value . :foo-bar)))))))))

(ert-deftest parseclj-unparse-clojure-ratio ()
(should (equal "12/34"
(parseclj-unparse-clojure-to-string
'((:node-type . :root)
(:position . 1)
(:children ((:node-type . :number)
(:position . 1)
(:form . "12/34")
(:value . 0.35294117647058826))))))))

(ert-deftest parseclj-unparse-clojure-vector ()
(should (equal "[123]"
(parseclj-unparse-clojure-to-string
Expand Down