Maths Operator Overcharge

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Maths Operator Overcharge

Post 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
Chrono Syndrome
Enthusiast
Enthusiast
Posts: 169
Joined: Thu Oct 05, 2006 6:44 am
Contact:

Post by Chrono Syndrome »

Hmmm... Interesting idea.
Don't try to catch ze Night !
Remember: 'z' is better zen 'th' =) !
Sorry for bad english.
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post by Dr. Dri »

it would be better with any operator...

Dri ;)
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

I have always thought about it.
bye,
Daniel
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post 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.
%1>>1+1*1/1-1!1|1&1<<$1=1
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Post 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
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post 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
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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)
Last edited by Psychophanta on Thu Mar 08, 2007 8:09 pm, edited 3 times in total.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post by Dr. Dri »

it's no good if you need other variables...

Dri
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post 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
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post by Dr. Dri »

Vector3 * Matrix33 gives a Vector3
so Operator.ReturnType *(LHS.Type1, RHS.Type2) is needed

Dri
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Mmhh... right :?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply