You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/quad/quad/quad.rkt

40 lines
1.5 KiB
Racket

6 years ago
#lang debug racket/base
(require racket/match racket/function "generic.rkt")
6 years ago
(provide (all-defined-out))
(module+ test (require rackunit))
6 years ago
(struct $quad (attrs elems) #:transparent
#:methods gen:quad
[(define (elems q) ($quad-elems q))
(define (attrs q) ($quad-attrs q))
(define (entrance-point q) (hash-ref (attrs q) 'entrance 'entrance))
(define (exit-point q) (hash-ref (attrs q) 'exit 'exit))
(define (inner-point q) (hash-ref (attrs q) 'inner 'inner))
(define (size q) (length (elems q)))
(define (draw q) "<···>")])
6 years ago
(define (quad-attrs? x) (and (hash? x) (hash-eq? x)))
(define (quad-elem? x) (or (char? x) (string? x) ($quad? x)))
(define (quad-elems? xs) (and (pair? xs) (andmap quad-elem? xs)))
(define (quad #:type [type $quad] . xs)
6 years ago
(match xs
[(list #f xs ...) (apply quad #:type type (hasheq) xs)]
[(list (? quad-attrs? attrs) (? quad-elem? elems) ...) (type attrs elems)]
[(list (? quad-elem? elems) ...) (apply quad #:type type #f elems)]
6 years ago
[else (error 'bad-quad-input)]))
6 years ago
(define q quad)
6 years ago
(define (quads? xs) (andmap quad? xs))
6 years ago
(define (atomic-quad? x) (and (quad? x) (match (elems x)
[(list (? char?)) #t]
[else #f])))
6 years ago
(define (atomic-quads? xs) (andmap atomic-quad? xs))
6 years ago
(module+ test
(check-true (atomic-quad? ($quad '#hasheq() '(#\H))))
(check-true (atomic-quads? (list ($quad '#hasheq() '(#\H))))))
(struct $break $quad () #:transparent)
(define (break . xs) (apply quad #:type $break xs))
(define b break)
6 years ago