POV-Ray : Newsgroups : povray.advanced-users : pass variable from macro? Server Time
24 Nov 2024 08:07:59 EST (-0500)
  pass variable from macro? (Message 1 to 9 of 9)  
From: [GDS|Entropy]
Subject: pass variable from macro?
Date: 26 Jan 2008 03:40:18
Message: <479af1f2$1@news.povray.org>
Hello,

A little stuck here... evidently povray does not support any type of 
Return() function, and I am not familiar enough with sdl yet to figure this 
out any other way.
This little macro does what it says, but not in this form.

#macro isEvenOrOdd(integer)
 #if(mod(integer,2) = 0)
  #local isEven = 1;
 #else
  #local isOdd = 1;
 #end
#end

I had to use an instance of the following each time that I wished to use 
this function:
       // Determines if even or odd
       #if(mod(glowTypeRand,2) = 0)
        type 0
       #else
        type 2
      #end

There HAS to be some way to pass variables from a macro...right? :-\

Thanks,
Ian


Post a reply to this message

From: Mike Williams
Subject: Re: pass variable from macro?
Date: 26 Jan 2008 04:01:38
Message: <gc5TVmB6avmHFwia@econym.demon.co.uk>
Wasn't it [GDS|Entropy] who wrote:
>Hello,
>
>A little stuck here... evidently povray does not support any type of
>Return() function, and I am not familiar enough with sdl yet to figure this
>out any other way.
>This little macro does what it says, but not in this form.
>
>#macro isEvenOrOdd(integer)
> #if(mod(integer,2) = 0)
>  #local isEven = 1;
> #else
>  #local isOdd = 1;
> #end
>#end
 >
>There HAS to be some way to pass variables from a macro...right? :-\

You're confusing the concept of MACRO with the concept of FUNCTION.

The entire contents of a macro are "returned", so you can write:


#macro isEven(integer)
  #if(mod(integer,2) = 0)
   #local even = 1;
  #else
   #local even = 0;
  #end
  (even)
#end

#if (isEven(3))
   #debug "3 is even\n"
#else
   #debug "3 is odd\n"
#end


Or
#macro isOdd(integer)
  (mod(integer,2))
#end

#if (isOdd(3))
   #debug "3 is odd\n"
#else
   #debug "3 is even\n"
#end


Don't get confused into thinking that those macros get evaluated and 
then return integer values. They "return" chunks of SDL code like
   #if(mod(3,2) = 0) #local even=1; #else #local even = 0; #end (even)
And what happens is that
   #if (isEven(3))
effectively becomes
   #if (#if(mod(3,2) = 0) #local even=1; #else #local even = 0; #end 
(even))
and THEN gets evaluated.

-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

From: [GDS|Entropy]
Subject: Re: pass variable from macro?
Date: 26 Jan 2008 04:56:50
Message: <479b03e2$1@news.povray.org>
Ahh...I think I see now.

So essentially a macro in pov-sdl, when called, returns a code 
block/whatever its contents happen to be?

Am I correct to believe they can be written to represent so-called 
"high-order" functions, which will accept a functions as a variable, and can 
return other functions as a result?

If this is the case, it will now be much easier to work with macros, as 
before I was treating them as low-level functions.

I guess that debug/concat feature might help in determining this sort of 
thing in the future, huh? hehehe :-)

Thanks,
Ian

"Mike Williams" <nos### [at] econymdemoncouk> wrote in message 
news:gc5### [at] econymdemoncouk...
> Wasn't it [GDS|Entropy] who wrote:
>>Hello,
>>
>>A little stuck here... evidently povray does not support any type of
>>Return() function, and I am not familiar enough with sdl yet to figure 
>>this
>>out any other way.
>>This little macro does what it says, but not in this form.
>>
>>#macro isEvenOrOdd(integer)
>> #if(mod(integer,2) = 0)
>>  #local isEven = 1;
>> #else
>>  #local isOdd = 1;
>> #end
>>#end
> >
>>There HAS to be some way to pass variables from a macro...right? :-\
>
> You're confusing the concept of MACRO with the concept of FUNCTION.
>
> The entire contents of a macro are "returned", so you can write:
>
>
> #macro isEven(integer)
>  #if(mod(integer,2) = 0)
>   #local even = 1;
>  #else
>   #local even = 0;
>  #end
>  (even)
> #end
>
> #if (isEven(3))
>   #debug "3 is even\n"
> #else
>   #debug "3 is odd\n"
> #end
>
>
> Or
> #macro isOdd(integer)
>  (mod(integer,2))
> #end
>
> #if (isOdd(3))
>   #debug "3 is odd\n"
> #else
>   #debug "3 is even\n"
> #end
>
>
> Don't get confused into thinking that those macros get evaluated and then 
> return integer values. They "return" chunks of SDL code like
>   #if(mod(3,2) = 0) #local even=1; #else #local even = 0; #end (even)
> And what happens is that
>   #if (isEven(3))
> effectively becomes
>   #if (#if(mod(3,2) = 0) #local even=1; #else #local even = 0; #end 
> (even))
> and THEN gets evaluated.
>
> -- 
> Mike Williams
> Gentleman of Leisure


Post a reply to this message

From: nemesis
Subject: Re: pass variable from macro?
Date: 26 Jan 2008 05:15:00
Message: <web.479b0704a2fa4d32d77696980@news.povray.org>
"[GDS|Entropy]" <gds### [at] hotmailcom> wrote:
> So essentially a macro in pov-sdl, when called, returns a code
> block/whatever its contents happen to be?

it doesn't "return" anything:  it textually copies it contents with parameters
substituted for real values.

> Am I correct to believe they can be written to represent so-called
> "high-order" functions, which will accept a functions as a variable, and can
> return other functions as a result?

it's not really like Lisp macros, more akin to C "macros"


Post a reply to this message

From: Warp
Subject: Re: pass variable from macro?
Date: 27 Jan 2008 02:14:45
Message: <479c2f65@news.povray.org>
nemesis <nam### [at] gmailcom> wrote:
> "[GDS|Entropy]" <gds### [at] hotmailcom> wrote:
> > So essentially a macro in pov-sdl, when called, returns a code
> > block/whatever its contents happen to be?

> it doesn't "return" anything:  it textually copies it contents with parameters
> substituted for real values.

  Not really. POV-Ray doesn't copy anything. When a macro call is encountered
it jumps to the macro body and continues parsing there. When the macro has
been parsed, it returns to the origin of the jump.

  The difference can be seen eg. with #locals.

-- 
                                                          - Warp


Post a reply to this message

From: nemesis
Subject: Re: pass variable from macro?
Date: 27 Jan 2008 07:35:00
Message: <web.479c7a40a2fa4d32f8eca44b0@news.povray.org>
Warp <war### [at] tagpovrayorg> wrote:
> nemesis <nam### [at] gmailcom> wrote:
> > "[GDS|Entropy]" <gds### [at] hotmailcom> wrote:
> > > So essentially a macro in pov-sdl, when called, returns a code
> > > block/whatever its contents happen to be?
>
> > it doesn't "return" anything:  it textually copies it contents with parameters
> > substituted for real values.
>
>   Not really. POV-Ray doesn't copy anything. When a macro call is encountered
> it jumps to the macro body and continues parsing there. When the macro has
> been parsed, it returns to the origin of the jump.
>
>   The difference can be seen eg. with #locals.

hmm, less bad...


Post a reply to this message

From: Tim Attwood
Subject: Re: pass variable from macro?
Date: 27 Jan 2008 17:39:00
Message: <479d0804$1@news.povray.org>
Remember that #local and #declare are not the same
scope. #local inside a macro is local to the macro,
but a #declare inside a macro is global. If you
use #declare inside a macro you can use the
declared variable to return values.


Post a reply to this message

From: Thomas de Groot
Subject: Re: pass variable from macro?
Date: 31 Jan 2008 03:46:34
Message: <47a18aea$1@news.povray.org>
"Warp" <war### [at] tagpovrayorg> schreef in bericht 
news:479c2f65@news.povray.org...
>
>  Not really. POV-Ray doesn't copy anything. When a macro call is 
> encountered
> it jumps to the macro body and continues parsing there. When the macro has
> been parsed, it returns to the origin of the jump.
>

...reminds me of the good old 'subroutines' in Fortran...

Thomas


Post a reply to this message

From: Darren New
Subject: Re: pass variable from macro?
Date: 31 Jan 2008 23:48:18
Message: <47a2a492@news.povray.org>
Thomas de Groot wrote:
> "Warp" <war### [at] tagpovrayorg> schreef in bericht 
> news:479c2f65@news.povray.org...
>>  Not really. POV-Ray doesn't copy anything. When a macro call is 
>> encountered
>> it jumps to the macro body and continues parsing there. When the macro has
>> been parsed, it returns to the origin of the jump.
>>
> 
> ....reminds me of the good old 'subroutines' in Fortran...

Nah. Fortran just didn't guarantee you had a stack on the machine. And 
many mainframes of the time didn't. You had "BAL" - Branch And Link, 
which left the previous program counter in a register and branched to 
the new address. Actually pushing that register onto a stack after was 
usually-unnecessary overhead.

-- 
   Darren New / San Diego, CA, USA (PST)
     On what day did God create the body thetans?


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.