+(load "utils.scm")
+
(define (ast-type x)
(define (builtin? x)
(case x
('if 'if)
('let 'let)
('lambda 'lambda)
+ ('case 'case)
('closure 'closure) ; only available in codegen
('static-string 'static-string) ; only available in codegen
('stack 'stack) ; only available in codegen (tag that value is passed via stack)
('app (map f x))
('lambda `(lambda ,(lambda-args x) ,(f (lambda-body x))))
('if `(if ,@(map f (cdr x))))
- ('stack `(stack ,(cadr x) ,(map f (caddr x))))
+ ('case `(case ,(f (case-expr x))
+ ,@(map (lambda (x)
+ (list (car x) (f (cadr x))))
+ (case-cases x))))
+ ('stack `(stack ,(cadr x) ,(f (caddr x))))
(else x)))
(define (ast-collect f x)
(inner (lambda-body x)))]
['if (append (f x)
(flat-map inner (cdr x)))]
+ ['case (append (f x)
+ (inner (case-expr x))
+ (flat-map inner (map cadr (case-cases x))))]
['stack (append (f x)
(inner (caddr x)))]
[else (f x)]))
(define let-bindings cadr)
(define let-body cddr)
+(define case-expr cadr)
+(define case-cases cddr)
+
; (let ([(foo a b) (foo 123 345)]) a)
; |
; v
; |
; v
; (foo . ((A foo constructor) . (abs Int (abs Bool A))))
- ; (foo~0 . ((A foo 0) . (abs A Int)))
- ; (foo~1 . ((A foo 1) . (abs A Bool)))
+ ; (foo~0 . ((A foo 0 Int) . (abs A Int)))
+ ; (foo~1 . ((A foo 1 Bool) . (abs A Bool)))
; (bar . ((A bar constructor) . (abs Bool A)))
- ; (bar~0 . ((A bar 0) . (abs A Bool)))
+ ; (bar~0 . ((A bar 0 Bool) . (abs A Bool)))
; ------+-------------------------------------
; tor | info | type
(fold-right (lambda (x acc) `(abs ,x ,acc)) t products))
(define (destructor ctor-name prod-type part-type index)
- (let ([name (dtor-name ctor-name index)])
- (cons name (cons (list prod-type ctor-name index) `(abs ,prod-type ,part-type)))))
+ (let* ([name (dtor-name ctor-name index)]
+ [info (list prod-type ctor-name index part-type)])
+ (cons name (cons info `(abs ,prod-type ,part-type)))))
(let ([type-name (car data-layout)]
[ctors (cdr data-layout)])
(strong-connect v)))
(car graph)))
result))
-
-
- ; utils
-
-(define (range s n)
- (if (= 0 n) '()
- (append (range s (- n 1))
- (list (+ s (- n 1))))))
-
-(define (flat-map f . xs) (fold-left append '() (apply map (cons f xs))))
-(define (repeat x n) (if (<= n 0) '()
- (cons x (repeat x (- n 1)))))
-
-
-(define-syntax push!
- (syntax-rules ()
- ((_ s x) (set! s (cons x s)))))
-
-(define-syntax pop!
- (syntax-rules ()
- ((_ s) (let ([x (car s)])
- (set! s (cdr s))
- x))))