Shop OBEX P1 Docs P2 Docs Learn Events
Spin2 Structures used as multidimensional arrays? — Parallax Forums

Spin2 Structures used as multidimensional arrays?

Can spin2 structures be used as multidimensional arrays? I had it in my mind that they could be, but when I dug into the syntax, that may not be the case.

BTW, reading through the current version of the Spin2 Document, I see so many things that I have ignored or didn't know about. Enumerated constants and comma-separated declarations are really cool.

Comments

  • JonnyMacJonnyMac Posts: 9,159
    edited 2024-06-30 18:04

    Can spin2 structures be used as multidimensional arrays? I had it in my mind that they could be, but when I dug into the syntax, that may not be the case.

    I wouldn't think so, and you may want to hold off on P2 structures until Chip and the other compiler writers finalize implementation.

    Enumerated constants and comma-separated declarations are really cool.

    Agreed. In may game device programs I use them to define states.

      #0, #GS_PREGAME, #GS_PLAY, #GS_GAMEOVER
    

    My main() method is often as simple as

    pub main()
    
      repeat
        case gameState
          GS_PREGAME  : pre_game()
          GS_PLAY     : run_game()
          GS_GAMEOVER : end_game()
          other       : pre_game()  
    

    Of course, gameState is a global variable so it can be modified as required by any active state.

  • ke4pjwke4pjw Posts: 1,169

    Thanks Jon! I believe I have made a workaround, as the array that I am attempting to enumerate is simple.

  • JonnyMacJonnyMac Posts: 9,159
    edited 2024-07-01 00:33

    Since clarity is a big driver of how I write code you might consider access methods. As the king of simple code, I've done things like this:

    con
    
      ROWS = 5
      COLS = 5
    
    
    var  
    
      byte  array[ROWS * COLS]
    

    Then use methods that are this simple. Using a method adds a bit of overhead, but provides a lot of clarity.

    pub wr_array(value, r, c)
    
      byte[@array+(r*COLS)+c] := value
    
    
    pub rd_array(r, c) : value
    
      value := byte[@array+(r*COLS)+c]
    
  • ke4pjwke4pjw Posts: 1,169

    That's exactly what I did, except I didn't make it generic. There is only one multidimensional array and it has 12 columns.

    PRI codeword_parameters(row,column) : r
      return codeword_parameters_array[(row*12) + column]
    
    
  • msrobotsmsrobots Posts: 3,709

    i remember chip using square brackets on each level in his demo, but not not how to declare them

Sign In or Register to comment.