From d5d68659241a5a9148577c27bccc399f84f449bf Mon Sep 17 00:00:00 2001 From: alex-eg Date: Fri, 16 Oct 2026 13:54:41 +0300 Subject: [PATCH] promote the prelude's integer types like C's own u8 and i32 are typedefs of names nothing parses, so they had no rank: (+ a a) over two u8 200s was a u8 holding 144, and (+ n 1) over an i32 could not be typed at all. --- tests/sex-programs/wildcards.sex | 9 +++++++++ tests/types.scm | 9 ++++++--- types.scm | 10 ++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/tests/sex-programs/wildcards.sex b/tests/sex-programs/wildcards.sex index 558a6d5..eaeea12 100644 --- a/tests/sex-programs/wildcards.sex +++ b/tests/sex-programs/wildcards.sex @@ -11,6 +11,7 @@ "partial: 1 2.5" "joined: 43.5 84 49 1" "promoted: 200 60000 3705032704" + "prelude: 400 6" "from elements: 4 9 0") (return 0) @@ -105,6 +106,14 @@ (var unpromoted _ (+ kept kept)) (printf "promoted: %d %d %u\n" narrow narrower unpromoted) + ;; the prelude's widths are C's own types under other names, and + ;; promote the way those do + (var b8 u8 200) + (var w32 i32 5) + (var bytes _ (+ b8 b8)) + (var next _ (+ w32 1)) + (printf "prelude: %d %d\n" bytes next) + ;; a brace initializer has no type of its own, but its elements solve ;; the hole in the array type around it -- and the length stays as ;; written, whether or not every slot is initialized diff --git a/tests/types.scm b/tests/types.scm index 144e770..74e9876 100644 --- a/tests/types.scm +++ b/tests/types.scm @@ -42,14 +42,17 @@ #f (get-fields 't-color)) - (add-typedef 't-u8 '(typedef t-u8 uint8-t)) + (add-typedef 't-u8 '(typedef t-u8 gl-ubyte)) (test "a typedef resolves to its target" - 'uint8-t + 'gl-ubyte (get-underlying-type 't-u8)) (add-typedef 't-byte '(typedef t-byte t-u8)) (test "and chains are followed to the end" - 'uint8-t + 'gl-ubyte (get-underlying-type 't-byte)) + (test "stdint.h's exact-width types are C's own" + '(unsigned char) + (get-underlying-type 'uint8-t)) (test "a struct is not a typedef" #f (get-underlying-type 't-point)) diff --git a/types.scm b/types.scm index 2a7080f..9d99c58 100644 --- a/types.scm +++ b/types.scm @@ -125,6 +125,16 @@ (hash-table-set! +type-db+ name (list 'typedef name (last (strip-pub form))))) +;;; , which nothing parses, as far as arithmetic asks: the +;;; prelude's `u8' is a `uint8-t', so (+ a a) over two of them is an +;;; `int', promoted like any narrow type. The 64-bit pair is `long' on +;;; some hosts; it ranks above the narrower ones either way. +(for-each (lambda (entry) (add-typedef (car entry) (cons 'typedef entry))) + '((int8-t (signed char)) (uint8-t (unsigned char)) + (int16-t short) (uint16-t (unsigned short)) + (int32-t int) (uint32-t (unsigned int)) + (int64-t (long long)) (uint64-t (unsigned long long)))) + ;;; (define name value ...) -- a C #define, kept so a macro can read a ;;; compile-time constant rather than re-parse the source. (define (add-define name form)