From 9a107dde565b610c6bd70edfdb55eb289e3a21e4 Mon Sep 17 00:00:00 2001 From: Ben Greenman Date: Fri, 1 Oct 2021 21:27:05 -0400 Subject: [PATCH] add defines (define*) from https://github.com/syntax-objects/Summer2021/issues/7 cc @Fictitious-Rotor --- defines/defines-test.rkt | 12 ++++++++++++ defines/defines.rkt | 10 ++++++++++ defines/defines.scrbl | 36 ++++++++++++++++++++++++++++++++++++ index.scrbl | 1 + 4 files changed, 59 insertions(+) create mode 100644 defines/defines-test.rkt create mode 100644 defines/defines.rkt create mode 100644 defines/defines.scrbl diff --git a/defines/defines-test.rkt b/defines/defines-test.rkt new file mode 100644 index 0000000..bcaae0d --- /dev/null +++ b/defines/defines-test.rkt @@ -0,0 +1,12 @@ +#lang racket/base +(module+ test + (require rackunit syntax-parse-example/defines/defines) + + (test-case "quot/rem" + (defines + [x 4] + [y 18] + [(quot rem) (quotient/remainder x y)]) + (check-equal? quot 0) + (check-equal? rem 4)) +) diff --git a/defines/defines.rkt b/defines/defines.rkt new file mode 100644 index 0000000..52274e6 --- /dev/null +++ b/defines/defines.rkt @@ -0,0 +1,10 @@ +#lang racket +(provide defines) +(require syntax/parse/define) + +(define-syntax-parser defines + [(_ (~or [id:id expr:expr] + [(idv:id ...+) expr:expr]) ...+) + #'(begin + (~? (define id expr) + (define-values (idv ...) expr)) ...)]) diff --git a/defines/defines.scrbl b/defines/defines.scrbl new file mode 100644 index 0000000..e9826dc --- /dev/null +++ b/defines/defines.scrbl @@ -0,0 +1,36 @@ +#lang syntax-parse-example +@require[ + (for-label racket/base syntax/parse syntax-parse-example/defines/defines)] + +@(define defines-eval + (make-base-eval '(require syntax-parse-example/defines/defines))) + +@title{@tt{defines}} +@stxbee2021["Fictitious-Rotor" 7] + +@; ============================================================================= + +@defmodule[syntax-parse-example/defines/defines]{} + +@defform[(defines expr ...)]{ + Make a sequence of definitions --- similar to @racket[let*]. + + Definitions are automatically dispatched either to @racket[define] or @racket[define-values] + depending on the manner in which identifiers are supplied. + + @examples[#:eval defines-eval + (defines + [x 4] + [y 18] + [(quot rem) (quotient/remainder x y)]) + quot + rem + ] + + The macro uses the @racket[~?] fallthrough syntax to choose between + @racket[define] and @racket[define-values]. + + @racketfile{defines.rkt} + + @; TODO add description here +} diff --git a/index.scrbl b/index.scrbl index c785f22..c84e575 100644 --- a/index.scrbl +++ b/index.scrbl @@ -17,6 +17,7 @@ @; ============================================================================= @include-example{displaylns} +@include-example{defines} @include-example{first-class-or} @include-example{optional-assert} @include-example{make-variable}