POV-Ray : Newsgroups : povray.newusers : if inside a macro syntax (if...else) Server Time
4 Nov 2024 17:32:39 EST (-0500)
  if inside a macro syntax (if...else) (Message 1 to 8 of 8)  
From: Rafal 'Raf256' Maj
Subject: if inside a macro syntax (if...else)
Date: 29 Dec 2004 23:51:16
Message: <Xns95CF3BB3DD30Araf256com@203.29.75.35>
Hi,
syntax like

#local size = big ? 100 : 50;

(from C "operator?" ) isnt working, and writting it normaly as:

#if (big) #local size=100; #else #local size=50; #end

is quite long (I like to keep my syntax short). So I wanted to write a tiny 
IfElse function, like:

#macro IfElse(A,B,C) #if (A) 55 #else 30 #end #end

#local DD = IfElse(variant_Wide, 50, 100);

but it gaves a pares error.

I replaced it with good sytnax:
  #macro IfElse(A,B,C) select(A-1,C,B,C) #end
that do work, but just for curiosity, how to make previous syntax work 
also?

-- 
http://www.raf256.com/3d/
Rafal Maj 'Raf256', home page - http://www.raf256.com/me/
Computer Graphics


Post a reply to this message

From: Neil Kolban
Subject: Re: if inside a macro syntax (if...else)
Date: 30 Dec 2004 00:44:43
Message: <41d395cb$1@news.povray.org>
Rafal,
Hi my friend.

The expression "?" component works fine but I think you have two errors:

1. You can't have a variable called "size".  That is a keyword.
2. The syntax for the conditional expression is:

( C ? A : B )

and not

C ? A : B

You had missed the brackets

In my tests,

#local big=false;
#local msize = (big ? 100 : 50);
#debug concat("Output: ",str(msize,1,1),"\n")

and

#local big=true;
#local msize = (big ? 100 : 50);
#debug concat("Output: ",str(msize,1,1),"\n")

work as expected.

See 3.2.1.3.3 Operators in the POV-Ray docs.

Neil


"Rafal 'Raf256' Maj" <spa### [at] raf256com> wrote in message
news:Xns95CF3BB3DD30Araf256com@203.29.75.35...
>
> Hi,
> syntax like
>
> #local size = big ? 100 : 50;
>
> (from C "operator?" ) isnt working, and writting it normaly as:
>
> #if (big) #local size=100; #else #local size=50; #end
>
> is quite long (I like to keep my syntax short). So I wanted to write a
tiny
> IfElse function, like:
>
> #macro IfElse(A,B,C) #if (A) 55 #else 30 #end #end
>
> #local DD = IfElse(variant_Wide, 50, 100);
>
> but it gaves a pares error.
>
> I replaced it with good sytnax:
>   #macro IfElse(A,B,C) select(A-1,C,B,C) #end
> that do work, but just for curiosity, how to make previous syntax work
> also?
>
> -- 
> http://www.raf256.com/3d/
> Rafal Maj 'Raf256', home page - http://www.raf256.com/me/
> Computer Graphics


Post a reply to this message

From: Mike Williams
Subject: Re: if inside a macro syntax (if...else)
Date: 30 Dec 2004 01:45:56
Message: <R9s3QCAcQ60BFw+k@econym.demon.co.uk>
Wasn't it Rafal 'Raf256' Maj who wrote:
>
>Hi,
>syntax like
>
>#local size = big ? 100 : 50;
>
>(from C "operator?" ) isnt working, and writting it normaly as:
>
>#if (big) #local size=100; #else #local size=50; #end
>
>is quite long (I like to keep my syntax short). So I wanted to write a tiny 
>IfElse function, like:
>
>#macro IfElse(A,B,C) #if (A) 55 #else 30 #end #end
>
>#local DD = IfElse(variant_Wide, 50, 100);
>
>but it gaves a pares error.
>
>I replaced it with good sytnax:
>  #macro IfElse(A,B,C) select(A-1,C,B,C) #end
>that do work, but just for curiosity, how to make previous syntax work 
>also?


See Neil Kolban's reply for the correct syntax for using the ? operator.

Here's an explanation of why your first attempt failed and your second
one succeeded.

You need to stop thinking of macros as if they were functions.

In this particular case you could have used a function, like this:
#declare IFELSE=function(A,B,C){select((A=0),B,C)}
#local DD = IFELSE(variant_Wide, 50, 100); 

When you use a function, the calculations are performed inside the
function at run time and a value is returned. So, at parse time, the
"IFELSE(...)" looks syntactically like a variable and the syntax is
correct. #local DD = <variable>;

When you use a macro, POV replaces the call at parse time with a copy of
the *code* contained within the macro. The macro itself is not executed
independently. In your fist case, unrolling the macro produces

        #local DD = #if (variant_Wide) 55 #else 30 #end;

Which you can see is syntactically incorrect.

In the second case, unrolling the macro produces

        #local DD = select(A-1,C,B,C);

Which is syntactically correct.

-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

From: Warp
Subject: Re: if inside a macro syntax (if...else)
Date: 30 Dec 2004 09:07:56
Message: <41d40bbc@news.povray.org>
Mike Williams <nos### [at] econymdemoncouk> wrote:
> When you use a macro, POV replaces the call at parse time with a copy of
> the *code* contained within the macro. The macro itself is not executed
> independently.

  The actual parsing process is a bit more complicated than that, but one
can perfectly think that there's a replacement being done (except for
#local commands, which are handled in a special way).

  It's difficult to come up with a short expression to describe what
POV-Ray is actually doing when it calls a macro. The closest term would
perhaps be "subroutine call", although it's not quite that either.

  When the parser encounters a macro call at some point in the file
being parsed, the parser will "write down" the location of the call
and then literally jump to the place where the macro was defined.
It really does this literally by opening the file where the macro
was located as necessary if it was a different file than the current
one and seeking to the location where the macro (with the C file
seeking functions). Then the parser continues parsing from this
point forward in the usual way. When it encounters the #end of
the macro, it jumps back to the calling location (again by seeking
the file).

  There's no literal code replacement being done here (as is the
case with eg. the C preprocessor #define macros) but a kind of
subroutine call. However, the net effect of this process is that
the macro works as if the call was being replaced by the body
of the macro. The body of the macro is parsed as if it was located
at the calling location (although the file is literally being parsed
in a completely different place).

  #local is the one single thing which makes POV-Ray #macros different
from true replacement macros (such the C preprocessor macros). They
are not parsed as if they were located at the calling location but
are instead parsed as if they were truely inside a subroutine. The
mixed technique POV-Ray uses to handle macros makes this possible,
and is in fact very handy. POV-Ray macros kind of combine the advantages
of true replacement macros and the advantages of true subroutines.
(Of course the mixing of two metaphors causes sometimes confusion
to people.)

-- 
#macro M(A,N,D,L)plane{-z,-9pigment{mandel L*9translate N color_map{[0rgb x]
[1rgb 9]}scale<D,D*3D>*1e3}rotate y*A*8}#end M(-3<1.206434.28623>70,7)M(
-1<.7438.1795>1,20)M(1<.77595.13699>30,20)M(3<.75923.07145>80,99)// - Warp -


Post a reply to this message

From: kurtz le pirate
Subject: Re: if inside a macro syntax (if...else)
Date: 30 Dec 2004 09:56:39
Message: <kurtzlepirate-2D5B3C.15563830122004@news.povray.org>
In article <Xns95CF3BB3DD30Araf256com@203.29.75.35>,
 "Rafal 'Raf256' Maj" <spa### [at] raf256com> wrote:

>>Hi,
>>syntax like
>>
>>#local size = big ? 100 : 50;
>>
>>(from C "operator?" ) isnt working, and writting it normaly as:
>>
>>#if (big) #local size=100; #else #local size=50; #end
>>
>>is quite long (I like to keep my syntax short). So I wanted to write a tiny 
>>IfElse function, like:
>>
>>#macro IfElse(A,B,C) #if (A) 55 #else 30 #end #end
>>
>>#local DD = IfElse(variant_Wide, 50, 100);
>>
>>but it gaves a pares error.
>>
>>I replaced it with good sytnax:
>>  #macro IfElse(A,B,C) select(A-1,C,B,C) #end
>>that do work, but just for curiosity, how to make previous syntax work 
>>also?

hi,

 - "size" is a reserved keyword from pov.
 - "c" like comparaison works as this : #local mySize=(bib?100:50);
 - macro in pov is not the same as macro in c. there is no "code 
substitution".
 - you ca write : #macro IfElse(A,B,C) (A?B:C) #end ... but it's more 
long that the direct pov sdl (A?B:C) syntax form.


Post a reply to this message

From: Warp
Subject: Re: if inside a macro syntax (if...else)
Date: 30 Dec 2004 12:11:12
Message: <41d436b0@news.povray.org>
kurtz le pirate <kur### [at] yahoofr> wrote:
>  - macro in pov is not the same as macro in c. there is no "code 
> substitution".

  There is and there isn't. It's a bit complicated. See my other post
on the subject.

-- 
#macro N(D)#if(D>99)cylinder{M()#local D=div(D,104);M().5,2pigment{rgb M()}}
N(D)#end#end#macro M()<mod(D,13)-6mod(div(D,13)8)-3,10>#end blob{
N(11117333955)N(4254934330)N(3900569407)N(7382340)N(3358)N(970)}//  - Warp -


Post a reply to this message

From: kurtz le pirate
Subject: Re: if inside a macro syntax (if...else)
Date: 30 Dec 2004 17:36:19
Message: <kurtzlepirate-B13F12.23361930122004@news.povray.org>
In article <41d436b0@news.povray.org>, Warp <war### [at] tagpovrayorg> 
wrote:

>>kurtz le pirate <kur### [at] yahoofr> wrote:
>>>  - macro in pov is not the same as macro in c. there is no "code 
>>> substitution".
>>
>>  There is and there isn't. It's a bit complicated. See my other post
>>on the subject.

  yes you're right but in c, macro is for the pre-processor and c has 
real functions. in sdl pov, there is no "real" function.

  for exemple max() in c is a macro defined by
  #define max(a,b) (a>=b?a:b)

  when in c source there is a line like that :
  foo=max(big,small)

  before compiling, the pre-processor transforme the line to :
  foo=(big>=small?big:small)

  not the same mechanic than sdl pov as you say in your previous post.

  klp


Post a reply to this message

From: Warp
Subject: Re: if inside a macro syntax (if...else)
Date: 31 Dec 2004 05:45:30
Message: <41d52dca@news.povray.org>
kurtz le pirate <kur### [at] yahoofr> wrote:
> in sdl pov, there is no "real" function.

  That's not completely true either...

-- 
#macro M(A,N,D,L)plane{-z,-9pigment{mandel L*9translate N color_map{[0rgb x]
[1rgb 9]}scale<D,D*3D>*1e3}rotate y*A*8}#end M(-3<1.206434.28623>70,7)M(
-1<.7438.1795>1,20)M(1<.77595.13699>30,20)M(3<.75923.07145>80,99)// - Warp -


Post a reply to this message

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