Oxygen Basic
Programming => Problems & Solutions => Topic started by: jumpandrun on May 03, 2012, 12:16:19 AM
-
Hello people!
I discovered Oxygen Basic (OB) literally two days or so ago, browsing the project section in freebasic.org's forum; what caught my attention was
OB's feature to compile & run on-the-fly as an embedded system. I knew this feature from a C called Tiny C Compiler, but I appreciate OB's richer feature set and more flexible syntax :D
The only thing is there seem not to be much documentation around. So I'd have a few questions.
At least I could figure out how to embbed OB, i.e. in freebasic:
declare sub o2_basic lib "oxygen" alias "o2_basic" (as zstring)
declare function o2_exec lib "oxygen" alias "o2_exec" (as long) as long
o2_basic(!"print \"hello, world!\"")
o2_exec(0)
Observation: The output ("hello, world!") display in the usual pop-up window and not in a console, if run from a console. Is there no way to make it print to std out?
How does communication work between the host and the embedded (oxygen.dll) code?
About arrays: Only one dimension is supported?
In general, arrays seem buggy or I do not quite understand, i.e. first code will print 1 and 2, as expected; but second code will not:
int a => (1, 2)
type R
(
int r
)
R c
'c <= 3
for i = 1 to 2
print a[i]
next
int a => (1, 2)
type R
(
int r
)
R c
c <= 3
for i = 1 to 2
print a[i]
next
-
Hi Jumpandrun,
A warm welcome to the Forum!
Yes the manual is very terse but there are quite a large number of examples. The syntax is still evolving, but the core functions are minimal and have not grown very much in the last 18 months or so.
The compiler front-end embeds the Oxygen dll. They are written in FreeBasic and you can find them in tools/Compilers/Exo2FB/
There is a mini Console API in tools/Compilers/
and also two console projects in /projects/ made by Peter and Kent.
With regard to Oxygen Arrays, they are low-level static types but can be overlayed onto any memory space, including strings. Dynamic arrays and multidimensionality are left to the user. But the use of OOP techniques can substantially eliminate the need for arrays
Creating and filling static arrays
These are equivalent:
long[10]<=(1,2,3,4,5,6,7,8,9,0)
long[10]<={1,2,3,4,5,6,7,8,9,0}
dim long(10)<=(1,2,3,4,5,6,7,8,9,0)
dim as long(10)=>(1,2,3,4,5,6,7,8,9,0)
and a few other permutations...
Charles
-
Thank you, very useful, yes.
Still I do not understand why the two pieces of code provided earlier print different values for the same array, as the array's values have not been changed intentinally by the program logic..
-
You need to dimension your arrays. (square brackets). There are no boundary checks. Without an array specification, simple variables only have enough space for one element.
A few more examples
type point
float x,y
end type
'These have the same space
point a
point a[1]
'These have 100 elements
point a[100]
dim as point a(100)
'Filling
a<=1,10,2,20,3,30,4,40
indexbase 1
print a.x " " a.y
print a[1].x " " a[1].y
print a[4].x " " a[4].y
'additional filling
a[5]<=5,50,6,60,7,70
Charles
-
When you ,me or anyone else catch the frequency of Oxygen Basic whole picture is much
clear.. :)
-
Ok, still this does not do the job right:
int i[4] => (1, 2, 4, 5)
print i[1] : print i[3]
print i[1] " " i[3]
-
Looks like a problem with auto-conversion and arrays (mixing string and numeric arrays), I will have to fix that
This works:
int i[4] => (1, 2, 3, 4)
print i[1] : print i[3]
print str(i[1]) " " str(i[3])
Thanks.
Charles
-
It's not just arrays; try this:
qword qw = 10
byte b = 10
print qw
print b
-
Hi Jumpandrun,
Qwords are not supported as a core type. Have you tried quads instead? These are signed 8 byte integers, though rarely used.
I fixed the previous array conversion problem
-
No I have not tried quads because I assumed that quads refer to quadruple-precision floating-points.. :D
Still I think that there is a problem with the print function or so, it produces artifacts on floating point numbers and significantly differs from other languages in this aspect (i.e. java, freebasic), i.e.:
float f = 1.11111
print 1.11111 " " f
-
Ok, another enigma: OB does not let me use non-static variables to index complex type static array?
type Rec
int a, b
end type
class Demo
Rec rec[10]
int i
method test()
rec[i].a = 1 /*OK*/
int j
rec[j].a = 1 /*not OK?*/
end method
end class
-
Did you try this:
type Rec
int a, b
end type
class Demo
Rec rec[10]
int i
method test()
rec[i].a => 1 /*OK*/
int j
rec[j].a => 1 /*work now OK?*/
end method
end class
I don't prefer (read don't like OOP way) but seems that compile fine in this case.
Nothing to show that is another story....
-
Yes that works now, thanks.
OOP is great but only if you know how the language works :D
I am used to java and such, so I would intuitively try to do things that way.
-
Hmm.. yes I found a few problems there. It would not resolve the members of an indexed inherited structure.
Here is a slightly more elaborate test and the fixed DLL to handle them.
NB: using str(num,dp) to remove float artefacts. floats are only good for about 6 decimal places
Thank you both. I will permit myself to sleep now :)
Oxygen.zip further below
Charles
indexbase 0
type tRec
float a, b
end type
class Demo
tRec recA[10]
tRec recB[10]
int i
method test()
i=3
recB[i].b = 1.1111111 /*OK*/
int j=3
recB[j].b = 1.1111111 /*OK*/
end method
end class
#recordof demo
Demo d
d.test
print str (d.recB[3].b, 6)
-
Here is another test. This time the main class inherits from 2 different classes, arrays of 100 members each.
The object pointer passed to the inherited class methods is an offset of the main object pointer
I bet JAVA can't do this directly :)
Charles
'INDEXED INHERITED MEMBERS - USING INHERITED METHODS
class cA
'=======
sys a,b
method set(sys aa,bb) as sys
a=aa : b=bb
end method
method sum() as sys
return a+b
end method
end class
class cB
'=======
sys a,b
method set(sys aa,bb)
a=aa : b=bb
end method
method sum() as sys
return a+b
end method
end class
Class cM
'=======
sys d,e
has cA A[100]
has cB B[100]
method sum() as sys
indexbase 1
sys i,t
for i=1 to 100
t+=a[i].sum
t+=b[i].sum
next
return t
end method
end class
'#recordof cA
'#recordof cB
'#recordof cM
cm c
c.a[3].set 1,2
c.b[3].set 2,3
print c.a[3].sum 'answer 3
print c.b[3].sum 'answer 5
print c.sum 'answer 8
PS: update:
[attachment deleted by admin]