POV-Ray : Newsgroups : povray.advanced-users : macro argument / output / variable question Server Time
22 Dec 2024 12:31:47 EST (-0500)
  macro argument / output / variable question (Message 1 to 4 of 4)  
From: Bald Eagle
Subject: macro argument / output / variable question
Date: 14 Jul 2017 14:40:00
Message: <web.59690fad8e7f472cc437ac910@news.povray.org>
So, I'm trying to work through some of the PASCAL90 source code for
tetrahedrons, and I started with the first one:

I'm assuming "R8" is a real 8-bit number, or something like that.

!! R8_SWAP swaps two R8's.
!
!  Licensing:
!
!    This code is distributed under the GNU LGPL license.
!
!  Modified:
!
!    22 December 2000
!
!  Author:
!
!    John Burkardt
!
!  Parameters:
!
!    Input/output, real ( kind = 8 ) X, Y.  On output, the values of X and
!    Y have been interchanged.
!
  implicit none

  real ( kind = 8 ) x
  real ( kind = 8 ) y
  real ( kind = 8 ) z

  z = x
  x = y
  y = z

  return
end

Which in SDL translates (roughly) to:

#macro SwapXY (X, Y)
 // Input/output: X, Y
 // On output, the values of X and Y have been interchanged.
 #local Z = X; // Z is a placeholder variable
 #local X = Y;
 #local Y = Z;
#end

My question is, does a mechanism exist by which the true identities of the
variable names passed to the macro can be accessed?
Such that if I do:
#macro SwapXY (OneVariable, Another)

it will actually redefine THOSE variables?
(because until the macro is instantiated, X and Y are not yet defined (in the
macro microcosm) and are throw-away temporary variable names.  Using #declare
would only create new variables or redefine any existing global-level X and Y.)

I'm guessing not, and that I'd best do a tuple-style assignment and have the
macro "return" those two values on the last line...


Post a reply to this message

From: clipka
Subject: Re: macro argument / output / variable question
Date: 16 Jul 2017 16:05:55
Message: <596bc723$1@news.povray.org>
Am 14.07.2017 um 20:38 schrieb Bald Eagle:

> #macro SwapXY (X, Y)
>  // Input/output: X, Y
>  // On output, the values of X and Y have been interchanged.
>  #local Z = X; // Z is a placeholder variable
>  #local X = Y;
>  #local Y = Z;
> #end
> 
> My question is, does a mechanism exist by which the true identities of the
> variable names passed to the macro can be accessed?
> Such that if I do:
> #macro SwapXY (OneVariable, Another)
> 
> it will actually redefine THOSE variables?

Whenever you pass a variable name as a macro parameter, that macro
parameter will actually behave as a reference to that variable. So the
macro above would indeed work.

The variable name must be defined beforehand though, and the macro can't
undefine it; so the following will fail:

    #declare A = 1;
    // leave B undefined.
    Swap(A,B)


Post a reply to this message

From: Bald Eagle
Subject: Re: macro argument / output / variable question
Date: 17 Jul 2017 07:55:00
Message: <web.596ca51ae1619e6cc437ac910@news.povray.org>
clipka <ano### [at] anonymousorg> wrote:

I always thought that the value of OneVariable would get copied to X, and
Another would get copied to Y, and the macro would operate on X and Y, not
OneVariable and Another.  So I would have always tried to output any values from
the macro via
#declare A = macro ThisMacro (A)

But apparently X and Y are just pointers / links / containers for the REAL
variables, and when I perform operations on X and Y, the underlying actual
variables that get passed into the macro get changed.


.... and indeed after running the below, it works just like that.
Good to know!  That will help with a lot of debugging.

[X] Learned something new today

#macro SwapXY (X, Y)
 // Input/output: X, Y
 // On output, the values of X and Y have been interchanged.
 #local Z = X; // Z is a placeholder variable
 #local X = Y;
 #local Y = Z;
#end

#declare A = 1;
#declare B = 2;

#debug concat ( "A = ", str (A, 3, 1), "B = ", str (B, 3, 1), "\n")

SwapXY (A, B)

#debug concat ( "A = ", str (A, 3, 1), "B = ", str (B, 3, 1), "\n")


Post a reply to this message

From: Bald Eagle
Subject: Re: macro argument / output / variable question
Date: 17 Jul 2017 09:40:00
Message: <web.596cbdf0e1619e6cc437ac910@news.povray.org>
"It ain't what you don't know that gets you into trouble. It's what you know for
sure that just ain't so."


Post a reply to this message

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