You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sugar/sugar/define.rkt

79 lines
2.4 KiB
Racket

9 years ago
#lang racket/base
8 years ago
(require (for-syntax racket/base racket/syntax syntax/strip-context "private/syntax-utils.rkt")
racket/contract)
9 years ago
8 years ago
(define-syntax (make-safe-module stx)
9 years ago
(syntax-case stx ()
8 years ago
[(_ [id contract])
;; need to put `racket/contract` inside calling location's context
(with-syntax ([require-racket-contract (datum->syntax #'id '(require racket/contract))])
#'(module+ safe
require-racket-contract
(provide (contract-out [id contract]))))]
[(_ id)
#'(module+ safe
(provide id))]))
9 years ago
8 years ago
(define-syntax (define+provide+safe stx)
(with-syntax ([(id contract lambda-exp) (lambdafy-with-contract stx)])
#'(begin
(define id lambda-exp)
(provide id)
(make-safe-module [id contract]))))
9 years ago
;; for previously defined identifiers
8 years ago
;; takes args like (provide+safe [id contract]) or just (provide+safe id)
9 years ago
;; any number of args.
8 years ago
(define-syntax-rule (provide+safe thing ...)
(begin
(provide+safe/once thing) ...))
;; `provide+safe` might have interleaved ids or [id contract] args so handle them individually.
(define-syntax (provide+safe/once stx)
(with-syntax ([(id msm-arg) (syntax-case stx ()
[(_ [id contract])
#'(id [id contract])]
[(_ id)
#'(id id)])])
#'(begin
(provide id)
(make-safe-module msm-arg))))
9 years ago
(define-syntax (define+provide/contract stx)
8 years ago
(with-syntax* ([(id contract lambda-exp) (lambdafy-with-contract stx)]
[require-racket-contract (datum->syntax #'id '(require racket/contract))])
#'(begin
require-racket-contract
(provide (contract-out [id contract]))
(define id lambda-exp))))
9 years ago
(define-syntax (define/contract+provide stx)
8 years ago
(with-syntax* ([(id contract lambda-exp) (lambdafy-with-contract stx)]
[require-racket-contract (datum->syntax #'id '(require racket/contract))])
#'(begin
require-racket-contract
(provide id)
(define/contract id contract lambda-exp))))
9 years ago
(define-syntax (define+provide stx)
8 years ago
(with-syntax ([(id lambda-exp) (lambdafy stx)])
#'(begin
(provide id)
(define id lambda-exp))))
(provide+safe make-safe-module
9 years ago
define+provide+safe
provide+safe
define+provide/contract
define/contract+provide
define+provide)