Page 1 of 1

Maths Operator Overcharge

Posted: Sat Mar 03, 2007 6:43 pm
by Guimauve
The possibilities to define basic maths operator for structured elements. Just like in C++ and many other language.

Like this :

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Suggestion : Operator Overcharge
; Suggested by : Guimauve
; Date : 03-03-2007
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Structure Vector3
   
   i.f
   j.f
   k.f
   
EndStructure

MathsOperator *RHS.Vector3 Equal *LHS.Vector3 ;>
   
   *RHS\i = *LHS\i
   *RHS\j = *LHS\j
   *RHS\k = *LHS\k
   
EndMathsOperator ;<

MathsOperator *RHS.Vector3 Plus *LHS.Vector3 ;>
   
   *RHS\i + *LHS\i
   *RHS\j + *LHS\j
   *RHS\k + *LHS\k
   
EndMathsOperator ;<

MathsOperator *RHS.Vector3 Minus *LHS.Vector3 ;>
   
   *RHS\i - *LHS\i
   *RHS\j - *LHS\j
   *RHS\k - *LHS\k
   
EndMathsOperator ;<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Inside the program code <<<<<

VectorA.Vector3
VectorB.Vector3
VectorC.Vector3
VectorR.Vector3

VectorR = VectorA + VectorB - VectorC

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Regards
Guimauve

Posted: Sun Mar 04, 2007 9:17 am
by Chrono Syndrome
Hmmm... Interesting idea.

Posted: Sun Mar 04, 2007 11:45 am
by Dr. Dri
it would be better with any operator...

Dri ;)

Posted: Sun Mar 04, 2007 1:19 pm
by DarkDragon
I have always thought about it.

Posted: Sun Mar 04, 2007 1:33 pm
by Froggerprogger
...I suppose it will be hard to implement this feature in a non-OOP-designed language, so I don't think it will ever come to PB - though it would be very nice.

Posted: Sun Mar 04, 2007 8:16 pm
by Guimauve
Dr. Dri wrote:it would be better with any operator...

Dri ;)
Yes of course. I have just given a general idea about how it can be done. All operator must be supported (+, -, *, /, < , >, <>)

And this not very hard to implement. It can be done by the editor just like a Macro replacement. Be it's more complicated than a macro because the type of each pointer need to match the operator definition. Also, the testing routine must be capable to detect sometime *RHS and *LHS are the same.

Exemple :

VectorR = VectorA + VectorB - VectorC
*RHS = (*LHS = *RHS) + (*LHS = *RHS) - *RHS

Also, in my code snippet I have introduced new keyword but I'm not sure it's the best way to do the job. I think the use of the Maths operator (+, -, *, /, < , >, <>) are better but ...

Regards
Guimauve

Posted: Mon Mar 05, 2007 5:19 pm
by Dr. Dri
Maybe something like this for binary and unary operators

Code: Select all

Operator.ReturnType +(LHS.LeftType, RHS.RightType)
  ProcedureReturn Result
EndOperator

Operator.Type -(Variable.SameType)
  ProcedureReturn Result
EndOperator
Dri

Posted: Wed Mar 07, 2007 1:54 pm
by Psychophanta
I would approach this suggestion with something like this:

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Suggestion : Operator Macro
; Suggested by : Psychophanta (Albert)
; Date : 2007-03-07
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Structure Vector3

   i.f
   j.f
   k.f

EndStructure

Operator =.Vector3(RHS,LHS)

   RHS#\i = LHS#\i
   RHS#\j = LHS#\j
   RHS#\k = LHS#\k

EndOperator

Operator +.Vector3(RHS,LHS)

   RHS#\i + LHS#\i
   RHS#\j + LHS#\j
   RHS#\k + LHS#\k

EndOperator

Operator -.Vector3(RHS,LHS)

   RHS#\i - LHS#\i
   RHS#\j - LHS#\j
   RHS#\k - LHS#\k

EndOperator

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
; <<<<< Inside the program code <<<<< 

VectorA.Vector3
VectorB.Vector3
VectorC.Vector3
VectorR.Vector3

VectorR [=] VectorA [+] VectorB [-] VectorC

; <<<<<<<<<<<<<<<<<<<<<<< 
; <<<<< END OF FILE <<<<< 
; <<<<<<<<<<<<<<<<<<<<<<<
In this way it is almost like normal macros work, but here the parameters are obtained from the both sides of the operators when operating code.

EDITED after the Dr.Dri point (below)

Posted: Wed Mar 07, 2007 7:35 pm
by Dr. Dri
it's no good if you need other variables...

Dri

Posted: Wed Mar 07, 2007 8:23 pm
by Psychophanta
Dr. Dri wrote:it's no good if you need other variables...

Dri
:shock:
What is the operator which needs more than 2 inputs?

Posted: Thu Mar 08, 2007 7:31 pm
by Dr. Dri

Code: Select all

Operator *(LHS.Matrix44, RHS.Matrix44)
  Res.Matrix44
  
  For i = 0 To 3
    For j = 0 To 3
      For k = 0 To 3
        [...]
EndOperator
but the operator overload would only be possible if structure assignment was possible.

Code: Select all

a.point = b.point
Dri

Posted: Thu Mar 08, 2007 7:58 pm
by Psychophanta
What about this way then?

Code: Select all

Operator *.Matrix44(LHS,RHS)
  For i = 0 To 3
    For j = 0 To 3
      For k = 0 To 3
        [...]
EndOperator
Operator *.Matrix22(LHS,RHS)
  For i = 0 To 1
    For j = 0 To 1
        [...]
EndOperator
So when you use [*] operator, it should be between 2 .Matrix44 type or between 2 .Matrix22 type variables.

(Brackets '[]' are only for legibility to not merge native operators with custom ones)

Posted: Fri Mar 09, 2007 8:35 pm
by Dr. Dri
Vector3 * Matrix33 gives a Vector3
so Operator.ReturnType *(LHS.Type1, RHS.Type2) is needed

Dri

Posted: Fri Mar 09, 2007 9:39 pm
by Psychophanta
Mmhh... right :?