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

72 lines
2.4 KiB
Racket

9 years ago
#lang racket/base
6 years ago
(require (for-syntax racket/base
racket/syntax
syntax/strip-context
"private/syntax-utils.rkt")
8 years ago
racket/contract)
9 years ago
8 years ago
(define-syntax (make-safe-module stx)
9 years ago
(syntax-case stx ()
6 years ago
[(_ [ID CONTRACT])
8 years ago
;; need to put `racket/contract` inside calling location's context
6 years ago
(with-syntax ([RACKET/CONTRACT (datum->syntax #'ID 'racket/contract)])
8 years ago
#'(module+ safe
6 years ago
(require RACKET/CONTRACT)
(provide (contract-out [ID CONTRACT]))))]
[(_ ID)
8 years ago
#'(module+ safe
6 years ago
(provide ID))]))
8 years ago
(define-syntax (define+provide+safe stx)
6 years ago
(with-syntax ([(ID CONTRACT LAMBDA-EXP) (lambdafy-with-contract stx)])
8 years ago
#'(begin
6 years ago
(define ID LAMBDA-EXP)
(provide+safe [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.
6 years ago
(define-syntax-rule (provide+safe THING ...)
8 years ago
(begin
6 years ago
(provide+safe/once THING) ...))
8 years ago
;; `provide+safe` might have interleaved ids or [id contract] args so handle them individually.
(define-syntax (provide+safe/once stx)
6 years ago
(with-syntax ([(ID MSM-ARG) (syntax-case stx ()
[(_ [ID contract])
#'(ID [ID contract])]
8 years ago
[(_ id)
#'(id id)])])
#'(begin
6 years ago
(provide ID)
(make-safe-module MSM-ARG))))
9 years ago
(define-syntax (define+provide/contract stx)
6 years ago
(with-syntax* ([(ID CONTRACT LAMBDA-EXP) (lambdafy-with-contract stx)]
[RACKET/CONTRACT (datum->syntax #'ID 'racket/contract)])
8 years ago
#'(begin
6 years ago
(require RACKET/CONTRACT)
(provide (contract-out [ID CONTRACT]))
(define ID LAMBDA-EXP))))
9 years ago
(define-syntax (define/contract+provide stx)
6 years ago
(with-syntax* ([(ID CONTRACT LAMBDA-EXP) (lambdafy-with-contract stx)]
[RACKET/CONTRACT (datum->syntax #'ID 'racket/contract)])
8 years ago
#'(begin
6 years ago
(require RACKET/CONTRACT)
(provide ID)
(define/contract ID CONTRACT LAMBDA-EXP))))
9 years ago
(define-syntax (define+provide stx)
6 years ago
(with-syntax ([(ID LAMBDA-EXP) (lambdafy stx)])
8 years ago
#'(begin
6 years ago
(provide ID)
(define ID LAMBDA-EXP))))
8 years ago
(provide+safe make-safe-module
9 years ago
define+provide+safe
provide+safe
define+provide/contract
define/contract+provide
define+provide)