orgMINT / MINT

MINT2 The latest version of MINT
GNU General Public License v3.0
18 stars 3 forks source link

Array Declaration: #37

Open SteveJustin1963 opened 1 month ago

SteveJustin1963 commented 1 month ago

arrays are declared using [ ]. but we cannot declare an empty one as [ ] n ! with a given for later use we can get the array size with /S set array size with [ 3 /S ] // not sure whats best here - future feature ?

this works

:A /Ka! /Kb! /Kc!;
:B [a b c] d! ;
:C d0?. d1?. d2?. ;   // or :C d0?/C d1?/C d2?/C  ;
:D 1(A B C) ;

> D 
123
49 50 51
>D 
abc
97 98 99
>
> [ a b c] a! 
> a/S .
3
>

we can use loop counters /i and /j to also control array size and access

also this works

// Define an array of 5 elements and store its address in 'a'
[ 0 0 0 0 0 ] a! // need to initialise each time 

// Loop 5 times to read input and store in the array
5 (
  `Enter a digit: `       // Prompt the user
  /K 48 - n!              // Read a character, convert ASCII digit to number, store in 'n'
  n a /i ?!  /N              // Store 'n' into array at index /i
)

// Loop 5 times to print each element of the array
5 (
  `Value is: `            // Print label
  a /i ? .  /N              // Fetch array element at index /i and print it
)

/////////////

: A [ 0 0 0 0 0 ] a! ; 
: B 5 ( `Enter a digit: `  /K 48 - n!  n a /i ?!  /N ) ;
: C 5 ( `Value is: ` a /i ? .  /N ) ;

// run it
ABC

///////////////////
jhlagado commented 1 week ago

Changing the array size after declaration is not possible in MINT unless it is the last to be declared on the heap. Because of the complexity I haven't allowed it. The solution would require a more sophisticated approach to be taken with memory management. Better to allocate the size needed. [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...etc ] If this syntax is unwieldy then maybe we need a better syntax similar to /A for declaring arrays based on a desired size Needs more thought