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)
23 (alloc-value type)) ;; TODO use dims to allocate
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 body-so-far (list ast)))))))))
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.continue source)
150 (continue source cte cont))
151 ((form? 'six.goto source)
152 (goto source cte cont))
153 ((form? 'six.compound source)
154 (block source cte cont))
156 (expression source cte cont))))
158 (define (return source cte cont)
160 (define (ret asts cte)
161 (cont (new-return asts)
164 (if (null? (cdr source))
166 (expression (cadr source)
169 (ret (list ast) cte)))))
171 (define (break source cte cont)
175 (define (continue source cte cont)
179 (define (goto source cte cont)
180 (cont (new-goto (cadadr source)) ; label
183 (define (if1 source cte cont)
184 (expression (cadr source)
187 (statement (caddr source)
190 (cont (new-if (list ast1 ast2))
193 (define (if2 source cte cont)
194 (expression (cadr source)
197 (statement (caddr source)
200 (statement (cadddr source)
203 (cont (new-if (list ast1 ast2 ast3))
206 (define (switch source cte cont)
207 (expression (cadr source)
209 (lambda (ast1 cte) ; we matched the paren expr
210 (expect-form 'six.compound (caddr source))
211 (block (caddr source)
214 (cont (new-switch (cons ast1 (ast-subasts ast2))) ; we only need the contents of the generated block, which would be a named block list
217 (define (while source cte cont)
218 (expression (cadr source)
221 (statement (caddr source)
224 (cont (new-while (list ast1 ast2))
227 (define (do-while source cte cont)
228 (statement (cadr source)
231 (expression (caddr source)
234 (cont (new-do-while (list ast1 ast2))
237 (define (for source cte cont)
239 (define (opt-expr source cte cont)
241 (expression source cte cont)
244 (statement (cadr source)
247 (opt-expr (caddr source)
250 (opt-expr (cadddr source)
253 (statement (car (cddddr source))
259 (new-literal 'int 1))
265 (define (expression source cte cont)
266 (cond ((form? 'six.literal source)
267 (literal source cte cont))
268 ((form? 'six.identifier source)
269 (ref source cte cont))
270 ((form? 'six.call source)
271 (call source cte cont))
275 (operation op source cte cont)))
277 (error "expected expression" source))))
279 (define (operation op source cte cont)
281 (expression (cadr source)
285 (new-oper (list ast1) #f op)))
286 (expr-type-set! ast ((op-type-rule op) ast))
287 (cont ((op-constant-fold op) ast)
289 (expression (cadr source)
292 (expression (caddr source)
296 (new-oper (list ast1 ast2) #f op)))
297 (expr-type-set! ast ((op-type-rule op) ast))
298 (cont ((op-constant-fold op) ast)
301 (define (call source cte cont)
302 (let* ((id (get-id (cadr source)))
303 (binding (cte-lookup cte id)))
304 (if (def-procedure? binding)
305 (expressions (cddr source)
308 (cont (new-call args (def-procedure-type binding) binding)
310 (error "expected procedure" source))))
312 (define (expressions source cte cont)
313 (cond ((null? source)
317 (let ((head (car source))
325 (cont (cons ast asts)
328 (define (literal source cte cont)
329 (cont (new-literal 'int (cadr source))
332 (define (ref source cte cont)
333 (let* ((id (cadr source))
334 (binding (cte-lookup cte id)))
335 (if (def-variable? binding)
336 (cont (new-ref (def-variable-type binding) binding)
338 (error "expected variable" source))))
340 (define (toplevel source cte cont) ;; TODO have an implicit main
341 (cond ((form? 'six.define-variable source)
342 (define-variable source cte cont))
343 ((form? 'six.define-procedure source)
344 (define-procedure source cte cont))
346 (statement source cte cont))))
348 (define (program source cte cont)
350 (define (p source cte cont)
351 (cond ((null? source)
355 (let ((head (car source))
363 (cont (cons ast asts)
369 (cont (new-program asts)