Author Topic: New, and some questions  (Read 4542 times)

0 Members and 1 Guest are viewing this topic.

jumpandrun

  • Guest
New, and some questions
« 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:
Code: [Select]
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:
Code: [Select]
int a => (1, 2)

type R
(
int r
)

R c
'c <= 3

for i = 1 to 2
print a[i]
next
Code: [Select]
int a => (1, 2)

type R
(
int r
)

R c
c <= 3

for i = 1 to 2
print a[i]
next

Charles Pegge

  • Guest
Re: New, and some questions
« Reply #1 on: May 03, 2012, 05:05:07 AM »
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


jumpandrun

  • Guest
Re: New, and some questions
« Reply #2 on: May 03, 2012, 05:31:41 AM »
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..

Charles Pegge

  • Guest
Re: New, and some questions
« Reply #3 on: May 03, 2012, 06:42:21 AM »

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
Code: [Select]
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




Aurel

  • Guest
Re: New, and some questions
« Reply #4 on: May 03, 2012, 07:32:29 AM »
When you ,me or anyone else catch the frequency of Oxygen Basic whole picture is much
clear.. :)

jumpandrun

  • Guest
Re: New, and some questions
« Reply #5 on: May 03, 2012, 11:44:55 PM »
Ok, still this does not do the job right:
Code: [Select]
int i[4] => (1, 2, 4, 5)

print i[1] : print i[3]

print i[1] " " i[3]

Charles Pegge

  • Guest
Re: New, and some questions
« Reply #6 on: May 04, 2012, 12:32:26 AM »

Looks like a problem with auto-conversion and arrays (mixing string and numeric arrays), I will have to fix that

This works:

Code: [Select]
int i[4] => (1, 2, 3, 4)

print i[1] : print i[3]

print str(i[1]) "   " str(i[3])

Thanks.

Charles

jumpandrun

  • Guest
Re: New, and some questions
« Reply #7 on: May 05, 2012, 08:33:34 AM »
It's not just arrays; try this:

Code: [Select]
qword qw = 10
byte b = 10

print qw
print b

Charles Pegge

  • Guest
Re: New, and some questions
« Reply #8 on: May 05, 2012, 09:07:40 AM »
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
« Last Edit: May 09, 2012, 07:50:37 PM by Charles Pegge »

jumpandrun

  • Guest
Re: New, and some questions
« Reply #9 on: May 05, 2012, 02:17:42 PM »
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.:
Code: [Select]
float f = 1.11111
print 1.11111 " " f



jumpandrun

  • Guest
Re: New, and some questions
« Reply #10 on: May 09, 2012, 04:15:49 AM »
Ok, another enigma: OB does not let me use non-static variables to index complex type static array?
Code: [Select]
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

Aurel

  • Guest
Re: New, and some questions
« Reply #11 on: May 09, 2012, 05:41:00 AM »
Did you try this:
Code: [Select]
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....

jumpandrun

  • Guest
Re: New, and some questions
« Reply #12 on: May 09, 2012, 01:21:57 PM »
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.

Charles Pegge

  • Guest
Re: New, and some questions
« Reply #13 on: May 09, 2012, 07:42:58 PM »
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

Code: [Select]

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)
« Last Edit: May 10, 2012, 02:18:35 AM by Charles Pegge »

Charles Pegge

  • Guest
Re: New, and some questions
« Reply #14 on: May 10, 2012, 02:15:49 AM »
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

Code: [Select]


'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]
« Last Edit: May 10, 2012, 02:23:08 AM by Charles Pegge »