PDA

View Full Version : New Data Types and Operators



Charles Pegge
23-03-2009, 15:19
O2H allow data types to have their own specialised operator procedures (operator overloading.).
The data types do not have to be OOP objects to use this facility.

As an example this is part of an operator set for complex numbers.




'--------------------------
'TYPES OPERATORS OPERATIONS
'==========================

'COMPLEX OPERATOR SET
'--------------------

'Example Supporting + - * / ^ = += -=
'Revised 23 Mar 2009
'Charles Pegge



uses "oxygen","File"

dim d as double
dim src as string

src="


'------------------------------------
'DEFINE STRUCTURE FOR COMPLEX NUMBERS
'====================================

type complex
r as double
i as double
end type
'
'
'------------------------------------------------------
'CREATE GLOBAL STACK FOR COMPLEX ACCUMULATOR / OPERANDS
'======================================================
'
'use var instead of dim (avoids recursive initialisation)
'
var complex complexa(64) ' 64 LEVELS
var long complexj,complexi ' STACK POINTERS
complexi=&complexa ' SET POINTER TO BASE




'-------------------------------------------------
'DEFINE THE OPERATOR SET AS A SERIES OF PROCEDURES
'=================================================

operations of complex


'-----------------------------------------------------
'LOAD PUSH POP DUMP SWAP ARE REQUIRED FOR INTERNAL USE
'-----------------------------------------------------

operation load(v as complex)
complexi += 16
copyn complexi, &v, 16
end operation

operation push(v as complex)
complexj=complexi+16
copyn complexj, complexi, 16
complexi=complexj
end operation

operation pop(v as complex)
complexi-=16
end operation

operation dump(v as complex)
complexj=complexi-16
copyn complexj, complexi, 16
complexi=complexj
end operation

operation swap(v as complex)
dim cxt=complexi+16
complexj=complexi-16
copyn cxt,complexj,16
copyn complexj,complexi,16
copyn complexi,cxt,16
end operation

'-----------------
'VISIBLE OPERATORS
'-----------------

operation = (v as complex)
copyn &v, complexi, 16
complexi -= 16
end operation

operation + (v as complex)
var complex c at complexi
c.r += v.r
c.i += v.i
end operation

operation - (v as complex)
var complex c at *complexi
c.r -= v.r
c.i -= v.i
end operation

operation `+=` (v as complex)
var complex a at complexi
v.r += a.r
v.i += a.i
end operation

operation `-=` (v as complex)
var complex a at complexi
v.r -= a.r
v.i -= a.i
end operation

operation * (v as complex)
dim as complex c at complexi
dim as double d=c.r
c.r = c.r * v.r - c.i * v.i
c.i = c.i * v.r + d * v.i
end operation

operation / (v as complex)
'
' (a*c+b*d) / (c*c+d*d) real
' (b*c-a*d) / (c*c+d*d) imaginary
'
dim as complex t, c at *complexi
dim as double d
d=v.r * v.r + v.i * v.i
t.r=c.r : t.i=c.i
c.r = ( t.r * v.r + t.i * v.i ) / d
c.i = ( t.i * v.r - t.r * v.i ) / d
end operation


end operations




;//////////////////////////////////////


dim z1,z2,z3 as complex

z1 => 0, 0
z2 => 2, 1
z3 => 2, 3


z1 = z2 * z3

z1 += z1

print `Complex Number Result:

`+
str(z1.r)+` Real
`+
str(z1.i)+` Imaginary
`


;//////////////////////////////////////

terminate"

file_save ("t.txt",o2_error()+o2_prep ("O2H "+src))
o2_basic src
if len(o2_error) then
msgbox 0," : "+o2_error()+o2_prep ("O2H "+src)
stop
else
o2_exec
end if