chicken-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-hackers] [PATCH] Make quasiquote extensible with custom handler


From: Moritz Heidkamp
Subject: [Chicken-hackers] [PATCH] Make quasiquote extensible with custom handlers
Date: Fri, 22 Feb 2013 09:02:45 +0100

Fellow Chickeneers,

find attached a patch which allows user code to hook into the quasiquote
code expander. "What good can come from that?" you might ask. Let me
elaborate! As you know, Chicken supports custom reader extensions via
set-read-syntax! and friends. This is all fine unless you try to add
custom read syntax for compound data structures. One example of those
are SRFI 10 reader constructors which are also supported by core
Chicken. Let me give you an example how they work:

  #;1> (define-record point x y)
  #;2> (define-reader-ctor 'point make-point)
  #;3> '#,(point 10 20)
  #<point>
  #;4> (point-x #3)
  10
  #;5> (point-y #3)
  20

That's pretty straight forward. Now try this:

  #;6> `#,(point ,(+ 5 5) 20)
  #<point>
  #;7> (point-x #6)
  (unquote (+ 5 5))

Oops, not quite what we'd expect! Now, this problem was also discovered
in 2004 by Bradd W. Szonye (see
http://srfi.schemers.org/srfi-10/post-mail-archive/msg00000.html), alas
it was in the post-finalization phase of SRFI 10 and unfortunatley it
doesn't seem like there was much interest in that anymore then. So I
went ahead and tried to come up with a solution for that problem. What I
think is needed for this to work is to be able to hook into the
quasiquote expander (as noted above). The result of that is the attached
patch. It allows us to do this:

  #;8> (define-quasiquote-handler point? (lambda (r walk n p) (list (r 
'make-point) (walk (point-x p) n) (walk (point-y p) n))))
  #;9> `#,(point ,(+ 5 5) 20)
  #<point>
  #;10> (point-x #9)
  10

I think this kind of mechanism is something we should have in order to
provide proper support for custom read syntax and to make SRFI 10 syntax
behave as expected. Let me know if you have any suggestions for
improving the API (not sure if quasiquote-handler is the best name for
this kind of thing) or implementation (I don't know whether we need to
pass the `n' argument to handlers or if we can hide it in a closure, for
example). Also I'm not sure about where to put the code (maybe
define-quasiquote-handler should not live in expand.scm).

Thanks!
Moritz

Attachment: 0001-Make-quasiquote-extensible-with-custom-handlers.patch
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]