[gambit-list] How to define "set-macro-character" like in Gambit.

Ryuho Yokoyama ryuho8 at ybb.ne.jp
Fri Jan 23 06:06:03 EST 2015


Thank you very much for taking time to explain and the code.

Now I can convert all my lisp code to Gambit.

-----Original Message----- 
From: Marc Feeley
Sent: Friday, January 23, 2015 2:42 PM
To: Ryuho Yokoyama
Cc: gambit-list at iro.umontreal.ca
Subject: Re: [gambit-list] How to define "set-macro-character" like in 
Gambit.

First of all, you shouldn窶冲 expect to have source code to change the 
readtable in the program and expect that the rest of the file will use the 
new readtable.  The whole source code file is read using the readtable in 
effect, and then the code is executed (so any change of the readtable will 
have no effect on the source code file).

You need to load a file that sets up the readtable.  Then subsequent files 
that are loaded will use the new readtable.

You also need to understand that the reader automatically 窶忤raps窶・each 
datum (and sub-datum) in a 窶徭ource窶・object to attach location 
information (filename, line number, column number).

So a proper implementation will look like this:

;; File: read-macro.scm

(define *Data* '((aa 11) (bb 22) (cc 33)))

(##include "~~lib/_gambit#.scm")

(define (##read-hat re c)

  ;; re is the read-environment
  ;; c will be equal to #\^

  (let ((start-pos (##readenv-current-filepos re)))
    (macro-read-char (macro-readenv-port re)) ;; skip #\^
    (let* ((wrapped-name (##read-datum-or-label re))
           (name (macro-readenv-unwrap re wrapped-name)))
      (macro-readenv-filepos-set! re start-pos) ;; set pos to start of datum
      (let ((obj (cadr (assoc name *Data*))))
        (macro-readenv-wrap re obj)))))

(let ((rt (##current-readtable)))
  (##readtable-char-class-set! rt #\^ #t ##read-hat))

;;(pp (call-with-input-string "(^aa ^bb)" read))


So you could use this file like this:

gsi -e '(load "read-macro.scm")' -

or

gsc -e '(load "read-macro.scm")' file-to-compile.scm

Marc

> On Jan 22, 2015, at 7:44 PM, Ryuho Yokoyama <ryuho8 at ybb.ne.jp> wrote:
>
> Hello,
>
> For example,
> in Common Lisp
> ---
> (defvar *Data* '((aa 11) (bb 22) (cc 33)))
>
> (set-macro-character #\^
>    #'(lambda (stream char)
>        `(cadr (assoc ',(read stream t nil t) *Data*))))
>
> (print ^bb) ; => 22
> ---
> in Gambit I tried like
> ---
> (define *Data* '((aa 11) (bb 22) (cc 33)))
>
> (##readtable-char-handler-set!
>    (##current-readtable) #\^
>       (lambda (stm ch)
>          `(cadr (assoc ',(##read stm) *Data*))
>          ;               ^^^^^^^^^^^^ ??
>          ))
>
> (pp  ^bb)  ; => #!unbound
> ---
>
> and I faild.
>
> Could you please teach me how to define "set-macro-character" like in 
> Gambit.
>
> Thanks in advance.
>
>
> _______________________________________________
> Gambit-list mailing list
> Gambit-list at iro.umontreal.ca
> https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list




More information about the Gambit-list mailing list