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:
26
semen.scm
26
semen.scm
@@ -13,6 +13,7 @@
|
||||
sex-modules
|
||||
types
|
||||
matchable ; pattern matching
|
||||
reader ; c-literal
|
||||
srfi-1 ; list routines
|
||||
srfi-69 ; hash tables
|
||||
utils
|
||||
@@ -1212,9 +1213,32 @@
|
||||
;;; 42 -> int (. p x) -> that field's type
|
||||
;;; "hi" -> (* const char) (& p) -> (* (struct point))
|
||||
;;; (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)
|
||||
(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)
|
||||
((string? expr) '(* const char))
|
||||
((char? expr) 'char)
|
||||
|
||||
Reference in New Issue
Block a user