From: Olivier Dion <olivier.dion@polymtl.ca>
To: guile-user@gnu.org
Cc: rekado@elephly.net, Olivier dion <olivier-dion@proton.me>
Subject: [RFC PATCH 1/2] Add reader extension for interpolated strings.
Date: Sat, 10 Dec 2022 17:17:23 -0500 [thread overview]
Message-ID: <20221210221724.23998-2-olivier.dion@polymtl.ca> (raw)
In-Reply-To: <20221210221724.23998-1-olivier.dion@polymtl.ca>
From: Olivier dion <olivier-dion@proton.me>
Interpolate strings of the form #"The string @(eval this)".
When no interpolation is required, simply return the raw string. For
example, #"foo" will return "foo".
When interpolation is required, return an expression that format the
string with the embedded expressions withing it. For example
#"1+1=@(+ 1 1)" will return (format #f "1+1=~a" (+ 1 1)).
* module/ice-9/boot-9.scm: Extend read hash for strings interpolation.
---
module/ice-9/boot-9.scm | 37 +++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git a/module/ice-9/boot-9.scm b/module/ice-9/boot-9.scm
index a46145ed5..5c0f2eef2 100644
--- a/module/ice-9/boot-9.scm
+++ b/module/ice-9/boot-9.scm
@@ -2254,6 +2254,43 @@ name extensions listed in %load-extensions."
(error
"#. read expansion found and read-eval? is #f."))))
+
+(letrec ((interpolate
+ (lambda (port chars exps)
+ (let ((char (read-char port)))
+ (cond
+ ((eof-object? char)
+ (values
+ (reverse-list->string chars)
+ (reverse exps)))
+ ((char=? char #\@)
+ (let ((ahead (peek-char port)))
+ (cond
+ ((eof-object? ahead)
+ (interpolate port chars exps))
+ ((char=? ahead #\@)
+ (read-char port)
+ (interpolate port
+ (cons #\@ chars)
+ exps))
+ (else
+ (interpolate port
+ (cons #\a (cons #\~ chars))
+ (cons (read port) exps))))))
+ (else
+ (interpolate port (cons char chars) exps)))))))
+ (read-hash-extend #\"
+ (lambda (char port)
+ (unread-char char port)
+ (call-with-values
+ (lambda () (call-with-input-string (read port)
+ (lambda (port)
+ (interpolate port '() '()))))
+ (lambda (fmt args)
+ (if (null? args)
+ fmt
+ `(format #f ,fmt ,@args)))))))
+
\f
;;; {Low Level Modules}
--
2.38.1
next prev parent reply other threads:[~2022-12-10 22:17 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-10 22:17 [RFC PATCH 0/2] Strings interpolation Olivier Dion
2022-12-10 22:17 ` Olivier Dion [this message]
2022-12-10 22:17 ` [RFC PATCH 2/2] Add tests for strings interpolation reader extension Olivier Dion
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221210221724.23998-2-olivier.dion@polymtl.ca \
--to=olivier.dion@polymtl.ca \
--cc=guile-user@gnu.org \
--cc=olivier-dion@proton.me \
--cc=rekado@elephly.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).