Files
sex/tests/reader.scm
alex-eg 8ddeac7bab write number literals the way C reads them
A C suffix was no number to string->number, so 1u and 1.5f became
symbols, and C got them inside the |pipes| CHICKEN writes those with.
They read now into a literal that prints its C spelling and is typed by
its suffix: an integer in decimal, so 0b101u needs no C23, and a float
in the digits it was written with, since through a double 0.1f could
round to another float and 0.1L would lose what it has over one.

Scheme also reads numbers C has none of. 1/2 reached C as integer
division and +inf.0 as no C at all; those, and integers past 64 bits,
are read errors now. An integer is typed by size as well, so 4000000000
is no longer an int, and one past long long gets the u C would
otherwise only warn about.
2026-10-27 01:31:37 +03:00

147 lines
6.3 KiB
Scheme

(import (chicken condition)
(chicken port)
reader)
(define-syntax feature-test
(syntax-rules ()
((feature-test result features string)
(test result
(parameterize ((current-features 'features))
(with-input-from-string string
(lambda () (read-raw-forms 'stdin))))))))
(define (read-error string)
"The message of the error reading STRING raises, or #f if it reads."
(handle-exceptions e ((condition-property-accessor 'exn 'message) e)
(with-input-from-string string (lambda () (read-raw-forms 'stdin)))
#f))
(define-syntax reader-test
(syntax-rules ()
((reader-test result string)
(test result
(with-input-from-string string
(lambda () (read-raw-forms 'stdin)))))))
(test-group "reader"
;; []-syntax. For array types and array access expressions
(reader-test '((¤ * char)) "[* char]")
(reader-test '((¤ * * char const 512)) "[* * char const 512]")
(reader-test '((¤)) "[]")
(reader-test '((¤ (¤))) "[[]]")
(reader-test '((¤ (¤ const char))) "[[const char]]")
;; leading `.' becomes the dot-access operator
(reader-test '((dot-access obj field)) "(. obj field)")
(reader-test '((dot-access obj method a b)) "(. obj method a b)")
(reader-test '((dot-access a b)) "(. a b)")
;; nested leading dot
(reader-test '((foo (dot-access a b))) "(foo (. a b))")
;; dotted pairs are preserved (only a *leading* dot is special)
(reader-test '((a . b)) "(a . b)")
(reader-test '((a b . c)) "(a b . c)")
(reader-test '((quote (a . b))) "'(a . b)")
;; a `.'-prefixed symbol is an ordinary symbol, not dot-access
(reader-test '((.field obj)) "(.field obj)")
;; a leading `-' on a name is negation, the way it is on a number:
;; no name is spelled with one, and -INFINITY otherwise mangled into
;; _INFINITY. The operators that start with one are left alone
(reader-test '((- INFINITY)) "-INFINITY")
(reader-test '((var best double (- INFINITY))) "(var best double -INFINITY)")
(reader-test '((- _x)) "-_x")
(reader-test '(-1 -1.5) "-1 -1.5")
(reader-test '(- -> ->> -- -=) "- -> ->> -- -=")
;; a number in another radix, spelled the way C does or the way
;; Scheme does; either is an ordinary integer from here on
(reader-test '(224 224 5 5 -16) "0xE0 0Xe0 0b101 0B101 -0x10")
(reader-test '(128 128 5 8 -16) "#x80 #X80 #b101 #o10 #x-10")
;; ...and only a number: a prefix with nothing after it is a name
(reader-test '(0x 0xg) "0x 0xg")
;; Scheme reads numbers C has no literal for, and they reached C as
;; written: 1/2 is integer division there, and +inf.0 no number at all
(test "a ratio is not a C number"
"C has no literal for this number" (read-error "1/2"))
(test "nor a double out of range"
"C has no literal for this number" (read-error "1e400"))
(test "nor a float" "C has no literal for this number" (read-error "1e39f"))
(test "but a long double may be wider than a double"
#f (read-error "1e400L"))
(test "nor an infinity" "C has no literal for this number" (read-error "-inf.0"))
(test "nor a NaN" "C has no literal for this number" (read-error "+nan.0"))
(test "nor a complex number" "C has no literal for this number" (read-error "1+2i"))
(test "an integer has 64 bits at most"
"too large for any C integer type" (read-error "18446744073709551616"))
(test "and a signed suffix leaves it 63"
"too large for its suffix" (read-error "9223372036854775808ll"))
(test "an unsigned one does not"
#f (read-error "18446744073709551615ull"))
;; a name that only begins like a number is still a name
(reader-test '(3dmath.h) "3dmath.h")
;; `;' comments are preserved as (comment "...") forms
(reader-test '((comment " hi")) "; hi")
(reader-test '((comment ";; Prototypes")) ";;; Prototypes")
(reader-test '((foo (comment " c") bar)) "(foo ; c\n bar)")
;; a trailing top-level comment is its own form
(reader-test '((a b) (comment " t")) "(a b) ; t")
;; a `;' inside a string is not a comment
(reader-test '("a;b") "\"a;b\"")
;; #+ / #- feature expressions. What does not apply is read and
;; dropped, so it never reaches the compiler at all
(feature-test '((a)) (linux) "#+linux (a)")
(feature-test '() (macosx) "#+linux (a)")
(feature-test '() (linux) "#-linux (a)")
(feature-test '((a)) (macosx) "#-linux (a)")
;; the guarded datum can be anything, not only a list
(feature-test '(42) (x) "#+x 42")
(feature-test '("s") (x) "#+x \"s\"")
;; and / or / not
(feature-test '((a)) (unix linux) "#+(and unix linux) (a)")
(feature-test '() (unix) "#+(and unix linux) (a)")
(feature-test '((a)) (unix) "#+(or linux unix) (a)")
(feature-test '() (bsd) "#+(or linux unix) (a)")
(feature-test '((a)) (unix) "#+(and unix (not macosx)) (a)")
(feature-test '() (unix macosx) "#+(and unix (not macosx)) (a)")
;; (and) is true and (or) is false, as they are in CL
(feature-test '((a)) () "#+(and) (a)")
(feature-test '() () "#+(or) (a)")
;; a guard inside a form, including as the last element -- dropping
;; continues with the next token, so the closing paren still arrives
(feature-test '((f 1 3)) (a) "(f #+a 1 #-a 2 3)")
(feature-test '((f 2 3)) (b) "(f #+a 1 #-a 2 3)")
(feature-test '((f 1)) (a) "(f #+a 1 #+b 2)")
(feature-test '((f)) (b) "(f #+a 1)")
;; ...and as the last form in the file
(feature-test '((a)) (x) "(a) #+y (b)")
;; guards nest
(feature-test '((a)) (x y) "#+x #+y (a)")
(feature-test '((b)) (x) "#+x #+y (a) (b)")
;; ...and the inner one may leave nothing behind: the end of the
;; file, or the paren closing the list, is what the outer one then
;; produces, and neither is an error
(feature-test '() (x) "#+x #+y (a)")
(feature-test '((f)) (x) "(f #+x #+y 1)")
(feature-test '((f 2)) (x) "(f #+x #+y 1 2)")
(feature-test '((a)) (x) "(a) #+x #+y (b)")
;; a comment between a guard and the form it guards describes the
;; guard. Taking it for the guarded datum would leave the form itself
;; unconditional
(feature-test '((a)) (x) "#+x ;; why\n (a)")
(feature-test '() (y) "#+x ;; why\n (a)")
(feature-test '((b)) (y) "#+x ;; why\n (a) (b)")
;; a comment after the guarded form is an ordinary form, and stays
(feature-test '((comment "; tail")) (y) "#+x (a) ;; tail")
;; a feature the program was not given is simply absent
(feature-test '() () "#+anything (a)")
)