Oxygen Basic

Programming => Problems & Solutions => Topic started by: Brian Alvarez on May 01, 2018, 12:48:21 PM

Title: Dimension an array of zstrings.
Post by: Brian Alvarez on May 01, 2018, 12:48:21 PM
 Hello Charles, i will need to ask everything. Is that ok?

 How would you dimension an array of ASCIIZ with a length of 255 characters?
Title: Re: Dimension an array of zstrings.
Post by: Charles Pegge on May 01, 2018, 03:01:34 PM
Hi Brian,

I would say primitively :)

PS: I may have misunderstood your question but here is an array of 100 C-style strings, each with a length of 255 chars and null terminator char.

Code: [Select]
'2018-05-01 T 23:46:36
'ARRAY OF ZSTRING PTR / char*
indexbase 0
typedef char *pchar
char s[25600] 'static char buffer
pchar ps[100] 'array of char pointers
int i,j
for i=0 to 99
  @ps[i]=@s[j] 'map positions in char buffer
  j+=256 'stride
next
'
'TEST:
copy @ps[2],"Helo",4
copy @ps[3],"Wrld",4
copy @ps[4]," !",2
print ps[2] " " ps[3] ps[4] 'Helo Wrld !
Title: Re: Dimension an array of zstrings.
Post by: Brian Alvarez on May 01, 2018, 03:30:19 PM
I see. It is just a little lower level. Thats going to be just a little harder to convert.
Title: Re: Dimension an array of zstrings.
Post by: Brian Alvarez on May 01, 2018, 03:34:11 PM

 Before going ahead to do it, Is there a way to do it with its BASIC mode?
Also, i am making searches for all this before but i dont get any matches,
so, please bear with me:

 Is there a way to have several statements per line? That would make conversions a little easier (tab positions).

Title: Re: Dimension an array of zstrings.
Post by: Charles Pegge on May 01, 2018, 08:28:00 PM
Yes Brian, the Basic syntax is much the same as in PB and FB. You can have multiple statements with colon separators on one line. Underscore line continuations are also supported, but usually unnecessary. Lines ending with an operator or comma are continued onto the next line, ignoring comments.

Simple zstring (char / asciiz) variables are usually straightforward and integrate well with the native (OLE) strings. Only char-pointer assignments are a bit tricky.

All these declarations are supported
Code: [Select]
'zstrings
dim a as asciiz*80
dim as asciiz a[80]="Hello World !"
'asciiz a[80]="Hello World !"
'asciiz a[]="Hello World !"
'asciiz a="Hello World !"
print a


Title: Re: Dimension an array of zstrings.
Post by: Brian Alvarez on May 01, 2018, 08:45:58 PM
Can i do

Code: [Select]
dim a(100) as asciiz * 256
?
Title: Re: Dimension an array of zstrings.
Post by: Brian Alvarez on May 01, 2018, 08:55:36 PM
PluriBASIC supports this syntax:

Code: [Select]
DIM Varname[Symbol][(Bound [TO Bound])][, VarName[Symbol][(Bound [TO Bound])]][...] [AS [SCOPE] DataType] [PTR] [AT Address&] [* Length&] [= DefaultValue]
 Im going to have to know which ones are supported natively by Oxygen so that i can just copy/paste (faster) or convert (normal).
Title: Re: Dimension an array of zstrings.
Post by: Aurel on May 01, 2018, 09:00:10 PM
I use zstring only in filedialog like this:

Dim filename[256] As zstring

but what you need looks to me like
string array element which can hold 256 chars
and this can be created with ordinary string array
BUT maybe is better with asciiz... i am not sure.
Title: Re: Dimension an array of zstrings.
Post by: Brian Alvarez on May 01, 2018, 09:05:21 PM
... maybe is better with asciiz... i am not sure...

 Hi Aurel, thanks. The thing is that in this case, better or worse is irrelevant... i need to add support for it, as efficient as possible but not trying to force efficiency. Just allowing the programmer to do it. :)

 When writing parts of a compiler, i usually look for help in StackOverflow, and many people give alternative solutions based in what i
am trying to do... but that doesnt apply while writing a compiler, because i need to do *everything*. Allow to do everything, that is.

 
Title: Re: Dimension an array of zstrings.
Post by: Brian Alvarez on May 01, 2018, 09:27:41 PM

 I am guessing that while declaring a zstring the dimensions apply as the length of the null terminated string, but for anything else it applies as the number of elements of the array? Am i right? If so, that may be a little inconsistent...
Title: Re: Dimension an array of zstrings.
Post by: Charles Pegge on May 01, 2018, 10:21:46 PM
Yes, it only dims an array of chars. O2 arrays are very primitive by comparison to PB. Do you use a lot of fixed length strings, Brian?

I rarely use fixed length strings but they could be defined as a type, then you could create arrays of them easily:

Code: [Select]
'2018-05-02 T 07:30:23
'fixed length strings
type zstr256 char s[256]
zstr256 z[100]
'
z[1].s="Hello"
z[2].s=" "
z[3].s="World"
print z[1].s + z[2].s + z[3].s
Title: Re: Dimension an array of zstrings.
Post by: Brian Alvarez on May 01, 2018, 11:03:54 PM
I rarely use them. But i need to add them for those who do.
Title: Re: Dimension an array of zstrings.
Post by: Brian Alvarez on May 01, 2018, 11:07:41 PM
 Thanks for the solution Charles, the problem with it is that i would need to define a different TYPE for every different ZSTRING length.... perhaps a class with the string length specified in the constructor?
Title: Re: Dimension an array of zstrings.
Post by: Charles Pegge on May 01, 2018, 11:49:54 PM
No problem.

types can be defined locally and are quite disposable. And we can use a macro the abstract out the dot-s.

Code: [Select]
macro dim_zstring(z,m,n    zz,zu)
  type zz char s[n]
  zz zu[m]
  def z zu[%1].s
end macro

dim_zstring z,100,256
'
z[1]="Hello"
z[2]=" "
z[3]="World"
print z[1] + z[2] + z[3]
Title: Re: Dimension an array of zstrings.
Post by: Brian Alvarez on May 02, 2018, 12:00:09 AM
Thanks again Charles... the problem with this would be that the DIM statement would vary depending on the Datatype of the variable being declared.

Edit:
I think i understand your code, and it is quite elegant. Let me see if i can modify it a bit to make it easier to implement. :) The problem here is, how would i go around using the declared array with REDIM, RESET, ARRAY SORT, ARRAY SCAN, being passed as a parameter of type STRINGZ... et cetera? Thats what is making it hard for me, trying to adapt to every other situation. There are simply too many things to consider... The array cannot be a type, i think i will need to create a mix of your last and your first... but it cannot be a PTR array... Maybe this will have to be fixed at lowest level... reimplementation of the DIM statement? More ideas?
Title: Re: Dimension an array of zstrings.
Post by: Charles Pegge on May 02, 2018, 12:52:54 AM

I've expanded it, and added some annotations

Code: [Select]
'2018-05-02 T 07:30:23
'fixed length string arrays

macro dim_arraytype(ty,va,y,x  ityp,ivar)
  'ty type of element
  'va variable name
  'y  row count of elements
  'x  column count of elements
  'INTERNAL:
  'ityp
  'ivar
  type ityp ty s[x]
  ityp ivar[y]
  def va ivar[%1].s  'eg: va[29].s
end macro

dim_arraytype char,z,100,256
'
z[1]="Hello"
z[2]=" "
z[3]="World"
print z[1] + z[2] + z[3]
Title: Re: Dimension an array of zstrings.
Post by: Brian Alvarez on May 02, 2018, 01:05:13 AM
Thanks Charles, you are very nice. Please see the editions i made to my previous post.
Title: Re: Dimension an array of zstrings.
Post by: Charles Pegge on May 02, 2018, 01:47:41 AM
RESET and REDIM for these fixed-length strings is quite easy to accommodate. SORT SCAN, and other complex operations would be better handled with a class.

REDIM and RESET
Code: [Select]
def reset
  %1_buffer=nuls len(%1_buffer)
  @%1=strptr %1_buffer
end def

macro redim_arraytype(ty,va,y,x)
  'ty type of element
  'va variable name
  'y  row count of elements
  'x  column count of elements
  type va##_type ty s[x]
  redim va##_type va##_var[y]
  def va va##_var[%1].s  'eg: va[29].s
end macro

redim_arraytype char,z,100,256
#recordof z_var_buffer
'
z[1]="Hello"
z[2]=" "
z[3]="World"
redim char_type z_var[200]
'reset z_var
print z[1] + z[2] + z[3]
Title: Re: Dimension an array of zstrings.
Post by: Brian Alvarez on May 02, 2018, 08:12:07 AM
Thanks Charles.

Will this code work for every other array?
Or will i have to generate different code for resetting/redimensioning other arrays?
Can i use the generated array for sorting?
Can i pass the array as a parameter, as regular ZSTRING array?

Edit:
 I hope all my pickiness is not too obnoxious. Please be patient. One of the problems i found with JAVA is that code reusability is almost null. I am trying to make easy for the programmers the repurposing of code, as sometimes it is necessary to use a portion of code for a similar purpose. I'm trying to avoid forcing programmers to rewrite the whole thing just because the array is of type STRING instead of ZSTRING.
Title: Re: Dimension an array of zstrings.
Post by: Mike Lobanovsky on May 02, 2018, 10:16:57 AM
Brian,

You don't have to apologize for every question you put and every issue you report. They are natural in one's learning curve I used to mention.

We know who you are and why you are here. Feel yourself at home; you're serving the cause that's important for both O2 an PB communities. Welcome to the ranks! :)
Title: Re: Dimension an array of zstrings.
Post by: JRS on May 02, 2018, 11:37:06 AM
The beauty of the rose is created by its stem of thorns.
Title: Re: Dimension an array of zstrings.
Post by: Brian Alvarez on May 02, 2018, 12:34:47 PM
 Thanks Guys. Oxygen is not difficult to learn, as far as i have seen its quite simple and straightforward. The problem is not its learning curve, the problem is that i need to learn it by figuring it out. Not too hard, but some explanation of the statements would help a lot.

 For example, in this statement:

Code: [Select]
macro dim_arraytype(ty,va,y,x  ityp,ivar)
 Are ityp and ivar something like MACROTEMPs? If so, what determines them as such? 3 spaces with no comma?

 Also, another issue i am having is the freedom of syntax. As things are now, Its a double edged sword, because i dont know if we are talking about a language or another, and the code needs to be interpreted every time. Is it correct? Is it oxygen?

 Its great to have the flexibility!! but sometimes also consistency is needed.

 Anyway, i am slowly progressing. Soon i will have something to show to the world. :)
Title: Re: Dimension an array of zstrings.
Post by: Brian Alvarez on May 02, 2018, 01:20:05 PM
A couple questions...

 In Oxygen, If i need to get the variable address of the string, would i need to @va[29].s or va[29].@s, will i need another macro for that?

 How would i get the length of the fixed length string?

 If the strings are defined as a contiguous 255 character length + null terminated, would i need an extra character for the null or the next string would interfere? I think Oxygen will need to check for either the null character or the max length of 255... not sure if that can be done at higher level. Also i am guessing that passing it to core API functions that require a null terminated string, would cause issues.

 I think i will for now focus in some other areas, in the meantime i trust you guys will do wonders to sort this out. :)

 Thanks.
Title: Re: Dimension an array of zstrings.
Post by: Charles Pegge on May 02, 2018, 07:57:20 PM
Hi Brian,

macro dim_arraytype(ty,va,y,x  ityp,ivar)


Quote
Are ityp and ivar something like MACROTEMPs? If so, what determines them as such? 3 spaces with no comma?

Yes, they are the equivalent of MacroTemp. Any unused terms in the macro header will be renamed with a unique symbol, so they are not identifiable outside the macro.

In this form of macro header, commas are not significant, but there should be one after the x, for consistency.

Quote
Also, another issue i am having is the freedom of syntax. As things are now, Its a double edged sword, because i dont know if we are talking about a language or another, and the code needs to be interpreted every time. Is it correct? Is it oxygen?

Yes, I agree. I think we will eventually arrive at a preferred syntax, which gives the clearest possible expression of BASIC code.

Quote
In Oxygen, If i need to get the variable address of the string, would i need to @va[29].s or va[29].@s, will i need another macro for that?

The address of string chars is always strptr(..), for example:  strptr(va[29].s)

This is distinct from the resolved address of the variable @va,  @va[29].s


Quote
How would i get the length of the fixed length string?

 If the strings are defined as a contiguous 255 character length + null terminated, would i need an extra character for the null or the next string would interfere? I think Oxygen will need to check for either the null character or the max length of 255... not sure if that can be done at higher level. Also i am guessing that passing it to core API functions that require a null terminated string, would cause issues.

len(..) also works for fixed length strings. It counts up to, but not including a null character. It won't tell you the allocated size.

When creating a fixed length string, include the null terminator in your count
Title: Re: Dimension an array of zstrings.
Post by: Brian Alvarez on May 02, 2018, 09:12:06 PM
Thanks Charles, that saves me time by not having to convert it. How about passing contiguous ZSTRINGS as API parameters? some windows APIs read up to the next null character. So, if i have, lets say, 2 ZSTRINGS of length 5 and i store in both of them: "Hello", if i pass the first item as a parameter, will the API read "HelloHello"? Just making sure Oxygen will handle this correctly.

Also, will LEN work correctly in such situations?
Title: Re: Dimension an array of zstrings.
Post by: Brian Alvarez on May 02, 2018, 09:31:21 PM
Im used to having it store, for example:

Code: [Select]
"Hell" + nul + "Hell" + null
I can generate strings correctly terminated with null, but i personally believe Oxygen should do it natively... thats what ZSTINGS are all about. Would it be too hard to implement it?
Title: Re: Dimension an array of zstrings.
Post by: Charles Pegge on May 02, 2018, 09:37:39 PM
Yes, As long as there is a null terminator after the end of "Hello" this will be handled correctly, as an individual string.

So you can create multiple zstrings in one buffer string, separated by null characters.

O2 also supports wchar / zstring2. For unicode strings 2 null terminator bytes are required