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)
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)
62 (new-def-variable '() (get-id (car x)) '() type value '())))
66 (expect-form 'six.procedure-body body)
70 (new-def-procedure '() id '() type value params))
72 (cte-extend cte (list ast))))
73 (multi-link-parent! params ast)
75 (cte-extend cte params)
76 (lambda (body-ast body-cte)
77 (ast-subasts-set! ast (list body-ast))
78 (link-parent! body-ast ast)
82 (define (block source cte cont)
83 (define (b source cte cont)
86 (let ((head (car source))
88 (if (or (form? 'six.label head) ; we complete the block with a list of named blocks
89 (form? 'six.case head))
90 (named-block-list source
92 cont) ; will return a list of named blocks
104 (cont (new-block asts)
107 ;; handles named blocks (implicit blocks delimited by labels)
108 ;; useful for switch and goto
109 (define (named-block-list source cte cont)
110 (define (b source cte cont name body-so-far)
112 (cont (list (new-named-block name body-so-far)) ; last block
114 (let ((curr (car source)))
115 (if (or (form? 'six.label curr) ; we reached another named block
116 (form? 'six.case curr))
117 (named-block-list source
119 (lambda (named-blocks cte)
120 (cont (cons (new-named-block name body-so-far)
130 (append body-so-far (list ast)))))))))
133 (statement (caddar source)
140 ;; the first statement is in the case/label form
143 (if (form? 'six.case (car source)) ; the label is a case
144 (literal (cadar source)
147 (new-cont (list 'case (literal-val name)) cte)))
148 (new-cont (cadar source) cte)))) ; ordinary label
150 (define (statement source cte cont)
151 (cond ((form? 'six.define-variable source)
152 (define-variable source cte cont))
153 ((form? 'six.if source)
154 (if (null? (cdddr source))
155 (if1 source cte cont)
156 (if2 source cte cont)))
157 ((form? 'six.switch source)
158 (switch source cte cont))
159 ((form? 'six.while source)
160 (while source cte cont))
161 ((form? 'six.do-while source)
162 (do-while source cte cont))
163 ((form? 'six.for source)
164 (for source cte cont))
165 ((form? 'six.return source)
166 (return source cte cont))
167 ((form? 'six.break source)
168 (break source cte cont))
169 ((form? 'six.continue source)
170 (continue source cte cont))
171 ((form? 'six.goto source)
172 (goto source cte cont))
173 ((form? 'six.compound source)
174 (block source cte cont))
176 (expression source cte cont))))
178 (define (return source cte cont)
180 (define (ret asts cte)
181 (cont (new-return asts)
184 (if (null? (cdr source))
186 (expression (cadr source)
189 (ret (list ast) cte)))))
191 (define (break source cte cont)
195 (define (continue source cte cont)
199 (define (goto source cte cont)
200 (cont (new-goto (cadadr source)) ; label
203 (define (if1 source cte cont)
204 (expression (cadr source)
207 (statement (caddr source)
210 (cont (new-if (list ast1 ast2))
213 (define (if2 source cte cont)
214 (expression (cadr source)
217 (statement (caddr source)
220 (statement (cadddr source)
223 (cont (new-if (list ast1 ast2 ast3))
226 (define (switch source cte cont)
227 (expression (cadr source)
229 (lambda (ast1 cte) ; we matched the paren expr
230 (expect-form 'six.compound (caddr source))
231 (block (caddr source)
234 (cont (new-switch (cons ast1 (ast-subasts ast2)))
237 (define (while source cte cont)
238 (expression (cadr source)
241 (statement (caddr source)
244 (cont (new-while (list ast1 ast2))
247 (define (do-while source cte cont)
248 (statement (cadr source)
251 (expression (caddr source)
254 (cont (new-do-while (list ast1 ast2))
257 (define (for source cte cont)
259 (define (opt-expr source cte cont)
261 (expression source cte cont)
264 (statement (cadr source)
267 (opt-expr (caddr source)
270 (opt-expr (cadddr source)
273 (statement (car (cddddr source))
279 (new-literal 'byte 1))
285 (define (expression source cte cont)
286 (cond ((form? 'six.literal source)
287 (literal source cte cont))
288 ((form? 'six.prefix source)
289 ;; this is a hack to support hexadecimal values
290 ;; SIX does not parse C-style hex values (0x...), but falls back on
291 ;; the regular parser when it encounters Scheme-style hex values
292 ;; (#x...) and wraps them in a six.prefix.
293 ;; TODO support C-style hex values
294 (literal `(six.literal ,(cadr source)) cte cont))
295 ((form? 'six.identifier source)
296 (ref source cte cont))
297 ((form? 'six.call source)
298 (call source cte cont))
302 (operation op source cte cont)))
304 (error "expected expression" source))))
306 (define (operation op source cte cont)
307 (define (continue ast cte)
308 (expr-type-set! ast ((op-type-rule op) ast))
309 (cont ((op-constant-fold op) ast)
316 (continue (new-oper (list ast1) #f op) cte))))
326 (continue (new-oper (list ast1 ast2) #f op) cte))))))
327 (else ; ternary operator
340 (continue (new-oper (list ast1 ast2 ast3) #f op)
343 (define (call source cte cont)
344 (let* ((id (get-id (cadr source)))
345 (binding (cte-lookup cte id)))
346 (if (def-procedure? binding)
347 (expressions (cddr source)
350 (cont (new-call args (def-procedure-type binding) binding)
352 (error "expected procedure" source))))
354 (define (expressions source cte cont)
355 (cond ((null? source)
359 (let ((head (car source))
367 (cont (cons ast asts)
370 (define (literal source cte cont)
371 (let ((n (cadr source)))
372 ;; TODO might need to be expanded
373 (cont (new-literal (cond ((and (>= n 0) (< n 256))
375 ((and (>= n 0) (< n 65536))
382 (define (ref source cte cont)
383 (let* ((id (cadr source))
384 (binding (cte-lookup cte id)))
385 (if (def-variable? binding)
386 (cont (new-ref (def-variable-type binding) binding)
388 (error "expected variable" source))))
390 (define (toplevel source cte cont) ;; TODO have an implicit main
391 (cond ((form? 'six.define-variable source)
392 (define-variable source cte cont))
393 ((form? 'six.define-procedure source)
394 (define-procedure source cte cont))
396 (statement source cte cont))))
398 (define (program source cte cont)
400 (define (p source cte cont)
401 (cond ((null? source)
405 (let ((head (car source))
413 (cont (cons ast asts)
419 (cont (new-program asts)