Page 1 of 2

C to PureBasic Transpiler

Posted: Tue Mar 29, 2022 7:42 pm
by fsw
Does anyone know if there is a C to PB transpiler?

A long, long, long, long, long time ago we talked about it on the forum (maybe 15 years ago) but I don’t remember if someone actually coded one.

Thank you

Re: C to PureBasic Transpiler

Posted: Wed Mar 30, 2022 3:03 am
by skywalk
Given the recent developments of PB v6, does this request still matter?
I am more interested including and compiling the many, many C lib's such as idle has presented in another topic.

Re: C to PureBasic Transpiler

Posted: Wed Mar 30, 2022 3:18 am
by BarryG
Why wouldn't it matter? It's about converting C sources to PureBasic sources, isn't it? That would be fantastic!

Re: C to PureBasic Transpiler

Posted: Wed Mar 30, 2022 7:39 am
by fsw
Hi skywalk,
this is a fair question to ask, especially now that PB compiles to C.

I have a specialized graphical app that is coded in plain 32-bit C for Windows.
This app is complex (but the code is easy to read) and I'm thinking of achieving 2 goals:
1. Make it 64-bit
2. Make it multi platform

Surely, a C to PB transpiler would help to get things going faster than doing everything manually...

I found C to Go transpilers, but the graphical portion of the code would still be hard to make multi platform.
(maybe starting out with the Walk UI lib for Go on Windows, and then go from there... but where, which UI?)
That's where PureBasic would come in handy...

BTW: I know transpilers are not the 'holy grail' but still they could cut the time a lot...

Re: C to PureBasic Transpiler

Posted: Wed Mar 30, 2022 2:21 pm
by skywalk
Yes, I wrote a C header to PB prototyper for external C libs but it still required corner cases for macros and complex syntax. Going full C source would be quite a lot more work. But I do see the advantage. If your code is well defined, you could create an equally controlled parser.

Re: C to PureBasic Transpiler

Posted: Wed Mar 30, 2022 6:43 pm
by davido
@fsw,
I don't recall seeing a transpiler, however I think it would be very useful, should one be made available.

Re: C to PureBasic Transpiler

Posted: Wed Mar 30, 2022 7:51 pm
by Tenaja
Sure, it would be nice. But I will be pleasantly surprised if it happens. Just look at how long it has taken Fred to convert PB to C...and he's on it full time.

Now to go the other way (C to PB), with C having so many more features and options for doing the same thing, and it will take even longer. Strings will not get translated to strings, because in C they are arrays. Pointers will be a mess. Case statements with fall-through (i.e. no "break") will be impossible without adding GoTo's everywhere. I'm not saying it would be impossible, but certainly more difficult than PB to C.

It kind of seems like it would be pointless, since you can now include C files into your PB project. What would be the point?

Re: C to PureBasic Transpiler

Posted: Wed Mar 30, 2022 8:29 pm
by Marc56us
Agree: Transcompiling from C to PB would be like trying to put the toothpaste back in the tube. Don't even think about it. Even with very well written C code (which does not exist :D )
I've already rewritten programs. It has always been easier and more viable to start from the analyst's work (Diagram and Pseudo code) and never from the programmer's code whatever language (except for some algorithms)
:wink:

Re: C to PureBasic Transpiler

Posted: Wed Mar 30, 2022 11:07 pm
by idle
It's not really practical as you'd only get a subset of c and it'd be 10 times harder to write than it was to write a c target

you can of course embed c into your program and work with it but it's clunky. The auto c importer I'm working on will eventually facilitate us to just include a c header to import a library and use it in PB with 100% compatibility but it only works with symbols to inform the pb compiler and ide of the c symbols, it doesn't even touch the c syntax as it works directly off of the AST. so it's verbose enough to work with but it still has a number of issues to contend with and I don't have time to work on it at the moment. it's a job for winter and will likely take 1 to 3 months to do.

Re: C to PureBasic Transpiler

Posted: Thu Mar 31, 2022 2:11 am
by fsw
Thank you all for your comments as it gave me some things to think about.

Idle,
Winter in New Zealand?
And all this time I thought New Zealand as almost paradise :P

Anyway, first things first...
...where is my toothpaste?

Live long and prosper...

Re: C to PureBasic Transpiler

Posted: Thu Mar 31, 2022 2:28 am
by idle
It's relative, it gets wet and a little bit chilly for 3 months it's still a sunny 26c today with a light breeze.

Re: C to PureBasic Transpiler

Posted: Thu Mar 31, 2022 6:44 am
by StarBootics
Hello everyone,

Sorry to be later on this topic but there is take on this subject. Remember when I have published a code about RabbitCipher which was written in C originally. Take time to review this code to understand how complicated it can be when you deal with C code that uses unsigned long integers while PureBasic does not. The tactic I have used to solve the problem was to use the positive part of a Quad to simulate the unsigned long integer. Basically I'm using a 64bit variables but only the considering the first 32bit of it. I'm wasting 32bit of memory for each variables for absolutely nothing. Even if it is working it is not memory efficient.

If both language supports all possible types a transpiller would be possible but as you know PureBasic don't support all standard C types. This doesn't mean it can't be done to partially work on some codes.

Best regards
StarBootics

Re: C to PureBasic Transpiler

Posted: Thu Mar 31, 2022 11:07 am
by NicTheQuick
StarBootics wrote: Thu Mar 31, 2022 6:44 am Hello everyone,

Sorry to be later on this topic but there is take on this subject. Remember when I have published a code about RabbitCipher which was written in C originally. Take time to review this code to understand how complicated it can be when you deal with C code that uses unsigned long integers while PureBasic does not. The tactic I have used to solve the problem was to use the positive part of a Quad to simulate the unsigned long integer. Basically I'm using a 64bit variables but only the considering the first 32bit of it. I'm wasting 32bit of memory for each variables for absolutely nothing. Even if it is working it is not memory efficient.

If both language supports all possible types a transpiller would be possible but as you know PureBasic don't support all standard C types. This doesn't mean it can't be done to partially work on some codes.

Best regards
StarBootics
Since the word size of todays operating systems and CPU is 64 bit you are not wasting anything except you are using unpadded structures with fields smaller than 64 bit. I mean, even booleans use 64 bit. ;-)

Re: C to PureBasic Transpiler

Posted: Thu Mar 31, 2022 4:50 pm
by juergenkulow
Could Fred do that?

Re: C to PureBasic Transpiler

Posted: Thu Mar 31, 2022 7:59 pm
by idle
You can use unsigned types now in c backend but you need to redefine the variables in c.
https://www.purebasic.fr/english/viewtopic.php?t=78386