chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] [Q] How can I convert this lisp(SBCL) macro to chick


From: Sungjin Chun
Subject: Re: [Chicken-users] [Q] How can I convert this lisp(SBCL) macro to chicken scheme?
Date: Fri, 7 Nov 2014 11:21:05 +0900

Thank you. Though I'm not sure on my code is in good style, I managed to convert my
code to chicken scheme like this;

(set-read-syntax!
 #\[
 (lambda (port)
   (let loop ((c (peek-char port)) (exps '()))
     (cond ((eof-object? c)
            (error "EOF encountered while parsing [ ... ] clause"))
           ((char=? c #\])
            (read-char port) ; discard                                                                                                                                                                                              
            `(vector ,@(reverse exps)))
           ((char-whitespace? c)
            (read-char port) ; discard whitespaces                                                                                                                                                                                  
            (loop (peek-char port) exps))
           (else
            (let ((exp (read port)))
              (loop (peek-char port)
                    (cons exp exps))))))))

(print (with-input-from-string "[1 2 3 4 5]" read))

(set-read-syntax!
 #\{
 (lambda (port)
   (let loop ((c (peek-char port)) (exps '()))
     (cond ((eof-object? c)
            (error "EOF encountered while parsing [ ... ] clause"))
           ((char=? c #\})
            (read-char port) ; discard                                                                                                                                                                                              
            `(alist->hash-table (chop (list ,@(reverse exps)) 2)))
           ((char-whitespace? c)
            (read-char port) ; discard whitespaces                                                                                                                                                                                  
            (loop (peek-char port) exps))
           ((char=? c #\,)
            (read-char port) ; discard whitespaces                                                                                                                                                                                  
            (loop (peek-char port) exps))
           (else
            (let ((exp (read port)))
              (loop (peek-char port)
                    (cons exp exps))))))))

(print (with-input-from-string "{'a 10, 'b 20, 'c 30}" read))
(print (with-input-from-string "{'a 10 'b 20 'c 30}" read))

I post these code for people with similar needs like myself :-).


On Fri, Nov 7, 2014 at 10:33 AM, Evan Hanson <address@hidden> wrote:
Hi Sungjin,

These are typically referred to as "reader extensions" in the Scheme
world: http://api.call-cc.org/doc/library#sec:Reader_extensions

You'll probabably want `set-read-syntax!` in particular:
http://api.call-cc.org/doc/library/set-read-syntax!

Cheers,

Evan


reply via email to

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