read the unit tests' sources from a file, the way sexc does

The reader exported parse-all only so the tests could give a source the
file name its locations carry; read-from-file gives it one, through the
reader's own interface. The pipeline helpers move out of codegen.scm for
every suite to use.
This commit is contained in:
2026-10-20 11:48:02 +03:00
parent 8488a58be1
commit 4758802810
6 changed files with 89 additions and 88 deletions

View File

@@ -6,66 +6,7 @@
;;; the existing suite, because both need an operand shape that no
;;; earlier test program happened to use.
(import (chicken condition)
(chicken port)
(chicken string)
srfi-13
fmt-c-writer
reader
semen
utils)
(define (read-source source)
(with-input-from-string source
(lambda ()
(parameterize ((current-source-file "codegen.sex"))
(parse-all (current-input-port))))))
(define (sex->c source)
"Compile SOURCE, a string of Sex, and return the generated C."
(let ((forms (read-source source)))
(with-output-to-string
(lambda ()
(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 (silently (lambda () (sex->c source))) fragment) #t))
(define (failure-message thunk)
"The error THUNK raises as the user sees it -- message plus arguments,
the way CHICKEN prints it -- or #f if it raises none."
(handle-exceptions e
(with-output-to-string
(lambda ()
(display ((condition-property-accessor 'exn 'message) e))
(for-each (lambda (a) (display " ") (write a))
((condition-property-accessor 'exn 'arguments) e))))
(begin (thunk) #f)))
(define (fails-with? thunk fragment)
(let ((m (failure-message thunk)))
(and m (string-contains m fragment) #t)))
(define (reports? source fragment)
(fails-with? (lambda () (sex->c source)) fragment))
(define (followed-by? source first then)
"Is FIRST in the C compiled from SOURCE, and the next thing past it,
whitespace aside, THEN?"
(let* ((c (silently (lambda () (sex->c source))))
(at (string-contains c first)))
(and at
(let ((next (string-skip c char-whitespace? (+ at (string-length first)))))
(and next (string-prefix? then (substring c next)))))))
(import srfi-13)
(define (in-fn body)
(string-append "(fn f ((a int) (b int)) void " body ")"))
@@ -80,13 +21,6 @@ whitespace aside, THEN?"
" (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"
;; A case body shared the switch's scope, so two cases could not