Programmable delimiters

9 views
Skip to first unread message

Eduardo Cavazos

unread,
Dec 20, 2009, 8:09:12 PM12/20/09
to chibi-...@googlegroups.com
Hello,

On the Ikarus mailing list, I brought up the subject of programmable
delimiters:

http://ikarus-scheme.org/pipermail/ikarus-users/2009-December/000692.html

Chibi, being so small and simple, was easy to coax into allowing the
'brackets' idea I mention there. :-)

>From an actual Chibi session:

> [list 1 2 3]
ERROR: undefined variable: brackets

;; Remember, [...] expands into (brackets ...). So, let's define
;; 'brackets' to be a macro such that [...] act like parenthesis:

> (define-syntax brackets
(syntax-rules ()
((brackets expr ...)
(expr ...))))

;; And try again:

> [list 1 2 3]
(1 2 3)

;; Now let's try a different 'brackets' macro which enables a shorthand
;; syntax for vector element getting and setting:

> (define-syntax brackets
(syntax-rules ()
((brackets v i)
(vector-ref v i))
((brackets v i val)
(begin
(vector-set! v i val)
v))))
> (define vec (vector 'a 'b 'c 'd 'e))
> [vec 0]
a
> [vec 0 'xyz]
#(xyz b c d e)
> vec
#(xyz b c d e)
>

I only needed to change 'sexp.c' and 'sexp.h'. I included the hacked
versions.

Ed

sexp.c
sexp.h

Alex Shinn

unread,
Dec 21, 2009, 2:36:10 AM12/21/09
to chibi-...@googlegroups.com
Hi,

Eduardo Cavazos <wayo.c...@gmail.com> writes:

> On the Ikarus mailing list, I brought up the subject of programmable
> delimiters:
>
> http://ikarus-scheme.org/pipermail/ikarus-users/2009-December/000692.html
>
> Chibi, being so small and simple, was easy to coax into allowing the
> 'brackets' idea I mention there. :-)

Cute :) If you're going to use this it's probably worth
making {braces} programmable as well. And for real-world
use I think it may be important to ensure the closing
delimiters match their opening counterparts correctly, so
that [foo) would be an error.

... unless you wanted to make both names programmable so
that [0, 1) would read as (bracket paren 0 (unquote 1))
which could expand into a half-open set representation :)

--
Alex

Reply all
Reply to author
Forward
0 new messages