From b4743f08e0d1dfb86146c1617b4fd52736a5b142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Bar=C4=87?= Date: Sat, 2 Oct 2021 13:26:16 -0400 Subject: [PATCH] add make-variable from https://github.com/syntax-objects/Summer2021/issues/8 cc @xgqt --- index.scrbl | 1 + make-variable/make-variable-test.rkt | 12 +++++++++ make-variable/make-variable.rkt | 16 ++++++++++++ make-variable/make-variable.scrbl | 38 ++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 make-variable/make-variable-test.rkt create mode 100644 make-variable/make-variable.rkt create mode 100644 make-variable/make-variable.scrbl diff --git a/index.scrbl b/index.scrbl index bdfa20e..43120d9 100644 --- a/index.scrbl +++ b/index.scrbl @@ -18,6 +18,7 @@ @include-example{first-class-or} @include-example{optional-assert} +@include-example{make-variable} @include-example{cross-macro-communication} @include-example{let-star} @include-example{while-break} diff --git a/make-variable/make-variable-test.rkt b/make-variable/make-variable-test.rkt new file mode 100644 index 0000000..44df4c0 --- /dev/null +++ b/make-variable/make-variable-test.rkt @@ -0,0 +1,12 @@ +#lang racket/base +(module+ test + (require rackunit syntax-parse-example/make-variable/make-variable) + + (test-case "make-variable" + (check-equal? + (make-variable "this_variable_will_probably_change") + "this_variable_will_probably_change=\"this_variable_will_probably_change\"") + (define Z "Zzz...") + (check-equal? (make-variable Z) "Z=\"Zzz...\"")) + +) diff --git a/make-variable/make-variable.rkt b/make-variable/make-variable.rkt new file mode 100644 index 0000000..4387804 --- /dev/null +++ b/make-variable/make-variable.rkt @@ -0,0 +1,16 @@ +#lang racket/base +(provide make-variable as-variable) +(require (for-syntax racket/base syntax/parse)) + +(define (as-variable lhs rhs) + (format "~a=\"~a\"" lhs rhs)) + +(define-syntax (make-variable stx) + (syntax-parse stx + [(_ name:id) + #'(as-variable (symbol->string 'name) name)] + [(_ str:string) + #'(as-variable str str)] + [(_ ((~literal quote) sym)) + #'(as-variable (symbol->string 'sym) 'sym)])) + diff --git a/make-variable/make-variable.scrbl b/make-variable/make-variable.scrbl new file mode 100644 index 0000000..a37ab2d --- /dev/null +++ b/make-variable/make-variable.scrbl @@ -0,0 +1,38 @@ +#lang syntax-parse-example +@require[ + (for-label racket/base racket/contract syntax/parse syntax-parse-example/make-variable/make-variable)] + +@(define make-variable-eval + (make-base-eval '(require syntax-parse-example/make-variable/make-variable))) + +@title{@tt{make-variable}} +@stxbee2021["xgqt" 8] +@nested[#:style 'inset @emph{Adapted from the @hyperlink["https://gitlab.com/xgqt/racket-ebuild" @tt{racket-ebuild}] project.}] + +@; ============================================================================= + +@defmodule[syntax-parse-example/make-variable/make-variable]{} + +@defform[(make-variable v)]{ + Formats a variable declaration for POSIX shell scripts. + @itemlist[ + @item{When given a string or symbol, the input is the name and value of the new variable.} + @item{When given an identifier, the identifier name is the variable name and the identifier value is the variable's value.} + ] + + @examples[#:eval make-variable-eval + (make-variable "this_variable_will_probably_change") + (define Z "Zzz...") + (make-variable Z) + ] + + The macro uses @racket[syntax-parse] to decide how to process its input. + It then outputs code that calls the @racket[as-variable] helper function. + + @racketfile{make-variable.rkt} + +} + +@defproc[(as-variable [lhs any/c] [rhs any/c]) string?]{ + Formats two values into a POSIX variable declaration. +}