stumped on a macro
From: David Wallis (dww_at_dwws-computer.local)
Date: 03/28/05
- Next message: Frank Buss: "Re: stumped on a macro"
- Previous message: M Jared Finder: "Re: Newbie lisper looking for hints"
- Next in thread: Frank Buss: "Re: stumped on a macro"
- Reply: Frank Buss: "Re: stumped on a macro"
- Reply: mmcconnell17704_at_yahoo.com: "Re: stumped on a macro"
- Reply: Pascal Bourguignon: "Re: stumped on a macro"
- Reply: Alan Crowe: "Re: stumped on a macro"
- Reply: Kalle Olavi Niemitalo: "Re: stumped on a macro"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 28 Mar 2005 17:52:01 +0100
I'm trying to write my first non-trivial macro, and it's had me stumped
for about 3 weeks. Can anyone help?
I contacted the list a while back about implementing operators to do array
arithmetic. I got that working (see www.cpom.org/nlisp/index.html), but I
want to re-do them as macros instead of functions and I'm struggling.
I want to write expressions like the following (Note (.iseq a b) is a
function that returns an array of numbers between the a and b. I'm just
using .iseq as an example of something that will return an array. It's not
important to the problem).
(.+ (.iseq 1 10) 3d0 (.iseq 11 20))
And I want .+ to be a macro that will expand the above to something like:
(let ((r (make-array 10 :element-type '(complex double-float)))
(a (.iseq 1 10))
(b 3d0)
(c (.iseq 11 20)))
(dotimes (i 10)
(setf (aref r i) (+ (aref a i) b (aref c i)))) r)
(Of course, for the real code, I should use gensyms, and I'll have a load
of type declarations etc. for efficiancy).
One advantage of using a macro is that I want to get the array size at
compile time to pass to make-array and for the dotimes loop. This will
save doing them at run time and will speed it up no end. I can't think of
any reason why this shouldn't be possible.
The macro has to look at each argument that I pass and insert either (aref
x i) or just x, depending on the 'type' of the argument (if it's an array
or a scaler) in the last line.
Here's the problem. If I have a function called .+ and call it like this:
(.+ (.iseq 1 10) 3d0)
lisp will evaluate the call to .iseq and pass to .+ an array and 3d0. With
a macro call, the .iseq will not be evaluated, so the macro will get the
expression (.iseq 1 10) and not the value.
So, if I don't have the values of the arguments, how can I find the type
and array size? Or to put it another way, I need to evaluate the arguments
to the macro to expand it.
My final macro will probably take a function as the fist argument, so I
can define functions like .+ at compile time with any array dimensions and
combination of array and scaler arguments. Or even pass a lambda
expression and some arguments. Any function that operates on scaler
arguments will do, and the macro writes the appropriate loop. This is all
easy if I can just solve the current problem.
If anyone can suggest a solution or offer any help, I would be very
greatful. Maybe my whole approach is wrong?
- Next message: Frank Buss: "Re: stumped on a macro"
- Previous message: M Jared Finder: "Re: Newbie lisper looking for hints"
- Next in thread: Frank Buss: "Re: stumped on a macro"
- Reply: Frank Buss: "Re: stumped on a macro"
- Reply: mmcconnell17704_at_yahoo.com: "Re: stumped on a macro"
- Reply: Pascal Bourguignon: "Re: stumped on a macro"
- Reply: Alan Crowe: "Re: stumped on a macro"
- Reply: Kalle Olavi Niemitalo: "Re: stumped on a macro"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|