Author Topic: Classes, Unions and Arrays Help Please  (Read 20505 times)

0 Members and 2 Guests are viewing this topic.

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #15 on: June 11, 2012, 04:53:39 PM »
Quote
You can also create custom operators by defining them before the class that uses them like this:

operator "aga" 9, "ogo" 8 ,...
where the number indicates the precedence level (ascending priority)
a aga b ogo (c+d)
  I thought this would eliminate the required methods for load, push.. etc.
But couldn't get it to work.

Code: OxygenBasic
  1. string crlf = chr( 13 ) + chr( 10 )
  2.  
  3. operator "?" 1, "??" 2
  4.  
  5. class tiny
  6.     'member variables
  7.    private
  8.         string mPhrase
  9.     'methods
  10.    public
  11.         method Set( string aString )
  12.             mPhrase = aString
  13.         end method
  14.        
  15.         method Prn()
  16.             print mPhrase
  17.         end method
  18.        
  19.         method "?"( string aString)
  20.             print aString
  21.         end method
  22.        
  23.         method "??"()
  24.             print "to " crlf "believe."
  25.         end method
  26.        
  27.         ' method ?( string aString)
  28.            ' print aString
  29.        ' end method
  30.        
  31.         ' method ??()
  32.            ' print "to " crlf "believe."
  33.        ' end method
  34. end class
  35.  
  36. tiny t
  37. t.Set "The X Files"
  38. t.Prn
  39. t.? "I want "
  40. t.??
  41. ' t."?" "I want "
  42. ' t."??"
  43. ' t ? "I want "
  44. ' t ??

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #16 on: June 11, 2012, 05:06:47 PM »
I tried this too and no luck.
Code: OxygenBasic
  1. string crlf = chr( 13 ) + chr( 10 )
  2.  
  3. 'operator "?" 1, "??" 2
  4.  
  5. class tiny
  6.     'member variables
  7.    private
  8.         string mPhrase
  9.        
  10.     'Code for Operator Overloading
  11.    static sys i,pp  
  12.     static string accum[32]  
  13.      
  14.     macro GetOperand()  
  15.       string *a,*b  
  16.       @a=@accum+i          'establish current stack position  
  17.      if pp then  
  18.         @b=@a+sizeof accum 'operand is on the stack (next position up)  
  19.        pp=0  
  20.       else  
  21.         @b=@this           'operand is this object (not on the stack)  
  22.      end if  
  23.     end macro
  24.    
  25.     method "load"()  
  26.         GetOperand    
  27.     end method  
  28.      
  29.     method "push"()  
  30.         i+=sizeof accum  
  31.     end method  
  32.      
  33.     method "pop"()  
  34.         pp=1 'take operand from the stack (next operation)  
  35.        i-=sizeof accum  
  36.     end method
  37.  
  38.     'methods
  39.    public
  40.         method Set( string aString )
  41.             mPhrase = aString
  42.         end method
  43.        
  44.         method Prn()
  45.             print mPhrase
  46.         end method
  47.        
  48.         method "?"( string aString)
  49.             GetOperand
  50.             print aString
  51.         end method
  52.        
  53.         method "??"()
  54.             GetOperand
  55.             print "to " crlf "believe."
  56.         end method
  57. end class
  58.  
  59. tiny t
  60. t.Set "The X Files"
  61. t.Prn
  62. ' t.? "I want "
  63. ' t.??
  64. ' t."?" "I want "
  65. ' t."??"
  66. t ? "I want "
  67. t ??

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #17 on: June 11, 2012, 05:14:36 PM »
Operator names are restricted. "?" is already in use and "??" will not parse as one symbol.

Objects belonging to an operator class do not use dot syntax when behaving as operands in an expression. Thus vectors v1=v2+v3*v4 is legit.

But accessing other (non-operator) methods and members is done in the usual way
v1.x=2 : v1.y=2.5 ..

I think an AI Logic class would be a useful way to illustrate the use of custom operators "common" "merge" etc

I will dream about it. 2.00 AM: Siesta time here :)

Charles

ps

operator tadd 5
...
method "tadd"()
  GetOperand
  a.x+=b.x
  a.y+=b.y
  a.z+=b.z
end method

...

vc=vb[1] tadd va[1]
print str(vc.x,3) "," str(vc.y,3) "," str(vc.z,3)

« Last Edit: June 11, 2012, 05:31:16 PM by Charles Pegge »

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #18 on: June 11, 2012, 07:48:58 PM »
Sleep well. I just got up from a nap to give you more nightmares.  Check this one out and think about >=, <=, != 
I will think about this when I sleep for good tonight too.
« Last Edit: June 11, 2012, 09:58:18 PM by kryton9 »

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #19 on: June 12, 2012, 03:07:47 AM »
Good Morning Kent!

I'm upgrading operators to include comparators. It will take a few more hours to debug and test.

Charles

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #20 on: June 12, 2012, 06:05:31 AM »

Here it is. New Oxygen with your test and a sample Vectors class.

Code: OxygenBasic
  1. method "boolean"() as bool
  2.   GetOperand
  3.   if a.x + a.y + a.z<>0 then return -1
  4. end method
  5.  
  6. method "=="() as bool
  7.   GetOperand
  8.   if a.x=b.x and a.y=b.y and a.z=b.z then return -1
  9. end method
  10.  
  11. method "<>"() as bool
  12.   GetOperand
  13.   if not a.x=b.x and a.y=b.y and a.z=b.z then return -1
  14. end method
  15.  
  16. macro hyp(a)
  17.     sqrt(a.x*a.x+a.y*a.y+a.z*a.z)
  18. end macro
  19.  
  20. method "<"() as bool
  21.   GetOperand
  22.   if hyp(a) < hyp(b) then return -1
  23. end method
  24.  
  25. method ">"() as bool
  26.   GetOperand
  27.   if hyp(a) > hyp(b) then return -1
  28. end method
  29.  
  30. method "<="() as bool
  31.   GetOperand
  32.   if hyp(a) <= hyp(b) then return -1
  33. end method
  34.  
  35. method ">="() as bool
  36.   GetOperand
  37.   if hyp(a) >= hyp(b) then return -1
  38. end method
  39.  
  40.  

Charles

[attachment deleted by admin]

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #21 on: June 12, 2012, 09:23:16 AM »
Thanks Charles, I hope it wasn't too much of a headache!  I will test it in coming days. Thanks, this is super!

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #22 on: June 12, 2012, 10:40:14 AM »
No headaches, Kent. It's good to have a full set of operators available, even though some of them are rarely used for higher types.

One limitation of this design: due to the use of a static accumulator, it is not safe to share the class between multiple threads. They will all fight over the accumulator and scramble it! So each thread must have its own independent class. This could be done with a macro for easy deployment.

Charles

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #23 on: June 12, 2012, 04:21:23 PM »
I had to sit hear and read and reread your post and think about each idea out to understand it. But I get it, finally. I do have a question though, why is the accumulator sized to 32? I don't understand that.

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #24 on: June 12, 2012, 05:50:31 PM »
Hi Kent

Nesting an expression to 31 levels (very generous :))

This expression would require an accumulator stack with three levels (2 nested levels ):

a=((b+c)*(d+e) and 127)+4

The compiler's precedence evaluator automatically inserts brackets, so nestings are not always explicit.

Charles


kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #25 on: June 12, 2012, 07:19:44 PM »
I misunderstood it to be 32 items in an expression and not nesting levels :)

Aurel

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #26 on: June 12, 2012, 09:08:56 PM »
People..
Discussion is interesting ,but where is practical advantage of all this operators...etc.
And for what is all this stuff good.?

kryton9

  • Guest
A thank you to Charles
« Reply #27 on: June 12, 2012, 09:36:00 PM »
The only way I can thank you for putting up with my questions the last few days is a new class and demo. This is a result of learning this stuff. We now have a new class for booleans.
All that is necessary is included. The exe has the runtime so no need for the oxygen.dll.

Hope this answers your questions too Aurel. Pretty neat what we can do with Oxygen. Java doesn't have operator overloading, but pretty much all the other popular languages do.

Just extract to your projects folder.

kryton9

  • Guest
Thought of this while sleeping and couldn't get it to work
« Reply #28 on: June 13, 2012, 07:53:07 AM »
I thought about the cBool and thought of this addition today and can't resolve it.
Looking at the code you will see the areas I have commented out. These were my attempts at figuring it out.

The problem, how to have it equal to a non class type, that is something that is not cBool as in the working demo before.
What about a sys value of true or false.

It should be clearer to see when looking at the code.

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #29 on: June 13, 2012, 11:08:24 AM »

Hi Kent,
Thanks for the cBool class.

I suggest making the mbool member public. Then you can load it like this

bVal <= true

Code: OxygenBasic
  1. 'test cBool 2.o2bas
  2. 'by Kent Sarikaya June 2012
  3.  
  4. ' $ filename "./../_bin/test cBool 2.exe"
  5. ' #include "../../../inc/RTL32.inc"
  6.  
  7. ' includepath + "./../_inc/"
  8. include once "cConsole.inc"
  9. include once "cBool.inc"
  10.  
  11. new cConsole c "test cBool.o2bas 2"
  12. new cBool bVal
  13. bVal <= true '<<<<<<<<
  14. c.Print "bVal is " bVal.Get + crlf
  15. bval <= false
  16. c.Print "bVal is " bVal.Get + crlf
  17. c.WaitKey  
  18. del bVal
  19. del c
  20.  

The technique is also good for filling complex numbers and vectors:

vector v
v<=x,y,z



Charles