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 try to fit this with the predefined variables in cte.scm, then store its value somewhere in a global, I guess
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 ;; TODO pass it the first statement
92 cont) ; will return a list of named blocks
104 (cont (new-block asts)
107 ;; returns a list of the named blocks (implicit blocks delimited by labels) present in the given tree
108 ;; useful for switch and goto
109 ;; TODO returns that ?
110 (define (named-block-list source cte cont)
111 (define (b source cte cont name body-so-far)
113 (cont (list (new-named-block name body-so-far)) ; last block
115 (let ((curr (car source)))
116 (if (or (form? 'six.label curr) ; we reached another named block
117 (form? 'six.case curr))
118 (named-block-list source
120 (lambda (named-blocks cte)
121 (cont (cons (new-named-block name body-so-far)
131 (append body-so-far (list ast)))))))))
134 (statement (caddar source)
141 ;; the first statement is in the case/label form
144 (if (form? 'six.case (car source)) ; the label is a case
145 (literal (cadar source)
148 (new-cont (list 'case (literal-val name)) cte)))
149 (new-cont (cadar source) cte)))) ; ordinary label
151 (define (statement source cte cont)
152 (cond ((form? 'six.define-variable source)
153 (define-variable source cte cont))
154 ((form? 'six.if source)
155 (if (null? (cdddr source))
156 (if1 source cte cont)
157 (if2 source cte cont)))
158 ((form? 'six.switch source)
159 (switch source cte cont))
160 ((form? 'six.while source)
161 (while source cte cont))
162 ((form? 'six.do-while source)
163 (do-while source cte cont))
164 ((form? 'six.for source)
165 (for source cte cont))
166 ((form? 'six.return source)
167 (return source cte cont))
168 ((form? 'six.break source)
169 (break source cte cont))
170 ((form? 'six.continue source)
171 (continue source cte cont))
172 ((form? 'six.goto source)
173 (goto source cte cont))
174 ((form? 'six.compound source)
175 (block source cte cont))
177 (expression source cte cont))))
179 (define (return source cte cont)
181 (define (ret asts cte)
182 (cont (new-return asts)
185 (if (null? (cdr source))
187 (expression (cadr source)
190 (ret (list ast) cte)))))
192 (define (break source cte cont)
196 (define (continue source cte cont)
200 (define (goto source cte cont)
201 (cont (new-goto (cadadr source)) ; label
204 (define (if1 source cte cont)
205 (expression (cadr source)
208 (statement (caddr source)
211 (cont (new-if (list ast1 ast2))
214 (define (if2 source cte cont)
215 (expression (cadr source)
218 (statement (caddr source)
221 (statement (cadddr source)
224 (cont (new-if (list ast1 ast2 ast3))
227 (define (switch source cte cont)
228 (expression (cadr source)
230 (lambda (ast1 cte) ; we matched the paren expr
231 (expect-form 'six.compound (caddr source))
232 (block (caddr source)
235 (cont (new-switch (cons ast1 (ast-subasts ast2))) ; we only need the contents of the generated block, which would be a named block list
238 (define (while source cte cont)
239 (expression (cadr source)
242 (statement (caddr source)
245 (cont (new-while (list ast1 ast2))
248 (define (do-while source cte cont)
249 (statement (cadr source)
252 (expression (caddr source)
255 (cont (new-do-while (list ast1 ast2))
258 (define (for source cte cont)
260 (define (opt-expr source cte cont)
262 (expression source cte cont)
265 (statement (cadr source)
268 (opt-expr (caddr source)
271 (opt-expr (cadddr source)
274 (statement (car (cddddr source))
280 (new-literal 'byte 1))
286 (define (expression source cte cont)
287 (cond ((form? 'six.literal source)
288 (literal source cte cont))
289 ((form? 'six.identifier source)
290 (ref source cte cont))
291 ((form? 'six.call source)
292 (call source cte cont))
296 (operation op source cte cont)))
298 (error "expected expression" source))))
300 (define (operation op source cte cont)
302 (expression (cadr source)
306 (new-oper (list ast1) #f op)))
307 (expr-type-set! ast ((op-type-rule op) ast))
308 (cont ((op-constant-fold op) ast)
310 (expression (cadr source)
313 (expression (caddr source)
317 (new-oper (list ast1 ast2) #f op)))
318 (expr-type-set! ast ((op-type-rule op) ast))
319 (cont ((op-constant-fold op) ast)
322 (define (call source cte cont)
323 (let* ((id (get-id (cadr source)))
324 (binding (cte-lookup cte id)))
325 (if (def-procedure? binding)
326 (expressions (cddr source)
329 (cont (new-call args (def-procedure-type binding) binding)
331 (error "expected procedure" source))))
333 (define (expressions source cte cont)
334 (cond ((null? source)
338 (let ((head (car source))
346 (cont (cons ast asts)
349 (define (literal source cte cont)
350 (let ((n (cadr source)))
351 ;; TODO might need to be expanded
352 (cont (new-literal (cond ((and (>= n 0) (< n 256))
354 ((and (>= n 0) (< n 65536))
361 (define (ref source cte cont)
362 (let* ((id (cadr source))
363 (binding (cte-lookup cte id)))
364 (if (def-variable? binding)
365 (cont (new-ref (def-variable-type binding) binding)
367 (error "expected variable" source))))
369 (define (toplevel source cte cont) ;; TODO have an implicit main
370 (cond ((form? 'six.define-variable source)
371 (define-variable source cte cont))
372 ((form? 'six.define-procedure source)
373 (define-procedure source cte cont))
375 (statement source cte cont))))
377 (define (program source cte cont)
379 (define (p source cte cont)
380 (cond ((null? source)
384 (let ((head (car source))
392 (cont (cons ast asts)
398 (cont (new-program asts)