POV-Ray : Newsgroups : povray.bugreports : #if/#else/#end in nested macros--*possible* problem? Server Time
29 Apr 2024 02:55:40 EDT (-0400)
  #if/#else/#end in nested macros--*possible* problem? (Message 1 to 3 of 3)  
From: Kenneth
Subject: #if/#else/#end in nested macros--*possible* problem?
Date: 9 Sep 2009 11:35:00
Message: <web.4aa7ca00e97b83e6f50167bc0@news.povray.org>
Concerns v3.6.1c (sorry, I'm working on an elaborate animation and don't want to
switch to the newer version until everything's done and rendered.)

I've lately come across a 'situation' in using nested macros--all containing #if
blocks--causing a hard-to-pinpoint error.  (I can't say for sure what the
'problem' actually is, because my code is so complex that it's difficult to
construct some meaningful test code.)  Essentially, I'm curious about
something: Is it *possible* that macros-within-macros, each using #if
blocks--some WITH an #else statement and some without--might create a problem
for POV's core code trying to determine which #else statement to use for which
macro? Here's a very simplistic example of what I mean:

#macro my_macro_1()
 #if (*something is true*)
 --#local *do something*--
 // #else// left out, not specifically needed for code. #if does nothing.
 #end
#end

#macro my_macro_2()
  #if(*something else is true)
  my_macro_1()
  #else // an ACTUAL #else
  --#local *do something else*
  #end
#end

#macro my_macro_3()
  #if(*something different is true)
  my_macro_2() // contains macros 1 and 2
  // #else // again left out, not needed. #if does nothing.
  #end
#end

Then invoking the third macro:
my_macro_3()


In macros 1 and 3, the #else statement is left out in the macros' #if blocks
(but implied to be there, according to docs.) I'm aware that macros using
#locals invoke the 'most immediate' use of variables. But I'm wondering about
the 'implied' #else statements-- if, somehow, POV could perhaps be mistakenly
using macro 2's REAL #else in macros 1 or 3 (depending on which of the three
#if's is true or false.) OR, vice versa--the 'invisible' #else in  macros 1 and
3 affecting macro 2. I'm sorry I can't be more exact as to a test-code scene;
this simple example may not even be appropriate. (My own real code has far more
complex macros.)

What prompts my question is that the problem in my own code *seems* to have been
solved by going back and adding ALL of the #else terms to the macros, even
though some were not actually needed.  (And I've since taken to doing this as a
'safety measure' in all my scenes.) But right now, this 'solution' is kind of a
mystery to me, not knowing if it's really necessary. There could be something
else wrong with my code (although I've gone over it in detail, looking for
mistakes)--in which case I simply stumbled onto a 'workable' solution that
didn't solve the real problem!

Ken


Post a reply to this message

From: clipka
Subject: Re: #if/#else/#end in nested macros--*possible* problem?
Date: 9 Sep 2009 12:24:30
Message: <4aa7d6be$1@news.povray.org>
Kenneth schrieb:
> Is it *possible* that macros-within-macros, each using #if
> blocks--some WITH an #else statement and some without--might create a problem
> for POV's core code trying to determine which #else statement to use for which
> macro?

No, unless there would be a serious bug somewhere in there.

Such things can happen in languages where the if statement does not 
necessarily open a statement block, but can be followed by a single 
statement, and the else clause is optional, as in C, allowing for the 
syntax variants

     if (CONDITION) STATEMENT
     if (CONDITION) STATEMENT else STATEMENT

as shorthand for

     if (CONDITION) { STATEMENT }
     if (CONDITION) { STATEMENT } else { STATEMENT }

causing ambiguity whenever the shorthand form is used in nested if 
constructs with some else clause being omitted:

     if (CONDITION) if (CONDITION) STATEMENT else STATEMENT

By definition the else is then attributed to the last if, which may or 
may not be the intended behavior.

However, this cannot happen in POV-Ray, as each #if statement must 
always be matched by an #end, so there is never any ambiguity which #if 
an #else belongs to.

You can still confuse the parser by forgetting the #end for an #if, but 
in this case the parser will notice that something is bogus:

     #if (foo)
         #if (bar)
             execute_if_foo_and_bar()
         /* #end forgotten here */
     #else
         execute_if_not_foo()
     #end
     /* parser will complain about a missing #end here */

As #end is also used to terminate macros, there's also no way to define 
partial #if statements in a macro.


Post a reply to this message

From: Kenneth
Subject: Re: #if/#else/#end in nested macros--*possible* problem?
Date: 9 Sep 2009 12:50:01
Message: <web.4aa7dbeae0f8b7caf50167bc0@news.povray.org>
clipka <ano### [at] anonymousorg> wrote:
> Kenneth schrieb:
> > Is it *possible* that macros-within-macros, each using #if
> > blocks--some WITH an #else statement and some without--might create a problem
> > for POV's core code trying to determine which #else statement to use for which
> > macro?
>
> No, unless there would be a serious bug somewhere in there.
>
> Such things can happen in languages where the if statement does not
> necessarily open a statement block...
>
> However, this cannot happen in POV-Ray, as each #if statement must
> always be matched by an #end, so there is never any ambiguity which #if
> an #else belongs to.
>

Thanks. I guess I need to go back over my entire code in even more detail (ugh!)
to see if I've left out anything. (My animation scene incorporates four
elaborate #include files!) But I'll also keep my 'safety measure' intact for
now. ;-)

Ken


Post a reply to this message

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