dispatch . on the type it reads from

Which of C's two member operators applies is a question the compiler
can already answer -- (.length v) and (.length p) have picked & or *
off the receiver's type since methods moved into the head -- so making
the programmer answer it again asks them to restate what semen knows,
and to revisit every reader of a field the day it becomes a pointer.

A path resolves a step at a time, since only the type in hand answers
for the next field. A receiver nothing can type is read as a value and
warned about rather than refused: refusing leaves no spelling at all
for a struct out of a header nobody has parsed.

Two things had to come along. A parameter written (p * struct point)
was recorded with nothing but the `*' for a type, which left half the
paths in list-macros unanswerable. And fmt-c parenthesised a member
access written inside another, which went unnoticed while only match
nested them -- its bindings have been reading ((s.u).circle).r since
sums went in.
This commit is contained in:
2026-10-05 02:08:15 +03:00
parent 95f39e4b44
commit fb4b8aa1f9
4 changed files with 125 additions and 15 deletions

View File

@@ -26,8 +26,16 @@
(parameterize ((sex-line-directives 'none))
(emit-c (semen-process forms)))))))
;;; Warnings are what `warns?' is for; a test that happens to trip one
;;; should not print it
(define (silently thunk)
"THUNK's value, with whatever it wrote to stderr dropped."
(let ((value #f))
(with-error-output-to-string (lambda () (set! value (thunk))))
value))
(define (emits? source fragment)
(and (string-contains (sex->c source) fragment) #t))
(and (string-contains (silently (lambda () (sex->c source))) fragment) #t))
(define (error-message source)
"Compile SOURCE and return the error text as the user sees it --
@@ -48,6 +56,23 @@ compiles."
(define (in-fn body)
(string-append "(fn f ((a int) (b int)) void " body ")"))
;;; A struct, a struct holding a pointer to one, and locals of every
;;; shape a member path can start from
(define (in-line body)
(string-append
"(struct point ((x int) (y int)))"
"(struct line ((a (* struct point)) (b (struct point))))"
"(fn f ((lp (* struct line))) void"
" (var p (struct point)) (var pp (* struct point) (& p))"
" (var l (struct line)) " body ")"))
(define (warnings source)
"What compiling SOURCE writes to stderr."
(with-error-output-to-string (lambda () (sex->c source))))
(define (warns? source fragment)
(and (string-contains (warnings source) fragment) #t))
(test-group "codegen"
;; c-switch handed its scrutinee straight to `cat', which only works
@@ -635,6 +660,32 @@ compiles."
(test-assert "nor one behind a pointer"
(emits? "(struct t ((z int))) (struct s ((a (¤ * struct t))))" "struct t* a[]")))
;; `.' is C's `.' or its `->' depending on what is being read from,
;; so a path reads the same however the thing it starts from is held
(test-group "member access"
(test-assert "a value reads with a dot"
(emits? (in-line "(= (. p x) 1)") "p.x = 1"))
(test-assert "a pointer reads with an arrow"
(emits? (in-line "(= (. pp x) 1)") "pp->x = 1"))
;; each step asks the type in hand, so one path may be both
(test-assert "a pointer field inside a value"
(emits? (in-line "(= (. l a x) 1)") "l.a->x = 1"))
(test-assert "a value field inside a pointer"
(emits? (in-line "(= (. lp b y) 1)") "lp->b.y = 1"))
(test-assert "and two pointers in a row"
(emits? (in-line "(= (. lp a x) 1)") "lp->a->x = 1"))
;; a path is left-associative and its right operand is always a
;; name, so none of it needs parenthesising
(test-assert "a path carries no parens"
(not (emits? (in-line "(= (. lp a x) 1)") "(lp->a)")))
;; ...which is not to say nothing does
(test-assert "though a dereference still does"
(emits? (in-line "(= (. (* pp) x) 1)") "(*pp).x = 1"))
(test-assert "a type we cannot work out is read as a value"
(emits? (in-line "(= (. (guess) x) 1)") "guess().x = 1"))
(test-assert "and said so"
(warns? (in-line "(= (. (guess) x) 1)") "cannot type this")))
;; Toplevel forms used to be separated by a blank line unconditionally,
;; which spread a block of adjacent typedefs over twice the lines its
;; author wrote