View Full Version : Classes Simplified
Charles Pegge
01-05-2009, 08:13
You can now place methods inside the class block instead of in a separate methods of block. Oxygen will detect that you have done this and perform the necessary `paperwork`.
This example also shows how to create implicit GET and SET methods using function overloading.
Also methods can now be treated as pseudo-variables:
instead of o.va(42)
o.va=42
Oxygen update:
http://community.thinbasic.com/index.php?topic=2517.0
'
'A SIMPLER WAY OF CREATING CLASSES
'The methods are defined inside the class block.
'
'
'also demonstrating implicit GET and SET methods
'by using function overloading.
uses "oxygen","file"
dim s as string
s="
'-------
class io
'=======
static a=5 as long
private iva as long
'these are overloaded function (same name different params)
method va() as long 'GET
method=this.iva
end method
method va(v as long) 'SET
this.iva=v
end method
end class
dim o as io
o.va=42 'THIS IS A PSEUDO-VARIABLE (method pretending to be a variable)
print `Answer: ` str o.va
"
o2_basic s
if len(o2_error) then msgbox 0,o2_error : stop
'msgbox 0,o2_prep "o2h "+s
'file_save ("t.txt",o2_prep "o2h "+s)
o2_exec
Charles, thanks, so we can still use this or the previous form I assume for methods and classes?
This of course is more inline with other languages, but if one were to develop a class as a separate file, where nothing but code relating to that class would exist, the syntax you came up with before was really nice for that.
So I really like having both options.
Using this format for small classes in small programs.
The old format for big projects where classes are separate files.
Thanks again!!
Petr Schreiber
01-05-2009, 10:18
Thanks Charles,
new form of syntax is looking very good.
Petr
Charles Pegge
01-05-2009, 10:34
Yes kent, both forms are available. (and the original pre-Basic format too)
For multiple inheritance in this format I had to introduce a new word: has
For single inheritance the of word is used as before.
has abase, ba as abase, bb as abase
You can have as many `has` statements as you please.
'
'A SIMPLER WAY OF CREATING CLASSES
'The methods are defined inside the class block.
'
'INHERITANCE USING `HAS`
uses "oxygen","file"
dim s as string
s="
'----------
class abase
'==========
method greet()
print `hello`
end method
end class
'-------
class io
'=======
'
'INHERIT 3 LOTS OF `abase`
'--------------------------
'
has abase, ba as abase, bb as abase
private iva as long
'these are overloaded function (same name different params)
method va() as long 'GET
'-----------------------
method=this.iva
end method
method va(v as long) 'SET
'------------------------
this.iva=v
end method
end class
dim o as io
o.va=42 'THIS IS A PSEUDOVARIABLE (method pretending to be a variable)
o.greet
o.ba.greet
o.bb.greet
print `Answer: ` str o.va
"
o2_basic s
if len(o2_error) then msgbox 0,o2_error
'msgbox 0,o2_prep "o2h "+s
'file_save ("t.txt",o2_prep "o2h "+s)
o2_exec
Lionheart008
01-05-2009, 13:36
:) ok... that's all for today... tried to grasp it, but the result at the end is impossible for me... and the code isn't correct.. but it was just a fast experiment...
'
'A SIMPLER WAY OF CREATING CLASSES
'The methods are defined inside the class block.
'
'INHERITANCE USING `HAS`
uses "oxygen","file"
dim s as string
s="
'----------
class abase
'==========
method greet()
print `hello sunshine :)`
end method
method feed()
print `hello, I am hungry`
end method
end class
'-------
class io
'=======
'
'INHERIT 3 LOTS OF `abase`
'--------------------------
'
has abase, ba as abase, bb as abase
'static a=5 as long
private iva as long
'these are overloaded function (same name different params)
method va() as long 'GET
'-----------------------
method=this.iva
end method
method va(v as long) 'SET
'------------------------
this.iva=v
end method
end class
dim o as io
o.va=42+16 'THIS IS A PSEUDOVARIABLE (method pretending to be a variable)
o.greet
o.ba.greet
o.bb.greet
print `Answer: ` str o.va
o.feed
o.ba.feed
o.bb.feed
print `Answer feed: ` str o.feed
terminate
"
o2_basic s
if len(o2_error) then msgbox 0,o2_error
'msgbox 0,o2_prep "o2h "+s
'file_save ("t.txt",o2_prep "o2h "+s)
o2_exec
good idea with methods... and 'has' as word... I will check it closer another time ... uff! It's a lot of stuff to grasp... must laugh again...
servus, cheerio, Lionheart needs more sunshine :D
Charles Pegge
01-05-2009, 14:19
Hi Lionheart,
All is well, (apart from your pangs of hunger.)
o2.feed is a procedure with a print command already in it, but no formal return - so the return value you saw at the end is just whatever happens to be in the eax register.
Charles
To demonstrate this:
method feed()
print `hello, I am hungry`
mov eax,5000
end method
Charles Pegge
06-05-2009, 11:03
This is a class for reading words from a string of text. It uses Assembler to max the performance, and demonstrates how to access THIS members efficiently, taking care to avoid register conflicts. (They are extremely volatile :) ).
The lexword.next method is adequate for reading plain text. More sophisticated reading rules are required for programs, for example capturing negative numbers or quoted strings or detecting the end of a line.
Classes can also contain their own private macros, subs and functions and static elements shared by all objects of the class. Here, lowercase is a private macro.
Charles
Oxygen Update http://community.thinbasic.com/index.php?topic=2517
reading.tbasic
' READING WORDS
'--------------
'combining Assembler and OOP
uses "oxygen","file"
dim src as string, vv as long
src="
o2h
class lexword
stt as long ' start of word
end as long ' end boundary of word
len as long ' length of word
asc as long ' lowercase ascii
typ as long ' type of char: 0 null 2 alphanumeric 3 symbol/punctuation
nxt as long ' lowercase ascii of next word / boundary
ptxt as long ' base pointer to text
ltxt as long ' length of text being read
etxt as long ' boundary of text being read
def lowercase
block
cmp al,64 : jle exit
cmp al,90 : jg exit
add al,32
end block
end def
method read()
dim p
p=this.ptxt-4
this.ltxt= *p
this.etxt= this.ptxt + this.ltxt
this.stt=this.ptxt
this.end=this.ptxt
this.len=0
this.asc=0
this.nxt=0
end method
method readtext(t as string)
this.ptxt=*t
this.read
end method
method next() as string
dim e=this.etxt
dim as lexword i at [edx] ' direct THIS for asm
mov edx,[ebp+8] ' this pointer
mov i.typ,0
mov i.len,0
mov edi,i.end
(
cmp edi,0 : jz long exit
(
mov al,[edi] : inc edi
cmp edi,e : jg fwd xit
cmp al,32 : jle repeat
)
dec edi
mov i.stt,edi
lowercase
mov i.asc,al
(
mov al,[edi] : inc edi
;
lowercase 'macro
;
mov ecx,0
;
;NUMBERS
(
cmp al,48 : jl exit
cmp al,58 : jge exit
mov ecx,1
)
;ALPHA LOWER
(
cmp al,97 : jl exit
cmp al,123 : jge exit
mov ecx,2
)
cmp edi,e : jg exit
cmp ecx,0 : jnz repeat
)
dec edi
mov ecx,2 'ALPANUMERIC
;
;SELF TERMINATING SYMBOLS
(
cmp edi,i.stt : jg exit
inc edi
mov ecx,3 'SYMBOL
)
mov i.nxt,al
mov i.end,edi
mov eax,edi
sub eax,i.stt
mov i.len,eax
)
cmp i.len,0 : jz fwd xit
mov i.typ,ecx
method=nuls this.len 'dynamic string
;
;COPY LOWERCASE
;
mov edx,[ebp+8]
mov edi,eax
mov ecx,i.stt
push esi
mov esi,i.end
(
mov al,[ecx]
lowercase 'CONVERT
mov [edi],al
inc edi
inc ecx
cmp ecx,esi
jl repeat
)
pop esi
xit:
end method
end class
dim as string s
dim lw as lexword
's=getfile `s.txt`
s=`Mary had a little lamb`
lw.readtext s
#view
'print lw.next
'print lw.next
'print lw.next
'print lw.next
'print lw.next
print `
` lw.next() `
` lw.next() `
` lw.next() `
` lw.next() `
` lw.next() `
` lw.next() `
`
''print str lw.len
#endv
"
'file_save("t.txt", "Test: "+o2_error+o2_prep src)
'msgbox 0,"Test: "+o2_error+o2_prep (src)
o2_asmo src
if len(o2_error) then
msgbox 0,"Test: "+o2_error+o2_view (src)
'file_save("t.txt",o2_view (src))
stop
end if
vv=o2_exec
Petr Schreiber
06-05-2009, 11:17
Yes yes yes!,
that's fantastic Charles, maybe I wasn't (C)sharp enough when looking at OOP languages, but I've never seen class having local macros.
And I wonder why, as this is very useful feature.
Great work!,
Petr
Charles Pegge
06-05-2009, 11:53
Thanks Petr,
Earlier on you suggested static libraries, and I thought yes, static source code libraries for classes would be a good place to start. Invoking a library class would be logistically very simple: if it is not located in the source code then look for it in the library folder, then either patch the class into the project or use it dynamically.
Charles
zlatkoAB
06-05-2009, 20:11
Charles I think that you are better programmer then some
author of basic languages :D
What to say - AMAZING !!!!!!
Charles Pegge
06-05-2009, 21:48
Hi Zlatko,
Developing a language is very satisfying and provides the freedom to explore new ways of programming, though it does take up a huge amount of time. I expect you find this too?
Charles
ErosOlmi
06-05-2009, 22:12
Developing a language is very satisfying and provides the freedom to explore new ways of programming
Same here !
I get a lot of satisfaction in developing a programming language.
Even more if my programming language can be useful to others ;)
Thanks to you guys for developing languages... it is lots of work.