05-23-2024, 07:03 PM
(This post was last modified: 05-23-2024, 07:10 PM by SpriggsySpriggs.)
I gave QB64 GPT my sub for handling any argument as a MEM block. I asked it to rewrite the sub. Here is the original code:
And the revisions QB64 GPT made (after minor changes and me asking it to redo some syntax errors):
It separated out a lot of processing out to separate subs. It is quite impressive how little input I had to give it to fix its mistakes. The code actually worked just as well as it did before the changes, which blows my mind.
Note: I didn't post the sample code I used.
It actually even listened to me when I told it that it would need to cast an OFFSET type by using VAL(STR$(var)).
Code: (Select All)
Sub anyArg (args() As _MEM)
Dim As _Unsigned Integer x, y
Dim As _Unsigned _Offset z
Dim As _Unsigned Long size, elementsize
For x = LBound(args) To UBound(args)
If _MemExists(args(x)) Then
z = 0
size = Val(Str$(args(x).SIZE))
elementsize = Val(Str$(args(x).ELEMENTSIZE))
If _ReadBit(args(x).TYPE, 7) And _ReadBit(args(x).TYPE, 13) = 0 Then '_BYTE, INTEGER, LONG, _INTEGER64
If _ReadBit(args(x).TYPE, 10) Then
If _ReadBit(args(x).TYPE, 16) Then
Select Case args(x).ELEMENTSIZE
Case 1
Dim As _Unsigned _Byte unsignedbytearray(1 To (size / elementsize))
For y = LBound(unsignedbytearray) To UBound(unsignedbytearray)
_MemGet args(x), args(x).OFFSET + z, unsignedbytearray(y)
z = z + args(x).ELEMENTSIZE
Print unsignedbytearray(y), "UBYTE ARRAY"
Next
Exit Select
Case 2
Dim As _Unsigned Integer unsignedintarray(1 To (size / elementsize))
For y = LBound(unsignedintarray) To UBound(unsignedintarray)
_MemGet args(x), args(x).OFFSET + z, unsignedintarray(y)
z = z + args(x).ELEMENTSIZE
Print unsignedintarray(y), "USHORT ARRAY"
Next
Exit Select
Case 4
Dim As _Unsigned Long unsignedlongarray(1 To (size / elementsize))
For y = LBound(unsignedlongarray) To UBound(unsignedlongarray)
_MemGet args(x), args(x).OFFSET + z, unsignedlongarray(y)
z = z + args(x).ELEMENTSIZE
Print unsignedlongarray(y), "ULONG ARRAY"
Next
Exit Select
Case 8
Dim As _Unsigned _Integer64 unsignedint64array(1 To (size / elementsize))
For y = LBound(unsignedint64array) To UBound(unsignedint64array)
_MemGet args(x), args(x).OFFSET + z, unsignedint64array(y)
z = z + args(x).ELEMENTSIZE
Print unsignedint64array(y), "UINT64 ARRAY"
Next
Exit Select
End Select
Else
Select Case args(x).SIZE
Case 1
Print _MemGet(args(x), args(x).OFFSET, _Unsigned _Byte), "UBYTE"
Exit Select
Case 2
Print _MemGet(args(x), args(x).OFFSET, _Unsigned Integer), "USHORT"
Exit Select
Case 4
Print _MemGet(args(x), args(x).OFFSET, _Unsigned Long), "ULONG"
Exit Select
Case 8
Print _MemGet(args(x), args(x).OFFSET, _Unsigned _Integer64), "UINT64"
Exit Select
End Select
End If
Else
If _ReadBit(args(x).TYPE, 16) Then
Select Case args(x).ELEMENTSIZE
Case 1
Dim As _Byte bytearray(1 To (size / elementsize))
For y = LBound(bytearray) To UBound(bytearray)
_MemGet args(x), args(x).OFFSET + z, bytearray(y)
z = z + args(x).ELEMENTSIZE
Print bytearray(y), "BYTE ARRAY"
Next
Exit Select
Case 2
Dim As Integer intarray(1 To (size / elementsize))
For y = LBound(intarray) To UBound(intarray)
_MemGet args(x), args(x).OFFSET + z, intarray(y)
z = z + args(x).ELEMENTSIZE
Print unsignedintarray(y), "SHORT ARRAY"
Next
Exit Select
Case 4
Dim As Long longarray(1 To (size / elementsize))
For y = LBound(longarray) To UBound(longarray)
_MemGet args(x), args(x).OFFSET + z, longarray(y)
z = z + args(x).ELEMENTSIZE
Print longarray(y), "LONG ARRAY"
Next
Exit Select
Case 8
Dim As _Integer64 int64array(1 To (size / elementsize))
For y = LBound(int64array) To UBound(int64array)
_MemGet args(x), args(x).OFFSET + z, int64array(y)
z = z + args(x).ELEMENTSIZE
Print int64array(y), "INT64 ARRAY"
Next
Exit Select
End Select
Else
Select Case args(x).SIZE
Case 1
Print _MemGet(args(x), args(x).OFFSET, _Byte), "BYTE"
Exit Select
Case 2
Print _MemGet(args(x), args(x).OFFSET, Integer), "SHORT"
Exit Select
Case 4
Print _MemGet(args(x), args(x).OFFSET, Long), "LONG"
Exit Select
Case 8
Print _MemGet(args(x), args(x).OFFSET, _Integer64), "INT64"
Exit Select
End Select
End If
End If
ElseIf _ReadBit(args(x).TYPE, 8) Then 'SINGLE, DOUBLE, FLOAT
If _ReadBit(args(x).TYPE, 16) Then
Select Case args(x).ELEMENTSIZE
Case 4
Dim As Single singlearray(1 To (size / elementsize))
For y = LBound(singlearray) To UBound(singlearray)
_MemGet args(x), args(x).OFFSET + z, singlearray(y)
z = z + args(x).ELEMENTSIZE
Print singlearray(y), "SINGLE ARRAY"
Next
Exit Select
Case 8
Dim As Double doublearray(1 To (size / elementsize))
For y = LBound(doublearray) To UBound(doublearray)
_MemGet args(x), args(x).OFFSET + z, doublearray(y)
z = z + args(x).ELEMENTSIZE
Print doublearray(y), "DOUBLE ARRAY"
Next
Exit Select
Case 32
Dim As _Float floatarray(1 To (size / elementsize))
For y = LBound(floatarray) To UBound(floatarray)
_MemGet args(x), args(x).OFFSET + z, floatarray(y)
z = z + args(x).ELEMENTSIZE / 2
Print floatarray(y), "FLOAT ARRAY"
Next
Exit Select
End Select
Else
Select Case args(x).SIZE
Case 4
Print _MemGet(args(x), args(x).OFFSET, Single), "SINGLE"
Exit Select
Case 8
Print _MemGet(args(x), args(x).OFFSET, Double), "DOUBLE"
Exit Select
Case 32
Print _MemGet(args(x), args(x).OFFSET, _Float), "FLOAT"
Exit Select
End Select
End If
ElseIf _ReadBit(args(x).TYPE, 9) Then 'STRING
If _ReadBit(args(x).TYPE, 16) Then
Dim As String stringarray(1 To (size / elementsize))
For y = LBound(stringarray) To UBound(stringarray)
stringarray(y) = Space$(args(x).ELEMENTSIZE)
_MemGet args(x), (args(x).OFFSET) + (y * args(x).ELEMENTSIZE - args(x).ELEMENTSIZE), stringarray(y)
Print stringarray(y), "STRING ARRAY"
Next
Else
Dim As String stringtest: stringtest = Space$(args(x).ELEMENTSIZE)
_MemGet args(x), args(x).OFFSET, stringtest
Print stringtest
End If
ElseIf _ReadBit(args(x).TYPE, 13) And _ReadBit(args(x).TYPE, 7) Then '_OFFSET
If _ReadBit(args(x).TYPE, 10) Then
If _ReadBit(args(x).TYPE, 16) Then
Dim As _Unsigned _Offset unsignedoffsetarray(1 To (size / elementsize))
For y = LBound(unsignedoffsetarray) To UBound(unsignedoffsetarray)
_MemGet args(x), args(x).OFFSET + z, unsignedoffsetarray(y)
z = z + args(x).ELEMENTSIZE
Print unsignedoffsetarray(y), "ULONG_PTR ARRAY"
Next
Else
Print _MemGet(args(x), args(x).OFFSET, _Unsigned _Offset), "ULONG_PTR"
End If
Else
If _ReadBit(args(x).TYPE, 16) Then
Dim As _Offset offsetarray(1 To (size / elementsize))
For y = LBound(offsetarray) To UBound(offsetarray)
_MemGet args(x), args(x).OFFSET + z, offsetarray(y)
z = z + args(x).ELEMENTSIZE
Print unsignedoffsetarray(y), "LONG_PTR ARRAY"
Next
Else
Print _MemGet(args(x), args(x).OFFSET, _Offset), "LONG_PTR"
End If
End If
ElseIf args(x).TYPE = 0 And args(x).SIZE > 0 Then '_MEMSOUND
If Not _SndPlaying(args(x).SOUND) Then
_SndPlay (args(x).SOUND)
End If
Print "SOUND", args(x).SIZE, args(x).ELEMENTSIZE
ElseIf _ReadBit(args(x).TYPE, 14) Then
Print args(x).SIZE, "MEM"
'todo
End If
If _ReadBit(args(x).TYPE, 11) Then '_MEMIMAGE
Screen args(x).IMAGE
End If
End If
Next
End Sub
And the revisions QB64 GPT made (after minor changes and me asking it to redo some syntax errors):
Code: (Select All)
Sub anyArg (args() As _MEM)
Dim As _Unsigned Integer x, y
Dim As _Unsigned _Offset z
Dim As _Unsigned Long size, elementsize
For x = LBound(args) To UBound(args)
If _MemExists(args(x)) Then
z = 0
size = Val(Str$(args(x).SIZE))
elementsize = Val(Str$(args(x).ELEMENTSIZE))
If _ReadBit(args(x).TYPE, 7) And _ReadBit(args(x).TYPE, 13) = 0 Then
HandleNumericType args(x), size, elementsize, z
ElseIf _ReadBit(args(x).TYPE, 8) Then
HandleFloatingType args(x), size, elementsize, z
ElseIf _ReadBit(args(x).TYPE, 9) Then
HandleStringType args(x), size, elementsize
ElseIf _ReadBit(args(x).TYPE, 13) And _ReadBit(args(x).TYPE, 7) Then
HandleOffsetType args(x), size, elementsize, z
ElseIf args(x).TYPE = 0 And args(x).SIZE > 0 Then
HandleSoundType args(x)
ElseIf _ReadBit(args(x).TYPE, 14) Then
Print args(x).SIZE, "MEM"
' TODO: Handle other types if necessary
End If
If _ReadBit(args(x).TYPE, 11) Then
Screen args(x).IMAGE
End If
End If
Next
End Sub
' Subroutines for handling specific types
Sub HandleNumericType (arg As _MEM, size As _Unsigned Long, elementsize As _Unsigned Long, z As _Unsigned _Offset)
If _ReadBit(arg.TYPE, 10) Then
If _ReadBit(arg.TYPE, 16) Then
Select Case elementsize
Case 1
Dim As _Unsigned _Byte unsignedbytearray(1 To (size / elementsize))
ProcessArray_UByte unsignedbytearray(), arg, size, elementsize, z, "UBYTE ARRAY"
Case 2
Dim As _Unsigned Integer unsignedintarray(1 To (size / elementsize))
ProcessArray_UInteger unsignedintarray(), arg, size, elementsize, z, "USHORT ARRAY"
Case 4
Dim As _Unsigned Long unsignedlongarray(1 To (size / elementsize))
ProcessArray_ULong unsignedlongarray(), arg, size, elementsize, z, "ULONG ARRAY"
Case 8
Dim As _Unsigned _Integer64 unsignedint64array(1 To (size / elementsize))
ProcessArray_UInt64 unsignedint64array(), arg, size, elementsize, z, "UINT64 ARRAY"
End Select
Else
PrintSingleValue arg, size, elementsize
End If
Else
If _ReadBit(arg.TYPE, 16) Then
Select Case elementsize
Case 1
Dim As _Byte bytearray(1 To (size / elementsize))
ProcessArray_Byte bytearray(), arg, size, elementsize, z, "BYTE ARRAY"
Case 2
Dim As Integer intarray(1 To (size / elementsize))
ProcessArray_Integer intarray(), arg, size, elementsize, z, "SHORT ARRAY"
Case 4
Dim As Long longarray(1 To (size / elementsize))
ProcessArray_Long longarray(), arg, size, elementsize, z, "LONG ARRAY"
Case 8
Dim As _Integer64 int64array(1 To (size / elementsize))
ProcessArray_Int64 int64array(), arg, size, elementsize, z, "INT64 ARRAY"
End Select
Else
PrintSingleValue arg, size, elementsize
End If
End If
End Sub
Sub HandleFloatingType (arg As _MEM, size As _Unsigned Long, elementsize As _Unsigned Long, z As _Unsigned _Offset)
If _ReadBit(arg.TYPE, 16) Then
Select Case elementsize
Case 4
Dim As Single singlearray(1 To (size / elementsize))
ProcessArray_Single singlearray(), arg, size, elementsize, z, "SINGLE ARRAY"
Case 8
Dim As Double doublearray(1 To (size / elementsize))
ProcessArray_Double doublearray(), arg, size, elementsize, z, "DOUBLE ARRAY"
Case 32
Dim As _Float floatarray(1 To (size / elementsize))
ProcessArray_Float floatarray(), arg, size, elementsize, z, "FLOAT ARRAY"
End Select
Else
Select Case size
Case 4
Print _MemGet(arg, arg.OFFSET, Single), "SINGLE"
Case 8
Print _MemGet(arg, arg.OFFSET, Double), "DOUBLE"
Case 32
Print _MemGet(arg, arg.OFFSET, _Float), "FLOAT"
End Select
End If
End Sub
Sub HandleStringType (arg As _MEM, size As _Unsigned Long, elementsize As _Unsigned Long)
If _ReadBit(arg.TYPE, 16) Then
Dim As String stringarray(1 To (size / elementsize))
Dim As _Unsigned Long y
For y = LBound(stringarray) To UBound(stringarray)
stringarray(y) = Space$(elementsize)
_MemGet arg, (arg.OFFSET) + (y * elementsize - elementsize), stringarray(y)
Print stringarray(y), "STRING ARRAY"
Next
Else
Dim As String stringtest: stringtest = Space$(elementsize)
_MemGet arg, arg.OFFSET, stringtest
Print stringtest
End If
End Sub
Sub HandleOffsetType (arg As _MEM, size As _Unsigned Long, elementsize As _Unsigned Long, z As _Unsigned _Offset)
If _ReadBit(arg.TYPE, 10) Then
If _ReadBit(arg.TYPE, 16) Then
Dim As _Unsigned _Offset unsignedoffsetarray(1 To (size / elementsize))
ProcessArray_UOffset unsignedoffsetarray(), arg, size, elementsize, z, "ULONG_PTR ARRAY"
Else
Print _MemGet(arg, arg.OFFSET, _Unsigned _Offset), "ULONG_PTR"
End If
Else
If _ReadBit(arg.TYPE, 16) Then
Dim As _Offset offsetarray(1 To (size / elementsize))
ProcessArray_Offset offsetarray(), arg, size, elementsize, z, "LONG_PTR ARRAY"
Else
Print _MemGet(arg, arg.OFFSET, _Offset), "LONG_PTR"
End If
End If
End Sub
Sub HandleSoundType (arg As _MEM)
If Not _SndPlaying(arg.SOUND) Then
_SndPlay (arg.SOUND)
End If
Print "SOUND", arg.SIZE, arg.ELEMENTSIZE
End Sub
' Subroutines for processing arrays
Sub ProcessArray_UByte (unsignedbytearray() As _Unsigned _Byte, arg As _MEM, size As _Unsigned Long, elementsize As _Unsigned Long, z As _Unsigned _Offset, typeName As String)
Dim As _Unsigned Long y
For y = LBound(unsignedbytearray) To UBound(unsignedbytearray)
_MemGet arg, arg.OFFSET + z, unsignedbytearray(y)
z = z + elementsize
Print unsignedbytearray(y), typeName
Next
End Sub
Sub ProcessArray_UInteger (unsignedintarray() As _Unsigned Integer, arg As _MEM, size As _Unsigned Long, elementsize As _Unsigned Long, z As _Unsigned _Offset, typeName As String)
Dim As _Unsigned Long y
For y = LBound(unsignedintarray) To UBound(unsignedintarray)
_MemGet arg, arg.OFFSET + z, unsignedintarray(y)
z = z + elementsize
Print unsignedintarray(y), typeName
Next
End Sub
Sub ProcessArray_ULong (unsignedlongarray() As _Unsigned Long, arg As _MEM, size As _Unsigned Long, elementsize As _Unsigned Long, z As _Unsigned _Offset, typeName As String)
Dim As _Unsigned Long y
For y = LBound(unsignedlongarray) To UBound(unsignedlongarray)
_MemGet arg, arg.OFFSET + z, unsignedlongarray(y)
z = z + elementsize
Print unsignedlongarray(y), typeName
Next
End Sub
Sub ProcessArray_UInt64 (unsignedint64array() As _Unsigned _Integer64, arg As _MEM, size As _Unsigned Long, elementsize As _Unsigned Long, z As _Unsigned _Offset, typeName As String)
Dim As _Unsigned Long y
For y = LBound(unsignedint64array) To UBound(unsignedint64array)
_MemGet arg, arg.OFFSET + z, unsignedint64array(y)
z = z + elementsize
Print unsignedint64array(y), typeName
Next
End Sub
Sub ProcessArray_Byte (bytearray() As _Byte, arg As _MEM, size As _Unsigned Long, elementsize As _Unsigned Long, z As _Unsigned _Offset, typeName As String)
Dim As _Unsigned Long y
For y = LBound(bytearray) To UBound(bytearray)
_MemGet arg, arg.OFFSET + z, bytearray(y)
z = z + elementsize
Print bytearray(y), typeName
Next
End Sub
Sub ProcessArray_Integer (intarray() As Integer, arg As _MEM, size As _Unsigned Long, elementsize As _Unsigned Long, z As _Unsigned _Offset, typeName As String)
Dim As _Unsigned Long y
For y = LBound(intarray) To UBound(intarray)
_MemGet arg, arg.OFFSET + z, intarray(y)
z = z + elementsize
Print intarray(y), typeName
Next
End Sub
Sub ProcessArray_Long (longarray() As Long, arg As _MEM, size As _Unsigned Long, elementsize As _Unsigned Long, z As _Unsigned _Offset, typeName As String)
Dim As _Unsigned Long y
For y = LBound(longarray) To UBound(longarray)
_MemGet arg, arg.OFFSET + z, longarray(y)
z = z + elementsize
Print longarray(y), typeName
Next
End Sub
Sub ProcessArray_Int64 (int64array() As _Integer64, arg As _MEM, size As _Unsigned Long, elementsize As _Unsigned Long, z As _Unsigned _Offset, typeName As String)
Dim As _Unsigned Long y
For y = LBound(int64array) To UBound(int64array)
_MemGet arg, arg.OFFSET + z, int64array(y)
z = z + elementsize
Print int64array(y), typeName
Next
End Sub
Sub ProcessArray_Single (singlearray() As Single, arg As _MEM, size As _Unsigned Long, elementsize As _Unsigned Long, z As _Unsigned _Offset, typeName As String)
Dim As _Unsigned Long y
For y = LBound(singlearray) To UBound(singlearray)
_MemGet arg, arg.OFFSET + z, singlearray(y)
z = z + elementsize
Print singlearray(y), typeName
Next
End Sub
Sub ProcessArray_Double (doublearray() As Double, arg As _MEM, size As _Unsigned Long, elementsize As _Unsigned Long, z As _Unsigned _Offset, typeName As String)
Dim As _Unsigned Long y
For y = LBound(doublearray) To UBound(doublearray)
_MemGet arg, arg.OFFSET + z, doublearray(y)
z = z + elementsize
Print doublearray(y), typeName
Next
End Sub
Sub ProcessArray_Float (floatarray() As _Float, arg As _MEM, size As _Unsigned Long, elementsize As _Unsigned Long, z As _Unsigned _Offset, typeName As String)
Dim As _Unsigned Long y
For y = LBound(floatarray) To UBound(floatarray)
_MemGet arg, arg.OFFSET + z, floatarray(y)
z = z + elementsize / 2
Print floatarray(y), typeName
Next
End Sub
Sub ProcessArray_UOffset (unsignedoffsetarray() As _Unsigned _Offset, arg As _MEM, size As _Unsigned Long, elementsize As _Unsigned Long, z As _Unsigned _Offset, typeName As String)
Dim As _Unsigned Long y
For y = LBound(unsignedoffsetarray) To UBound(unsignedoffsetarray)
_MemGet arg, arg.OFFSET + z, unsignedoffsetarray(y)
z = z + elementsize
Print unsignedoffsetarray(y), typeName
Next
End Sub
Sub ProcessArray_Offset (offsetarray() As _Offset, arg As _MEM, size As _Unsigned Long, elementsize As _Unsigned Long, z As _Unsigned _Offset, typeName As String)
Dim As _Unsigned Long y
For y = LBound(offsetarray) To UBound(offsetarray)
_MemGet arg, arg.OFFSET + z, offsetarray(y)
z = z + elementsize
Print offsetarray(y), typeName
Next
End Sub
Sub PrintSingleValue (arg As _MEM, size As _Unsigned Long, elementsize As _Unsigned Long)
Select Case size
Case 1
Print _MemGet(arg, arg.OFFSET, _Byte), "BYTE"
Case 2
Print _MemGet(arg, arg.OFFSET, Integer), "SHORT"
Case 4
Print _MemGet(arg, arg.OFFSET, Long), "LONG"
Case 8
Print _MemGet(arg, arg.OFFSET, _Integer64), "INT64"
End Select
End Sub
It separated out a lot of processing out to separate subs. It is quite impressive how little input I had to give it to fix its mistakes. The code actually worked just as well as it did before the changes, which blows my mind.
Note: I didn't post the sample code I used.
It actually even listened to me when I told it that it would need to cast an OFFSET type by using VAL(STR$(var)).
Tread on those who tread on you