Purebasic Subprograms for this and that (AI generated)

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Theo Gottwald

Coming from PowerBasic, developers might find that PureBasic lacks some frequently used programming tools, such as functions equivalent to "ExistFile()" or "ReverseFindString" in other languages. On the other hand, PureBasic boasts a wealth of very useful libraries. For example, it includes a JSON and HTTP library with options for a proxy server, which simplifies several tasks significantly.

However, most of the missing features can be implemented manually, as demonstrated below. The good news is that it doesn't require an excessive amount of manual work.

Meanwhile, GPT-4, as a machine learning model, can assist in generating code snippets in various programming languages, including PureBasic. For example, you can provide it with a JSON structure, and GPT-4 might generate a PureBasic code snippet that attempts to extract all data from the structure, complete with error handling attempts, and more. Sometimes it needs a little guidance, but generally speaking, it can be a helpful tool.

So, when might GPT-4 need help?

Cases where it suggests commands that PureBasic does not have, like "IsNumeric()" or "ExistFile()". It seems that GPT-4, based on its training data, might suggest code snippets in a more common language like Python or VB and then attempt to translate it to PureBasic. While it may not have complete and up-to-date knowledge of PureBasic, it generally understands the syntax of many programming languages and can create templates that might require only minor adjustments. If you have equivalent commands for these "missing commands" in PureBasic, then everything works more smoothly. That's why I'm sharing these commands below.

Cases where it overlooks something. If you prefer code with a single exit point for easier debugging, you can simply instruct GPT-4 to generate code with a "single exit point". It will attempt to comply. You can also request, "Please include an error handler 'Set_Error("errorcode")' at all relevant places." It will attempt to do that too.

Occasions when GPT-4 mixes PureBasic and PowerBasic syntax. For instance, it might generate a function that is mostly in PureBasic, but it might use "Function" and "End Function" at the top and bottom, which are not PureBasic syntax. In such cases, a gentle reminder, "We use PureBasic; we must write 'Procedure'..." is enough for it to attempt a correction.

As you can see from the programming style, all of this PureBasic code is AI-generated.

;--------------------------------------------------------------------
Procedure.i Max(a.i, b.i)
  If a > b
    ProcedureReturn a
  Else
    ProcedureReturn b
  EndIf
EndProcedure
;--------------------------------------------------------------------
Procedure.i Min(a.i, b.i)
  If a < b
    ProcedureReturn a
  Else
    ProcedureReturn b
  EndIf
EndProcedure
;--------------------------------------------------------------------
Procedure.l Min3(a.l, b.l, c.l)
  If a < b And a < c
    ProcedureReturn a
  ElseIf b < c
    ProcedureReturn b
  Else
    ProcedureReturn c
  EndIf
EndProcedure
;--------------------------------------------------------------------
;            IsNumeric Function für Purebasic
;--------------------------------------------------------------------
ProcedureDLL.b IsNumeric(sValue.s)
  Protected i
  Protected S02.s,T01.l
  S02=Trim(sValue)
 
  If Len(S02) = 0
    ProcedureReturn #False
  EndIf
 
  For i = 1 To Len(S02)
    T01=Asc(Mid(sValue, i, 1))
    If  T01< 48 Or T01 > 57     
      ProcedureReturn #False
    EndIf
  Next
 
  ProcedureReturn #True
EndProcedure
;--------------------------------------------------------------------
;        IsNumeric incl. neg. Numbers Function für Purebasic
;--------------------------------------------------------------------

; Allow negative numbers
ProcedureDLL.b IsNeNumeric(sValue.s)
  Protected i
  Protected S02.s,T01.l
  S02=Trim(sValue)
 
  If Len(S02) = 0
    ProcedureReturn #False
  EndIf
 
  For i = 1 To Len(S02)
    T01=Asc(Mid(sValue, i, 1))
    If  T01< 48 Or T01 > 57
      If i = 1 And T01=45 ;"-" ; Allow negative numbers
        Continue
      EndIf
      ProcedureReturn #False
    EndIf
  Next
 
  ProcedureReturn #True
EndProcedure

;--------------------------------------------------------------------
;           
;--------------------------------------------------------------------

ProcedureDLL.l LevenshteinDistance(string1.s, string2.s)
  Protected length1.l
  Protected length2.l 
  Protected i.l, j.l
 
  length1 = Len(string1)
  length2 = Len(string2)
 
  Dim matrix.l(length1, length2)
 
  For i = 0 To length1
    matrix(i, 0) = i
  Next i
 
  For j = 0 To length2
    matrix(0, j) = j
  Next j
 
  For j = 1 To length2
    For i = 1 To length1
      If Mid(string1, i, 1) = Mid(string2, j, 1)
        matrix(i, j) = matrix(i-1, j-1)
      Else
        matrix(i, j) = Min3(matrix(i-1, j) + 1, matrix(i, j-1) + 1, matrix(i-1, j-1) + 1)
      EndIf
    Next i
  Next j
  i=matrix(length1, length2)
  ProcedureReturn i
EndProcedure

;--------------------------------------------------------------------
Procedure.s GetBeforeColon(input.s) 
  Protected position
  If Asc(Left(input, 1)) = 34 ; Check if the string starts with a quote
    input = Mid(input, 2) ; If so, remove the quote at the start
  EndIf
  position = FindString(input, Chr(58)) ; Find the position of the colon
  If position
    S01=Left(input, position - 1) ; Return the part before the colon
  Else
    S01="" ; If there's no colon, return an empty string
  EndIf
  ProcedureReturn S01
EndProcedure
;--------------------------------------------------------------------
Procedure.s GetAfterColon(input.s)
  Protected po.l
  If Asc(Right(input, 1)) = 34 ; Check if the string ends with a quote
    input = Left(input, Len(input) - 1) ; If so, remove the quote at the end
  EndIf 
  po = FindString(input, Chr(58)) ; Find the position of the colon
  If po
    S01=Mid(input, po+1) ; Return the part after the colon
  Else
    S01="" ; If there's no colon, return an empty string
  EndIf
  ProcedureReturn S01
EndProcedure
;--------------------------------------------------------------------
Procedure.s RemoveLastCharacter(input.s)
  Protected S02.s
  S02=Trim(Input)
  If Len(S02) > 0
    S01=Left(S02,Len(S02)-1)
  Else
    S01=S02
  EndIf
  ProcedureReturn S01
EndProcedure
;--------------------------------------------------------------------



ProcedureDLL FileExist(FilePath.s)
    If FileSize(FilePath) = -1
        ProcedureReturn #False
    Else
        ProcedureReturn #True
    EndIf
EndProcedure
 

  ; Function to find a substring in a string, searching from the end
ProcedureDLL.l FindStringReverse(MainString.s, SubString.s)
    ; Reverse the main string and the substring
    ReverseMainString.s = ReverseString(MainString)
    ReverseSubString.s = ReverseString(SubString)
   
    ; Find the position of the reversed substring in the reversed main string
    Position = FindString(ReverseMainString, ReverseSubString)
   
    ; If the substring was found, convert the position back to the original string
    If Position <> 0
        ; Convert the position to be relative to the original (non-reversed) string
        Position = Len(MainString) - Position - Len(SubString) + 2
    EndIf
   
    ; Return the position, or 0 if the substring was not found
    ProcedureReturn Position
EndProcedure
; Sub-procedure to generate a unique filename
ProcedureDLL.s GenerateUniqueFilename(OriginalFilePath.s)
    Protected UniqueFilePath.s, FileBase.s, FileExtension.s, Counter.l, Pos.l
   
    FileBase = GetFilePart(OriginalFilePath, #PB_FileSystem_NoExtension)
    FileExtension = GetExtensionPart(OriginalFilePath)
   
    ; Find the position of the last underscore in FileBase
    Pos = FindString(FileBase, "_", -1)
   
    ; Check if an underscore was found
    If Pos > 0
        ; Try to extract the numeric part at the end of the FileBase
        Counter = Val(Right(FileBase, Len(FileBase) - Pos))
       
        ; Remove the numeric part from FileBase
        If Counter > 0
            FileBase = Left(FileBase, Pos - 1)
        Else
            Counter = 1
        EndIf
    Else
        ; If there is no underscore, initialize Counter to 1
        Counter = 1
    EndIf
   
    ; Loop until we find a unique filename or reach a maximum number of attempts
    For Attempt = 1 To 100000
        ; Construct the new filename
        UniqueFilePath = GetPathPart(OriginalFilePath) + FileBase + "_" + Str(Counter) + "." + FileExtension
       
        ; Check if the file already exists
        If FileSize(UniqueFilePath) = -1
            ; If the file does not exist, we have found a unique filename
            Break
        Else
            ; If the file exists, increment the counter for the next iteration
            Counter = Counter + 1
        EndIf
    Next Attempt
    ;MBX(UniqueFilePath)
    ; Return the unique file path
    ProcedureReturn UniqueFilePath
EndProcedure

How do you transfer a String-Array from Powerbasic to Purebasic?

There are two parts to this, first the Powerbasic Part, then the Purebasic Part (in a DLL):

The Powerbasic Code is from me, easy to see.
;--------------------------------------------------------------------
; ' Sendet ein String-Array an die DLL und dort in das globale Array GArray()
; ' U01 - STRPTR(S01)
; ' U02 - Len(S01)
; ' U03 - Dimensinierung
; '

Declare SUB TransferStringArray LIB "Purebasic.dll" ALIAS "TransferStringArray" (BYVAL arrayPointer As LONG, BYVAL arraySize As LONG,BYVAL U03 As LONG)
; ' U01() - Array 0 ... U02-1
; '
SUB Send_Array(BYREF U01() As STRING) 
    REGISTER i As LONG
    REGISTER j As LONG
    LOCAL T01,T02 As LONG
    LOCAL S01 As STRING
    ' Upper Array-Bound bestimmen
    T01=LBOUND(U01(1)):T02=UBOUND(U01(1))

    ' Concatenate all strings into a single string, with a null character as separator
    For i = T01 To T02
        S01 += U01(i)+CHR$(0)
    Next
    ' Am Ende noch eine Extra-0
    S01 += CHR$(0)

    ' Call the DLL function
    j=Len(S01)
    TransferStringArray(STRPTR(S01),j,T02-T01)
End SUB 

;--------------------------------------------------------------------
Global Dim GArray.s(1) ; Adjust size as needed
Global GDim.l        ; Dimensionierung vom Array

ProcedureDLL ReDim_GArray(U01.l)
  FreeArray(GArray()) 
  Global Dim GArray.s(1) ; Adjust size as needed 
  ReDim GArray(U01)      ; Resize and clear the array once at the start
EndProcedure
;--------------------------------------------------------------------
; Wird von Powerbasic aus aufgerufen und transferiert ein
; StringArray in die DLL
; Size ist ArraySize(GArray())-1 (0 bis X)
ProcedureDLL TransferStringArray(arrayPointer.i, arraySize.l,U03.l)
  Protected i = 0, start = arrayPointer, current = arrayPointer, tempString.s
  ReDim_GArray(U03)
  GDim=U03
  While current < start + arraySize-1
    If PeekB(current) = 0 ; Null terminator found
      GArray(i) = tempString
      tempString = ""
      i + 1
    Else
      tempString + Chr(PeekB(current))
    EndIf
    current + 1
  Wend

; Zum Anzeigen ob das Array angekommen ist 
CompilerIf 0
  For i = 0 To GDim ;ArraySize(GArray()) - 1
    MBX(GArray(i))
  Next
CompilerEndIf
EndProcedure

José Roca

#1
https://edition.cnn.com/2023/08/13/business/ai-quantum-computer-kaku/index.html

The public's anxiety over new AI technology is misguided, according to theoretical physicist Michio Kaku.

In an interview with CNN's Fareed Zakaria on Sunday, the futurologist said chatbots like OpenAI's ChatGPT will benefit society and increase productivity. But fear has driven people to largely focus on the negative implications of the programs, which he terms "glorified tape recorders."

"It takes snippets of what's on the web created by a human, splices them together and passes it off as if it created these things," he said. "And people are saying, 'Oh my God, it's a human, it's humanlike.'"

However, he said, chatbots cannot discern true from false: "That has to be put in by a human."

  •  

Theo Gottwald

#2
Just my opinion.
A.I. is somehow the next Google.

Generally you still type something and you get an answer.

With GPT-5 you may be able to directly talk to the AI and get an answer but technically its still the same.
There is no danger from A.I. unless you have to fear that there is knowledge that people should not get.

Let me add that AI works "as an Assistant".
You copy an JSON Structure that you get from an API in the WEB.
And instead of manually coding something to analyze this structure, you can just copy the structure to GPT for and say "Make me Procedure in Purebasic that will ....".
Especially for complicated structures with variable arrays (and if you do not even know much on how to do that in Purebasic) this saves a lot of time.
And this is just an example, it works with any other structure also.
Registry, INI ... whatever, GPT-4 can make you a code for this.
Currently the maximum Token number is the limit so i guess it will be at around 5 kb of code-result.
Below this it can debug code that you already have, make changes to code that you present,
and anything an human assistant could also do.
It may even be able to translate some tasks into ASM-Code, however i think it may need some corrections here and there doing so.
I think it should also be able to assist with making Header files and to translate them in any way.
But thats a topic i can not talk much about.
But if you have a set of rules, that you can feed it (use the Code-Interpreter Add ON),
then it should be able to do such tasks according to these rules. Line by Line.
Because using Code-Interpreter it can "code these rules" into a Python Script and run this script over the files that you feed.

Examples: Sort case-Lines
2023-08-21 21_32_22-Sort Lines - Brave.jpg

Make Declares for DLL's
2023-08-21 21_33_29-Make PowerBasic Declares - Brave.jpg

If you can provide Sample Code, GPT-4 will even use your Macros and Subprograms:
2023-08-21 21_36_56-Make PB-Subs II - Brave.jpg