It is currently Sun Sep 27, 2020 1:32 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Nested Macros
PostPosted: Tue Jan 29, 2013 4:29 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Jun 22, 2003 7:43 pm
Posts: 586
Location: Germany, Saarbrücken
Nested Makros does not work as expected.
Code:
Procedure.s sowhat_(a, b)
   ProcedureReturn "sowhat: " + Str(a - b)
EndProcedure

Macro Dq
   "
EndMacro

Macro Concat(prefix, suffix, a, b, moin=ro)
   Mac#moin prefix#suffix(para1, para2)
      Debug "Test: " + Dq#prefix#suffix#Dq + prefix#suffix#_(para1, para2)
   ;EndMac#moin      ;This line doesn't work
EndMacro

Concat(so, what, 1, 2)
EndMacro   ;Instead I need it here

sowhat(1, 2)

_________________
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB 5.10b5] Nested Makros
PostPosted: Tue Jan 29, 2013 11:38 am 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 14090
Location: France
It's not supported.


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB 5.10b5] Nested Makros
PostPosted: Tue Jan 29, 2013 11:53 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Jun 22, 2003 7:43 pm
Posts: 586
Location: Germany, Saarbrücken
Is it already requested as a feature?
Practically it's working well except for this "EndMacro at an other position" thing.

_________________
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB 5.10b5] Nested Makros
PostPosted: Tue Jan 29, 2013 11:55 am 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 14090
Location: France
I don't think it is, so I moved the topic.


Top
 Profile  
Reply with quote  
 Post subject: Re: Nested Macros
PostPosted: Tue Jan 29, 2013 12:22 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Jun 22, 2003 7:43 pm
Posts: 586
Location: Germany, Saarbrücken
With nested macros you are able to do overloading of procedures.

The following code works and overloads four Procedures to multiply, add, subtract and divide 3D-vectors with 3D-vectors, 3D-vectors with skalars or skalars with 3D-vectors.
Code:
EnableExplicit

Structure Vector
   x.f
   y.f
   z.f
EndStructure

Macro MacroRo
   ro
EndMacro

Macro VectorOp(op, opName)
   Procedure Vector#opName#Vector(*a.Vector, *b.Vector)
      *a\x op *b\x
      *a\y op *b\y
      *a\z op *b\z
   EndProcedure
   
   Procedure Vector#opName#Skalar(*a.Vector, b.f)
      *a\x op b
      *a\y op b
      *a\z op b
   EndProcedure
   Mac#MacroRo Vector#opName(a, b)
      CompilerIf Defined(a, #PB_Variable)   ;a is a variable?
         CompilerIf Defined(b, #PB_Variable) ;b is a variable?
            CompilerIf TypeOf(b) = #PB_Structure ;b has a structure (unfortunality we can not check if the structure is really Vector)
               CompilerIf TypeOf(a) = #PB_Structure ;a has a structure
                  Vector#opName#Vector(a, b)   ;a and b have a structur
               CompilerElse
                  Vector#opName#Skalar(b, a)   ;b has a structure and a is a normal variable, maybe float or integer
               CompilerEndIf
            CompilerElse
               CompilerIf TypeOf(a) = #PB_Structure
                  Vector#opName#Skalar(a, b)   ;a has a structure and b is a normal variable, maybe float or integer
               CompilerElse
                  CompilerError "VectorMul(Float, Float): At least on parameter has to be of type Vector."
               CompilerEndIf
            CompilerEndIf
         CompilerElse
            CompilerIf TypeOf(a) = #PB_Structure
               Vector#opName#Skalar(a, b)   ;a has a structure und b is a constant value (until now only integers are possible)
            CompilerElse
               CompilerError "VectorMul(Float, Constant): At least on parameter has to be of type Vector."
            CompilerEndIf
         CompilerEndIf
      CompilerElse
         CompilerIf Defined(b, #PB_Variable)
            CompilerIf TypeOf(b) = #PB_Structure
               Vector#opName#Skalar(b, a)      ;b has a structure und a is a constant value
            CompilerElse
               CompilerError "VectorMul(Constant, Float): At least on parameter has to be of type Vector."
            CompilerEndIf
         CompilerElse ;a und b sind keine Variablen -> Fehler
            CompilerError "VectorMul(Constant, Constant): At least on parameter has to be of type Vector."
         CompilerEndIf
      CompilerEndIf
;   EndMac#MacroRo
EndMacro

;For some reason I had to put the EndMacro after VectorOp instead of uncomment it three lines above
VectorOp(*, Mul)
EndMacro
VectorOp(+, Add)
EndMacro
VectorOp(/, Div)
EndMacro
VectorOp(-, Sub)
EndMacro

; After these macro executions the following procedures exist
; - VectorMulVector(*a.Vector, *b.Vector)
; - VectorMulSkalar(*a.Vector, b.f)
; - VectorAddVector(*a.Vector, *b.Vector)
; - VectorAddSkalar(*a.Vector, b.f)
; - VectorDivVector(*a.Vector, *b.Vector)
; - VectorDivSkalar(*a.Vector, b.f)
; - VectorSubVector(*a.Vector, *b.Vector)
; - VectorSubSkalar(*a.Vector, b.f)
;
; The following four macros help you ignoring the type and order of the parameters (overloading)
; - VectorMul(a, b)
; - VectorAdd(a, b)
; - VectorDiv(a, b)
; - VectorSub(a, b)

Define a.Vector, b.Vector, c.f
a\x = 1 : a\y = 2 : a\z = 3
b\x = 1 : b\y = 2 : b\z = 3
c = 4

Debug "a = (" + a\x + ", " + a\y + ", " + a\z + ")"
Debug "b = (" + b\x + ", " + b\y + ", " + b\z + ")"
Debug "c = " + c

VectorAdd(a, b)   ;Vector + Vector
Debug "a + b"
Debug "a = (" + a\x + ", " + a\y + ", " + a\z + ")"

VectorMul(c, a) ;Skalar * Vector
Debug "c * a"
Debug "a = (" + a\x + ", " + a\y + ", " + a\z + ")"

VectorDiv(a, c) ;Vector / Skalar
Debug "a / c"
Debug "a = (" + a\x + ", " + a\y + ", " + a\z + ")"

VectorSub(a, b) ;Vector - Vector
Debug "a - b"
Debug "a = (" + a\x + ", " + a\y + ", " + a\z + ")"

VectorMul(a, 4) ;Vector * 4    ;Until now, it is not possible to using floating point numbers, but integers work as expected
Debug "a * 4"
Debug "a = (" + a\x + ", " + a\y + ", " + a\z + ")"

;To test the error checking uncomment one or more of these lines
;VectorAdd(3, 4)
;VectorAdd(c, c)
;VectorAdd(c, 2)

With native support for nested macros and a better TypeOf() command which also can distinguish between different structures, "constant strings" and constant floating point numbers (123.456), this example could be only the beginning of more advanced macroing.

_________________
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.


Top
 Profile  
Reply with quote  
 Post subject: Re: Nested Macros
PostPosted: Tue Jan 29, 2013 4:31 pm 
Offline
Addict
Addict

Joined: Sat Dec 31, 2005 5:24 pm
Posts: 2970
Location: Where ya would never look.....
I agree with Nic, native nested macro support would be a very nice addition

_________________
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye