* Use [ ] as parens in guile
@ 2004-01-11 8:21 Richard Todd
2004-01-20 23:57 ` Marius Vollmer
0 siblings, 1 reply; 2+ messages in thread
From: Richard Todd @ 2004-01-11 8:21 UTC (permalink / raw)
[-- Attachment #1.1.1: Type: text/plain, Size: 1590 bytes --]
Hi...I've seen a request from time to time in the history of guile
(via google) to allow #\[ and #\] to work as parens. Other schemes
support this, and apparently some people have gotten used to them.
Well, as a fun exercise I hacked up a module that you use with
#:use-syntax which allows this for all parens except the outermost
parens of an expression. There should be no performance penalty once
your module has been read in, but that initial load will take a hit
with this implementation since it basically forces the reader to read
each expression twice.
Anyway, I thought I'd post it in case some of the people wanting this
ability are still out there. It seems to work on the code I throw at
it, but please let me know if you encounter any problems.
This module (along with two modules that allow you to have alternative
keyword syntaxes-- keyw: and :keyw) will be in the next release of my
guile library. Hopefully savannah will be able to approve my project
entry soon.
Here's an example use (it's the let construction that people mostly
seem to like doing with brackets):
------------------------
(define-module (tst)
#:use-syntax (guile-syntax square-parens))
(let ([a (+ 15 3)]
[b 5]
[c 6])
(display (+ a b c)))
(newline)
;; more complicated expression....
(display [+ [+ [[if [= 0 1] + -] 5 1] 2]])
(newline)
(display "[I am not confused by] [brackets] [in] [[text]")
(display #\[) (display #\])(newline)
;; [brackets] [in comments] [obviously wouldn't count either!]
---------------------
Richard Todd
[-- Attachment #1.1.2: square-parens.scm --]
[-- Type: text/plain, Size: 1485 bytes --]
(define-module (guile-syntax square-parens)
#:export (square-parens)
#:use-module (ice-9 regex))
(define (square-parens exp)
(read
(open-input-string
(with-output-to-string
(lambda () (transform-input exp))))))
(define (transform-input exp)
;; we basically want to (write exp), except for the symbols, which we need
;; to check for #\[ and #\] along the way.
;;
;; We can't just regexp-substitute the whole thing, because someone could have
;; brackets in strings, etc., and we don't want to harm those.
(cond
;; lists and pairs
((pair? exp)
(display " ( ")
(let lp ((elt exp))
(let ((rest (cdr elt)))
(transform-input (car elt))
(cond ((pair? rest) (lp rest))
((null? rest) (display " ) "))
(else (display " . ")
(lp (cons rest '())))))))
;; vectors
((vector? exp)
(display " #( ")
(let lp ((i 0))
(if (>= i (vector-length exp))
#t
(begin (transform-input (vector-ref exp i))
(lp (+ i 1)))))
(display " ) "))
;; symbols
((symbol? exp)
(regexp-substitute/global
(current-output-port)
"\\[|\\]"
(with-output-to-string (lambda () (write exp)))
'pre (lambda (match)
(if (string=? (match:substring match) "[")
"(" ")")) 'post)
(display " "))
;; all others...
(else (write exp) (display " "))))
[-- Attachment #1.2: Type: application/pgp-signature, Size: 186 bytes --]
[-- Attachment #2: Type: text/plain, Size: 139 bytes --]
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Use [ ] as parens in guile
2004-01-11 8:21 Use [ ] as parens in guile Richard Todd
@ 2004-01-20 23:57 ` Marius Vollmer
0 siblings, 0 replies; 2+ messages in thread
From: Marius Vollmer @ 2004-01-20 23:57 UTC (permalink / raw)
Cc: guile-user
Richard Todd <richardt@vzavenue.net> writes:
> Hi...I've seen a request from time to time in the history of guile
> (via google) to allow #\[ and #\] to work as parens. Other schemes
> support this, and apparently some people have gotten used to them.
It would be great to have a customizable reader in Guile, something
like Common Lisp has. There really should be a SRFI for such a thing,
too...
--
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3 331E FAF8 226A D5D4 E405
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2004-01-20 23:57 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-01-11 8:21 Use [ ] as parens in guile Richard Todd
2004-01-20 23:57 ` Marius Vollmer
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).