From c8064354c73dbc665b055600766b44473a2753a3 Mon Sep 17 00:00:00 2001 From: Jay McCarthy Date: Thu, 11 Dec 2014 09:57:27 -0500 Subject: [PATCH] notes --- remix/README | 2 + remix/exp/cond.rkt | 25 +++++++++++++ remix/exp/template.rkt | 84 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 remix/exp/cond.rkt create mode 100644 remix/exp/template.rkt diff --git a/remix/README b/remix/README index a1060e4..edc1d7d 100644 --- a/remix/README +++ b/remix/README @@ -133,3 +133,5 @@ array.[{1 + 1}].(foo) [for define things too] - zos don't appear to users (switch to interp if no write access) + +- (define+ (f case) body) diff --git a/remix/exp/cond.rkt b/remix/exp/cond.rkt new file mode 100644 index 0000000..cebb7b4 --- /dev/null +++ b/remix/exp/cond.rkt @@ -0,0 +1,25 @@ +#lang racket/base +(require (for-syntax racket/base + syntax/parse)) + +(define-syntax (brackets-cond stx) + (syntax-parse stx + [(_) + (syntax/loc stx + (error 'cond "Reach the end of cond"))] + [(_ ((~literal #%brackets) q . a) . more) + (syntax/loc stx + (if q (let () . a) (brackets-cond . more)))] + [(_ e . more) + (syntax/loc stx + (let () e (brackets-cond . more)))])) + +(module+ test + (brackets-cond + (define x 5) + (#%brackets (even? x) + 27) + (define y (+ x 6)) + (#%brackets (odd? y) + 28) + 19)) diff --git a/remix/exp/template.rkt b/remix/exp/template.rkt new file mode 100644 index 0000000..12605e3 --- /dev/null +++ b/remix/exp/template.rkt @@ -0,0 +1,84 @@ +#lang racket/base +(require (for-syntax racket/base + syntax/parse) + racket/stxparam) + +(begin-for-syntax + (define-syntax-class template-ell + #:attributes ([x 1]) + (pattern () + #:attr [x 1] null) + (pattern (a:template-ell . d:template-ell) + #:attr [x 1] + (append (attribute a.x) (attribute d.x))) + (pattern ((~literal unquote) [depth:number one:id]) + #:attr [x 1] + (if (< (syntax-parameter-value #'template-depth) + (syntax->datum #'depth)) + (list #'one) + null)) + (pattern _:id + #:attr [x 1] null))) + +(define-syntax-parameter template-depth 0) + +(define-syntax (template-list stx) + (syntax-parse stx + [(_) + (syntax/loc stx + null)] + [(~and (_ _ (~literal ...) ~! . _) + (_ a:template-ell (~literal ...) . more)) + (syntax/loc stx + (append + (for/list ([a.x (in-list a.x)] + ...) + (syntax-parameterize + ([template-depth (add1 (syntax-parameter-value #'template-depth))]) + (template a))) + (template-list . more)))] + [(_ ((~literal unquote-splicing) a) . more) + (syntax/loc stx + (append (template a) (template-list . more)))] + [(_ a . more) + (syntax/loc stx + (cons (template a) (template-list . more)))])) + +(define-syntax (template stx) + (syntax-parse stx + ;; xxx check depth of this to be equal to template-depth? + [(_ ((~literal unquote) [_ a])) + #'a] + [(_ ((~literal unquote-splicing) . _)) + (raise-syntax-error 'template "unquote-splicing not allowed in template" stx)] + [(_ (~and a ((~literal quote) _))) + #'a] + [(_ (op more ...)) + (syntax/loc stx + (apply op (template-list more ...)))] + [(_ a) + #'a])) + +(module+ test + (require rackunit) + (define a 10) + (define x (list 1 2 3 4)) + (define y (list 5 6 7 8)) + (check-equal? + (template (+ (* ,[0 a] ,[1 x] ,[1 y]) ...)) + (+ (* 10 1 5) + (* 10 2 6) + (* 10 3 7) + (* 10 4 8))) + (check-equal? + (template (list '+ (list '* ,[0 a] ,[1 x] ,[1 y]) ...)) + '(+ + (* 10 1 5) + (* 10 2 6) + (* 10 3 7) + (* 10 4 8))) + (check-equal? + (template (+ ,[0 a] ,@(map add1 ,[0 x]) ,@,[0 y])) + (+ 10 + 2 3 4 5 + 5 6 7 8)))