How to write a macro for printing variable's name and its value 
Author Message
 How to write a macro for printing variable's name and its value

I want a macro debug-format  such that

  (debug-format x y)

translates into

  (format t "~A = ~A  ~A = ~A" x <value-of-x> y <value-of-y>)

E.g.. (let ((x 3)
            (y 4))
         (debug-format x y))

should print

x = 3   y = 4

The problem is I don't know how to get the value of lexical variables
during a macroexpansion.

Any suggestion?



Wed, 25 Jan 1995 02:34:03 GMT  
 How to write a macro for printing variable's name and its value
   I want a macro debug-format  such that

     (debug-format x y)

   translates into

     (format t "~A = ~A  ~A = ~A" x <value-of-x> y <value-of-y>)

   E.g.. (let ((x 3)
               (y 4))
            (debug-format x y))

   should print

   x = 3   y = 4

   The problem is I don't know how to get the value of lexical variables
   during a macroexpansion.

How about

(defmacro debug-format (&rest vars)
  (let ((vars-and-vals (mapcan #'(lambda (var) `(',var ,var))
                               vars)))

--

"Wind in my hair - Shifting and drifting - Mechanical music - Adrenaline surge"
        - Rush



Tue, 24 Jan 1995 20:59:36 GMT  
 How to write a macro for printing variable's name and its value
|>
|> I want a macro debug-format  such that
|>   (debug-format x y)
|> translates into
|>   (format t "~A = ~A  ~A = ~A" x <value-of-x> y <value-of-y>)
|> The problem is I don't know how to get the value of lexical variables
|> during a macroexpansion.
   ^^^^^^^^^^^^^^^^^^^^^^^


        (debug-format x y)
should expand into
        (format t "~A = ~A  ~A = ~A" 'x x 'y y)

 ..................................................
                                  Len Charest, Jr.
                 JPL Artificial Intelligence Group



Wed, 25 Jan 1995 04:18:31 GMT  
 How to write a macro for printing variable's name and its value
    Date: Fri, 7 Aug 1992 14:34 EDT

    I want a macro debug-format  such that

      (debug-format x y)

    translates into

      (format t "~A = ~A  ~A = ~A" x <value-of-x> y <value-of-y>)

(defmacro debug-format (&rest vars)

    E.g.. (let ((x 3)
                (y 4))
             (debug-format x y))

    should print

    x = 3   y = 4

    The problem is I don't know how to get the value of lexical variables
    during a macroexpansion.

    Any suggestion?



Wed, 25 Jan 1995 04:57:55 GMT  
 How to write a macro for printing variable's name and its value
You should know better than to rplacd (or mapcan, in this case) a
backquoted structure.  And, btw, I recommend using ~S, not ~A, to
see the values of variables.  (In some cases, I'd use them for the
names, too, but probably not for debugging.) So:

 (defmacro debug-format (&rest vars)

As an aside, note that you can also do the following...

 (defmacro debug-format (&rest vars)



Wed, 25 Jan 1995 06:38:59 GMT  
 How to write a macro for printing variable's name and its value

writes:

Quote:

>I want a macro debug-format  such that
>  (debug-format x y)
>translates into
>  (format t "~A = ~A  ~A = ~A" x <value-of-x> y <value-of-y>)

>E.g.. (let ((x 3)
>            (y 4))
>         (debug-format x y))

>should print
>x = 3   y = 4

Since the question seems to be how to get at lexical vars, not how
to generalize to an arbitrary number of args, here's a simple case:

(defmacro debug-format (a b)
  `(format t "~%~S = ~S, ~S = ~S" ',a ,a ',b ,b))

The original posting mentioned something about having trouble getting
the values of lexical vars, so I would guess the problem involved
quoting the var and evaluating it outside, as with something like

(symbol-value ',a)

Remember:

(setq a 5)
(let ((a 6))
  (print (symbol-value 'a)))

Prints 5, not 6. Same with "eval"...

                                - Marty
------------------------------
Q: Did you bring your Rollerblades?
A: (catholic-p 'pope)
                        - Conversation with Paul McNamee



Wed, 25 Jan 1995 04:26:06 GMT  
 How to write a macro for printing variable's name and its value


Quote:
> As an aside, note that you can also do the following...
>  (defmacro debug-format (&rest vars)


Let me respectfully suggest that although you could, you shouldn't.
Consider
  (debug-format ~a) => (format t "~A = ~S " ~a)

Using FORMAT NIL to build a format string should probably go onto the
pitfalls list, despite being highly unlikely to break anything.
__
Pekka P. Pirinen
Harlequin Ltd, once HMS Government deigns to grant me a work permit
(handler-bind ((wetware-error ...)) (post-to-usenet "..."))



Sat, 28 Jan 1995 03:07:34 GMT  
 How to write a macro for printing variable's name and its value

Quote:

>Using FORMAT NIL to build a format string should probably go onto the
>pitfalls list, despite being highly unlikely to break anything.

I've seen it break things.  Not with variables containing "~" in them, but
with messages about files.  I remember going into the de{*filter*} in Symbolics
Zmacs or Zmail when it was trying to print a message about a file, and it
was a Unix GNU Emacs backup file, with a name like "foo~".

Another gotcha is using an ordinary string as the format string, e.g.:

  (format t string)

rather than

  (format t "~A" string)

I've seen this same mistake made with C printf() and even years ago on
Multics with ioa_().  It's one of those mistakes that people seem bound to
make with control-string-driven formatted I/O.

Perhaps the C++ people have the right idea with their overloadable <<
operator.  For those who don't know, the simple case is

        stream << obj1 << obj2 << obj3

which is parsed and executed as

        (((stream << obj1) << obj2) << obj3)

The convention is that << implementations return the stream, so this
cascading has the intuitive effect.

However, in addition to outputting ordinary objects, you can also output
special marker objects.  These aren't printed to the stream, but instead
change the stream's state.  I don't know the details, but you can do
something like:

        stream "Value is: " << setw(5) << 12.34567

setw(5) returns a "width" object, which indicates that further objects
should be printed in 5-character fields.  This doesn't really fit into the
Lisp style, though, where we like everything to have a printed
representation; i.e. how would you print a width object?
--
Barry Margolin
System Manager, Thinking Machines Corp.




Sat, 28 Jan 1995 03:46:12 GMT  
 How to write a macro for printing variable's name and its value

Quote:
>    I want a macro debug-format  such that

>      (debug-format x y)

>    translates into

>      (format t "~A = ~A  ~A = ~A" x <value-of-x> y <value-of-y>)

>    Any suggestion?

The trick to this macro is that the macro doesn't access the lexical
environment, the EXPANSION does. So, your macro needs to build quoted
forms of the expressions in EXPRS to do the left hand side of the "="

(defmacro debug-print (&rest exprs)
  `(format t
           "~&~{~s = ~s~^~&~}"
           (list ,.(mapcan #'(lambda (expr)
                               `(',expr ,expr))
                           exprs))))

(let ((x 10)
      (y "why")
      (z 'zee))
  (debug-print x y z (list x y z)))

X = 10
Y = "why"
Z = ZEE
(LIST X Y Z) = (10 "why" ZEE)
NIL

This code uses the ~{ and ~} format directives but could just as
easily consed up the format string.

Hope this helps...

--
Rich Duncan                     Black Diamond Software, Inc.
(203)438-3050                   127 Wilton Road East



Sat, 28 Jan 1995 06:03:19 GMT  
 How to write a macro for printing variable's name and its value

Quote:

>  [...] how would you print a width object?


2. By having some functions or streams that obeyed format control
   objects and others that didn't.  (Sort of like the way we can
   sometimes get #\newline and sometimes
   .)

Anyway, objects that change the state of the stream may be a bad idea
just because their effects aren't limited by to a dynamic extent.



Sat, 28 Jan 1995 22:20:57 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. (SETF (#'named-function variable) value)

2. values of variable variable-names

3. Using a variable name for a variable name??

4. Write statement does not print correct value?

5. writing macro's in clarion

6. Printing the value of a variable

7. dbx: how to print the values of all variables in a common block

8. redirecting print output to variable name in awk

9. Printing to a file with a variable name

10. Help with macros writing macros in Bigloo

11. Square Root in ASM (I'd also like a print literal Macro)

12. Printing variable names?

 

 
Powered by phpBB® Forum Software