Skip to content

Commit

Permalink
add make-variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Barć authored and bennn committed Oct 27, 2021
1 parent 875f36f commit b4743f0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
12 changes: 12 additions & 0 deletions make-variable/make-variable-test.rkt
Original file line number Diff line number Diff line change
@@ -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...\""))

)
16 changes: 16 additions & 0 deletions make-variable/make-variable.rkt
Original file line number Diff line number Diff line change
@@ -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)]))

38 changes: 38 additions & 0 deletions make-variable/make-variable.scrbl
Original file line number Diff line number Diff line change
@@ -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.
}

0 comments on commit b4743f0

Please sign in to comment.