From 85e747ffacd8183796baa7ad741fd27cd2c871ac Mon Sep 17 00:00:00 2001 From: Darshal Shetty Date: Tue, 5 Mar 2024 18:08:10 -0500 Subject: [PATCH] Interpolate multiple expressions in a string --- main.rkt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/main.rkt b/main.rkt index 2b93dbc..97f8247 100644 --- a/main.rkt +++ b/main.rkt @@ -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 @@ -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)}"))