-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
from syntax-objects/Summer2021#8 cc @xgqt
- Loading branch information
Showing
4 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...\"")) | ||
|
||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)])) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} |