Spin2 - Conversion from P1 Spin to P2 Spin2 - discussion and questions
Cluso99
Posts: 18,071
in Propeller 2
Just trying to understand what is required for converting some routines...
I realise this is not the most efficient, as it would be faster to use if ... else return:=0
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.
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
Plain ABORT without an argument returns 0, while 'ABORT value' returns 'value'.
That makes it easier. Just have to ensure I preset the result to 0 first.
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)
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, 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.