diff options
| author | Rose Hogenson <rhogenson@posteo.net> | 2022-04-02 16:57:02 -0700 |
|---|---|---|
| committer | Rose Hogenson <rhogenson@posteo.net> | 2022-04-02 16:57:02 -0700 |
| commit | c56e734008e790660faec2db21941ee423412484 (patch) | |
| tree | c6d2b7d955525049c2417af20ae8305875cc936f /csc/macros.csc | |
| parent | 42c60036dd9b07c474c4c8425669c33495529f85 (diff) | |
| download | chromatopelma-c56e734008e790660faec2db21941ee423412484.tar.zst | |
Write a test for case-lambda.
I'm realizing that I should have started with builtin-exit and
implemented the hard stuff later.
Diffstat (limited to 'csc/macros.csc')
| -rw-r--r-- | csc/macros.csc | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/csc/macros.csc b/csc/macros.csc index 08c5fd2..da2e7b7 100644 --- a/csc/macros.csc +++ b/csc/macros.csc @@ -27,6 +27,7 @@ make-lambda-case make-letrec make-sequence + make-toplevel-define make-toplevel-ref make-void sequence-head @@ -623,7 +624,7 @@ (let* ((transformer (expand-syntax-object transformer-form)) (body (expand-syntax-object (with-binding ident transformer body-form)))) body)) - (_ (raise-syntax-error "unexpected for in let-syntax")))))) + (_ (raise-syntax-error "unexpected form in let-syntax")))))) (define (split-args-rest formals) @@ -635,7 +636,7 @@ (values (cons (identifier-name var) args) rest))) (var (when identifier? var) (values '() (identifier-name var))) - (_ (raise-syntax-error "unexpected form in case-lambda" formals)))) + (_ (raise-syntax-error "unexpected form in split-args-rest" formals)))) (define (collect-defines acc body) @@ -664,18 +665,20 @@ collect (gensym) into gensyms collect (toplevel-define-expression def) into vals finally (return (values names gensyms vals)))) - (make-letrec - #t - names - gensyms - vals - rest)) + (if (null? names) + rest + (make-letrec + #t + names + gensyms + vals + rest))) (define (case-lambda-helper form) (syntax-case form ('() '()) - ((_ (formals body) . clauses) + (((formals body) . clauses) (let-values (((args rest) (split-args-rest formals))) (make-lambda-case args @@ -687,7 +690,7 @@ args)) (fix-lambda-body (expand-syntax-object body)) (case-lambda-helper clauses)))) - (_ (raise-syntax-error "unexpected form in case-lambda" form)))) + (_ (raise-syntax-error "unexpected form in case-lambda-helper" form)))) (define builtin-case-lambda @@ -695,18 +698,28 @@ (lambda (x) (syntax-case x ((_ clause . clauses) - (case-lambda-helper x)) + (case-lambda-helper (with-wrap (cons clause clauses) x))) (_ (raise-syntax-error "unexpected form in case-lambda" x)))))) + (define builtin-define + (make-macro-transformer + (lambda (x) + (syntax-case x + ((_ symbol expression) + (make-toplevel-define (identifier-name symbol) (expand-syntax-object expression))) + (_ (raise-syntax-error "unexpected form in builtin-define" x)))))) + + (define builtins-environment (make-environment (alist->substitutions (list (cons 'syntax-rules builtin-syntax-rules) (cons '_ (make-toplevel-ref '_)) + (cons 'builtin-let-syntax builtin-let-syntax) (cons 'quote builtin-quote) (cons 'case-lambda builtin-case-lambda) - (cons 'builtin-let-syntax builtin-let-syntax))))) + (cons 'builtin-define builtin-define))))) (define (expand-body environment body) |
