3 (define (form? keyword source)
5 (eq? (car source) keyword)))
7 (define (expect-form keyword source)
8 (if (not (form? keyword source))
9 (error "expected" keyword source)))
11 (define (get-id source)
12 (expect-form 'six.identifier source)
15 (define (define-variable source cte cont)
16 (let* ((id (get-id (cadr source)))
18 (dims (cadddr source))
19 (val (car (cddddr source))))
21 (define (def asts cte)
25 (new-def-variable asts id '() type value '()))
27 (cte-extend cte (list ast))))
32 (expression val cte (lambda (ast cte) (def (list ast) cte)))
35 (define (define-procedure source cte cont)
36 (let* ((id (get-id (cadr source)))
37 (proc (caddr source)))
38 (expect-form 'six.procedure proc)
47 (new-def-variable '() (get-id (car x)) '() type value '())))
51 (expect-form 'six.procedure-body body)
55 (new-def-procedure '() id '() type value params))
57 (cte-extend cte (list ast))))
58 (multi-link-parent! params ast)
60 (cte-extend cte params)
61 (lambda (body-ast body-cte)
62 (ast-subasts-set! ast (list body-ast))
63 (link-parent! body-ast ast)
67 (define (block source cte cont)
68 (define (b source cte cont)
71 (let ((head (car source))
73 (if (or (form? 'six.label head) ; we complete the block with a list of named blocks
74 (form? 'six.case head))
75 (named-block-list source
77 cont) ; will return a list of named blocks
89 (cont (new-block asts)
92 ;; returns a list of the named blocks (implicit blocks delimited by labels) present in the given tree
93 ;; useful for switch and goto
94 (define (named-block-list source cte cont)
95 (define (b source cte cont name body-so-far)
97 (cont (list (new-named-block name body-so-far)) ; last block
99 (let ((curr (car source)))
100 (if (or (form? 'six.label curr) ; we reached another named block
101 (form? 'six.case curr))
102 (named-block-list source
104 (lambda (named-blocks cte)
105 (cont (cons (new-named-block name body-so-far)
115 (append (list ast) body-so-far))))))))
123 (if (form? 'six.case (car source)) ; the label is a case
124 (literal (cadar source)
127 (new-cont (list 'case (literal-val name)) cte)))
128 (new-cont (cadar source) cte)))) ; ordinary label
130 (define (statement source cte cont)
131 (cond ((form? 'six.define-variable source)
132 (define-variable source cte cont))
133 ((form? 'six.if source)
134 (if (null? (cdddr source))
135 (if1 source cte cont)
136 (if2 source cte cont)))
137 ((form? 'six.switch source)
138 (switch source cte cont))
139 ((form? 'six.while source)
140 (while source cte cont))
141 ((form? 'six.do-while source)
142 (do-while source cte cont))
143 ((form? 'six.for source)
144 (for source cte cont))
145 ((form? 'six.return source)
146 (return source cte cont))
147 ((form? 'six.break source)
148 (break source cte cont))
149 ((form? 'six.goto source)
150 (goto source cte cont))
151 ((form? 'six.compound source)
152 (block source cte cont))
154 (expression source cte cont))))
156 (define (return source cte cont)
158 (define (ret asts cte)
159 (cont (new-return asts)
162 (if (null? (cdr source))
164 (expression (cadr source)
167 (ret (list ast) cte)))))
169 (define (break source cte cont)
173 (define (goto source cte cont)
174 (cont (new-goto (cadadr source)) ; label
177 (define (if1 source cte cont)
178 (expression (cadr source)
181 (statement (caddr source)
184 (cont (new-if (list ast1 ast2))
187 (define (if2 source cte cont)
188 (expression (cadr source)
191 (statement (caddr source)
194 (statement (cadddr source)
197 (cont (new-if (list ast1 ast2 ast3))
200 (define (switch source cte cont)
201 (expression (cadr source)
203 (lambda (ast1 cte) ; we matched the paren expr
204 (expect-form 'six.compound (caddr source))
205 (block (caddr source)
208 (cont (new-switch (list ast1 ast2))
211 (define (while source cte cont)
212 (expression (cadr source)
215 (statement (caddr source)
218 (cont (new-while (list ast1 ast2))
221 (define (do-while source cte cont)
222 (statement (cadr source)
225 (expression (caddr source)
228 (cont (new-do-while (list ast1 ast2))
231 (define (for source cte cont)
233 (define (opt-expr source cte cont)
235 (expression source cte cont)
238 (statement (cadr source)
241 (opt-expr (caddr source)
244 (opt-expr (cadddr source)
247 (statement (car (cddddr source))
253 (new-literal 'int 1))
259 (define (expression source cte cont)
260 (cond ((form? 'six.literal source)
261 (literal source cte cont))
262 ((form? 'six.identifier source)
263 (ref source cte cont))
264 ((form? 'six.call source)
265 (call source cte cont))
269 (operation op source cte cont)))
271 (error "expected expression" source))))
273 (define (operation op source cte cont)
275 (expression (cadr source)
279 (new-oper (list ast1) #f op)))
280 (expr-type-set! ast ((op-type-rule op) ast))
281 (cont ((op-constant-fold op) ast)
283 (expression (cadr source)
286 (expression (caddr source)
290 (new-oper (list ast1 ast2) #f op)))
291 (expr-type-set! ast ((op-type-rule op) ast))
292 (cont ((op-constant-fold op) ast)
295 (define (call source cte cont)
296 (let* ((id (get-id (cadr source)))
297 (binding (cte-lookup cte id)))
298 (if (def-procedure? binding)
299 (expressions (cddr source)
302 (cont (new-call args (def-procedure-type binding) binding)
304 (error "expected procedure" source))))
306 (define (expressions source cte cont)
307 (cond ((null? source)
311 (let ((head (car source))
319 (cont (cons ast asts)
322 (define (literal source cte cont)
323 (cont (new-literal 'int (cadr source))
326 (define (ref source cte cont)
327 (let* ((id (cadr source))
328 (binding (cte-lookup cte id)))
329 (if (def-variable? binding)
330 (cont (new-ref (def-variable-type binding) binding)
332 (error "expected variable" source))))
334 (define (toplevel source cte cont) ;; TODO have an implicit main
335 (cond ((form? 'six.define-variable source)
336 (define-variable source cte cont))
337 ((form? 'six.define-procedure source)
338 (define-procedure source cte cont))
340 (statement source cte cont))))
342 (define (program source cte cont)
344 (define (p source cte cont)
345 (cond ((null? source)
349 (let ((head (car source))
357 (cont (cons ast asts)
363 (cont (new-program asts)