1 ;; variables which, when found in programs, have special meanings
2 ;; when any of these are encountered, it's value is given to the appropriate
4 ;; for now, these variables must have a literal value
5 (define special-variables
6 '((SIXPIC_MEMORY_DIVIDE . memory-divide)))
10 (define (form? keyword source)
12 (eq? (car source) keyword)))
14 (define (expect-form keyword source)
15 (if (not (form? keyword source))
16 (error "expected" keyword source)))
18 (define (get-id source)
19 (expect-form 'six.identifier source)
22 (define (define-variable source cte cont)
23 (let* ((id (get-id (cadr source)))
25 (dims (cadddr source))
26 (val (car (cddddr source))))
28 (define (def asts cte)
30 (alloc-value type)) ;; TODO use dims to allocate
32 (new-def-variable asts id '() type value '()))
34 (cte-extend cte (list ast))))
38 ;; if it's a special variable, use it's value
39 (let ((target (assq id special-variables)))
41 (eval `(set! ,(cdr target) ,(cadr val))))) ; the value must be a literal
43 (expression val cte (lambda (ast cte) (def (list ast) cte)))
46 (define (define-procedure source cte cont)
47 (let* ((id (get-id (cadr source)))
48 (proc (caddr source)))
49 (expect-form 'six.procedure proc)
58 (new-def-variable '() (get-id (car x)) '() type value '())))
62 (expect-form 'six.procedure-body body)
66 (new-def-procedure '() id '() type value params))
68 (cte-extend cte (list ast))))
69 (multi-link-parent! params ast)
71 (cte-extend cte params)
72 (lambda (body-ast body-cte)
73 (ast-subasts-set! ast (list body-ast))
74 (link-parent! body-ast ast)
78 (define (block source cte cont)
79 (define (b source cte cont)
82 (let ((head (car source))
84 (if (or (form? 'six.label head) ; we complete the block with a list of named blocks
85 (form? 'six.case head))
86 (named-block-list source
88 cont) ; will return a list of named blocks
100 (cont (new-block asts)
103 ;; returns a list of the named blocks (implicit blocks delimited by labels) present in the given tree
104 ;; useful for switch and goto
105 (define (named-block-list source cte cont)
106 (define (b source cte cont name body-so-far)
108 (cont (list (new-named-block name body-so-far)) ; last block
110 (let ((curr (car source)))
111 (if (or (form? 'six.label curr) ; we reached another named block
112 (form? 'six.case curr))
113 (named-block-list source
115 (lambda (named-blocks cte)
116 (cont (cons (new-named-block name body-so-far)
126 (append body-so-far (list ast)))))))))
134 (if (form? 'six.case (car source)) ; the label is a case
135 (literal (cadar source)
138 (new-cont (list 'case (literal-val name)) cte)))
139 (new-cont (cadar source) cte)))) ; ordinary label
141 (define (statement source cte cont)
142 (cond ((form? 'six.define-variable source)
143 (define-variable source cte cont))
144 ((form? 'six.if source)
145 (if (null? (cdddr source))
146 (if1 source cte cont)
147 (if2 source cte cont)))
148 ((form? 'six.switch source)
149 (switch source cte cont))
150 ((form? 'six.while source)
151 (while source cte cont))
152 ((form? 'six.do-while source)
153 (do-while source cte cont))
154 ((form? 'six.for source)
155 (for source cte cont))
156 ((form? 'six.return source)
157 (return source cte cont))
158 ((form? 'six.break source)
159 (break source cte cont))
160 ((form? 'six.continue source)
161 (continue source cte cont))
162 ((form? 'six.goto source)
163 (goto source cte cont))
164 ((form? 'six.compound source)
165 (block source cte cont))
167 (expression source cte cont))))
169 (define (return source cte cont)
171 (define (ret asts cte)
172 (cont (new-return asts)
175 (if (null? (cdr source))
177 (expression (cadr source)
180 (ret (list ast) cte)))))
182 (define (break source cte cont)
186 (define (continue source cte cont)
190 (define (goto source cte cont)
191 (cont (new-goto (cadadr source)) ; label
194 (define (if1 source cte cont)
195 (expression (cadr source)
198 (statement (caddr source)
201 (cont (new-if (list ast1 ast2))
204 (define (if2 source cte cont)
205 (expression (cadr source)
208 (statement (caddr source)
211 (statement (cadddr source)
214 (cont (new-if (list ast1 ast2 ast3))
217 (define (switch source cte cont)
218 (expression (cadr source)
220 (lambda (ast1 cte) ; we matched the paren expr
221 (expect-form 'six.compound (caddr source))
222 (block (caddr source)
225 (cont (new-switch (cons ast1 (ast-subasts ast2))) ; we only need the contents of the generated block, which would be a named block list
228 (define (while source cte cont)
229 (expression (cadr source)
232 (statement (caddr source)
235 (cont (new-while (list ast1 ast2))
238 (define (do-while source cte cont)
239 (statement (cadr source)
242 (expression (caddr source)
245 (cont (new-do-while (list ast1 ast2))
248 (define (for source cte cont)
250 (define (opt-expr source cte cont)
252 (expression source cte cont)
255 (statement (cadr source)
258 (opt-expr (caddr source)
261 (opt-expr (cadddr source)
264 (statement (car (cddddr source))
270 (new-literal 'int 1))
276 (define (expression source cte cont)
277 (cond ((form? 'six.literal source)
278 (literal source cte cont))
279 ((form? 'six.identifier source)
280 (ref source cte cont))
281 ((form? 'six.call source)
282 (call source cte cont))
286 (operation op source cte cont)))
288 (error "expected expression" source))))
290 (define (operation op source cte cont)
292 (expression (cadr source)
296 (new-oper (list ast1) #f op)))
297 (expr-type-set! ast ((op-type-rule op) ast))
298 (cont ((op-constant-fold op) ast)
300 (expression (cadr source)
303 (expression (caddr source)
307 (new-oper (list ast1 ast2) #f op)))
308 (expr-type-set! ast ((op-type-rule op) ast))
309 (cont ((op-constant-fold op) ast)
312 (define (call source cte cont)
313 (let* ((id (get-id (cadr source)))
314 (binding (cte-lookup cte id)))
315 (if (def-procedure? binding)
316 (expressions (cddr source)
319 (cont (new-call args (def-procedure-type binding) binding)
321 (error "expected procedure" source))))
323 (define (expressions source cte cont)
324 (cond ((null? source)
328 (let ((head (car source))
336 (cont (cons ast asts)
339 (define (literal source cte cont)
340 (cont (new-literal 'int (cadr source))
343 (define (ref source cte cont)
344 (let* ((id (cadr source))
345 (binding (cte-lookup cte id)))
346 (if (def-variable? binding)
347 (cont (new-ref (def-variable-type binding) binding)
349 (error "expected variable" source))))
351 (define (toplevel source cte cont) ;; TODO have an implicit main
352 (cond ((form? 'six.define-variable source)
353 (define-variable source cte cont))
354 ((form? 'six.define-procedure source)
355 (define-procedure source cte cont))
357 (statement source cte cont))))
359 (define (program source cte cont)
361 (define (p source cte cont)
362 (cond ((null? source)
366 (let ((head (car source))
374 (cont (cons ast asts)
380 (cont (new-program asts)