more-tests-and-corner-cases #72
97
reader.scm
97
reader.scm
@@ -16,6 +16,7 @@
|
|||||||
scheme
|
scheme
|
||||||
(scheme base) ; make-parameter
|
(scheme base) ; make-parameter
|
||||||
(chicken base)
|
(chicken base)
|
||||||
|
(chicken irregex)
|
||||||
(chicken module)
|
(chicken module)
|
||||||
(chicken pathname)
|
(chicken pathname)
|
||||||
(chicken platform) ; software-version, machine-type
|
(chicken platform) ; software-version, machine-type
|
||||||
@@ -27,7 +28,11 @@
|
|||||||
read-raw-forms
|
read-raw-forms
|
||||||
|
|
||||||
current-features
|
current-features
|
||||||
platform-features)
|
platform-features
|
||||||
|
|
||||||
|
c-literal?
|
||||||
|
c-literal-value
|
||||||
|
c-literal-suffix)
|
||||||
|
|
||||||
;;; Sentinels for structural tokens
|
;;; Sentinels for structural tokens
|
||||||
(define close-paren (list '%close-paren))
|
(define close-paren (list '%close-paren))
|
||||||
@@ -186,11 +191,97 @@
|
|||||||
(define (finish-atom s)
|
(define (finish-atom s)
|
||||||
(cond
|
(cond
|
||||||
((string=? s ".") dot-token)
|
((string=? s ".") dot-token)
|
||||||
((string->number s) => identity)
|
((or (string->number s) (radix-number s))
|
||||||
((radix-number s) => identity)
|
=> (lambda (n) (c-number n s)))
|
||||||
|
((suffixed-number s) => identity)
|
||||||
((negated-name? s) (list '- (string->symbol (substring s 1))))
|
((negated-name? s) (list '- (string->symbol (substring s 1))))
|
||||||
(else (string->symbol s))))
|
(else (string->symbol s))))
|
||||||
|
|
||||||
|
;;; Scheme reads numbers C has no literal for -- 1/2, #e1.5, 1e400,
|
||||||
|
;;; +nan.0, 1+2i -- and they reached C as written. An integer too wide
|
||||||
|
;;; for long long is unsigned to C, which says so only by a warning
|
||||||
|
;;; unless it is written with a `u'; so it is.
|
||||||
|
(define +llong-min+ (- (expt 2 63)))
|
||||||
|
(define +llong-max+ (- (expt 2 63) 1))
|
||||||
|
(define +ullong-max+ (- (expt 2 64) 1))
|
||||||
|
|
||||||
|
(define (c-number n s)
|
||||||
|
(cond ((exact-integer? n)
|
||||||
|
(cond ((> (abs n) +ullong-max+)
|
||||||
|
(error "too large for any C integer type" s))
|
||||||
|
((> n +llong-max+) (make-c-literal n "u" #f))
|
||||||
|
((< n +llong-min+) (list '- (make-c-literal (- n) "u" #f)))
|
||||||
|
(else n)))
|
||||||
|
((and (real? n) (inexact? n) (finite? n)) n)
|
||||||
|
(else (error "C has no literal for this number" s))))
|
||||||
|
|
||||||
|
;;; 1u, 10L, 1ULL, 1.5f, 0.1L: a number with C's suffix, which the C is
|
||||||
|
;;; written with.
|
||||||
|
;;;
|
||||||
|
;;; 0b101u -> 5u in decimal, so C23's 0b never reaches C11
|
||||||
|
;;; 0.1f -> 0.1f in the digits written: through a double, 0.1f
|
||||||
|
;;; could round to another float, and 0.1L would
|
||||||
|
;;; lose all it has over one
|
||||||
|
;;; -1u -> (- 1u) which is what C reads it as too
|
||||||
|
(define-record-type <c-literal>
|
||||||
|
(make-c-literal value suffix digits)
|
||||||
|
c-literal?
|
||||||
|
(value c-literal-value)
|
||||||
|
(suffix c-literal-suffix)
|
||||||
|
(digits c-literal-digits))
|
||||||
|
|
||||||
|
(set-record-printer! <c-literal>
|
||||||
|
(lambda (literal port)
|
||||||
|
(display (or (c-literal-digits literal)
|
||||||
|
(number->string (c-literal-value literal)))
|
||||||
|
port)
|
||||||
|
(display (c-literal-suffix literal) port)))
|
||||||
|
|
||||||
|
(define suffixed-integer
|
||||||
|
(irregex "(0[xX][0-9a-fA-F]+|0[bB][01]+|[0-9]+)([uU](?:ll|LL|l|L)?|(?:ll|LL|l|L)[uU]?)"))
|
||||||
|
|
||||||
|
(define suffixed-float
|
||||||
|
(irregex "((?:[0-9]+\\.[0-9]*|\\.[0-9]+)(?:[eE][+-]?[0-9]+)?|[0-9]+[eE][+-]?[0-9]+)([fFlL])"))
|
||||||
|
|
||||||
|
(define (suffixed-number s)
|
||||||
|
(let* ((negative? (and (> (string-length s) 1) (char=? #\- (string-ref s 0))))
|
||||||
|
(body (if negative? (substring s 1) s))
|
||||||
|
;; each spelling begins with a digit or a point, and few names do
|
||||||
|
(literal (and (> (string-length body) 0)
|
||||||
|
(or (char-numeric? (string-ref body 0))
|
||||||
|
(char=? #\. (string-ref body 0)))
|
||||||
|
(or (suffixed-integer-literal body s)
|
||||||
|
(suffixed-float-literal body s)))))
|
||||||
|
(and literal (if negative? (list '- literal) literal))))
|
||||||
|
|
||||||
|
(define (suffixed-integer-literal body s)
|
||||||
|
(let ((m (irregex-match suffixed-integer body)))
|
||||||
|
(and m
|
||||||
|
(let* ((digits (irregex-match-substring m 1))
|
||||||
|
(suffix (irregex-match-substring m 2))
|
||||||
|
(n (or (radix-number digits) (string->number digits)))
|
||||||
|
(widest (if (irregex-search "[uU]" suffix) +ullong-max+ +llong-max+)))
|
||||||
|
(when (> n widest)
|
||||||
|
(error "too large for its suffix" s))
|
||||||
|
(make-c-literal n suffix #f)))))
|
||||||
|
|
||||||
|
(define (suffixed-float-literal body s)
|
||||||
|
(let ((m (irregex-match suffixed-float body)))
|
||||||
|
(and m
|
||||||
|
(let* ((digits (irregex-match-substring m 1))
|
||||||
|
(suffix (irregex-match-substring m 2))
|
||||||
|
(n (string->number digits)))
|
||||||
|
(unless (in-range? n suffix)
|
||||||
|
(error "C has no literal for this number" s))
|
||||||
|
(make-c-literal n suffix digits)))))
|
||||||
|
|
||||||
|
;;; A float overflows where it would round past FLT_MAX; a long double
|
||||||
|
;;; may be wider than any double, so whatever a double holds it does too
|
||||||
|
(define (in-range? n suffix)
|
||||||
|
(case (string-ref suffix 0)
|
||||||
|
((#\f #\F) (< (abs n) (* (- 2 (expt 2. -24)) (expt 2. 127))))
|
||||||
|
(else #t)))
|
||||||
|
|
||||||
;;; 0xE0 and 0b101 are C's spelling of 224 and 5, and are read as those
|
;;; 0xE0 and 0b101 are C's spelling of 224 and 5, and are read as those
|
||||||
;;; numbers: the C written from them says 224 and 5
|
;;; numbers: the C written from them says 224 and 5
|
||||||
(define (radix-number s)
|
(define (radix-number s)
|
||||||
|
|||||||
26
semen.scm
26
semen.scm
@@ -13,6 +13,7 @@
|
|||||||
sex-modules
|
sex-modules
|
||||||
types
|
types
|
||||||
matchable ; pattern matching
|
matchable ; pattern matching
|
||||||
|
reader ; c-literal
|
||||||
srfi-1 ; list routines
|
srfi-1 ; list routines
|
||||||
srfi-69 ; hash tables
|
srfi-69 ; hash tables
|
||||||
utils
|
utils
|
||||||
@@ -1212,9 +1213,32 @@
|
|||||||
;;; 42 -> int (. p x) -> that field's type
|
;;; 42 -> int (. p x) -> that field's type
|
||||||
;;; "hi" -> (* const char) (& p) -> (* (struct point))
|
;;; "hi" -> (* const char) (& p) -> (* (struct point))
|
||||||
;;; (area 3 4) -> what `area' returns
|
;;; (area 3 4) -> what `area' returns
|
||||||
|
;;; What C makes of a literal: of the types its suffix allows, the
|
||||||
|
;;; first that holds it, by the widths every host has -- int and long
|
||||||
|
;;; of 32 bits, long long of 64 -- so it is never narrower than C's own.
|
||||||
|
;;;
|
||||||
|
;;; 1 -> int 1u -> (unsigned int)
|
||||||
|
;;; 4000000000 -> (long long) 10L -> long
|
||||||
|
;;; 1.5f -> float 0.1L -> (long double)
|
||||||
|
(define (c-literal-type literal)
|
||||||
|
(let ((n (c-literal-value literal))
|
||||||
|
(suffix (map char-downcase (string->list (c-literal-suffix literal)))))
|
||||||
|
(cond ((exact? n) (integer-literal-type n suffix))
|
||||||
|
((memv #\f suffix) 'float)
|
||||||
|
(else '(long double)))))
|
||||||
|
|
||||||
|
(define (integer-literal-type n suffix)
|
||||||
|
(let ((ranks (drop (if (memv #\u suffix)
|
||||||
|
'(((unsigned int) 32) ((unsigned long) 32) ((unsigned long long) 64))
|
||||||
|
'((int 31) (long 31) ((long long) 63)))
|
||||||
|
(count (cut eqv? #\l <>) suffix))))
|
||||||
|
(let ((rank (find (lambda (rank) (< (abs n) (expt 2 (second rank)))) ranks)))
|
||||||
|
(and rank (first rank)))))
|
||||||
|
|
||||||
(define (expression-type expr env)
|
(define (expression-type expr env)
|
||||||
(cond
|
(cond
|
||||||
((and (number? expr) (exact? expr)) 'int)
|
((c-literal? expr) (c-literal-type expr))
|
||||||
|
((and (number? expr) (exact? expr)) (integer-literal-type expr '()))
|
||||||
((number? expr) 'double)
|
((number? expr) 'double)
|
||||||
((string? expr) '(* const char))
|
((string? expr) '(* const char))
|
||||||
((char? expr) 'char)
|
((char? expr) 'char)
|
||||||
|
|||||||
@@ -759,6 +759,22 @@
|
|||||||
(test-assert "sixteen when that is enough"
|
(test-assert "sixteen when that is enough"
|
||||||
(emits? (in-fn "(var d double 0.9871465422297019)") "d = 0.9871465422297019;")))
|
(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'
|
;; A define's body was pasted unwalked, and unparenthesised: `cast'
|
||||||
;; stayed a call, and (* x D) regrouped with D's own operator
|
;; stayed a call, and (* x D) regrouped with D's own operator
|
||||||
(test-group "defines"
|
(test-group "defines"
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
(import (chicken port)
|
(import (chicken condition)
|
||||||
|
(chicken port)
|
||||||
reader)
|
reader)
|
||||||
|
|
||||||
(define-syntax feature-test
|
(define-syntax feature-test
|
||||||
@@ -9,6 +10,12 @@
|
|||||||
(with-input-from-string string
|
(with-input-from-string string
|
||||||
(lambda () (read-raw-forms 'stdin))))))))
|
(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
|
(define-syntax reader-test
|
||||||
(syntax-rules ()
|
(syntax-rules ()
|
||||||
((reader-test result string)
|
((reader-test result string)
|
||||||
@@ -54,6 +61,27 @@
|
|||||||
;; ...and only a number: a prefix with nothing after it is a name
|
;; ...and only a number: a prefix with nothing after it is a name
|
||||||
(reader-test '(0x 0xg) "0x 0xg")
|
(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
|
;; `;' comments are preserved as (comment "...") forms
|
||||||
(reader-test '((comment " hi")) "; hi")
|
(reader-test '((comment " hi")) "; hi")
|
||||||
(reader-test '((comment ";; Prototypes")) ";;; Prototypes")
|
(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