From a2bbb735240f9fc154f754f447c4459227bad62d Mon Sep 17 00:00:00 2001 From: alex-eg Date: Tue, 27 Oct 2026 22:41:06 +0300 Subject: [PATCH] spell the least long long as C's LLONG_MIN -9223372036854775808 reached C as written, where it negates an unsigned constant, and had no type for inference: it is read as -9223372036854775807 - 1, which is long long on both sides. --- reader.scm | 5 ++++- tests/codegen.scm | 5 ++++- tests/sex-programs/c11.sex | 7 ++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/reader.scm b/reader.scm index 5110972a..f998827a 100644 --- a/reader.scm +++ b/reader.scm @@ -234,7 +234,9 @@ ;;; 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. +;;; unless it is written with a `u'; so it is. -9223372036854775808 is +;;; one too: C negates the unsigned 9223372036854775808, so it is +;;; spelled as LLONG_MIN is, -9223372036854775807 - 1. (define +llong-min+ (- (expt 2 63))) (define +llong-max+ (- (expt 2 63) 1)) (define +ullong-max+ (- (expt 2 64) 1)) @@ -245,6 +247,7 @@ (error "too large for any C integer type" s)) ((> n +llong-max+) (make-number-literal n "u" #f)) ((< n +llong-min+) (list '- (make-number-literal (- n) "u" #f))) + ((= n +llong-min+) (list '- (+ n 1) 1)) (else n))) ((and (real? n) (inexact? n) (finite? n)) n) (else (error "C has no literal for this number" s)))) diff --git a/tests/codegen.scm b/tests/codegen.scm index 2b4beabe..2044827a 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -1190,7 +1190,10 @@ (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;"))) + "n = 18446744073709551615u;")) + (test-assert "the least long long is spelled as LLONG_MIN, and typed" + (emits? (in-fn "(var n _ -9223372036854775808)") + "long long n = -9223372036854775807 - 1;"))) ;; A define's body was pasted unwalked, and unparenthesised: `cast' ;; stayed a call, and (* x D) regrouped with D's own operator diff --git a/tests/sex-programs/c11.sex b/tests/sex-programs/c11.sex index 6065ed90..7739e85c 100644 --- a/tests/sex-programs/c11.sex +++ b/tests/sex-programs/c11.sex @@ -1,6 +1,6 @@ (compilation "-- -std=c11 -pedantic-errors") (input) -(output "c11: 42 43 6 4 6 12 5 0" +(output "c11: 42 43 6 4 6 12 5 0 -9223372036854775808" "trigraphs: what??! ??= ???( ??/") (return 0) @@ -33,6 +33,7 @@ (var origin point #()) (var unit _ (shape/square 1)) (var pair [_ 2] #(#(point : 1 2) #(point : 3 4))) +(var least _ -9223372036854775808) (fn sides ((s shape)) int (var n int 0) @@ -46,8 +47,8 @@ (var k int 1) (var add-k (closure ((int)) int) (closure ((v int)) int (k) (return (+ v k)))) - (printf "c11: %d %d %d %d %d %d %d %d\n" + (printf "c11: %d %d %d %d %d %d %d %d %lld\n" (.area p) (add-k (.area p)) (+ (sides (shape/square 1)) (sides (shape/dot)) 2) - (sides unit) (.area corner) (.area [pair 1]) [primes 2] (. origin y)) + (sides unit) (.area corner) (.area [pair 1]) [primes 2] (. origin y) least) (printf "trigraphs: %s\n" "what??! ??= ???( ??/") (return 0))