tidy what the fixes wrote around existing helpers
All checks were successful
Sex CI / build-linux (pull_request) Successful in 5m55s
Sex CI / build-linux (push) Successful in 5m57s

written-type? matches the shapes instead of measuring them, array-form?
and pub-form? replace their spelled-out copies, both test files share
one fails-with?, and moved test comments are back above their groups.
This commit was merged in pull request #66.
This commit is contained in:
2026-10-19 18:21:14 +03:00
parent a4880f1b96
commit 540f5c75cd
5 changed files with 23 additions and 27 deletions

View File

@@ -795,14 +795,12 @@
;;; (* char) (* const char) (* size-t) (unsigned int) types ;;; (* char) (* const char) (* size-t) (unsigned int) types
;;; (* p) (* (g)) (¤ xs 0) expressions ;;; (* p) (* (g)) (¤ xs 0) expressions
(define (written-type? form env) (define (written-type? form env)
(and (pair? form) (match form
;; the length a dereference and a subscript have ((or ('* operand) ('¤ operand _))
(if (= (length form) (case (car form) ((*) 2) ((¤) 3) (else -1))) (if (symbol? operand)
(let ((operand (second form))) (not (or (lookup-name env operand) (get-name-type operand)))
(if (symbol? operand) (written-type? operand env)))
(not (or (lookup-name env operand) (get-name-type operand))) (_ (type-head? form))))
(written-type? operand env)))
(type-head? form))))
;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'. ;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'.
(define (resolve-wildcard form env) (define (resolve-wildcard form env)
@@ -1143,7 +1141,7 @@
;;; A field of a path, indexed or not: xs, [xs 0] -> xs ;;; A field of a path, indexed or not: xs, [xs 0] -> xs
(define (field-name field) (define (field-name field)
(if (and (pair? field) (eq? '¤ (car field)) (pair? (cdr field))) (if (and (array-form? field) (pair? (cdr field)))
(second field) (second field)
field)) field))

View File

@@ -185,8 +185,7 @@
(if (and (pair? v) (pair? (cdr v))) (field-types (second v)) (list))) (if (and (pair? v) (pair? (cdr v))) (field-types (second v)) (list)))
(remove comment-form? variants))) (remove comment-form? variants)))
(('ops type . methods) (('ops type . methods)
(append-map interface-types (append-map interface-types (filter pub-form? methods)))
(filter (lambda (m) (and (pair? m) (eq? (car m) 'pub))) methods)))
(('pub 'realize spec . methods) (('pub 'realize spec . methods)
(append-map (lambda (m) (interface-types (cons 'pub m))) (append-map (lambda (m) (interface-types (cons 'pub m)))
(remove comment-form? methods))) (remove comment-form? methods)))

View File

@@ -51,13 +51,12 @@ the way CHICKEN prints it -- or #f if it raises none."
((condition-property-accessor 'exn 'arguments) e)))) ((condition-property-accessor 'exn 'arguments) e))))
(begin (thunk) #f))) (begin (thunk) #f)))
(define (error-message source) (define (fails-with? thunk fragment)
"Compile SOURCE and return its error text, or #f if it compiles." (let ((m (failure-message thunk)))
(failure-message (lambda () (sex->c source)))) (and m (string-contains m fragment) #t)))
(define (reports? source fragment) (define (reports? source fragment)
(let ((m (error-message source))) (fails-with? (lambda () (sex->c source)) fragment))
(and m (string-contains m fragment) #t)))
(define (followed-by? source first then) (define (followed-by? source first then)
"Is FIRST in the C compiled from SOURCE, and the next thing past it, "Is FIRST in the C compiled from SOURCE, and the next thing past it,
@@ -90,9 +89,6 @@ whitespace aside, THEN?"
(test-group "codegen" (test-group "codegen"
;; c-switch handed its scrutinee straight to `cat', which only works
;; when it is an atom. Anything else was displayed as a raw
;; s-expression: `switch ((%. e type))'.
;; A case body shared the switch's scope, so two cases could not ;; A case body shared the switch's scope, so two cases could not
;; declare one name, and a label could not be followed by a ;; declare one name, and a label could not be followed by a
;; declaration before C23. Only a body that declares needs a block ;; declaration before C23. Only a body that declares needs a block
@@ -109,6 +105,9 @@ whitespace aside, THEN?"
(not (followed-by? (in-fn "(switch a (case 1 (do (var x int 1) (g x))))") (not (followed-by? (in-fn "(switch a (case 1 (do (var x int 1) (g x))))")
"{" "{")))) "{" "{"))))
;; c-switch handed its scrutinee straight to `cat', which only works
;; when it is an atom. Anything else was displayed as a raw
;; s-expression: `switch ((%. e type))'.
(test-group "switch scrutinee" (test-group "switch scrutinee"
(test-assert "member access" (test-assert "member access"
(emits? "(struct s ((type int))) (fn f ((e (struct s))) void (switch (. e type) (case 1 (g))))" (emits? "(struct s ((type int))) (fn f ((e (struct s))) void (switch (. e type) (case 1 (g))))"
@@ -336,8 +335,6 @@ whitespace aside, THEN?"
(test-assert "pub with nothing to define" (test-assert "pub with nothing to define"
(reports? "(pub 1)" "codegen.sex:1:"))) (reports? "(pub 1)" "codegen.sex:1:")))
;; A nested pointer chain used to silently lose a level:
;; (var p (* (* char))) emitted `char *p'
;; A grouped header, C's parens carried over, died in the writer with ;; A grouped header, C's parens carried over, died in the writer with
;; `(caddr) bad argument type' and no location ;; `(caddr) bad argument type' and no location
(test-group "malformed for" (test-group "malformed for"
@@ -357,6 +354,8 @@ whitespace aside, THEN?"
(reports? "(fn f () void\n (for ((var i int 0) (< i 4) (++ i)) (g i)))" (reports? "(fn f () void\n (for ((var i int 0) (< i 4) (++ i)) (g i)))"
"codegen.sex:2:"))) "codegen.sex:2:")))
;; A nested pointer chain used to silently lose a level:
;; (var p (* (* char))) emitted `char *p'
(test-group "malformed types are rejected" (test-group "malformed types are rejected"
(test-assert "nested pointer chain" (test-assert "nested pointer chain"
(reports? (in-fn "(var p (* (* char)))") "pointer chains are written flat")) (reports? (in-fn "(var p (* (* char)))") "pointer chains are written flat"))
@@ -784,7 +783,7 @@ whitespace aside, THEN?"
(test-assert "but a qualifier alone still waits for the type" (test-assert "but a qualifier alone still waits for the type"
(emits? "(typedef mytype int) (struct s ((a [const mytype])))" "const mytype a[]"))) (emits? "(typedef mytype int) (struct s ((a [const mytype])))" "const mytype a[]")))
;; `sizeof' takes a type or an expression, and `(* x)' is either. ;; `sizeof' takes a type or an expression, and `(* x)' is either.
;; What is in scope says which: a name C alone knows is a type ;; What is in scope says which: a name C alone knows is a type
(test-group "sizeof" (test-group "sizeof"
(test-assert "a pointer type" (test-assert "a pointer type"

View File

@@ -5,7 +5,7 @@
;;; is checked here is what the compiler refuses, and that it refuses ;;; is checked here is what the compiler refuses, and that it refuses
;;; it with the name of whatever is wrong. ;;; it with the name of whatever is wrong.
;;; ;;;
;;; `sex->c', `read-source', `failure-message', `emits?', `reports?' ;;; `sex->c', `read-source', `fails-with?', `emits?', `reports?'
;;; and `in-fn' come from codegen.scm, which run.scm includes first. ;;; and `in-fn' come from codegen.scm, which run.scm includes first.
(import sex-modules) (import sex-modules)
@@ -16,8 +16,7 @@ ahead of its pub forms."
(check-public-interface (read-source source))) (check-public-interface (read-source source)))
(define (interface-reports? source fragment) (define (interface-reports? source fragment)
(let ((m (failure-message (lambda () (interface-of source))))) (fails-with? (lambda () (interface-of source)) fragment))
(and m (string-contains m fragment) #t)))
(define (ops-src . body) (define (ops-src . body)
(string-append (string-append

View File

@@ -33,8 +33,9 @@ SEXC ?= ../../sexc
EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201\nvec2 7 7\nvec2(3 4)\ntally 2\nboard 42\nledger 2\ncolour 123 EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201\nvec2 7 7\nvec2(3 4)\ntally 2\nboard 42\nledger 2\ncolour 123
check: check:
@if [ "`$(SEXC) --public-interface greet.sex | head -3`" != "`printf '(record cell)\n(record ledger)\n(include stdio.h)'`" ]; then \ @head="`$(SEXC) --public-interface greet.sex | head -3`"; \
echo "modules FAILED, the interface begins:"; $(SEXC) --public-interface greet.sex | head -3; exit 1; \ if [ "$$head" != "`printf '(record cell)\n(record ledger)\n(include stdio.h)'`" ]; then \
echo "modules FAILED, the interface begins:"; echo "$$head"; exit 1; \
fi fi
@$(SEXC) ledger.sex -c -o ledger.o @$(SEXC) ledger.sex -c -o ledger.o
@$(SEXC) greet.sex -c -o greet.o @$(SEXC) greet.sex -c -o greet.o