PDA

View Full Version : For X At pStart To pEnd Step Sizeof(X)



ReneMiner
13-02-2016, 15:58
idea to use For-At-Syntax to push virtual variables along a row of data.



For X [As <Type>] At <pStart> To <pEnd> [Step <lSize>] [{While | Until | When} <TRUE>]

...

{statements}

[Exit For]

[Iterate For]

{statements}

...

Next

Don't know if instant declaration [As <Type>] were possible - since not always numeric type here...
pStart & pEnd would await memory-pointer
lSize were the amount to push the variable, default Step-size were +1
Iterate For were probably complicated?

A while ago i posted some suggestion to support-section about to improve SetAt (http://www.thinbasic.com/community/project.php?issueid=496) when pushing a "virtual surfing variable" along a row of data-elements...

Here the -in current tB-syntax running - example i've posted:


' #Filename "SetAt_surfer.tBasic"
Uses "console"

' data could be an array or a row of pointers or whatever anywhere

String data = "abcdefghijklmnopqrstuvwxyz"


' --------------------------------
' i want to avoid doing this:
' --------------------------------
' LOCAL I AS LONG ' avoid another local I !
' Local char(StrPtrLen(StrPtr(data))/SizeOf(Byte)) As Byte At StrPtr(data)
' For i = 1 to CountOf(char)
' Print Chr$(char(I)) ' avoid always indexing the same element
' Next
' --------------------------------


' type of data-elements in this case be Byte (chr$)
' so prepare a "surfer" at start of data:

Local char As Byte At StrPtr(data)

' As long our surfing variable is in valid area:
While GetAt(char) < StrPtr(data) + StrPtrLen(StrPtr(data))

' do something with current element of data
Print Chr$(char)

' move on to next element:
' SetAtNext(char)
SetAt( char, GetAt(char) + SizeOf(char) )
Wend

PrintL

' Now lets go backward:

Do

' move to previous element:
' SetAtPrevious(char)
SetAt( char, GetAt(char) - SizeOf(char) )

' do something with current element of data
Print Chr$(char)


Loop Until GetAt(char) <= StrPtr(data)

PrintL
PrintL "key to future"
WaitKey


Now this is my new idea:

still same variables as above, but not running yet:


' change data to be heap, could be String as well:
DWord pData = HEAP_AllocByStr(data)

For char At pData To HEAP_End(pData)
Print Chr$(char)
Next
PrintL

For char At HEAP_End(pData) To pData Step -SizeOf(char)
Print Chr$(char)
Next
PrintL
PrintL "key to get happy"
WaitKey

ReneMiner
14-02-2016, 10:30
one more running example:


' #Filename "For_At_example.tBasic"

Uses "console"


Function TBMain()

' create memory-pointer to an array of memory-pointers:
DWord pArray = HEAP_AllocByStr( MKDWD$(HEAP_AllocByStr("data of line 1"), _
HEAP_AllocByStr("data of line 2"), _
HEAP_AllocByStr("data of line 3"), _
HEAP_AllocByStr("data of line 4")) )

' prepare a surfing variable to read array-members:
Local pLine As DWord At 0

' current syntax:

For pos As DWord = pArray To HEAP_End(pArray) Step SizeOf(pLine)
SetAt(pLine, pos)
PrintL HEAP_Get(pLine)
Next

' desired syntax:
/*
For pLine At pArray To HEAP_End(pArray) Step SizeOf(pLine)
PrintL HEAP_Get(pLine)
Next
*/
PrintL
PrintL "key to end"
WaitKey
End Function