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 semen
utils) 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) (define (sex->c source)
"Compile SOURCE, a string of Sex, and return the generated C." "Compile SOURCE, a string of Sex, and return the generated C."
(let ((forms (with-input-from-string source (let ((forms (read-source source)))
(lambda ()
(parameterize ((current-source-file "codegen.sex"))
(parse-all (current-input-port)))))))
(with-output-to-string (with-output-to-string
(lambda () (lambda ()
(parameterize ((sex-line-directives 'none)) (parameterize ((sex-line-directives 'none))
@@ -37,17 +40,20 @@
(define (emits? source fragment) (define (emits? source fragment)
(and (string-contains (silently (lambda () (sex->c source))) fragment) #t)) (and (string-contains (silently (lambda () (sex->c source))) fragment) #t))
(define (error-message source) (define (failure-message thunk)
"Compile SOURCE and return the error text as the user sees it -- "The error THUNK raises as the user sees it -- message plus arguments,
message plus arguments, the way CHICKEN prints it -- or #f if SOURCE the way CHICKEN prints it -- or #f if it raises none."
compiles."
(handle-exceptions e (handle-exceptions e
(with-output-to-string (with-output-to-string
(lambda () (lambda ()
(display ((condition-property-accessor 'exn 'message) e)) (display ((condition-property-accessor 'exn 'message) e))
(for-each (lambda (a) (display " ") (write a)) (for-each (lambda (a) (display " ") (write a))
((condition-property-accessor 'exn 'arguments) e)))) ((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) (define (reports? source fragment)
(let ((m (error-message source))) (let ((m (error-message source)))

View File

@@ -5,23 +5,18 @@
;;; 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', `emits?', `reports?' and `in-fn' come from codegen.scm, ;;; `sex->c', `read-source', `failure-message', `emits?', `reports?'
;;; which run.scm includes first. ;;; and `in-fn' come from codegen.scm, which run.scm includes first.
(import sex-modules) (import sex-modules)
(define (interface-of source) (define (interface-of source)
"The declarations SOURCE, compiled as a module, hands its importers "The declarations SOURCE, compiled as a module, hands its importers
ahead of its pub forms." ahead of its pub forms."
(check-public-interface (check-public-interface (read-source source)))
(with-input-from-string source
(lambda ()
(parameterize ((current-source-file "codegen.sex"))
(parse-all (current-input-port)))))))
(define (interface-reports? source fragment) (define (interface-reports? source fragment)
(let ((m (handle-exceptions e ((condition-property-accessor 'exn 'message) e) (let ((m (failure-message (lambda () (interface-of source)))))
(begin (interface-of source) #f))))
(and m (string-contains m fragment) #t))) (and m (string-contains m fragment) #t)))
(define (ops-src . body) (define (ops-src . body)