Skip to content

Commit f4a3d8a

Browse files
committed
feat(core): add tuple printer registry and safe struct predicate
1 parent 5d63646 commit f4a3d8a

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/boot/print.scm

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
(define %tuple-printers (make-parameter '()))
2+
3+
(define (register-tuple-printer! type printer)
4+
(let ((printers (%tuple-printers)))
5+
(%tuple-printers
6+
(cons (cons type printer) printers))))
7+
18
(define procedure-printer
29
(make-parameter
310
(lambda (proc port slashify)
@@ -698,7 +705,17 @@
698705
(display "#<condition " port)
699706
(print-condition obj port)
700707
(display ">" port)]
701-
[else (display "#<unknown>" port)])))
708+
[else
709+
(let ([printers (%tuple-printers)])
710+
(let loop ((printers printers))
711+
712+
(cond
713+
[(null? printers)
714+
(display "#<UNKNOWN TUPLE>" port)]
715+
[(eq? (car (car printers)) (tuple-ref obj 0))
716+
((cdr (car printers)) obj port quote?)]
717+
[else
718+
(loop (cdr printers))])))])))
702719
(define (newline . port) (display "\n" (if (null? port) (current-output-port) (car port))))
703720

704721
(define (displayln x . rest)
@@ -709,3 +726,4 @@
709726
(let ((p (if (pair? rest) (car rest) (current-output-port))))
710727
(write x p)
711728
(newline p)))
729+

lib/core/struct.scm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
((_) (tuple ',desc-name ,@(make-list field-count '(unspecified)))))))
3838
(?pred-rules
3939
(datum->syntax #'k `(syntax-rules ()
40-
((_ obj) (eq? (tuple-ref obj 0) ',desc-name)))))
40+
((_ obj) (and (tuple? obj) (eq? (tuple-ref obj 0) ',desc-name))))))
4141
((?getter-name ...)
4242
(map (lambda (field-name)
4343
(datum->syntax #'?_ (string->symbol (format #f "~a-~a" struct-name field-name))))

0 commit comments

Comments
 (0)