1
0
forked from alex-eg/sex

share reading a test source and catching its error

encapsulation.scm had copies of both from codegen.scm.
This commit is contained in:
2026-10-14 20:09:17 +03:00
parent f87e9faa8f
commit c97ae8583b
2 changed files with 19 additions and 18 deletions

View File

@@ -15,12 +15,15 @@
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 (with-input-from-string source
(lambda ()
(parameterize ((current-source-file "codegen.sex"))
(parse-all (current-input-port)))))))
(let ((forms (read-source source)))
(with-output-to-string
(lambda ()
(parameterize ((sex-line-directives 'none))
@@ -37,17 +40,20 @@
(define (emits? source fragment)
(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 --
message plus arguments, the way CHICKEN prints it -- or #f if SOURCE
compiles."
(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 (sex->c source) #f)))
(begin (thunk) #f)))
(define (error-message source)
"Compile SOURCE and return its error text, or #f if it compiles."
(failure-message (lambda () (sex->c source))))
(define (reports? source fragment)
(let ((m (error-message source)))