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
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)))))
31 (cons 'SIXPIC_FSR0 ; these 3 must be int16
32 (lambda () ;; TODO this code repetition is ugly, but factoring it out did not work
37 (list ast) id '() 'int16
38 (new-value (list (get-register FSR0L)
39 (get-register FSR0H)))
42 (cte-extend cte (list new-var))))))))
43 (cons 'SIXPIC_FSR1 ;; TODO use the predefined stuff in cte.scm ?
49 (list ast) id '() 'int16
50 (new-value (list (get-register FSR1L)
51 (get-register FSR1H)))
54 (cte-extend cte (list new-var))))))))
61 (list ast) id '() 'int16
62 (new-value (list (get-register FSR2L)
63 (get-register FSR2H)))
66 (cte-extend cte (list new-var))))))))))
69 (define (def asts cte)
73 (new-def-variable asts id '() type value '()))
75 (cte-extend cte (list ast))))
78 ;; if it's a special variable, call its associated thunk instead
79 (let ((target (assq id special-variables)))
83 (expression val cte (lambda (ast cte) (def (list ast) cte)))
86 (define (define-procedure source cte cont)
87 (let* ((id (get-id (cadr source)))
88 (proc (caddr source)))
89 (expect-form 'six.procedure proc)
98 (new-def-variable '() (get-id (car x)) '() type value '())))
102 (expect-form 'six.procedure-body body)
106 (new-def-procedure '() id '() type value params))
108 (cte-extend cte (list ast))))
109 (multi-link-parent! params ast)
111 (cte-extend cte params)
112 (lambda (body-ast body-cte)
113 (ast-subasts-set! ast (list body-ast))
114 (link-parent! body-ast ast)
118 (define (block source cte cont)
119 (define (b source cte cont)
122 (let ((head (car source))
124 (if (or (form? 'six.label head) ; we complete the block with a list of named blocks
125 (form? 'six.case head))
126 (named-block-list source ;; TODO pass it the first statement
128 cont) ; will return a list of named blocks
135 (cont (cons ast asts)
140 (cont (new-block asts)
143 ;; returns a list of the named blocks (implicit blocks delimited by labels) present in the given tree
144 ;; useful for switch and goto
145 ;; TODO returns that ?
146 (define (named-block-list source cte cont)
147 (define (b source cte cont name body-so-far)
149 (cont (list (new-named-block name body-so-far)) ; last block
151 (let ((curr (car source)))
152 (if (or (form? 'six.label curr) ; we reached another named block
153 (form? 'six.case curr))
154 (named-block-list source
156 (lambda (named-blocks cte)
157 (cont (cons (new-named-block name body-so-far)
167 (append body-so-far (list ast)))))))))
170 (statement (caddar source)
177 ;; the first statement is in the case/label form
180 (if (form? 'six.case (car source)) ; the label is a case
181 (literal (cadar source)
184 (new-cont (list 'case (literal-val name)) cte)))
185 (new-cont (cadar source) cte)))) ; ordinary label
187 (define (statement source cte cont)
188 (cond ((form? 'six.define-variable source)
189 (define-variable source cte cont))
190 ((form? 'six.if source)
191 (if (null? (cdddr source))
192 (if1 source cte cont)
193 (if2 source cte cont)))
194 ((form? 'six.switch source)
195 (switch source cte cont))
196 ((form? 'six.while source)
197 (while source cte cont))
198 ((form? 'six.do-while source)
199 (do-while source cte cont))
200 ((form? 'six.for source)
201 (for source cte cont))
202 ((form? 'six.return source)
203 (return source cte cont))
204 ((form? 'six.break source)
205 (break source cte cont))
206 ((form? 'six.continue source)
207 (continue source cte cont))
208 ((form? 'six.goto source)
209 (goto source cte cont))
210 ((form? 'six.compound source)
211 (block source cte cont))
213 (expression source cte cont))))
215 (define (return source cte cont)
217 (define (ret asts cte)
218 (cont (new-return asts)
221 (if (null? (cdr source))
223 (expression (cadr source)
226 (ret (list ast) cte)))))
228 (define (break source cte cont)
232 (define (continue source cte cont)
236 (define (goto source cte cont)
237 (cont (new-goto (cadadr source)) ; label
240 (define (if1 source cte cont)
241 (expression (cadr source)
244 (statement (caddr source)
247 (cont (new-if (list ast1 ast2))
250 (define (if2 source cte cont)
251 (expression (cadr source)
254 (statement (caddr source)
257 (statement (cadddr source)
260 (cont (new-if (list ast1 ast2 ast3))
263 (define (switch source cte cont)
264 (expression (cadr source)
266 (lambda (ast1 cte) ; we matched the paren expr
267 (expect-form 'six.compound (caddr source))
268 (block (caddr source)
271 (cont (new-switch (cons ast1 (ast-subasts ast2))) ; we only need the contents of the generated block, which would be a named block list
274 (define (while source cte cont)
275 (expression (cadr source)
278 (statement (caddr source)
281 (cont (new-while (list ast1 ast2))
284 (define (do-while source cte cont)
285 (statement (cadr source)
288 (expression (caddr source)
291 (cont (new-do-while (list ast1 ast2))
294 (define (for source cte cont)
296 (define (opt-expr source cte cont)
298 (expression source cte cont)
301 (statement (cadr source)
304 (opt-expr (caddr source)
307 (opt-expr (cadddr source)
310 (statement (car (cddddr source))
316 (new-literal 'int 1))
322 (define (expression source cte cont)
323 (cond ((form? 'six.literal source)
324 (literal source cte cont))
325 ((form? 'six.identifier source)
326 (ref source cte cont))
327 ((form? 'six.call source)
328 (call source cte cont))
332 (operation op source cte cont)))
334 (error "expected expression" source))))
336 (define (operation op source cte cont)
338 (expression (cadr source)
342 (new-oper (list ast1) #f op)))
343 (expr-type-set! ast ((op-type-rule op) ast))
344 (cont ((op-constant-fold op) ast)
346 (expression (cadr source)
349 (expression (caddr source)
353 (new-oper (list ast1 ast2) #f op)))
354 (expr-type-set! ast ((op-type-rule op) ast))
355 (cont ((op-constant-fold op) ast)
358 (define (call source cte cont)
359 (let* ((id (get-id (cadr source)))
360 (binding (cte-lookup cte id)))
361 (if (def-procedure? binding)
362 (expressions (cddr source)
365 (cont (new-call args (def-procedure-type binding) binding)
367 (error "expected procedure" source))))
369 (define (expressions source cte cont)
370 (cond ((null? source)
374 (let ((head (car source))
382 (cont (cons ast asts)
385 (define (literal source cte cont)
386 (let ((n (cadr source)))
387 (cont (new-literal (cond ((and (>= n 0) (< n 256))
389 ((and (>= n 0) (< n 65536))
396 (define (ref source cte cont)
397 (let* ((id (cadr source))
398 (binding (cte-lookup cte id)))
399 (if (def-variable? binding)
400 (cont (new-ref (def-variable-type binding) binding)
402 (error "expected variable" source))))
404 (define (toplevel source cte cont) ;; TODO have an implicit main
405 (cond ((form? 'six.define-variable source)
406 (define-variable source cte cont))
407 ((form? 'six.define-procedure source)
408 (define-procedure source cte cont))
410 (statement source cte cont))))
412 (define (program source cte cont)
414 (define (p source cte cont)
415 (cond ((null? source)
419 (let ((head (car source))
427 (cont (cons ast asts)
433 (cont (new-program asts)