-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretty.rkt
104 lines (85 loc) · 2.05 KB
/
pretty.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#lang racket
(require "core.rkt"
"tree.rkt")
(provide (rename-out [pretty peg-pretty]))
(define (pretty g t)
(match g
[(peg-grammar rs p) (list->string (ppr rs p t))]))
(define (ppr-eps t)
(match t
[(tunit) '()]
[t1 (raise 'type-error)]))
(define (ppr-chr c t)
(match t
[(tchr c1) (if (eq? c c1)
(list c)
(raise 'type-error))]
[t1 (raise 'type-error)]))
(define (ppr-var g v t)
(match (assoc v g)
[#f (begin
(printf "Undefined variable: ~a\n~a\n" v g)
'())]
[(cons _ e1)
(ppr g e1 t)]))
(define (ppr-cat g e1 e2 t)
(match t
[(tpair t1 t2)
(append (ppr g e1 t1)
(ppr g e2 t2))]
[t1 (raise 'type-error)]))
(define (ppr-choice g e1 e2 t)
(match t
[(tleft t1) (ppr g e1 t1)]
[(tright t2) (ppr g e2 t2)]
[t1 (raise 'type-error)]))
(define (ppr-star g e t)
(match t
[(tlist ts)
(append-map
(lambda (t1) (ppr g e t1))
ts)]
[t1 (raise 'type-error)]))
(define (ppr-neg g e t)
(match t
[(tunit) '()]
[t1 (raise 'type-error)]))
(define (ppr-push g e t)
(match t
[(tpush t) (ppr g e t)]
[t1 (raise 'type-error)]))
(define (ppr-drop g t)
(match t
[(tdrop) '()]
[t1 (raise 'type-error)]))
(define (ppr-peekall g t)
(match t
[(tpeekall t) t]
[t1 (raise 'type-error)]))
(define (ppr-popall g t)
(match t
[(tpopall t) t]
[t1 (raise 'type-error)]))
(define (ppr-peek g t)
(match t
[(tpeek t) t]
[t1 (raise 'type-error)]))
(define (ppr-poop g t)
(match t
[(tpeek t) t]
[t1 (raise 'type-error)]))
(define (ppr g e t)
(match e
[(peps) (ppr-eps t)]
[(pchr c) (ppr-chr c t)]
[(pvar v) (ppr-var g v t)]
[(pcat e1 e2) (ppr-cat g e1 e2 t)]
[(pchoice e1 e2) (ppr-choice g e1 e2 t)]
[(pstar e1) (ppr-star g e1 t)]
[(pneg e1) (ppr-neg g e1 t)]
[(ppop) (ppr-poop g t)]
[(ppush e1) (ppr-push g e1 t)]
[(pdrop) (ppr-drop g t)]
[(ppeek) (ppr-peek g t)]
[(ppopall) (ppr-popall g t)]
[(ppeekall) (ppr-peekall g t)]))