Author Topic: Operators for Objects  (Read 2779 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Operators for Objects
« on: March 26, 2011, 04:08:27 AM »

Using 3d Vectors as an example; the vectors can be added, subtracted, multiplied and divided like simple numbers.

An array of vectors defined like this is contiguous and can be passed by reference to other systems like Opengl which are not Object-aware.

Code: [Select]

  '=====================
  'OPERATORS FOR OBJECTS
  'VECTOR DEMO
  '=====================


  type vec3d double x,y,z


  '===========
  class vector
  '===========
  '
  private
  '
  static vec3d  v[16]    'ACCUMULATOR STACK
  static sys    idx,pp   'INDEXES AND POINTERS
  static vec3d  *d       '
  static vec3d  *s       '
  '
  public
  '
  vec3d
  '
  '
  method constructor()
  '===================
  end method
  '
  method destructor()
  '==================
  end method
  '
  method mapio()
  '==============
  &d=&v+idx*sizeof vec3d 'accum (location in stack)
  if pp then
    &s=&d+sizeof(vec3d) 'read from stack (1 up from accum)
    pp=0
  else
    &s=&x 'read object x,y,z values
  end if
  end method
  '
  method "push"()
  '==============
  ++idx
  end method
  '
  method "pop"()
  '==============
  --idx
  pp=1
  end method
  '
  method "load"()
  '==============
  mapio()
  copy &d,&s,sizeof vec3d
  end method
  '
  method "="()
  '==============
  mapio()
  copy &s,&d,sizeof vec3d 'copy to object x,y,z
  end method
  '
  method "+"()
  '===========
  mapio()
  d.x+=s.x : d.y+=s.y : d.z+=s.z
  end method
  '
  method "-"()
  '===========
  mapio()
  d.x-=s.x : d.y-=s.y : d.z-=s.z
  end method
  '
  method "*"()
  '===========
  mapio()
  d.x*=s.x : d.y*=s.y : d.z*=s.z
  end method
  '
  method "/"()
  '===========
  mapio()
  d.x/=s.x : d.y/=s.y : d.z/=s.z
  end method
  '
  end class




  '====
  'MAIN
  '====


  vector v1[10]
  vector v2
  vector v3

  v1[1]<=10,20,30
  v2<=2,2,2
  v3<=3,3,3

  'VARIOUS EXPRESSIONS USED FOR TESTING

  'v1=v2
  'v1=v3
  'v1=v2+v3
  'v1=(v2+v3)
  'v1[1]=(v2+v3)
  'v1=v1*(v2+v3)
  'v1[2]=v1*(v2+v3)+v1
  'v1+=v1*(v2+v3)+v1
  '
  'v1[2]+=v2[1]+v3[1]
  'v1[1]=v2[1]+(v3[1]-v1)
  v1[1]=v2[1]+v3[1]*v1[1]

  string cm=" , "
  print "V1[n]=" v1[1].x cm v1[1].y cm v1[1].z

  'print structureof vector




Charles

Peter

  • Guest
Re: Operators for Objects
« Reply #1 on: March 26, 2011, 11:59:22 AM »
Thank you Charles,
 
For the a30 version.
Just I am writing some tests therefor.

What is now  &h ?  An address or a hex number ?
Must I all rewrite In my library ?

What about this ? Is cool ?
Code: [Select]
indexbase 0

sys lpvar,nuvar
lpvar =10      

lea  eax,lpvar    
push eax
mov  eax,[eax]    
add  eax,10
mov  edx,eax
pop  eax
mov  [eax],edx
mov  eax,[eax]
mov  nuvar, eax    

print Hex(nuvar-10)

Charles Pegge

  • Guest
Re: Operators for Objects
« Reply #2 on: March 26, 2011, 02:05:59 PM »

Yes that is easy to follow Peter.

I have fixed the problem with &h &o &b

Charles

Code: [Select]

sys h,o,b

print "Addresses:  " &h "  " &o "  " &b


Charles