-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.rkt
183 lines (160 loc) · 5.34 KB
/
parser.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#lang racket
(require "core.rkt"
"tree.rkt")
(provide (rename-out [parse peg-parse]))
(define (flattent t)
(match t
[(tunit) '()]
[(tchr c) (list c)]
[(tpair f s) (append (flattent f) (flattent s))]
[(tleft t) (flattent t)]
[(tright t) (flattent t)]
[(tlist xs) (flatten (map flattent xs))]
[(tpop xs) (flatten (map flattent xs))]
[(tpush xs) (flatten (map flattent xs))]
[(tdrop) '()]
[(tpeek xs) (flatten (map flattent xs))]
[(tpeek xs) (flatten (map flattent xs))]
[(tpeekall xs) (flatten (map flattent xs))]
[(tpopall xs) (flatten (map flattent xs))]
)
)
(define (match-input x s)
(cond [(null? x) s]
[(null? s) #f]
[(char=? (car x) (car s)) (match-input (cdr x) (cdr s))]
[else #f]
)
)
;; definition of the top level parser
(define (parse g s)
(match g
[(peg-grammar rs p)
(let* ([inp (string->list s)]
[r (run-expr '() rs p inp)])
(if (null? r)
(displayln "Could not parse the input string!")
r))]))
(define (run-eps stk s)
(list (tunit) s stk))
(define (run-chr stk c s)
(match s
['() '()]
[(cons c1 s1)
(if (eq? c c1)
(list (tchr c) stk s1)
'())]))
(define (run-any stk s)
(match s
['() '()]
[(cons c s1) (list (tchr c) stk s1)]))
(define (run-var stk g v s)
(match (assoc v g)
[#f (begin
(printf "Undefined variable: ~a\n~a\n" v g)
'())]
[(cons _ e1) (run-expr stk g e1 s)]))
(define (run-cat stk g e1 e2 s)
(match (run-expr stk g e1 s)
['() '()]
[(list t1 stk1 s1)
(match (run-expr stk1 g e2 s1)
['() '()]
[(list t2 stk2 s2)
(list (tpair t1 t2) stk2 s2)])]))
(define (run-choice stk g e1 e2 s)
(match (run-expr stk g e1 s)
['() (match (run-expr stk g e2 s)
['() '()]
[(list t2 stk2 s2)
(list (tright t2) stk2 s2)])]
[(list t1 stk1 s1)
(list (tleft t1) stk1 s1)]))
(define (run-neg stk g e1 s)
(match (run-expr stk g e1 s)
['() (cons (tunit) stk s)]
[(cons t s1) '()]))
(define (run-star stk g e s)
(match (run-expr stk g e s)
['() (list (tlist '()) stk s)]
[(list t stk1 s1)
(match (run-expr stk1 g (pstar e) s1)
['() (list (tlist (list t)) stk1 s1)]
[(list (tlist t2) stk2 s2)
(list (tlist (cons t t2)) stk2 s2)]
[(list t stk2 s2) (raise 'invalid-tree)])]))
(define (run-repeat-exact stk n g e s)
(cond
[(<= n 0) (list (tlist '()) stk s)]
[else (match (run-expr stk g e s)
['() '()]
[(list t stk1 s1)
(match (run-repeat-exact stk1 (- n 1) g e s1)
['() '()]
[(list (tlist t2) stk2 s2)
(list (tlist (cons t t2)) stk2 s2)]
[(list t stk2 s2) (raise 'invalid-tree)])])]))
(define (run-push stk g e s)
(match (run-expr stk g e s)
['() '()]
[(list t stk1 s1) (list (tpush t) (cons (flattent t) stk1) s1) ]))
(define (run-pop stk g s)
(if (null? stk)
'()
(match (match-input (car stk) s)
[#f '()]
[s1 (list (tpop (car stk)) (cdr stk) s1)])))
(define (run-peek stk g s)
(if (null? stk)
'()
(match (match-input (car stk) s)
[#f '()]
[s1 (list (tpop (car stk)) stk s1)])))
(define (run-drop stk g s)
(if (null? stk)
'()
(list (tdrop) (cdr stk) s)))
(define (run-repeat-while stk t f g e s)
(cond [(<= f 0) (list t stk s)]
[else (match (run-expr stk g e s)
['() (list t stk s)]
[(list t1 stk1 s1) (run-repeat-while stk1
(append t (list t1))
(- f 1)
g e s)]) ])
)
(define (run-repeat-interval stk i f g e s)
(cond
[(< f i) (list (tlist '()) stk s)]
[else (match (run-repeat-exact stk i g e s)
['() '()]
[(list t stk1 s1) (run-repeat-while stk1 (list t) f g e s1)])]))
(define (eval stk e)
(match e
[(ax-lit n) n]
[(ax-var 'len) (length (car stk))]
[(ax-var 'tonat) (let ([k (string->number (list->string (car stk)))])
(if (not k) (raise 'numberFormatFail) k))]
[(ax-op '+ e d) (+ (eval stk e) (eval stk d))]
[(ax-op '- e d) (max 0 (- (eval stk e) (eval stk d)))]
[(ax-op '* e d) (* (eval stk e) (eval stk d))]
)
)
(define (run-expr stk g e s)
(match e
[(peps) (run-eps stk s)]
[(pchr c) (run-chr stk c s)]
[(pany) (run-any stk s)]
[(pvar v) (run-var stk g v s)]
[(pcat e1 e2) (run-cat stk g e1 e2 s)]
[(pchoice e1 e2) (run-choice stk g e1 e2 s)]
[(pneg e1) (run-neg stk g e1 s)]
[(pstar e1) (run-star stk g e1 s)]
[(prepeat-exact e1 ae) (run-repeat-exact stk (eval stk ae) g e1 s)]
[(prepeat-interval e1 ai af) (run-repeat-interval stk (eval stk ai) (eval stk af) g e1 s)]
[(ppush e1) (run-push stk g e1 s)]
[(ppeek) (run-peek stk g s)]
[(ppop) (run-pop stk g s)]
[(pdrop) (run-drop stk g s)]
;[(ppopall) (run-expr (cdr stk) g e s)]
))