[plt-scheme] Macros and modules

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Fri Jun 11 06:18:11 EDT 2004

Jens Axel Søgaard wrote:

>   [soegaard at commander plt]$ mzc --exe main main.scm
>   MzScheme compiler (mzc) version 205, Copyright (c) 1996-2003 PLT
>   [output to "main"]
>   [soegaard at commander plt]$ ./main
>   reference to undefined identifier: constant

I found a bug in define-constant in "constants.scm".
The two occurences of (eval ...) needs to be identical.
See the fixed version below.

But - shouldn't I get an error in mzscheme then?


[soegaard at commander plt]$ mzc --exe main main.scm
MzScheme compiler (mzc) version 205, Copyright (c) 1996-2003 PLT
  [output to "main"]
[soegaard at commander plt]$ ./main
3.141592653589793
1.4142135623730951
4.555806215962888

[soegaard at commander plt]$ cat constants.scm
(module constants mzscheme
   (provide define-constant constant) ; exports

   (require-for-syntax "compile-time.scm")

            (let ((constant-name  (syntax-object->datum (syntax name)))
                  (constant-value
                   (eval `(begin
                            ; If <exp> in (define-constant <name> <exp>) contains
                           ; uses of (constant <name>) then we have to
                           ; inject it into the environment where <exp> is evaluated.
                           (define-syntax constant
                              (syntax-rules ()
                                ((_ name) (let ((a (assoc 'name ',constants)))
                                            (if a (cdr a) (error 'name))))))
                            ; now (constant <name>) is available
                           ,(syntax-object->datum (syntax exp))))))

    ...

             (with-syntax  (((compile-time-register) (generate-temporaries '(compile-time-register))))
                (syntax (define-syntax compile-time-register
                          (register-constant 'name
                                    (eval `(begin
                                              ;; If <exp> in (define-constant <name> <exp>) contains
                                              ;; uses of (constant <name>) then we have to
                                              ;; inject it into the environment where <exp> is 

                                              ;; evaluated.
                                             (define-syntax constant
                                                (syntax-rules ()
                                                  ((_ name) (let ((a (assoc 'name ',constants)))
                                                                      (if a (cdr a)
                                                                            (error 'name))))))
                                                     ;; now (constant <name>) is available
                                                     ,(syntax-object->datum (syntax exp))))))))))))))

-- 
Jens Axel Søgaard




Posted on the users mailing list.