• Welcome to Jose's Read Only Forum 2023.

Example Function converted PowB->PurB

Started by Theo Gottwald, October 29, 2013, 07:45:49 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Theo Gottwald

This is the original PowerBasic Function:

' WaitForInputIdle(PID,opt MaxTime in ms)
' If Maxtime was reached, function returns 1 Else 0 * Default mxt=5000
If (mxt=0) THEN mxt=5000
H = OpenProcess(%PROCESS_ALL_ACCESS,1,ProcID)
r = WaitForInputIdle(H,BYVAL mxt)
CloseHandle H
If (r<>0) THEN r=1

1. The first thing to do is, to replace the comment-sign ' against the ;
2. Second change the Variable declarations from "AS LONG" etc. to ".l"
3. Change the API-Constants from "%PROCESS*" to "#PROCESS*"
4. Change the API Names and add a "_" at their end.
5. Remove the "THEN" from all IF .. THEN and make them Multi-Liners
6. Change all "END IF" to "Endif" and also "END SELECT" to "EndSelect"
7. SUB and Function becomes "Procedure" with the appropriate ".x" Datatype
8. "LOCAL" becomes "Protected"
9. There is no "BYREF" or "BYVAL" available. Also there is no DWORD Datatype.
    Also there is not the WSTRING Datatype. In PureBasic you compile either with ANSI OR with Unicode. You can't easily mix.

Now in PureBasic it looks somehow like this:

; WaitForInputIdle(PID,opt MaxTime in ms)
; If Maxtime was reached, function returns 1 Else 0 * Default mxt=5000
Procedure.l X_CS(h.l,mxt.l=5000)
Protected r.l,ProcID.l
H = OpenProcess_(#PROCESS_ALL_ACCESS,1,ProcID)
r = WaitForInputIdle_(H,mxt)
If (r<>0)
ProcedureReturn r

Patrice Terrier

And the funny thing is that it is called PURE Basic, i would rather call it ObscureBasic  :P

What about this syntax:
// WaitForInputIdle(PID, opt MaxTime in ms)
// If Maxtime was reached, function returns 1 Else 0 * Default mxt=5000
FUNCTION X_CS(LOCAL h is int, LOCAL mxt is unsigned int)
r, ProcID is int
IF (mxt = 0) THEN mxt = 5000
GetWindowThreadProcessId(h, ProcID)
h = OpenProcess(PROCESS_ALL_ACCESS, 1, ProcID)
r = WaitForInputIdle(h, mxt)
IF (r<>0) THEN r = 1

Is not that closer to the original  ::)

Fill free to remove this post from your PureBasic thread, but i couldn't resist.

Patrice Terrier
GDImage (advanced graphic addon)

Mike Stefanik

Quote from: Patrice Terrier on October 29, 2013, 09:39:41 AM
And the funny thing is that it is called PURE Basic, i would rather call it ObscureBasic  :P

That's always been my issue with PureBasic. The technical foundation for the language is great, but the syntax of the language is just bizarre. Particularly the use of "." to designate type when virtually every other implementation of Basic out there uses it in an object-oriented context. It's one of the least "Basic-like" languages out there that claims to be in the Basic family (at least by name).

Daniel Corbier

Here's a transform based on Theo's steps.  It's not complete, plus there are several ways of doing it, but it gives an idea of how you might translate with uCalc Transform:

Notice how each line consists simply of listing the changes you want to make, without requiring a lot of extra code to actually do it.  You would simply load your file, then click Transform.
Daniel Corbier
uCalc Software

Patrice Terrier


Just out of curiosity, did you have done one already to translate from PowerBASIC to C++ ?
Patrice Terrier
GDImage (advanced graphic addon)

James C. Fuller

Just as Patrice I could not resist.
bc9Basic and C++ are quite fussy about data types so I added hh.
It takes a bit to get used to after PowerBASIC and MASM32.

Note: GetWindowThreadProcessId returns a value

The translated c++ code compiled without complaints with VS12 Express.


Function X_CS( h As HWND,  mxt=0 As DWORD) As long
    Dim AS DWORD r,ProcID,dwrv
    Dim As HANDLE hh
    If mxt= 0 Then mxt = 5000
    hh = OpenProcess(PROCESS_ALL_ACCESS,1,ProcID)
    r = WaitForInputIdle(hh, mxt)
    If r<> 0 Then r = 1
    Function = r
End Function

Daniel Corbier


It's not done.  The PowerBASIC to C++ open source project using uCalc Transform started just 2 months ago, and I've been the only one contributing so far.  I think it can be completed much more quickly if more people join.  The project is hosted at:


Everyone is invited to join.  Various levels of experience in C++ and PB is welcome.  In fact even PB programmers with no C++ experience have a lot they can contribute.  For instance, some of it involves refactoring PB code even before it gets converted to C++.

Yesterday's update involved a transform that replaces type specifiers with type names, and adds type names to arguments with neither specifier or type name based on DefType, etc.

So it would change this code:

DefLng a-z
DefByt b
DefInt i
DefStr s
DefQud q
DefSng n-p, x-z

Global MyValue!, Label$, Price@, ExtVal##, qNum&&

Function Test(a, b, c, i, s, n)
   Print MyValue!, Label$, Price@
End Function

Function Report$(LastName$, ByVal x&, ByRef NewPrice@, n, ByVal i, nValue, qValue)
   Dim FirstName$, Age?, Total@
   Static Index&

   Print LastName$, x&, NewPrice@, n, i, nValue, qValue
   Report$ = FirstName$ + LastName$
End Function

Function Hello(txt$) As String
   Print txt$
   Function = txt$ + " friend!"
End Function

Function Bye()
   Print "Bye"
End Function

to this:

Global MyValue As Single, Label As String, Price As Currency, ExtVal As Extended, qNum As Quad

Function Test(ByRef a As Long, ByRef b As Byte, ByRef c As Long, ByRef i As Integer, ByRef s As String, ByRef n As Single) As Long
   Print MyValue, Label, Price
End Function

Function Report(ByRef LastName As String, ByVal x As Long, ByRef NewPrice As Currency, ByRef n As Single, ByVal i As Integer, ByRef nValue As Single, ByRef qValue As Quad) As String
   Dim FirstName As String, Age As Byte, Total As Currency
   Static Index As Long

   Print LastName, x, NewPrice, n, i, nValue, qValue
   Report = FirstName + LastName
End Function

Function Hello(ByRef txt As String) As String
   Print txt
   Function = txt + " friend!"
End Function

Function Bye() As Byte
   Print "Bye"
End Function

The transform for this is at https://github.com/uCalc/powerbasic-to-cpp/blob/master/convert/typespecifiers.uc (note this is a plain text file in raw form; it's meant to be loaded into uCalc Transform).

I had considered BCX, but the syntax is just too different from PowerBASIC.  I want something that works with my existing PB code as-is; hence this project.
Daniel Corbier
uCalc Software

Brice Manuel

Quote from: Mike Stefanik on October 29, 2013, 05:18:24 PMbut the syntax of the language is just bizarre.
Bizarre and wordy.  Sometimes I feel like I am typing complete sentences for commands.