forked from alex-eg/sex
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.
This commit is contained in:
@@ -759,6 +759,22 @@
|
||||
(test-assert "sixteen when that is enough"
|
||||
(emits? (in-fn "(var d double 0.9871465422297019)") "d = 0.9871465422297019;")))
|
||||
|
||||
;; A suffix is C's, and reached C inside |pipes|, the way CHICKEN
|
||||
;; writes a symbol that starts with a digit
|
||||
(test-group "suffixed literals"
|
||||
(test-assert "an integer keeps its suffix"
|
||||
(emits? (in-fn "(var n (unsigned long long) 1ULL)") "n = 1ULL;"))
|
||||
(test-assert "and is written in decimal, which C11 has a suffix for"
|
||||
(emits? (in-fn "(var n (unsigned int) 0b101u)") "n = 5u;"))
|
||||
(test-assert "a float keeps its digits, which a double would round"
|
||||
(emits? (in-fn "(var x (long double) 3.14159265358979323846264338327950288L)")
|
||||
"x = 3.14159265358979323846264338327950288L;"))
|
||||
(test-assert "a minus is C's own"
|
||||
(emits? (in-fn "(var n (unsigned int) -1u)") "n = -1u;"))
|
||||
(test-assert "a number too wide for long long is unsigned, and says so"
|
||||
(emits? (in-fn "(var n (unsigned long long) 0xFFFFFFFFFFFFFFFF)")
|
||||
"n = 18446744073709551615u;")))
|
||||
|
||||
;; A define's body was pasted unwalked, and unparenthesised: `cast'
|
||||
;; stayed a call, and (* x D) regrouped with D's own operator
|
||||
(test-group "defines"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
(import (chicken port)
|
||||
(import (chicken condition)
|
||||
(chicken port)
|
||||
reader)
|
||||
|
||||
(define-syntax feature-test
|
||||
@@ -9,6 +10,12 @@
|
||||
(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)
|
||||
@@ -54,6 +61,27 @@
|
||||
;; ...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")
|
||||
|
||||
39
tests/sex-programs/numbers.sex
Normal file
39
tests/sex-programs/numbers.sex
Normal file
@@ -0,0 +1,39 @@
|
||||
(compilation "-- -std=c11 -pedantic-errors -Werror")
|
||||
(input)
|
||||
(output "unsigned: 4294967295 18446744073709551615 4294967295"
|
||||
"widths: 1 1 1"
|
||||
"a float stays a float: 1"
|
||||
"radix with suffix: 255 5"
|
||||
"by magnitude: 4000000000 18446744073709551615")
|
||||
(return 0)
|
||||
|
||||
;;; A number is written the way C writes it, suffix included, and `_'
|
||||
;;; takes the type C gives it: the suffix's, or for none the first type
|
||||
;;; wide enough to hold it.
|
||||
|
||||
(include stdio.h)
|
||||
|
||||
(pub fn main () int
|
||||
(var u _ 1u)
|
||||
(var ull _ 1ULL)
|
||||
(printf "unsigned: %u %llu %u\n" (- u 2) (- ull 2) -1u)
|
||||
|
||||
(var l _ 10L)
|
||||
(var f _ 1.5f)
|
||||
(var ld _ 0.1L)
|
||||
(printf "widths: %d %d %d\n"
|
||||
(== (sizeof l) (sizeof long))
|
||||
(== (sizeof f) (sizeof float))
|
||||
(== (sizeof ld) (sizeof (long double))))
|
||||
|
||||
;; the float nearest 0.1 is not the double nearest it
|
||||
(var tenth-f _ 0.1f)
|
||||
(var tenth _ 0.1)
|
||||
(printf "a float stays a float: %d\n" (!= tenth-f tenth))
|
||||
|
||||
(printf "radix with suffix: %u %u\n" 0xFFu 0b101u)
|
||||
|
||||
(var big _ 4000000000)
|
||||
(var mask _ 0xFFFFFFFFFFFFFFFF)
|
||||
(printf "by magnitude: %lld %llu\n" big mask)
|
||||
(return 0))
|
||||
Reference in New Issue
Block a user