POV-Ray : Newsgroups : povray.general : semi-colon in macro ? Server Time
1 Nov 2024 17:18:56 EDT (-0400)
  semi-colon in macro ? (Message 1 to 5 of 5)  
From: ingo
Subject: semi-colon in macro ?
Date: 16 Aug 1999 13:10:23
Message: <37b845ff@news.povray.org>
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?

#macro Een(A,B,C)
   A+B+C
#end //macro
#declare Som1=Een(1,2,3);
sphere {Som1,1}


#macro Twee(A,B,C)
   #local P=A+B+C;
   #if (P=0)
      A+B-C
   #else
      A+B+C
   #end
#end
#declare Som2=Twee(1,2,3);
sphere {Som2,1}


#macro Drie(A,B,C)
   #local P=A+B+C;
   #if (P=0)
      A+B-C;
   #else
      A+B+C;
   #end
#end
#declare Som3=Drie(1,2,3)
sphere {Som3,1}


ingo
--
Met dank aan de muze met het glazen oog.


Post a reply to this message

From: Thorsten Froehlich
Subject: Re: semi-colon in macro ?
Date: 16 Aug 1999 13:23:14
Message: <37b84902@news.povray.org>
In article <37b845ff@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?

The documentation, section "Are POV-Ray Macros a Function or a Macro?"
explains this matter in detail. The essence is that in POV-Ray macros are
_not_ completely a simple text replacement. This is why your macro doesn't
work as expected.


    Thorsten


____________________________________________________
Thorsten Froehlich
e-mail: mac### [at] povrayorg

I am a member of the POV-Ray Team.
Visit POV-Ray on the web: http://mac.povray.org


Post a reply to this message

From: PoD
Subject: Re: semi-colon in macro ?
Date: 16 Aug 1999 18:54:35
Message: <37B89725.17F74328@merlin.net.au>
ingo 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?
> 
> #macro Een(A,B,C)
>    A+B+C
> #end //macro
> #declare Som1=Een(1,2,3);
> sphere {Som1,1}
> 
> #macro Twee(A,B,C)
>    #local P=A+B+C;
>    #if (P=0)
>       A+B-C
>    #else
>       A+B+C
>    #end
> #end
> #declare Som2=Twee(1,2,3);
> sphere {Som2,1}
> 
> #macro Drie(A,B,C)
>    #local P=A+B+C;
>    #if (P=0)
>       A+B-C;
>    #else
>       A+B+C;
>    #end
> #end
> #declare Som3=Drie(1,2,3)
> sphere {Som3,1}
> 
> ingo
> --
> Met dank aan de muze met het glazen oog.

I can't say exactly why this is the case, Twee is the way it should
work.
Note that Drie won't work if you say sphere{Drie(1,2,3),1}
If you say #declare Som2=Twee(1,2,3) without the semi colon, it works
but gives a warning.

Cheers, PoD.


Post a reply to this message

From: Chris Colefax
Subject: Re: semi-colon in macro ?
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

From: ingo
Subject: Re: semi-colon in macro ?
Date: 17 Aug 1999 14:11:48
Message: <37b9a5e4@news.povray.org>
Thorsten Froehlich heeft geschreven in bericht <37b84902@news.povray.org>...
PoD heeft geschreven in bericht <37B89725.17F74328@merlin.net.au>...
Chris Colefax heeft geschreven in bericht <37b8cb37@news.povray.org>...

Thanks, to you all for comments.
I'll rewrite my macros to the way Chris suggested.

ingo
--
Met dank aan de muze met het glazen oog.


Post a reply to this message

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