-
Notifications
You must be signed in to change notification settings - Fork 0
/
Semantic.scm
302 lines (295 loc) · 8.36 KB
/
Semantic.scm
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
;;; fcl semantic checker
(define-structure
(Entry
(constructor Entry.new (name params body condn))
)
name ; symbol
params ; [symbol]
body ; [Node]
condn ; [Node]
)
(define (Entry.unpack p)
(list
"name: " (entry-name p)
", params: " (entry-params p)
", body: " (map node.unpack (entry-body p))
", condn: " (map node.unpack (entry-condn p)))
)
; lookup : [Entry],symbol(,symbol) -> Entry
(define (lookup table name . supername)
;(if debug.semantic (fmt "lookup" (if (null? table) '() (map entry.unpack table)) name supername))
(if (not (symbol? name))
(begin
(fmt "internal error (lookup): name must be a symbol")
'!internal-error
)
(let global-loop ((l table)) ; look up globally
(if (null? l)
(if (null? supername)
#f
(let ((entry (lookup table (car supername)))) ; look up supername globally
(cond
((not entry) #f)
((eq? (entry-name entry) name) entry) ; either the name was found as a global symbol
(else
(let param-loop ((p (entry-params entry))) ; or we step through the parameters
(if (null? p)
#f
(if (eq? (car p) name)
(entry.new '$pseudo '() '() '()) ; and return a pseudo entry
(param-loop (cdr p))
)
)
)
)
)
)
)
(if (eq? (entry-name (car l)) name)
(car l)
(global-loop (cdr l))
)
)
)
)
)
; insert : [Entry],symbol,Entry -> [Entry]
(define (insert table name entry)
(let loop ((l table))
;(if debug.semantic (fmt "insert" name value "=" l))
(let ((e (entry.new name (entry-params entry) (entry-body entry) (entry-condn entry))))
(if (null? l)
(cons e '())
(if (eq? (entry-name (car l)) name)
(cons e (cdr l))
(cons (car l) (loop (cdr l)))
)
)
)
)
)
; find-next : [Node],symbol,symbol -> [Node]|boolean
(define (find-next s0 n fail)
(let goto ((s s0))
(cond
((or (null? s) (null? (cdr s))) #f)
((eq? (node-name (cadr s)) n) (cdr s))
((eq? (node-name (cadr s)) fail) #f)
(else (goto (cdr s)))
)
)
)
; semantic : [Node],[Entry]->[Entry]
(define (semantic syntax0)
; Nodes:
; begin-def <name>, end-def
; begin-pars, end-pars
; begin-args, end-args
; begin-term, end-term
; begin-cond, end-cond, cond-else
; val <value>
; const <name>
; add, sub, mul, div
(call/cc (lambda (return)
; get-params : [Node] -> [Node],[symbol]
(define (get-params s0)
(let get-params-loop ((s s0) (p '()))
(if debug.semantic (fmt "get-params" (node.unpack (car s)) p))
(let ((nodetype (node-name (car s))) (parname (node-op (car s))))
(case nodetype
((begin-pars) (get-params-loop (cdr s) p)) ; skip starting node
((end-pars) (cons (cdr s) (reverse p)))
((const)
(if (not (contains p parname))
(get-params-loop (cdr s) (cons parname p))
(begin
(fmt "error: duplicate parameter" parname)
'!semantic-error
)
)
)
(else
(fmt "internal error: illegal syntax node" nodetype "in get-params")
'!internal-error
)
)
)
)
)
; get-args : [Node],[symbol],[Entry],symbol,symbol -> [Node],[Node]
(define (get-args s0 p table proc callee)
(define pc (length p))
(let get-args-loop ((s s0) (a '()) (ac 0))
(if debug.semantic (fmt "get-args" (node.unpack (car s)) p ac))
(let ((type (node-name (car s))) (value (node-op (car s))))
(case type
((begin-args)
(get-args-loop (cdr s) (cons (car s) a) ac)
)
((end-args)
(if (= ac pc)
(cons (cdr s) (cons (car s) a))
(begin
(fmt "argument list does not match parameters of " callee)
(return '!semantic-error)
)
)
)
((begin-term)
(let* ((r1 (get-term s table proc)) (s1 (car r1)) (t (cdr r1)))
(get-args-loop s1 (cons (node.new 'end-term '()) (append t (cons (car s) a))) (+ ac 1))
)
)
(else
(fmt "internal error: unknown node type" type "in get-args")
(return '!internal-error)
)
)
)
)
)
; get-term : [Node],[Entry],symbol -> [Node],[Node]
(define (get-term s0 table proc)
(let get-term-loop ((s s0) (t '()) (td 0))
(if debug.semantic (fmt "get-term" (node.unpack (car s)) td))
(let ((nodetype (node-name (car s))) (value (node-op (car s))))
(case nodetype
((begin-term) (get-term-loop (cdr s) (if (= td 0) t (cons (car s) t)) (+ td 1)))
((end-term)
(if (= td 1)
(cons (cdr s) t)
(get-term-loop (cdr s) (cons (car s) t) (- td 1))
)
)
((const)
(let ((entry (lookup table value proc)))
(if entry
(if (eq? (node-name (cadr s)) 'begin-args)
(let* ((r1 (get-args (cdr s) (entry-params entry) table proc value)) (s1 (car r1)) (args (cdr r1)))
(get-term-loop s1 (append args (cons (car s) t)) td)
)
(begin
(fmt "internal error: const node without args")
(return '!internal-error)
)
)
(begin
(fmt "error: reference to unknown constant" value)
(return '!semantic-error)
)
)
)
)
((val add sub mul div) (get-term-loop (cdr s) (cons (car s) t) td))
(else
(fmt "internal error: illegal syntax node" nodetype "in get-term")
(return '!internal-error)
)
)
)
)
)
; get-condn : [Node],[Entry],symbol -> [Node],[Node]
(define (get-condn s0 table proc)
(let get-condn-loop ((s s0) (c '()))
(if debug.semantic (fmt "get-condn" (node.unpack (car s))))
(let ((nodetype (node-name (car s))) (value (node-op (car s))))
(case nodetype
((begin-cond) (get-condn-loop (cdr s) c))
((end-cond)
(cons (cdr s) c))
((const val eq ne gt lt)
(get-condn-loop (cdr s) (cons (car s) c))
)
((begin-args)
(let ((entry (lookup table (node-op (car c)) proc)))
(if entry
(let* ((r1 (get-args (cdr s) (entry-params entry) table proc (node-op (car c)))) (s1 (car r1)) (args (cdr r1)))
(get-condn-loop s1 (append args (cons (car s) c)))
)
(begin
(fmt "error: reference to unknown constant" (node-op (car c)))
(return '!semantic-error)
)
)
)
)
((cond-else) (get-condn-loop (cdr s) (cons (car s) c)))
((begin-term)
; no guard given: return
(cons s0 '())
)
(else
(fmt "internal error: illegal syntax node" nodetype "in get-condn")
(return '!internal-error)
)
)
)
)
)
(define preloaded
(let semantic-preload ((syntax syntax0) (table '()))
(if (null? syntax)
table
(let ((x (node-name (car syntax))) (y (node-op (car syntax))))
(case x
((begin-def)
(if (not (lookup table y))
(let* ((r (get-params (cdr syntax))) (s (car r)) (p (cdr r)))
(semantic-preload s (insert table y (entry.new y p '() '())))
)
(begin
(fmt "error: redefinition of function" y)
(return '!semantic-error)
)
)
)
(else (semantic-preload (cdr syntax) table))
)
)
)
)
)
(let semantic-loop ((syntax syntax0) (table preloaded))
(if debug.semantic
(fmt "semantic" (if (null? syntax) '() (node.unpack (car syntax))) (map entry.unpack table))
)
(if (null? syntax)
(begin
(fmt "semantic analysis succeeded, created" (length table) "table entries")
table
)
(let ((x (node-name (car syntax))) (y (node-op (car syntax))))
(case x
((begin-def)
(let* ((pars (entry-params (lookup table y)))
(r2
(let ((s1 (find-next (cdr syntax) 'begin-cond 'begin-term)))
(if s1
(get-condn s1 table y)
(cons (find-next (cdr syntax) 'begin-term '()) '())
)
)
)
(s2 (car r2)) (condn (reverse (cdr r2))) (t2 (insert table y (entry.new y pars '() condn)))
(r3 (get-term s2 t2 y)) (s3 (car r3)) (body (reverse (cdr r3))) (t3 (insert table y (entry.new y pars body condn)))
)
(if (eq? (node-name (car s3)) 'end-def)
(semantic-loop (cdr s3) t3)
(begin
(fmt "internal error: unterminated function" y)
(return '!internal-error)
)
)
)
)
(else
(fmt "unknown symbol" x)
(return '!internal-error)
)
)
)
)
)
))
)