X-Git-Url: http://git.lukelau.me/?p=scheme.git;a=blobdiff_plain;f=ast.scm;h=d2328cc6449d014eacb085fdc0b734f620c1ceb2;hp=52e06bcb24d3b4783eb301c8fb657ec7f7e06071;hb=a457cd3bb5ce9366db3ca0731a07abc50ecbc1f3;hpb=1b7e2b53e68a39265fd7424910998d2607cc3815 diff --git a/ast.scm b/ast.scm index 52e06bc..d2328cc 100644 --- a/ast.scm +++ b/ast.scm @@ -17,6 +17,7 @@ ('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) @@ -35,6 +36,10 @@ ('app (map f x)) ('lambda `(lambda ,(lambda-args x) ,(f (lambda-body x)))) ('if `(if ,@(map f (cdr x)))) + ('case `(case ,(f (case-switch x)) + ,@(map (lambda (x) + (list (car x) (f (cadr x)))) + (case-cases x)))) ('stack `(stack ,(cadr x) ,(f (caddr x)))) (else x))) @@ -50,6 +55,9 @@ (inner (lambda-body x)))] ['if (append (f x) (flat-map inner (cdr x)))] + ['case (append (f x) + (inner (case-switch x)) + (flat-map inner (map cadr (case-cases x))))] ['stack (append (f x) (inner (caddr x)))] [else (f x)])) @@ -74,12 +82,54 @@ ['lambda (either (p x) (inner (lambda-body x)))] ['if (either (p x) (any inner (cdr x)))] + ['case (either (p x) + (any inner (map cadr (case-cases x))) + (inner (case-switch x)))] ['stack (either (p x) (inner (caddr x)))] [else (p x)])) (define let-bindings cadr) (define let-body cddr) +(define case-switch cadr) +(define case-cases cddr) + +;; (define (verify-cases data-layouts annotated-program) + +;; (define allowed-match-ast-types +;; '((Int . (int-literal var)) +;; (Bool . (bool-literal var)) +;; (String . (string-literal var)))) + +;; (define (check-pattern switch-type pat) + +;; (define (impossible-match) +;; (error "Can't pattern match ~a with ~a" switch-type (ann-expr pat))) + +;; (if (assoc switch-type data-layouts) +;; (begin +;; (let ([sums (cdr (assoc switch-type data-layouts))]) +;; (unless (eqv? (ast-type (ann-expr pat)) 'var) (impossible-match)) +;; (unless (assoc (car (ann-expr pat)) sums) (impossible-match)) +;; (unless +;; ) +;; (begin +;; (unless (assoc switch-type allowed-match-ast-types) +;; (error #f "Can't pattern match on ~a" switch-type)) + +;; (let ([allowed (cdr (assoc switch-type allowed-match-ast-types))]) +;; (unless (assoc (ast-type (ann-expr pat)) allowed) (impossible-match))))))) + + +;; (let ([expr (ann-expr annotated-program)]) +;; (case (ast-type expr) +;; ['case +;; (let* ([switch-type (ann-type (case-switch expr))] +;; [allowed (cdr (assoc switch-type allowed-match-ast-types))]) +;; (for-each +;; []))])))) + + ; (let ([(foo a b) (foo 123 345)]) a) ; | ; v @@ -87,7 +137,7 @@ ; [b (foo~1 (foo 123 345)]) a) (define (expand-pattern-matches program) (define (go x) - (define (pattern-match binding) + (define (let-pattern-match binding) (let ([binding-name (car binding)] [body (cadr binding)]) (if (eqv? (ast-type binding-name) 'var) @@ -121,11 +171,12 @@ binding))) (flat-map (lambda (y i) - (pattern-match (list y `(,(destructor i) ,body)))) + (let-pattern-match (list y `(,(destructor i) ,body)))) products (range 0 (length products))))))) + (case (ast-type x) - ['let `(let ,(flat-map pattern-match (let-bindings x)) + ['let `(let ,(flat-map let-pattern-match (let-bindings x)) ,@(map go (let-body x)))] [else (ast-traverse go x)])) (program-map-exprs go program))