Author Topic: End-user arrays for interpreter.  (Read 16263 times)

E.K.Virtanen

  • Guest
End-user arrays for interpreter.
« on: September 07, 2010, 06:44:22 AM »
Hi all.

I would like to have end-user arrays in my dialect. Currently i know how to deal with end-user variables, but good way to support arrays too is something i havent figured out.

Could anyone give me some guide/ideas how i could do this with out extremely complex ways?

Thanks, E.K.Virtanen

JRS

  • Guest
Re: End-user arrays for interpreter.
« Reply #1 on: September 07, 2010, 06:52:44 AM »
You might want to check out how Peter is doing arrays with BaCon. ScriptBasic's array management would be too difficult to try to implement IMHO.


E.K.Virtanen

  • Guest
Re: End-user arrays for interpreter.
« Reply #2 on: September 07, 2010, 07:37:34 AM »
Hi JRS.

I am not sure does that help due BaCon is converter and im planning to build run-time interpreter  :-\
Though for sure, checking it out wont do any harm either  :)

Thanks, E.K.Virtanenn

JRS

  • Guest
Re: End-user arrays for interpreter.
« Reply #3 on: September 07, 2010, 09:24:13 AM »
Have you considered writing your new Basic interpreter in BCX. I think it would be much easier than doing in in FreeBASIC. (and you have C source to tweak if needed)


E.K.Virtanen

  • Guest
Re: End-user arrays for interpreter.
« Reply #4 on: September 07, 2010, 09:25:02 AM »
Hi.

I dont have windows at all, so bcx is limited out. FB, BaCon or such is in my mind for now.

JRS

  • Guest
Re: End-user arrays for interpreter.
« Reply #5 on: September 07, 2010, 09:29:52 AM »
James Fuller has BCX running fine on Linux. (32 and 64 bit) Check out the www.basic-compiler.com/forum for more info.


Steve A.

  • Guest
Re: End-user arrays for interpreter.
« Reply #6 on: September 07, 2010, 09:46:55 AM »
Many years ago, I was trying to figure this exact same thing out.
After some digging and some help, I came up with the solution.
Here is the formula:

Code: [Select]
/* title: arraytst.c
      by: sarbayo, (c) 2001
    Desc: Dynamic Multi-Dimensional arrays.
          This example illustrates how to dynamically create and access,
          store to and retrieve from, multi-dimensional arrays,
          such as these Basic statements:
            <snip>
            DIM an_array(3,3,3)
            an_array(a,b,c) = value
            value = an_array(a,b,c)
            PRINT value
            <snip>

See comments.
*/

#include <stdio.h>
#include <conio.h>
#include <io.h>
#include <stdlib.h>
#include <ctype.h>
#include <malloc.h>

/* ------- #definitions -------- */
#define MAX_VARS    100
#define VAR_NAME    33


  struct array_integer
    {
        long *iv_array;
        char in_array[VAR_NAME];
    } iarray;


/* function prototypes */
    void integers(void);




/* ----- begin program ------------- */

int main(void)
{
    integers();
    return 0;

}
/*---------- end main -----------*/



void integers()             /* --- integer array --- */
{
                                  /* Basic statement: DIM an_array(3,3,3) */
    int L = 3, W = 3, D = 3;                   /* Length, Width, Depth.   */
                /* Note: L,W,D have to be stored somewhere for as long    */
                /* as this array is in use. Perhaps within the structure. */
                /* array's linear size: 3x3x3 = 27  */

    unsigned size=(L*W*D);
    int ii, x;
    int a = L, b = W, c = D;

    strcpy(iarray.in_array, "integer");    /* give this array a name */

                 /* now allocate memory for the array to hold 27 integers,*/
                 /* where:  iarray. = structure, iv_array = integer array */

    iarray.iv_array = malloc(size * sizeof(int));

                 /* now fill the array with some data, (for simplicity),  */
                 /* using the "linear" fashion: array[0]...[26]           */

    for(ii=0; ii < size; ii++)
    {
        iarray.iv_array[ii] = (c*ii);
    }

                 /* Display array contents: */
    x = 0;

    for(ii=0; ii < size; ii++)
    {
        printf("%s\(%d\)=%d\t", iarray.in_array, ii, iarray.iv_array[ii]);
        ii++;
        printf("%s\(%d\)=%d\t", iarray.in_array, ii, iarray.iv_array[ii]);
        ii++;
        printf("%s\(%d\)=%d\n", iarray.in_array, ii, iarray.iv_array[ii]);
        x++;

        if(x == 3)
        {
            printf("----------------------------------------------\n");
            x = 0;
        }
    }
                /* Basic statement: x = an_array(1,1,1)
                                       ' or (a,b,c),

                /* here a 'C' routine would interpret the above Basic   */
                /* statement and make the assignments to a,b,c.         */

    a = 1;      /* 0 to 2 */
    b = 1;      /* feel free to change the values of: */
    c = 1;      /* a,b,c to see the results. */

               /* the algorithm to calculate 3-Dimensional array offset */

    x = iarray.iv_array[c+D*(b+W*a)];                  /* that's it !!! */

                                /* now, print the contents of that cell */

    printf("a\(%d,%d,%d\)=%d\n", a, b, c, x);
}
/*--------- end integers ------------*/
 
 

Quote
/* Even tho a 3-D array is dimensioned in a: Length, Width, Depth fashion,
   I find it easiest to visualize a 3-D array, for accessing purposes,
   in the following manner:
      [page, row, column (or element)].

   On running this program, the screen will display something like this:
              (col0)(col1)(col2)
        (row0)
        (row1)
        (row2)
              ----------------------
   and a line separating the first group, of 3x3, from the second and
   third.

   The groups can be thought of as "Page-0", "Page-1" and "Page-2".
   So, with this Basic statement:  array(1,1,1) ,
   accessing the array is visualised as:
             array(page, row, col)

   The above would select the data cell at:
          page (1), row (1), column (1).

   *Note the "x" (below) on page(1), row(1), column(1):
       (page0)       | (page1)       | (page2)
           (0)(1)(2) |      (0)(1)(2) |     (0)(1)(2)
         (0) .  .  .   |   (0) .  .   .   |  (0) .  .   .
         (1) .  .  .   |   (1) .  x   .   |  (1) .  .   .
         (2) .  .  .   |   (2) .  .   .   |  (2) .  .   .

   Our 3-D array has 3 pages, numbered (0)(1)(2) and each page is a
   square that is dimensioned as 3x3.
       array(1,0,0) translates to: page(1), row(0), column(0).
       array(2,2,2) translates to: page(2), row(2), column(2).

   I have not thoroughly tested all the permutations or beyond 3-D
   arrays, but, what I have tested seems to be working without errors.
   For instance, try making a 4-page array, where "L"=4 and
   "a" ranges from (0)-(3).
   Our array will no longer be a cube, but, now a rectangle.
 

Now, this is written in C, but it should easily be ported to FreeBasic.
In fact, when done in FreeBasic it should be less messy than C.
 
« Last Edit: September 08, 2010, 06:17:00 AM by Steve A. »

rdc

  • Guest
Re: End-user arrays for interpreter.
« Reply #7 on: September 07, 2010, 01:09:47 PM »
In Simtronic (my little VM I am working on) I implement arrays using an allocated segment of memory and access the memory using an array addressing formula.

Edit:

See post below. It is a much easier example to follow than the mess I posted here. :)
« Last Edit: September 07, 2010, 01:32:22 PM by rdc »

rdc

  • Guest
Re: End-user arrays for interpreter.
« Reply #8 on: September 07, 2010, 01:26:34 PM »
Here is a simple example in FreeBasic:

Code: [Select]

'Example of dynamic arrays.

#Define NULL 0

'Our memory segment.
Dim intArray As Integer Ptr
Dim As Integer row = 5, col = 6, tot, cnt 'Dimensions. total

tot = row * col
intArray = Callocate(tot, SizeOf(Integer))
If intArray = 0 Then
   Print "Could not allocate array."
   Sleep
   End
EndIf

'Fill array with data. Arrays are zero based.
cnt = 0
For i As Integer = 0 To row - 1
   For j As Integer = 0 To col - 1
      cnt += 1
      intArray[j + i * col] = cnt
      Print intArray[j + i * col]; " ";
   Next
Next

Sleep

DeAllocate intArray

Steve A.

  • Guest
Re: End-user arrays for interpreter.
« Reply #9 on: September 07, 2010, 03:01:05 PM »
I had to make modifications to the code I posted prior.
It seems that some of the C code was being corrupted in the way it was displayed.
This ended up in the display looking bad, but, also some bits of code were actually being dropped off.
If you viewed it previously, please take a second look, as it is better now.

Thanks

E.K.Virtanen

  • Guest
Re: End-user arrays for interpreter.
« Reply #10 on: September 08, 2010, 06:59:27 AM »
Thanks folks.

E.K.Virtanen

  • Guest
Re: End-user arrays for interpreter.
« Reply #11 on: September 08, 2010, 12:39:20 PM »
@aurel:

You use Creative Basic for ABasic? Would you provide me a simple example which tells me more than a billion words  :)

rdc

  • Guest
Re: End-user arrays for interpreter.
« Reply #12 on: September 08, 2010, 01:07:58 PM »
DIM a[4]
-create integer array with 4 elements -> 0,1,2,3  zero based
i think that i need create 4 pointers... i think like:
a[0],a[1],a[2],a[3]
as you see there is 4 elements stored uder one array name....
what you think about this option...?

Yes, you could implement arrays using a linked list. Just insert as many items as you need in the list corresponding to the number of elements in the array. The addressing formulas that Steve and I presented resolve to a single index, so it should work just fine for a linked list. The problem will be accessing an particular element; you'll have to traverse the list to correct position to retrieve and set a value, so it will be a bit slower than random access. But it will certainly work.

E.K.Virtanen

  • Guest
Re: End-user arrays for interpreter.
« Reply #13 on: September 08, 2010, 01:26:32 PM »
uh, this is getting out of hobby programmer wisdom  :-[

rdc

  • Guest
Re: End-user arrays for interpreter.
« Reply #14 on: September 08, 2010, 02:04:32 PM »
Here is Steve's program converted to FreeBasic.

Code: [Select]

/' title: arraytst.c
      by: sarbayo, (c) 2001
    Desc: Dynamic Multi-Dimensional arrays.
          This example illustrates how to dynamically create and access,
          store to and retrieve from, multi-dimensional arrays,
          such as these Basic statements:
            <snip>
            DIM an_array(3,3,3)
            an_array(a,b,c) = value
            value = an_array(a,b,c)
            PRINT value
            <snip>

See comments.
'/

#define MAX_VARS 100
#define VAR_NAME 33
#Define NULL 0

Type array_integer
   iv_array As Integer Ptr
   in_array As String
End Type

Dim Shared iarray As array_integer

Sub IntArray()             '/* --- integer array --- */
                           '/* Basic statement: DIM an_array(3,3,3) */
   Dim As integer L = 3, W = 3, D = 3 '/* Length, Width, Depth.   */
                   '/* Note: L,W,D have to be stored somewhere for as long    */
                   '/* as this array is in use. Perhaps within the structure. */
                   '/* array's linear size: 3x3x3 = 27  */
   
   Dim As Integer size = L * W * D
   Dim As Integer ii, x
   Dim As Integer a = L, b = W, c = D
   
   iarray.in_array = "integer"    '/* give this array a name */

                 '/* now allocate memory for the array to hold 27 integers,*/
                 '/* where:  iarray. = structure, iv_array = integer array */

   iarray.iv_array = Callocate(size, sizeof(Integer))
   If iarray.iv_array = NULL Then
      Print "Could not allocate array."
      Exit Sub
   EndIf
                 '/* now fill the array with some data, (for simplicity),  */
                 '/* using the "linear" fashion: array[0]...[26]           */

   For ii = 0 To size - 1
      iarray.iv_array[ii] = (c*ii)
   Next

                 '/* Display array contents: */
   For ii = 0 To size - 1
      Print iarray.in_array & "(" & ii &") = " & iarray.iv_array[ii]
   Next
                '/* Basic statement: x = an_array(1,1,1)
                                       ' or (a,b,c),

                '/* here a 'C' routine would interpret the above Basic   */
                '/* statement and make the assignments to a,b,c.         */

   a = 1      '/* 0 to 2 */
   b = 1      '/* feel free to change the values of: */
   c = 1      '/* a,b,c to see the results. */

               '/* the algorithm to calculate 3-Dimensional array offset */

   
   x = iarray.iv_array[c+D*(b+W*a)]                  '/* that's it !!! */
   
   Print
   Print "Index a = " ; a
   Print "Index b = " ; b
   Print "Index c = " ; c
   Print "Value = " ; x
End Sub
'/*--------- end integers ------------*/

IntArray
Sleep
If iarray.iv_array <> NULL Then
   DeAllocate iarray.iv_array
End If