Dotted pairs reading/printing correctly

This commit is contained in:
Simon Brooke 2019-08-15 16:46:34 +01:00
parent 0321401c2a
commit d01bb68c0f
2 changed files with 15 additions and 2 deletions

View file

@ -54,7 +54,7 @@
;; Lisp 1.5 supported octal as well as decimal and scientific notation
"number := integer | decimal | scientific | octal;
integer := #'-?[1-9][0-9]*';
decimal := #'-?[1-9][0-9]*\\.?[0-9]*' | #'0\\.[0-9]*';
decimal := #'-?[1-9][0-9]*\\.?[0-9]*' | #'0.[0-9]*';
scientific := coefficient e exponent;
coefficient := decimal;
exponent := integer;
@ -248,7 +248,7 @@
(generate (nth p 2)))
:exponent (generate (second p))
:fncall (gen-fn-call p)
:mvar (upper-case (second p))
:mvar (symbol (upper-case (second p)))
:octal (let [n (read-string (strip-leading-zeros (second p) "0"))
scale (generate (nth p 2))]
(* n (expt 8 scale)))

View file

@ -1,6 +1,7 @@
(ns beowulf.sexpr-test
(:require [clojure.math.numeric-tower :refer [abs]]
[clojure.test :refer :all]
[beowulf.cons-cell :refer :all]
[beowulf.read :refer [parse simplify generate]]
[beowulf.print :refer :all]))
@ -74,3 +75,15 @@
actual (generate (simplify (parse "0.6E2")))]
(is (< (abs (- actual expected)) 0.0001))))
(deftest dotted-pair-tests
(testing "Reading dotted pairs"
(let [expected "(A . B)"
actual (prin (generate (simplify (parse expected))))]
(is (= actual expected)))
(let [expected "(A B C . D)"
actual (prin (generate (simplify (parse expected))))]
(is (= actual expected)))
(let [expected "(A B (C . D) E)"
actual (prin (generate (simplify (parse expected))))]
(is (= actual expected)))
))