POV-Ray : Newsgroups : povray.general : Macros in 3.1 (not a bug report!) Server Time
16 Nov 2024 13:21:05 EST (-0500)
  Macros in 3.1 (not a bug report!) (Message 1 to 3 of 3)  
From: Alan Grainger
Subject: Macros in 3.1 (not a bug report!)
Date: 17 Jul 1998 17:23:58
Message: <35afb2de.0@news.povray.org>
How do you exit from a macro?  For instance, you have called a macro, and a
certain condition is reached, and you want the macro to exit.  How?

#macro DoMacro ( a, b, c )
    blah blah...
    #if ( something = true )
        #endmacro
    #end
    more junk...
#end

Also, is there any way to goto somewhere?

#macro DoMacro ( a, b, c )
    blah blah...
here:
    blah blah...
    #if ( something = true )
        #goto here
    #end
    more stuff...
#end

Thanks for any help.  :)

--
Alan Grainger
E-mail - scy### [at] geocitiescom
Homepage - http://alansworld.home.ml.org


Post a reply to this message

From: Nathan O'Brien
Subject: Re: Macros in 3.1 (not a bug report!)
Date: 18 Jul 1998 01:13:46
Message: <35B01F61.EE0DCB6@no13.net>
Alan Grainger wrote:
> 
> How do you exit from a macro?  For instance, you have called a macro, and a
> certain condition is reached, and you want the macro to exit.  How?
> 
> #macro DoMacro ( a, b, c )
>     blah blah...
>     #if ( something = true )
>         #endmacro
>     #end
>     more junk...
> #end
> 
> Also, is there any way to goto somewhere?
> 
> #macro DoMacro ( a, b, c )
>     blah blah...
> here:
>     blah blah...
>     #if ( something = true )
>         #goto here
>     #end
>     more stuff...
> #end
> 
> Thanks for any help.  :)

The easiest way I know of doing the first is placing the code inside a
#while loop with a simple switch. say

#macro DoMacro ( etc )
	#local Switch = 0;
	#while (Switch = 0)
		#if ( something = true )
			#local Switch = 1;
		#end
	#end
#end

As for the second, again placing sitches for various sections means that
instead of a literal goto, segments of the macro will be skipped. And if
you want to go backwards, intead of forwards, maybe the goto location
should be another macro in its own right. 

-- 
regards

Nathan O'Brien
no1### [at] no13net
http://www.no13.net


Post a reply to this message

From: Johannes Hubert
Subject: Re: Macros in 3.1 (not a bug report!)
Date: 20 Jul 1998 12:40:40
Message: <35b364f8.0@news.povray.org>
Alan Grainger wrote in message <35afb2de.0@news.povray.org>...
>How do you exit from a macro?  For instance, you have called a macro, and a
>certain condition is reached, and you want the macro to exit.  How?

You can do it all with "whiles" and "ifs" (actually with "whiles" alone, but
"ifs" are nicer sometimes):

Your version:

>#macro DoMacro ( a, b, c )
>    blah blah...
>    #if ( something = true )
>        #endmacro
>    #end
>    more junk...
>#end

becomes:

#macro DoMacro( a, b, c )
   blah blah...
   #if (something = false)
      more junk...
   #end
#end

and this one:

>#macro DoMacro ( a, b, c )
>    blah blah 1...
>here:
>    blah blah 2...
>    #if ( something = true )
>        #goto here
>    #end
>    more stuff...
>#end


becomes:

#macro DoMacro ( a, b, c )
   blah blah 1...
   #declare first_time = true
   #while (something = true | first_time)
       blah blah 2...
       first_time = false
   #end
   more stuff...
#end

(I'm not sure that I got the syntax right, but you catch my meaning).

One of the first things you learn in advanced programming courses (and even
in good general/basic ones) is, that you don't need "Goto" at all.
Everything can be written in much better and readable style with "ifs" and
"whiles".
Simply ask yourselve whenever you crave for a "Goto": "How can I do it
without?". There always is an answer, and after some time you will find
yourself using "Goto" less and less, until it becomes natural not to use it.
Although it would be nice if POV-Ray had the other kind of while (which is
executed at least once), which would
create code without the "first_time" thing:

#macro DoMacro ( a, b, c )
   blah blah 1...
   #do
       blah blah 2...
   #while (something = true)
   more stuff...
#end


Hope it helps,
Johannes.


Post a reply to this message

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