Recent

Author Topic: Has anyone converted the C librarys to FreePascal?  (Read 3027 times)

del

  • Jr. Member
  • **
  • Posts: 72
Re: Has anyone converted the C librarys to FreePascal?
« Reply #45 on: September 27, 2019, 11:29:18 am »
OK philosophy is fine, but:

  • Are the C Style operators (+=  -=  *=  /=) here to stay or are we gonna break code in the future when they're taken away?
  • The C Style operators are more readable, less bloaty, and make it pretty clear the intention of the coder.
  • Nobody wants to turn Pascal into C. It would be great if there was a Lazarus for C++ / Linux, but there isn't. So people who are attracted to fun coding on Linux will be attracted to Lazarus.
  • So the table is set for Lazarus to be very successful - but if the community is a cloistered clique with a chip on its shoulder regarding C then that's gonna be a buzzkill.

Just sayin'.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7432
Re: Has anyone converted the C librarys to FreePascal?
« Reply #46 on: September 27, 2019, 11:34:47 am »
OK philosophy is fine, but:

  • Are the C Style operators (+=  -=  *=  /=) here to stay or are we gonna break code in the future when they're taken away?

Probably stay. Which is why they are still there even though it was a bad idea.

Quote
  • The C Style operators are more readable, less bloaty, and make it pretty clear the intention of the coder.

This is entirely subjective.  Pascal has inc and dec() for such things.  (and succ/pred for ++) such things. Having umpteen solutions for the same thing is not  constructive. C doesn't either.

Quote
  • Nobody wants to turn Pascal into C. It would be great if there was a Lazarus for C++ / Linux, but there isn't. So people who are attracted to fun coding on Linux will be attracted to Lazarus.

Well, that IS what you are suggesting. If C "has it", and you can whip up a mob that have no idea of language design principeles that says it is beneficial, it should be done.

Quote
  • So the table is set for Lazarus to be very successful - but if the community is a cloistered clique with a chip on its shoulder regarding C then that's gonna be a buzzkill.

Just sayin'.

There is no chip on the shoulder. One could argue that people that glorify C syntax are the one with the chip on their shoulder. (and personally I think this is true, though only on a moderate and subconscious level)

« Last Edit: September 27, 2019, 12:25:51 pm by marcov »

del

  • Jr. Member
  • **
  • Posts: 72
Re: Has anyone converted the C librarys to FreePascal?
« Reply #47 on: September 27, 2019, 12:18:32 pm »
Excellent. Answered.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7432
Re: Has anyone converted the C librarys to FreePascal?
« Reply #48 on: September 27, 2019, 12:24:12 pm »
Note that while the features might not go away, code using not recommended syntax might not be accepted for code by some of the projects (like fpc, lazarus etc,  FPC is a bit more prudent in such things than lazarus).

Not that I know the exact status, but if for some reason a feature is considered non done for general use, code won't be accepted for some projects, not removing the feature.

« Last Edit: October 10, 2019, 10:11:48 am by marcov »

PascalDragon

  • Hero Member
  • *****
  • Posts: 617
  • Compiler Developer
Re: Has anyone converted the C librarys to FreePascal?
« Reply #49 on: September 30, 2019, 09:23:26 am »
@howardpc

Now you get the complete list of all *.pas *.pp *.inc files containing +=.

Poor result of 61 files. And

* It is not checked if the += is inside a comment
* It is not checked if the += is inside a string

Further: A lot of the filse having to do with Java or Makefile.

As I said: Two of the testfiles are going wild with a lot of +=

In the rest of the poor list every file is containing less that 10 counts; most only 1,2 or 3.

The C syntax is not used in pascal - and that is a good result!
Which reminds me that I wanted to nuke the remaining uses of the C-operators in the compiler sources and then enforce {$coperators off} inside the compiler. :-[

Thaddy

  • Hero Member
  • *****
  • Posts: 8898
Re: Has anyone converted the C librarys to FreePascal?
« Reply #50 on: September 30, 2019, 09:33:08 am »
That looks like a tedious task for someone with time on hand and relatively low risk...You know where to find such a person?  ::)

Can be patched on a per unit basis.

[edit]
Looks like that is already done!
I grepped all of +=/-=/*= and /=

All hits are either comments or messages in the compiler directory. Now checking rtl.... Clean as well, just shell scripts and comments

According to my evaluation you can already disable C operators. Now compiling...

[edit]
compiler + fcl are clean compiled with OPT="-Sc-"  so you can disable it for the compiler and dependencies.
Ergo: you already done your job!
« Last Edit: September 30, 2019, 10:11:50 am by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

del

  • Jr. Member
  • **
  • Posts: 72
Re: Has anyone converted the C librarys to FreePascal?
« Reply #51 on: October 01, 2019, 12:28:34 am »
I discovered - by accident, about a year ago - that the C Style operators (+=  -=  *=  /=) worked in Lazarus. I thought it was some coolness added by the FPC guys, cuz I don't remember using them in Delphi. So I converted all my:

Code: Pascal  [Select]
  1. BigFatVariableName := BigFatVariableName * scale;

(ugh) into

Code: Pascal  [Select]
  1. BigFatVariableName *= scale;

and I thought that I was just the coolest coder ever. But now I'm getting the impression that I committed a huge faux pas(cal). I just made that up.

PascalDragon

  • Hero Member
  • *****
  • Posts: 617
  • Compiler Developer
Re: Has anyone converted the C librarys to FreePascal?
« Reply #52 on: October 01, 2019, 09:13:14 am »
Ergo: you already done your job!
Thanks for checking :D

Thaddy

  • Hero Member
  • *****
  • Posts: 8898
Re: Has anyone converted the C librarys to FreePascal?
« Reply #53 on: October 01, 2019, 09:39:02 am »
and I thought that I was just the coolest coder ever. But now I'm getting the impression that I committed a huge faux pas(cal). I just made that up.
No, not at all: but PascalDragon would rather not see it in the compiler sources itself. The feature will likely stay even if it is contended and not Pascal like.
Note I know for sure that it won't be expanded as I once suggested.
« Last Edit: October 01, 2019, 09:41:51 am by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

avra

  • Hero Member
  • *****
  • Posts: 1685
    • Additional info
Re: Has anyone converted the C librarys to FreePascal?
« Reply #54 on: October 03, 2019, 11:47:13 pm »
I don't know any really working C to Pascal Transpiler.
Try this one: https://github.com/WouterVanNifterick/C-To-Delphi
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

winni

  • Sr. Member
  • ****
  • Posts: 315
Re: Has anyone converted the C librarys to FreePascal?
« Reply #55 on: October 04, 2019, 01:27:19 am »
@avra

Sounds good! I will give it a try.

Thanx

Winni

avra

  • Hero Member
  • *****
  • Posts: 1685
    • Additional info
Re: Has anyone converted the C librarys to FreePascal?
« Reply #56 on: October 04, 2019, 08:30:25 am »
Sounds good! I will give it a try.
Do not expect that it can translate complex programms. For simple ones it works remarkably well. It's good for beginners in C or Pascal when they know the other language, and it's good for C header translations as a complement to existing tools.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Firewrath

  • New Member
  • *
  • Posts: 30
Re: Has anyone converted the C librarys to FreePascal?
« Reply #57 on: October 10, 2019, 12:50:05 am »
(First, just cause. I broke 3 brake-lines at the same time. Cause I'm lucky like that. -_-)
...and I don't mean to bump a 'dead' thread or anything, but I wanted to reply to this:

I don't get why you're so fixated on C libraries. If you want him to learn concepts of programming with Free Pascal than you don't need to resort to C libraries at all.

Well, as much as I like FreePascal / Lazarus, from what I found, there isn't much out there past the 'basics' while C has a ton of stuff for it.
(I know Delphi also has a lot for it that I can use too)
and while he would just be starting to learn programming, I'm still learning Pascal myself. So I also would like to use some of the C stuff that's out there, but I don't want to have to mess with really learning C itself. Just cause. :P

Maybe I'm lazy, but I think trying to convert stuff from C to FreePascal by hand would be a massive pain, cause I'd have to look up each function to see the Pascal version or write one if there isn't one and I'd be in trouble then if its not a simple one. :P
But IF I could just add like a converted C_Math (or whatever is needed) to 'uses' then go on with converting whatever I'm looking at from C to FreePascal, I think it'd be a lot easyer to do and help with learning.
But that's me.

Also, I have seen a post on the forum for this:
https://forum.lazarus.freepascal.org/index.php/topic,45579.0.html

Chet:
https://github.com/neslib/Chet
Which converts C to Delphi, though it has some issues,
A) It converts to Delphi not FreePascal
B) Requires a bit of setup (which me being on winXP at home, not sure I can do)
C) Only outputs files for the system it's installed, so if your'e using it on Windows, it outputs the .pas files for Windows only.

Still handy though for anyone reading through this post interested in the topic.
Sorry. I currently don't have Internet Access. So my replies might take a week. -_-

Thaddy

  • Hero Member
  • *****
  • Posts: 8898
Re: Has anyone converted the C librarys to FreePascal?
« Reply #58 on: October 10, 2019, 07:03:24 am »
1. The code that Chet generates is compatible with {$delphi} mode.  That code therefor can simply be used for FPC too. Erik van Bilsen uses a neat feature to use clang for syntax parsing.
2. h2pas and h2paspp that come with FPC do a similar job and like Chet needs still some manual intervention, about the same. Furthermore C macro's can be expanded first (cpp -E) and copied out to a separate header and fed to h2pas. Chet ignores macro's. h2pas is built using a compiler generator and a formal language description (plex/pyacc)
3. FPC already comes with a huge amount of Pascal bindings for C library code, see the package directories. These bindings are often generated with h2pas.
4. The availability of Pascal bindings for C library code depends on if somebody is willing to create them. It is not rocket science provided the translator is proficient in both Pascal and C.
5. I can not see how you came to any conclusion that C is a richer language by itself, on the contrary, as a language modern Pascal has way more features and specifically is type safe by nature, not by complex macro use. It has also better separation of declaration and implementation.
6. Pascal has built in type safe string types. C doesn't even know strings at the compiler level.
7. See PascalDragon's remarks.
« Last Edit: October 10, 2019, 07:13:08 am by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

440bx

  • Hero Member
  • *****
  • Posts: 1123
Re: Has anyone converted the C librarys to FreePascal?
« Reply #59 on: October 10, 2019, 08:50:47 am »
Well, as much as I like FreePascal / Lazarus, from what I found, there isn't much out there past the 'basics' while C has a ton of stuff for it.
It is a fact that there are many more libraries written for one thing or another in C than in Pascal.  That said, there are plenty of well implemented libraries for "basic" stuff, such as math, trees, sorts, etc, in Pascal.

The C math library is likely fully implemented in Free Pascal and, if there is something missing, no doubt an implementation of whatever is missing is somewhere not hard to find.

and while he would just be starting to learn programming, I'm still learning Pascal myself. So I also would like to use some of the C stuff that's out there, but I don't want to have to mess with really learning C itself. Just cause. :P
Someone who wants to learn programming has a decision to make.  The influence of the C language in programming is difficult to overstate.  The most popular operating systems and other sophisticated software systems are written in C, the consequence of that is, anyone who is serious about programming has no choice but to become, at least, reasonably familiar with the C language and, that is really a bare minimum.

Maybe I'm lazy, but I think trying to convert stuff from C to FreePascal by hand would be a massive pain, cause I'd have to look up each function to see the Pascal version or write one if there isn't one and I'd be in trouble then if its not a simple one. :P
But IF I could just add like a converted C_Math (or whatever is needed) to 'uses' then go on with converting whatever I'm looking at from C to FreePascal, I think it'd be a lot easyer to do and help with learning.
But that's me.
Aside from the fact that a "magic bullet" that can translate any C program into Pascal does not exist, automated translations are, very often a bad idea.  Manually porting C code to Pascal may be pain but, the success of porting the code almost always depends on understanding the original C code.  Tools that automate the process may not, and often do not, result in a working program.  Without any understanding of the original code, fixing whatever the problem is in the resulting translation will not be easy.

The real decision is, how much programming do the two of you want to learn ?.  If the answer is "enough to write some capable programs" then, you'll do fine with knowing only Pascal.  If the answer is "some really sophisticated programs", you can also do it in Pascal but, you'll need to know C because many applicable examples/samples will likely only be available in that language.

Pascal is a good choice because the language and the compiler encourage designing good code (note: encouraging is not the same as guaranteeing.)  That cannot be said about C, on the contrary, if you want to write binary garbage in Paris that will stink in Tokyo, C is the language of choice (note: it is possible to write good code in C, just like it is possible to see a unicorn, it's all about alignment.)

« Last Edit: October 10, 2019, 08:52:44 am by 440bx »
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.