• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

Translating PB to C/C++

Started by Patrice Terrier, July 25, 2013, 12:23:52 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

James C. Fuller

Quote from: Daniel Corbier on September 13, 2013, 02:52:20 PM
I'm glad I'm not the only person who wants to convert PowerBASIC source code to C++.  I created a tool named uCalc Transform that is specifically designed to help translate source code (among other uses).  This is what I intend to use to make my PB-written software available on other platforms (Android, Mac OS, Linux, etc), and also to create a 64-bit version of my uCalc Fast Math Parser DLL.  I already use it to auto-generate the headers for Delphi, C#, Visual Basic.NET, VB6, and various flavors of C++, taking PB source code as input.

I started an Open Source PB to C++ project.  Please join me to help complete the project.  Visit:

https://github.com/uCalc/powerbasic-to-cpp

It appears one has to purchase your Transform app in order to participate in an Open Source project?

James

Daniel Corbier

As I clarified in http://www.powerbasic.com/support/pbforums/showthread.php?t=53538 there is no requirement to pay to participate in the project.  uCalc Transform will display 2 messages boxes to those who haven't purchased a license.  However, beyond that the download is fully functional, with no time limits.  uCalc Transform is a commercial product.  The Open Source tool is the PB to C++ converter.  Basically I want everyone to join the project, regardless of whether they purchase a license for uCalc Transform.  I'm open to ideas about licensing options in case there's any concern.
Daniel Corbier
uCalc Software

Patrice Terrier

The IF equal comparison pitfall.

PowerBASIC code:
QuoteIF uMsg = WM_PAINT THEN

must be translated to
if (uMsg == WM_PAINT) {

unfortunatly, when you have a huge PB background, it is easy to forget one of the =, and write
if (uMsg = WM_PAINT) {
producing unexpected results, and hard to find bug...
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com


Patrice Terrier

I should have re-read my own thread  :-[
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

James C. Fuller

Patrice,
  Are you using any of the STL containers? (array,vector,deque,list).
For me the vector is the most important entity in C++.
The highly recommended book "Accelerated C++ Practical Programming by Example" starts right out with vectors

James

Patrice Terrier

#36
James--

WinLIFT
Quotevector<BYTE>           g_ByteArray;
vector<MEMBMP>         g_MemBmp;
vector<long>           g_nColor;
vector<long>           g_nMetrics;
vector<HWND>           g_SysBut;
vector<MAINPROPERTY>   g_Win;
vector<CHILDPROPERTY>  g_Child;
vector<ZSCROLLINFO>    g_SI;
vector<wstring>        g_sCfg;
vector<SKINCHECK>      g_NoSkin;
vector<HWND>           g_Zorder;
vector<ANCHORPROPERTY> g_Prop;
vector<MENUITEMS>      g_Mi;
vector<long>           g_mSkin;
vector<RECT>           g_rcb;
vector<ANCHORCTRL>     g_Chor;

;D
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

James C. Fuller


I also like the new c++(11) auto variables and range based for but unfortunately my VC++ from the Win7 SDK doesn't have it.
Will this compile with your VC++?
This compiles fine with the latest MinGW 4.8.1
James



#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main ()
{
// this is new for c++(11)
  vector<wstring>  s={L"one",L"two",L"three",L"four",L"five"};
  cout<<"s contains"<<endl;

// new c11 automatic variables 
  for(auto it=s.begin();it!=s.end();++it)
    {
      wcout<<" "<<*it;
    }

  cout<<endl;
  cout<<"in reverse"<<endl;
  for(auto rit=s.rbegin();rit!=s.rend();++rit)
    {
      wcout<<" "<<*rit;
    }

  cout<<endl;

// range based for loop 
  cout<<"range-based for loop"<<endl;
for ( auto it : s)
    {
      wcout<<" "<<it;
    }

//sort it
  cout<<endl;
  cout<<"sort it "<<endl;
  std::sort(s.begin(),s.end());

// another range based for
for ( auto it : s)
    {
      wcout<<" "<<it;
    }

  cout<<endl;
  system("pause");
  return 0;
}



output:

s contains
one two three four five
in reverse
five four three two one
range-based for loop
one two three four five
sort it
five four one three two

Daniel Corbier

I probably should have started my own separate thread to talk about my PB to C++ open source project, instead of inserting my announcement here.  Sorry about that.   So a moment ago, I posted the announcement in the general forum instead.

But in this thread you (Patrice) mentioned the thing about "=" vs "==".  With uCalc Transform, here's one approach:



Basically this tells it to replace all occurrences of "=" with "==", except for those patterns that are marked with the "Skip over" property.  Those patterns are for equate definitions or variable assignments, defined as a new line, colon, or "Then" keyword immediately followed by one followed by the equal sign.  All other equal signs are modified.  What do you think of that?

Here an example of some code before changes:

%abc = &H100

If x = 456 Then
  y = y+1 : n = 15
  z = x = y
  Print x = 5
  If z > 10 Then Result = 25
End If


Here's what it would look like afterwards:



%abc = &H100

If x == 456 Then
  y = y+1 : n = 15
  z = x == y
  Print x == 5
  If z > 10 Then Result = 25
End If




Then the code can be converted to C++.
Daniel Corbier
uCalc Software

Patrice Terrier

UCODE$

I have added a macro definition that could also be used for UCODE$ replacement.

The macro is here
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

James C. Fuller

Patrice,
  How would you go about SHRINK$?

I have a "c" version from bcx STRIM$ but not sure how to approach a wstring version.

I suppose I could cheat and convert the wstring to ansi, run it through STRIM$ and then convert back to wstring :)

James

José Roca

> I suppose I could cheat and convert the wstring to ansi, run it through STRIM$ and then convert back to wstring

Would be funny to see the result if used by someone that really needs unicode (Chinese, etc.).

James C. Fuller

José,
  That brings up a point if a UNICODE application is really the correct direction for euro/usa users?
I have never encountered a unicode file in all the source code I have downloaded. It seems like it would just add more complexity and confusion to my apps

James

Patrice Terrier

Long before SHRINK$, i wrote that one, that should not be too hard to translate. :)

QuoteFUNCTION Crunch(BYREF sExpr AS STRING, BYVAL UseChar AS BYTE) AS LONG
' Remove all extra chars left, right and inside sExpr
' such to have only one char between each string member.
' CHR$(0) are considerd as spaces.
' Function returns the number of string members found in sExpr
'
    LOCAL So, cMember AS LONG
    LOCAL sStrip AS STRING
    IF UseChar = 32 THEN REPLACE CHR$(0) WITH CHR$(32) IN sExpr
    sExpr = TRIM$(sExpr): sStrip = CHR$(UseChar)
    IF LEN(sExpr) THEN
       So = 1: nMember = 1
       DO
          So = INSTR(So, sExpr, sStrip)
          IF So THEN
             sExpr = LEFT$(sExpr, So) + LTRIM$(MID$(sExpr, So + 1))
             So += 1: nMember += 1
          END IF
       LOOP UNTIL So = 0
    END IF
    FUNCTION = nMember

END FUNCTION
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

James C. Fuller

Patrice,
  Got it.
Plus I found a couple other useful(?) functions to convert to/from string/wstring
They need a better name

James



#define WAITKEY system("pause")
//==============================================================================
std::string valueOf(std::wstring& str) {
    std::string ss;
    ss.assign(str.begin(), str.end());
    return ss;
}
//------------------------------------------------------------------------------

std::wstring valueOf(std::string& str) {
    std::wstring ss;
    ss.assign(str.begin(), str.end());
    return ss;
}
//==============================================================================
std::string SSTRIM(std::string s)
{
    std::istringstream iss(s);
    s = "";
    std::string ss;
    while(iss >> ss)
    {
        if(s != "") s += " " + ss;
        else s = ss;
    }
    return s;
}
//------------------------------------------------------------------------------
std::wstring SSTRIM(std::wstring s)
{
    std::wistringstream iss(s);
    s = L"";
    std::wstring ss;
    while(iss >> ss)
    {
        if(s != L"") s += L" " + ss;
        else s = ss;
    }
    return s;
}
//==============================================================================

int main (int argc, PCHAR* argv)
{
    wstring  ws = L"     abc   def james                c.  fuller \t\t xyz    ";
    wstring  ws2;
    wcout << ws << endl;
    ws2 = SSTRIM( ws);
    wcout << ws2 << endl;
    WAITKEY;
}