From 5e283216a650551fd84af55d35d385ecc719e0e8 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Mon, 28 Mar 2016 13:00:08 -0700 Subject: [PATCH] tiny refactoring --- sugar/cache.rkt | 10 +--------- sugar/coerce.rkt | 12 ++---------- sugar/define.rkt | 20 ++------------------ sugar/main.rkt | 26 +++++++++----------------- sugar/private/syntax-utils.rkt | 31 +++++++++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 54 deletions(-) create mode 100644 sugar/private/syntax-utils.rkt diff --git a/sugar/cache.rkt b/sugar/cache.rkt index 23c3dd2..c457bb9 100644 --- a/sugar/cache.rkt +++ b/sugar/cache.rkt @@ -1,5 +1,5 @@ #lang racket/base -(require (for-syntax racket/base) "define.rkt") +(require (for-syntax racket/base "private/syntax-utils.rkt") "define.rkt") (define+provide+safe (make-caching-proc base-proc) @@ -10,14 +10,6 @@ (hash-ref! cache (list* kws kw-args args) (λ () (keyword-apply base-proc kws kw-args args))))))) -(define-for-syntax (lambdafy stx) - (syntax-case stx () - [(_ (id arg ... . rest-arg) body ...) - #'(id (λ (arg ... . rest-arg) body ...))] - [(_ id body-exp) - #'(id body-exp)])) - - (provide+safe define/caching) (define-syntax (define/caching stx) (with-syntax ([(id lambda-expr) (lambdafy stx)]) diff --git a/sugar/coerce.rkt b/sugar/coerce.rkt index d402d32..ce5d9ed 100644 --- a/sugar/coerce.rkt +++ b/sugar/coerce.rkt @@ -1,12 +1,4 @@ #lang racket/base +(require "private/syntax-utils.rkt") -(define-syntax-rule (r+p modname ...) - (begin - (begin - (require modname) - (provide (all-from-out modname)) - (module+ safe - (require (submod modname safe)) - (provide (all-from-out (submod modname safe))))) ...)) - -(r+p "coerce/base.rkt" "coerce/contract.rkt") \ No newline at end of file +(require+provide/safe "coerce/base.rkt" "coerce/contract.rkt") diff --git a/sugar/define.rkt b/sugar/define.rkt index fa7036a..722eaea 100644 --- a/sugar/define.rkt +++ b/sugar/define.rkt @@ -1,5 +1,6 @@ #lang racket/base -(require (for-syntax racket/base racket/syntax syntax/strip-context) racket/contract) +(require (for-syntax racket/base racket/syntax syntax/strip-context "private/syntax-utils.rkt") + racket/contract) (define-syntax (make-safe-module stx) @@ -15,23 +16,6 @@ (provide id))])) -;; convert calling pattern to form (id contract body-exp) -(define-for-syntax (lambdafy-with-contract stx) - (syntax-case stx () - [(_ (id arg ... . rest-arg) contract body ...) - (replace-context #'id #'(id contract (λ (arg ... . rest-arg) body ...)))] - [(_ id contract lambda-exp) - (replace-context #'id #'(id contract lambda-exp))])) - - -;; convert calling pattern to form (id body-exp) -(define-for-syntax (lambdafy stx) - (syntax-case stx () - [(_ (id arg ... . rest-arg) body ...) - (replace-context #'id #'(id (λ (arg ... . rest-arg) body ...)))] - [(_ id lambda-exp) - (replace-context #'id #'(id lambda-exp))])) - (define-syntax (define+provide+safe stx) (with-syntax ([(id contract lambda-exp) (lambdafy-with-contract stx)]) diff --git a/sugar/main.rkt b/sugar/main.rkt index 75a659d..8ea212d 100644 --- a/sugar/main.rkt +++ b/sugar/main.rkt @@ -1,19 +1,11 @@ #lang racket/base +(require "private/syntax-utils.rkt") -(define-syntax-rule (r+p modname ...) - (begin - (begin - (require modname) - (provide (all-from-out modname)) - (module+ safe - (require (submod modname safe)) - (provide (all-from-out (submod modname safe))))) ...)) - -(r+p "cache.rkt" - "coerce.rkt" - "debug.rkt" - "define.rkt" - "file.rkt" - "list.rkt" - "test.rkt" - "xml.rkt") \ No newline at end of file +(require+provide/safe "cache.rkt" + "coerce.rkt" + "debug.rkt" + "define.rkt" + "file.rkt" + "list.rkt" + "test.rkt" + "xml.rkt") diff --git a/sugar/private/syntax-utils.rkt b/sugar/private/syntax-utils.rkt new file mode 100644 index 0000000..93a31f6 --- /dev/null +++ b/sugar/private/syntax-utils.rkt @@ -0,0 +1,31 @@ +#lang racket/base +(require (for-syntax racket/base) syntax/strip-context) +(provide (all-defined-out)) + + +(define-syntax-rule (require+provide/safe modname ...) + (begin + (begin + (require modname) + (provide (all-from-out modname)) + (module+ safe + (require (submod modname safe)) + (provide (all-from-out (submod modname safe))))) ...)) + + +;; convert calling pattern to form (id contract body-exp) +(define-syntax-rule (lambdafy-with-contract stx) + (syntax-case stx () + [(_ (id arg (... ...) . rest-arg) contract body (... ...)) + (replace-context #'id #'(id contract (λ (arg (... ...) . rest-arg) body (... ...))))] + [(_ id contract lambda-exp) + (replace-context #'id #'(id contract lambda-exp))])) + + +;; convert calling pattern to form (id body-exp) +(define-syntax-rule (lambdafy stx) + (syntax-case stx () + [(_ (id arg (... ...) . rest-arg) body (... ...)) + (replace-context #'id #'(id (λ (arg (... ...) . rest-arg) body (... ...))))] + [(_ id lambda-exp) + (replace-context #'id #'(id lambda-exp))])) \ No newline at end of file