Menu

Implementation of String Arrays in GCBASIC - Feedback Needed

Anobium
2025-02-20
2025-02-23
  • Anobium

    Anobium - 2025-02-20

    I am currently working on implementing String Arrays in GCBASIC.

    The concept is to build upon the existing GCBASIC string handler, where a String Array is essentially a memory allocation to repreent the array (up to the maximum addressable page). Aliases are then created for each of the String Array elements.

    I have completed two steps - works well.

    The identification of a String Array

        Dim myStringArray(3) * 10 ' this will create a string array
    
        'This creates the following automatically - these are memory addresses to each element
    
        ;Set aside memory locations for variables
        ;  Shared/Access RAM = (SA)
        MYSTRINGARRAY                    EQU     463          ; 0x1CF
    
        ;Aliases
        SYSMYSTRINGARRAY_0 EQU 463
        SYSMYSTRINGARRAY_1 EQU 474
        SYSMYSTRINGARRAY_2 EQU 486
        SYSMYSTRINGARRAY_3 EQU 497
    

    The creation of the Aliases to the different elements of the array.

    However, I am facing a challenge with handling the assignment, where the assignment to a String Array element is different from the assignment to an address within a String. For example, an assignment to a String using GCBASIC is straightforward - it like using MID()

    myString(2) = "A" ' Sets the 2nd character to "A" in the existing string called myString

    I want to achieve a similar functionality for String Arrays:

    myStringArray(2) = "A" ' Sets the 2nd element of the String Array to "A"

    But, as you can already understand…​ the array assignment will fail as this will not set the 2nd character of the desired element. It will be treater incorrectly as the compiler, as yet, does not know how to handle.

    Proposed Solution

    Define a New Command for String Array Assignment

    Concepts:

    Create a new command (e.g., SetStringArrayElement) to handle the assignment to specific elements within the String Array. This command will calculate the correct position within the String Array and assign the value to that position.

    Or,

    myStringArray(2,2) = "A" ' Sets the 2nd element of the String Array to "A"

    Or,

    Some other way ?

    Conclusion

    This all seems very clunky to me, and, I need more brains to improve.

    I would appreciate any feedback or suggestions on this approach. Thank you!

     
  • c-conversion

    c-conversion - 2025-02-20

    If I were to offer a preference, it would be:

    myStringArray(2,2) = "A" ' Sets the 2nd element of the String Array to "A"
    

    It makes most sense to my tiny mind.

     
    • Anobium

      Anobium - 2025-02-21

      @c-conversion

      I think your recommendation is a better solution. Mine was just dumb in comparison. :-)


      I have dumped the Alias approach. It created too many issues to be resolved.

      The current approach now creates variables at the correct memory location for each element. I can recreate the aliases, if needed.

      Dim myStringArray( 3 ) as String*10
      

      Now creates memory allocations. With the element address as a suffix.

      MYSTRINGARRAY_0                  EQU     485          ; 0x1E5
      MYSTRINGARRAY_1                  EQU     474          ; 0x1DA
      MYSTRINGARRAY_2                  EQU     463          ; 0x1CF
      

      Next steps. Revert all the changes. Recreate this memory allocation to ensure I have only this memory allocation process in the new code base.

      Then, work through the list following.

      1. Prevent myStringArray = "000" from creating a new variable called myStringArray. This currently works, incorrectly, as this is string assignment and not an illegal assignment to the multi element array.
      2. Enable assignments. `myStringArray(n) = "string"
      3. Enable reads. newString = myStringArray(n)
      4. Enable assignment to specific bytes within a string element
      5. Enable reads from specific bytes within a string element

      I have extended the GCBASIC internal data model for variables to cater for the multielement string arrays.....so, I am thinking should all work by testing the new variable data model. Ha ha.

      I think that if the compile handles the string arrays correctly, using the new data model, then these string arrays will simply leverage all the existing compiler methods to generate valid ASM across all chips. After all... an element of an array is simply a string variable - this is what I keep telling myself. :-)

       
  • Jim giordano

    Jim giordano - 2025-02-21

    I hesitate putting in my 2 cents worth, but looking toward a little more ability and conciseness in the future, I would like to see something like this- myarray(2,3)(4:6)="abcd". This would mean replace the forth thru sixth characters of row 2, column 3 of my matrix with "abcd". I know this is way beyond current design plans. I'm not saying do it now, you have enough to do, but don't code yourself into a corner, leave a little room for later expansion notation wise. For now, just myarray(3)(2) for second character of element 3 is fine.

    alternates for myarray(2,3)(4:6) -
    myarray(2,3:4,6) - more concise but I prefer the colon (4:6) to mean substring rather than having to wrap the whole thing in a mid function,

     
    • Anobium

      Anobium - 2025-02-21

      I understand the ask. I will keep this replacement in mind as this approach has to be faster than constructing a subroutine to do the same.

       
  • jackjames

    jackjames - 2025-02-21

    myStringArray(2,2) = "A"
    It looks like it's a two-dimensional array, i.e. it looks like a matrix of two rows and two columns.
    Practically as if it had been sized like this:
    Dim myStringArray (2) (2).

     
  • Anobium

    Anobium - 2025-02-22

    Any syntax needs to be unique, distinctive and syntactically meanful.

    Would this work? StringArray( element, position, ascii_code ). Like myStringArray( 2, 4, "A" ).. replace the 4th Ascii byte in Element 2 with "A".

     
    • jackjames

      jackjames - 2025-02-22

      This could be ok.
      But so you can only replace a character inside the string.
      Stringarray (element) = AnotherString to copy a string on the array
      Stringarray (element) = "NewString" to copy a string on the array

       
      👍
      1
  • Jim giordano

    Jim giordano - 2025-02-23

    For what it's worth, I've look at that for several minutes, and that is not intuitively obvious to me what it is supposed to mean. As well as truly backing you into a corner for future expansion.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.