First of all, you shouldn’t 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 “wraps” each datum (and sub-datum) in a “source” 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@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@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list