Modularised the reader; some general improvement

This commit is contained in:
Simon Brooke 2023-03-26 11:50:56 +01:00
parent 9b532d39a8
commit b5e418118b
14 changed files with 594 additions and 556 deletions

View file

@ -1,8 +1,9 @@
(ns beowulf.bootstrap-test
(:require [clojure.math.numeric-tower :refer [abs]]
[clojure.test :refer :all]
[beowulf.cons-cell :refer [make-beowulf-list make-cons-cell NIL T F]]
[beowulf.bootstrap :refer :all]
(:require [clojure.test :refer [deftest testing is]]
[beowulf.cons-cell :refer [make-cons-cell NIL T F]]
[beowulf.bootstrap :refer [APPEND ASSOC ATOM ATOM? CAR CAAAAR CADR
CADDR CADDDR CDR EQ EQUAL MEMBER
PAIRLIS SUBLIS SUBST]]
[beowulf.read :refer [gsp]]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -51,21 +52,6 @@
actual (ATOM? (gsp "(A B C D)"))]
(is (= actual expected) "A list is explicitly not an atom"))))
(deftest numberp-tests
(testing "NUMBERP"
(let [expected T
actual (NUMBERP 7)]
(is (= actual expected) "7 is a number"))
(let [expected T
actual (NUMBERP 3.14)]
(is (= actual expected) "3.14 is a number"))
(let [expected F
actual (NUMBERP NIL)]
(is (= actual expected) "NIL is not a number"))
(let [expected F
actual (NUMBERP (gsp "HELLO"))]
(is (= actual expected) "HELLO is not a number"))))
(deftest access-function-tests
(testing "CAR"
(let [expected 'A
@ -132,13 +118,18 @@
(let [expected 'T
actual (EQ 'FRED 'FRED)]
(is (= actual expected) "identical symbols"))
(let [expected 'F
(let [expected 'NIL
actual (EQ 'FRED 'ELFREDA)]
(is (= actual expected) "different symbols"))
(let [expected 'F
(let [expected 'T
l (gsp "(NOT AN ATOM)")
actual (EQ l l)]
(is (= actual expected) "identical lists (EQ is not defined for lists)")))
(is (= actual expected) "identically the same list"))
(let [expected 'NIL
l1 (gsp "(NOT AN ATOM)")
l2 (gsp "(NOT AN ATOM)")
actual (EQ l1 l2)]
(is (= actual expected) "different lists with the same content")))
(testing "equal"
(let [expected 'T
actual (EQUAL 'FRED 'FRED)]

View file

@ -1,11 +1,12 @@
(ns beowulf.cons-cell-test
(:require [clojure.test :refer :all]
[beowulf.cons-cell :refer :all]))
(:require [clojure.test :refer [deftest is testing]]
[beowulf.cons-cell :refer [make-beowulf-list make-cons-cell pretty-print]])
(:import [beowulf.cons_cell ConsCell]))
(deftest cons-cell-tests
(testing "make-cons-cell"
(let [expected "(A . B)"
actual (print-str (beowulf.cons_cell.ConsCell. 'A 'B))]
actual (print-str (ConsCell. 'A 'B (gensym "c")))]
(is (= actual expected) "Cons cells should print as cons cells, natch."))
(let [expected "(A . B)"
actual (print-str (make-cons-cell 'A 'B))]

View file

@ -1,8 +1,8 @@
(ns beowulf.core-test
(:require [clojure.java.io :refer [reader]]
[clojure.string :refer [split]]
[clojure.test :refer :all]
[beowulf.core :refer :all]))
[clojure.test :refer [deftest is testing]]
[beowulf.core :refer [-main repl stop-word]]))
;; (deftest a-test
;; (testing "FIXME, I fail."
@ -36,7 +36,7 @@
(testing "No flags"
(let [expected-greeting "Hider wilcuman. Béowulf is mín nama."
expected-quit-message (str "Sprecan '" stop-word "' tó laéfan")
expected-result #".*\(A \. B\)"
expected-result #".*\(3 \. 4\)"
expected-prompt "Sprecan:: "
expected-signoff "Færwell!"
;; anticipated output (note blank lines):
@ -45,11 +45,11 @@
; Sprecan 'STOP' tó laéfan
; Sprecan:: > (A . B)
; Sprecan:: > (3 . 4)
; Sprecan::
; Færwell!
[_ greeting _ _ quit-message _ result prompt signoff]
(with-open [r (reader (string->stream (str "cons[A; B]\n" stop-word)))]
(with-open [r (reader (string->stream (str "cons[3; 4]\n" stop-word)))]
(binding [*in* r]
(split (with-out-str (-main)) #"\n")))]
(is (= greeting expected-greeting))
@ -63,11 +63,11 @@
(let [expected-greeting "Hider wilcuman. Béowulf is mín nama."
expected-quit-message (str "Sprecan '" stop-word "' tó laéfan")
expected-error #"Unknown option:.*"
expected-result #".*\(A \. B\)"
expected-result #".*\(5 \. 6\)"
expected-prompt "Sprecan:: "
expected-signoff "Færwell!"
[_ greeting _ error quit-message _ result prompt signoff]
(with-open [r (reader (string->stream (str "cons[A; B]\n" stop-word)))]
(with-open [r (reader (string->stream (str "cons[5; 6]\n" stop-word)))]
(binding [*in* r]
(split (with-out-str (-main "--unknown")) #"\n")))]
(is (= greeting expected-greeting))

View file

@ -1,9 +1,8 @@
(ns beowulf.host-test
(:require [clojure.math.numeric-tower :refer [abs]]
[clojure.test :refer :all]
[beowulf.cons-cell :refer [make-beowulf-list make-cons-cell NIL T F]]
(:require [clojure.test :refer [deftest is testing]]
[beowulf.bootstrap :refer [CDR]]
[beowulf.host :refer :all]
[beowulf.cons-cell :refer [F make-beowulf-list NIL T]]
[beowulf.host :refer [DIFFERENCE NUMBERP PLUS2 RPLACA RPLACD TIMES2]]
[beowulf.read :refer [gsp]]))
(deftest destructive-change-test
@ -35,6 +34,21 @@
)
)
(deftest numberp-tests
(testing "NUMBERP"
(let [expected T
actual (NUMBERP 7)]
(is (= actual expected) "7 is a number"))
(let [expected T
actual (NUMBERP 3.14)]
(is (= actual expected) "3.14 is a number"))
(let [expected F
actual (NUMBERP NIL)]
(is (= actual expected) "NIL is not a number"))
(let [expected F
actual (NUMBERP (gsp "HELLO"))]
(is (= actual expected) "HELLO is not a number"))))
(deftest arithmetic-test
;; These are just sanity-test tests; they're by no means exhaustive.
(testing "PLUS2"

View file

@ -1,8 +1,6 @@
(ns beowulf.interop-test
(:require [clojure.test :refer :all]
[beowulf.cons-cell :refer [make-beowulf-list make-cons-cell NIL T F]]
(:require [clojure.test :refer [deftest is testing]]
[beowulf.bootstrap :refer [EVAL INTEROP QUOTE]]
[beowulf.host :refer :all]
[beowulf.read :refer [gsp]]))

View file

@ -1,9 +1,12 @@
(ns beowulf.mexpr-test
"These tests are taken generally from the examples on page 10 of
Lisp 1.5 Programmers Manual"
(:require [clojure.test :refer :all]
[beowulf.bootstrap :refer [*options*]]
[beowulf.read :refer [parse simplify generate gsp]]))
(:require [clojure.test :refer [deftest is testing]]
[beowulf.bootstrap :refer [*options*]]
[beowulf.read :refer [gsp]]
[beowulf.reader.generate :refer [generate]]
[beowulf.reader.parser :refer [parse]]
[beowulf.reader.simplify :refer [simplify]]))
;; These tests are taken generally from the examples on page 10 of
;; Lisp 1.5 Programmers Manual:
@ -39,13 +42,14 @@
;; Wrapping in a function call puts us into mexpr contest;
;; "T" would be interpreted as a sexpr, which would not be
;; quoted.
(let [expected "(ATOM (QUOTE A))"
(let [expected "(ATOM A)"
actual (print-str (gsp "atom[A]"))]
(is (= actual expected)
"Atoms should normally be quoted"))
(is (= actual expected)))
;; I'm not clear how `car[(A B C)]` should be translated, but
;; I suspect as (CAR (LIST 'A 'B 'C)).
;; I suspect as (CAR (LIST A B C)).
(let [expected "(CAR (LIST A B C))"
actual (print-str (gsp "car[(A B C)]"))]
(is (= actual expected)))
))
(deftest fncall-tests
@ -79,6 +83,6 @@
(deftest assignment-tests
(testing "Function assignment"
(let [expected "(SET (QUOTE FF) (LAMBDA (X) (COND ((ATOM X) X) (T (FF (CAR X))))))"
actual (gsp "ff[x]=[atom[x] -> x; T -> ff[car[x]]]")]
(let [expected "(SET (QUOTE FF) (QUOTE (LAMBDA (X) (COND ((ATOM X) X) (T (FF (CAR X)))))))"
actual (print-str (gsp "ff[x]=[atom[x] -> x; T -> ff[car[x]]]"))]
(is (= actual expected)))))

View file

@ -1,28 +1,27 @@
(ns beowulf.sexpr-test
(:require [clojure.math.numeric-tower :refer [abs]]
[clojure.test :refer :all]
[beowulf.cons-cell :refer :all]
(:require [clojure.test :refer [deftest is testing]]
[beowulf.bootstrap :refer [*options*]]
[beowulf.read :refer [parse simplify generate gsp]]))
[beowulf.cons-cell :refer []]
[beowulf.read :refer [gsp]]))
;; broadly, sexprs should be homoiconic
(deftest atom-tests
(testing "Reading atoms"
(let [expected 'A
actual (gsp(str expected))]
actual (gsp (str expected))]
(is (= actual expected)))
(let [expected 'APPLE
actual (gsp(str expected))]
actual (gsp (str expected))]
(is (= actual expected)))
(let [expected 'PART2
actual (gsp(str expected))]
actual (gsp (str expected))]
(is (= actual expected)))
(let [expected 'EXTRALONGSTRINGOFLETTERS
actual (gsp(str expected))]
actual (gsp (str expected))]
(is (= actual expected)))
(let [expected 'A4B66XYZ2
actual (gsp(str expected))]
actual (gsp (str expected))]
(is (= actual expected)))))
(deftest comment-tests
@ -41,13 +40,13 @@
B C)"))]
(is (= actual expected)
"Really important that comments work inside lists"))
;; ;; TODO: Currently failing and I'm not sure why
;; (binding [*options* {:strict true}]
;; (is (thrown-with-msg?
;; Exception
;; #"Cannot parse comments in strict mode"
;; (gsp "(A ;; comment
;; B C)"))))
;; ;; TODO: Currently failing and I'm not sure why
;; (binding [*options* {:strict true}]
;; (is (thrown-with-msg?
;; Exception
;; #"Cannot parse comments in strict mode"
;; (gsp "(A ;; comment
;; B C)"))))
))