POV-Ray : Newsgroups : povray.general : semi-colon in macro ? : Re: semi-colon in macro ? Server Time
11 Aug 2024 09:22:04 EDT (-0400)
  Re: semi-colon in macro ?  
From: Chris Colefax
Date: 16 Aug 1999 22:38:47
Message: <37b8cb37@news.povray.org>
ingo <ing### [at] ingodemonnl> wrote:
> Three little macro's. The first and third are ok, the second is not. The
only
> difference between Twee and Drie is the position of the semi-colons. I
> understand that a macro works by text replacement. That does not make me
> understand why Twee doesn't work and Drie does. Can somebody explain?

POV-Ray is really quite forgiving when it comes to using macros to return
values; for example, #declare Vector = <Twee(1, 2, 3), 0, 0>; will work when
#declare Float = Twee(1, 2, 3); will not.  One way to solve the problem is
the third example you gave, but hard coding the semi-colons limits the
places in which you can use the macro, eg. you can no longer do #declare
Float = Drie (1, 2, 3) + Drie (4, 5, 6);

If I had to give an explanation (always a dangerous thing to do!) I would
guess that when declaring floats POV-Ray will look for the semi-colon after
any numbers or arithmetic, and if it doesn't find it will return a warning,
eg:

   #declare Som2=Twee(1,2,3);

calls the macro, and reaches the "#else A+B+C #end" section.  When it parses
the #end it finishes the declare (and gives a warning) - then when it exits
the macro, it finds another, unwanted semi-colon -  and this is exactly the
error it returns.

The good news is that fixing the problem is quite simple, and it's possible
to create macros that perform many conditional statements yet return a
single value that can be declared or used elsewhere without problems: simply
rewrite your macro to locally declare the return value, and then make this
the last statement (on its own) in the macro:

   #macro Twee(A,B,C)
     #local P=A+B+C;
     #if (P=0)
        #local R = A+B-C;
     #else
        #local R = A+B+C;
     #end
     R
  #end

Now you can quite comfortably use #declare Som2=Twee(1,2,3); or sphere
(Twee(1,2,3), Twee(4,5,6)) or #declare MyValue = Twee(1,2,3)*Twee(4,5,6);,
or just about any other possible use you can think of.


Post a reply to this message

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