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 ;; variables which, when found in programs, have special meanings
22 ;; when any of these are encountered, its associated thunk is
24 (define special-variables
25 (list ;; TODO fit with the predefined variables in cte.scm ?
26 (cons 'SIXPIC_MEMORY_DIVIDE
28 (set! memory-divide (cadr val)) ; must be a literal
29 (expression val cte (lambda (ast cte)
30 (def (list ast) cte)))))))
33 (define (def asts cte)
35 (alloc-value type id))
37 (new-def-variable asts id '() type value '()))
39 (cte-extend cte (list ast))))
42 ;; if it's a special variable, call its associated thunk instead
43 (let ((target (assq id special-variables)))
47 (expression val cte (lambda (ast cte) (def (list ast) cte)))
50 (define (define-procedure source cte cont)
51 (let* ((id (get-id (cadr source)))
52 (proc (caddr source)))
53 (expect-form 'six.procedure proc)
58 (let* ((id (get-id (car x)))
60 (value (alloc-value type id)))
61 (new-def-variable '() id '() type value '())))
65 (expect-form 'six.procedure-body body)
67 (alloc-value type id))
69 (new-def-procedure '() id '() type value params))
71 (cte-extend cte (list ast))))
72 (multi-link-parent! params ast)
74 (cte-extend cte params)
75 (lambda (body-ast body-cte)
76 (ast-subasts-set! ast (list body-ast))
77 (link-parent! body-ast ast)
81 (define (block source cte cont)
82 (define (b source cte cont)
85 (let ((head (car source))
87 (if (or (form? 'six.label head) ; we complete the block with a list of named blocks
88 (form? 'six.case head))
89 (named-block-list source
91 cont) ; will return a list of named blocks
103 (cont (new-block asts)
106 ;; handles named blocks (implicit blocks delimited by labels)
107 ;; useful for switch and goto
108 (define (named-block-list source cte cont)
109 (define (b source cte cont name body-so-far)
111 (cont (list (new-named-block name body-so-far)) ; last block
113 (let ((curr (car source)))
114 (if (or (form? 'six.label curr) ; we reached another named block
115 (form? 'six.case curr))
116 (named-block-list source
118 (lambda (named-blocks cte)
119 (cont (cons (new-named-block name body-so-far)
129 (append body-so-far (list ast)))))))))
132 (statement (caddar source)
139 ;; the first statement is in the case/label form
142 (if (form? 'six.case (car source)) ; the label is a case
143 (literal (cadar source)
146 (new-cont (list 'case (literal-val name)) cte)))
147 (new-cont (cadar source) cte)))) ; ordinary label
149 (define (statement source cte cont)
150 (cond ((form? 'six.define-variable source)
151 (define-variable source cte cont))
152 ((form? 'six.if source)
153 (if (null? (cdddr source))
154 (if1 source cte cont)
155 (if2 source cte cont)))
156 ((form? 'six.switch source)
157 (switch source cte cont))
158 ((form? 'six.while source)
159 (while source cte cont))
160 ((form? 'six.do-while source)
161 (do-while source cte cont))
162 ((form? 'six.for source)
163 (for source cte cont))
164 ((form? 'six.return source)
165 (return source cte cont))
166 ((form? 'six.break source)
167 (break source cte cont))
168 ((form? 'six.continue source)
169 (continue source cte cont))
170 ((form? 'six.goto source)
171 (goto source cte cont))
172 ((form? 'six.compound source)
173 (block source cte cont))
175 (expression source cte cont))))
177 (define (return source cte cont)
179 (define (ret asts cte)
180 (cont (new-return asts)
183 (if (null? (cdr source))
185 (expression (cadr source)
188 (ret (list ast) cte)))))
190 (define (break source cte cont)
194 (define (continue source cte cont)
198 (define (goto source cte cont)
199 (cont (new-goto (cadadr source)) ; label
202 (define (if1 source cte cont)
203 (expression (cadr source)
206 (statement (caddr source)
209 (cont (new-if (list ast1 ast2))
212 (define (if2 source cte cont)
213 (expression (cadr source)
216 (statement (caddr source)
219 (statement (cadddr source)
222 (cont (new-if (list ast1 ast2 ast3))
225 (define (switch source cte cont)
226 (expression (cadr source)
228 (lambda (ast1 cte) ; we matched the paren expr
229 (expect-form 'six.compound (caddr source))
230 (block (caddr source)
233 (cont (new-switch (cons ast1 (ast-subasts ast2)))
236 (define (while source cte cont)
237 (expression (cadr source)
240 (statement (caddr source)
243 (cont (new-while (list ast1 ast2))
246 (define (do-while source cte cont)
247 (statement (cadr source)
250 (expression (caddr source)
253 (cont (new-do-while (list ast1 ast2))
256 (define (for source cte cont)
258 (define (opt-expr source cte cont)
260 (expression source cte cont)
263 (statement (cadr source)
266 (opt-expr (caddr source)
269 (opt-expr (cadddr source)
272 (statement (car (cddddr source))
278 (new-literal 'byte 1))
284 (define (expression source cte cont)
285 (cond ((form? 'six.literal source)
286 (literal source cte cont))
287 ((form? 'six.prefix source)
288 ;; this is a hack to support hexadecimal values
289 ;; SIX does not parse C-style hex values (0x...), but falls back on
290 ;; the regular parser when it encounters Scheme-style hex values
291 ;; (#x...) and wraps them in a six.prefix.
292 ;; TODO support C-style hex values
293 (literal `(six.literal ,(cadr source)) cte cont))
294 ((form? 'six.identifier source)
295 (ref source cte cont))
296 ((form? 'six.call source)
297 (call source cte cont))
301 (operation op source cte cont)))
303 (error "expected expression" source))))
305 (define (operation op source cte cont)
306 (define (continue ast cte)
307 (expr-type-set! ast ((op-type-rule op) ast))
308 (cont (if fold-constants? ((op-constant-fold op) ast) ast)
315 (continue (new-oper (list ast1) #f op) cte))))
325 (continue (new-oper (list ast1 ast2) #f op) cte))))))
326 (else ; ternary operator
339 (continue (new-oper (list ast1 ast2 ast3) #f op)
342 (define (call source cte cont)
343 (let* ((id (get-id (cadr source)))
344 (binding (cte-lookup cte id)))
345 (if (def-procedure? binding)
346 (expressions (cddr source)
349 (cont (new-call args (def-procedure-type binding) binding)
351 (error "expected procedure" source))))
353 (define (expressions source cte cont)
354 (cond ((null? source)
358 (let ((head (car source))
366 (cont (cons ast asts)
369 (define (literal source cte cont)
370 (let ((n (cadr source)))
371 (cont (new-literal (val->type n) n)
374 (define (ref source cte cont)
375 (let* ((id (cadr source))
376 (binding (cte-lookup cte id)))
377 (if (def-variable? binding)
378 (cont (new-ref (def-variable-type binding) binding)
380 (error "expected variable" source))))
382 (define (toplevel source cte cont) ;; TODO have an implicit main
383 (cond ((form? 'six.define-variable source)
384 (define-variable source cte cont))
385 ((form? 'six.define-procedure source)
386 (define-procedure source cte cont))
388 (statement source cte cont))))
390 (define (program source cte cont)
392 (define (p source cte cont)
393 (cond ((null? source)
397 (let ((head (car source))
405 (cont (cons ast asts)
411 (cont (new-program asts)