Shop OBEX P1 Docs P2 Docs Learn Events
Spin2 - Conversion from P1 Spin to P2 Spin2 - discussion and questions — Parallax Forums

Spin2 - Conversion from P1 Spin to P2 Spin2 - discussion and questions

Just trying to understand what is required for converting some routines...
PUB readByte                    ' returns a byte
  readData(@result, 1)

--becomes--------------------------------------------------------------

PUB readByte() : ch             ' returns a byte (actually it returns a long)
  ch := readData(@result, 1)
Is result still defined in spin2???
PUB readString(stringPointer, maximumStringLength)
  if(stringPointer and (maximumStringLength > 0))
    result := stringPointer

    bytefill(stringPointer--, 0, maximumStringLength--)
    repeat while(maximumStringLength--)
      ifnot( readData(++stringPointer, 1) and byte[stringPointer] and (byte[stringPointer] <> 10) and (byte[stringPointer] <> 13) )
        quit

--becomes--------------------------------------------------------------

PUB readString(stringPointer, maximumStringLength) : result
  result := 0
  if(stringPointer and (maximumStringLength > 0))
    result := stringPointer

    bytefill(stringPointer--, 0, maximumStringLength--)
    repeat while(maximumStringLength--)
      ifnot( readData(++stringPointer, 1) and byte[stringPointer] and (byte[stringPointer] <> 10) and (byte[stringPointer] <> 13) )
        quit
I have presumed here that result is no pre-initialised to 0, so I must explicitly do this now.
I realise this is not the most efficient, as it would be faster to use if ... else return:=0
PUB writeData(addressToGet, count) | stride 
  result := addressToGet
  if(lockFileSystem("W") and fileOpenCloseFlag and fileReadWriteFlag)
    count := ((count <# (posx - currentPosition)) #> 0)
    repeat while(count)

      if(((currentPosition >> 9) > currentSector) and fileBlockDirtyFlag)
        fileBlockDirtyFlag := false
        readWriteCurrentSector("W")

      currentByte := currentPosition
      ifnot(readWriteCurrentCluster("W"))
        quit

      stride := (count <# (512 - currentByteInSector))
      bytemove(addressDIREntry, addressToGet, stride)
      currentPosition += stride
      addressToGet += stride
      count -= stride

      currentSize #>= currentPosition
      fileBlockDirtyFlag := true

  unlockFileSystem
  return (addressToGet - result)

--becomes--------------------------------------------------------------

PUB writeData(addressToGet, count) : result | stride 
  result := addressToGet
  if(lockFileSystem("W") and fileOpenCloseFlag and fileReadWriteFlag)
    count := ((count <# (posx - currentPosition)) #> 0)
    repeat while(count)

      if(((currentPosition >> 9) > currentSector) and fileBlockDirtyFlag)
        fileBlockDirtyFlag := false
        readWriteCurrentSector("W")

      currentByte := currentPosition
      ifnot(readWriteCurrentCluster("W"))
        quit

      stride := (count <# (512 - currentByteInSector))
      bytemove(addressDIREntry, addressToGet, stride)
      currentPosition += stride
      addressToGet += stride
      count -= stride

      currentSize #>= currentPosition
      fileBlockDirtyFlag := true

  unlockFileSystem
  result := (addressToGet - result)
Again, I presume we don't have result so I need to declare it, and the return needs to become a result := (addressToGet - result)
In the case above, what happens with the quit statement?

None of this takes the abort into account which I'll have to deal with later.

Comments

  • cgraceycgracey Posts: 14,133
    Their is no automatic RESULT variable in methods. Any result value must be given a symbolic name, for their to be any return value(s). It is not necessary to use that name, however, if you do a 'RETURN resultvalue'.
  • cgraceycgracey Posts: 14,133
    ABORT is a little different. It always returns ONE value, regardless of the number of return values in a method.

    Plain ABORT without an argument returns 0, while 'ABORT value' returns 'value'.
  • Cluso99Cluso99 Posts: 18,066
    Thanks Chip.
    That makes it easier. Just have to ensure I preset the result to 0 first.
  • cgraceycgracey Posts: 14,133
    All results are preset to 0, so no need to initialize return values in case they aren't ever assigned anything.
  • Cluso99Cluso99 Posts: 18,066
    edited 2020-04-04 04:46
    So these would be the first rules for spin1 to spin2 conversion...

    Note: Spin1 has a limit of a single long returned. Using this as a basis...

    1. If there is no "(" following (ie no calling parameters) then PUB/PRI requires "()" following it. Spaces allowed.

    2. If the PUB/PRI returns a value, then the PUB/PRI needs the inclusion after the ")" of " : result"

    3. There is no default set for result. Therefore, if a default result of zero is required, then the line result := 0 must be inserted as the first statement. Code improvement can be done later.

    4. Single statements of method(params) that actually return a result need to be converted to result := method(params)
  • JonnyMacJonnyMac Posts: 8,912
    edited 2020-04-04 06:27
    3. There is no default set for result. Therefore, if a default result of zero is required, then the line result := 0 must be inserted as the first statement. Code improvement can be done later.
    No. In the P1, the implicit result variable is initialized to zero. In the P2, explicit return variables are also initialized to zero.

    Other changes:
    -- P2 methods can return multiple values (all must be declared)
    -- P2 local variables can be longs (default), words (must be declared by type), or bytes (must be declared by type)
  • Cluso99Cluso99 Posts: 18,066
    JonnyMac wrote: »
    3. There is no default set for result. Therefore, if a default result of zero is required, then the line result := 0 must be inserted as the first statement. Code improvement can be done later.
    No. In the P1, the implicit result variable is initialized to zero. In the P2, explicit return variables are also initialized to zero.
    Thanks Jon. I didn't know the P2 initialised them to zero, so I can remove that item :)
    Other changes:
    -- P2 methods can return multiple values (all must be declared)
    -- P2 local variables can be longs (default), words (must be declared by type), or bytes (must be declared by type)
    Yes, but currently I am only concerned with porting spin1 to spin2. Since these features were not available in spin1 they currently don't matter.

  • JonnyMacJonnyMac Posts: 8,912
    edited 2020-04-04 15:21
    Yes, but currently I am only concerned with porting spin1 to spin2. Since these features were not available in spin1 they currently don't matter.
    I mention that because I have Spin1 code that looks like this:
    pub some_method(param1, param2, paramN, p_var1, p_var2)
    
    ...where p_var1 and p_var2 are pointers to variables that I want updated by the method. Also, there are times -- I've found, anyway -- when a local byte array is convenient; the ability to type P2 locals makes this easier and gets rid of the variable.byte[n] syntax. It's a small thing, but clearer to newcomers, I think.
  • Cluso99Cluso99 Posts: 18,066
    edited 2020-04-04 18:43
    JonnyMac wrote: »
    Yes, but currently I am only concerned with porting spin1 to spin2. Since these features were not available in spin1 they currently don't matter.
    I mention that because I have Spin1 code that looks like this:
    pub some_method(param1, param2, paramN, p_var1, p_var2)
    
    ...where p_var1 and p_var2 are pointers to variables that I want updated by the method. Also, there are times -- I've found, anyway -- when a local byte array is convenient; the ability to type P2 locals makes this easier and gets rid of the variable.byte[n] syntax. It's a small thing, but clearer to newcomers, I think.

    Yes, I have passed a pointer to an array where I’ve used and updated variables in that array. It’s perhaps more common too. Certainly passing a pointer to a list of known parameters is quite common - eg a set of parameters for a list of pins, or a set of vga parameters.

    Of course these will still work as-is in spin2.
Sign In or Register to comment.