Skip to content

Commit

Permalink
Interpolate multiple expressions in a string
Browse files Browse the repository at this point in the history
  • Loading branch information
DarshalShetty committed Mar 5, 2024
1 parent a361c36 commit 85e747f
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
(define (to-stx val) (datum->syntax stx val))
(define (datum val) (to-stx (cons #'core-datum val)))
(define (source text) (to-stx (read (open-input-string text))))
(define (unescape text) (regexp-replace #rx"@\\\\{" text "@{"))
(define (unescape text) (regexp-replace* #rx"@\\\\{" text "@{"))
(let* ([text (syntax->datum stx)]
[matches (regexp-match* re text)]
[template (datum (unescape (regexp-replace re text "~a")))]
[template (datum (unescape (regexp-replace* re text "~a")))]
[values (map (compose source trim) matches)])
(if (null? matches)
template
Expand All @@ -31,5 +31,7 @@
(module+ test
(require rackunit)
(define answer 42)
(define two 2)
(check-equal? "What's the answer? 42" "What's the answer? @{answer}")
(check-equal? "4 + 2 = 6" "4 + 2 = @{(+ 4 2)}"))
(check-equal? "4 + 2 = 6" "4 + 2 = @{(+ 4 2)}")
(check-equal? "4 + 2 = 6" "4 + @{two} = @{(+ 4 2)}"))

0 comments on commit 85e747f

Please sign in to comment.