Lazarus

Programming => General => Topic started by: Firewrath on September 25, 2019, 07:08:59 pm

Title: Has anyone converted the C librarys to FreePascal?
Post by: Firewrath on September 25, 2019, 07:08:59 pm
So forgive me if I'm being stupid, I tend to do that. >.>
But I know Pascal is 'C like' or 'C based' and that there are some semi-auto conversion tools out there, but you still have to have a pretty good understanding of C to use them. Which I don't. =/

The main reason I'm asking is a friend of mine wants to learn Programming, and has Never done anything like programming before, I'd like to point him at Lazarus/FreePascal for this, since it's so easy to start with, but there is a ...lack of good comprehensive tutorials online.
But there is a TON of stuff for C out there.
While I have played with programming and scripts off and on for years and can get by on some guess work and reading the docs for what I do, he is looking to improve himself for trying to find a new job.

So I was wondering if anyone has ever converted the base C librarys for use with FreePascal?
Where I could start him with Lazarus / FreePascal and then point him at C tutorials for further learning without trying to convert the everything to FreePascal as he goes.

...if not, I'll probably just point him to C or Rust. >.>

And today, the brake line of my car broke, so i'm only 'here' till I get it towed. >.>
*Sigh* So I'm not ignoring replys or anything. =/
...just one of those days.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: winni on September 25, 2019, 07:20:15 pm
Just search Google for "Delphi Lazarus learn beginner" and you get a lot of hits. Or dont you have a Google?

Just like

https://delphi-lernen.de/ (https://delphi-lernen.de/)

Starts with Delphi and continues with Lazarus.
German, but we have translators.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: schuler on September 25, 2019, 07:33:50 pm
Pascal is a perfect language for learning programming. Tell your friend to start from here:
https://wiki.freepascal.org/Object_Pascal_Tutorial

https://www.google.com/search?q=free+pascal+tutorial&oq=free+pascal+tutorial&aqs=chrome..69i57j0l6.3920j0j7&sourceid=chrome&ie=UTF-8
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: winni on September 25, 2019, 07:50:51 pm
C was invented to write an operating system. Called Unix.
Pascal was invented to teach students.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: garlar27 on September 25, 2019, 08:10:03 pm
Forget about C.

Your friend should start with Pascal (to learn the basics and good practices) and then move to Kotlin, Android, NodeJS, Angular, Java, Python, what so ever. Although Object Pascal is powerful, on those languages is where the money is, right now.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Firewrath on September 25, 2019, 08:18:18 pm
I have downloaded all the SchoolFreeware FreePascal tutorial vids, and a collection of 30-40 others that I can't remember the name of.
I also have:

StartProgUsingPascal.pdf
FreePascalFromSquareOne.pdf
pascal_tutorial.pdf (from Tutorials Point)
pascal_quick_guide.pdf
modern_pascal_introduction.pdf
(not to mention the 50 other webpages/forum post I have downloaded -_- )

but all those just repeat the basics mostly.

So, Yes. I have heard of google. :P
And I have thought about just letting him learn Delphi, but I know there is weird conversion stuff between Delphi and Lazarus. So I thought I see if anyone had converted C, since it would hopefully be easyer to sort out without having confusing "Well it looks right but..." issues.


As for 'where the money is'...
Thats why I figure C or Rust.
C is used almost everywhere still and everyone is all excited about Rust and looking to move over to it for some reason. I heard Microsoft was looking at adding it to Windows.
I wont be able to help him if he has issues there, but it would probably be good for him to get in on it early'ish.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: winni on September 25, 2019, 08:37:17 pm
For beginner stuff there is absolute no difference between Delphi and Lazarus.

If you look look enough in Google you will find a lot of stuff for advanced.

Here: With pointers, objects, classes, interfaces:

http://www.ggde.gr/dmdocuments/pascal_tutorial.pdf (http://www.ggde.gr/dmdocuments/pascal_tutorial.pdf)

Nice that you have a Google. But you must spent some time to use it.

And for the more advanced issues there are a lot of tutorials for the special things around.
 
Pascal is the best to learn as first language. C messes up the customs and should be learned when you know how to handle code!

Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: 440bx on September 25, 2019, 08:40:39 pm
he is looking to improve himself for trying to find a new job.
That sounds like that he just wants to acquire one or more skills that makes him more "valuable" to a potential employer.  The reason I point that out is because, acquiring some programming knowledge, which is fairly easy, is a very different proposition than becoming a professional programmer.

From what you've said, it sounds like his goal isn't becoming a professional programmer.

If that conclusion happens to be correct, I'd recommend he acquires some knowledge of "simpler" programming languages such as Python and AWK.  Knowledge of such languages would allow him to quickly implement solutions to common problems, which is usually what an employer wants.

The above said, Pascal would be a good choice for learning programming and, what is more important than whatever programming language he chooses to study is, being able to analyze problems and design well thought out solutions for them.  For that, I recommend the book "Oh Pascal" from Doug Cooper because he teaches the reader to evaluate a problem in the context of designing a computer program to solve it.  _That_ is the valuable skill, much more than just knowing the language.

Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Firewrath on September 25, 2019, 09:47:52 pm
That sounds like that he just wants to acquire one or more skills that makes him more "valuable" to a potential employer.  The reason I point that out is because, acquiring some programming knowledge, which is fairly easy, is a very different proposition than becoming a professional programmer.

From what you've said, it sounds like his goal isn't becoming a professional programmer.

Hrm, you know, I don't know 'for sure' but you are probably right.
The way he talked he wanted to learn programming to change jobs, so I probably got ahead of myself thinking he wanted to do it as a job more then he actually does.

Thanks for the advice, I shall talk to him more about it when I get the chance and pass that book along to him if he is interested. ^-^

(Though just to stay on topic, I still say the C librarys in FreePascal would be rather cool / handy and did get a little excited about it when I had the idea. >.>
...Also, so far, 6hrs till I can get towed. OMG x.x)
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: winni on September 25, 2019, 10:06:42 pm
Hi!

I don't know any really working C to Pascal Transpiler. Seems to be too complicated on code level.

But I found something antique in the deepth of my harddisk:

In 1989 Derek from Knowledge Software wrote a C to Turbo Pascal Transpiler! No classes, no objects. It is a DOS program! Try it!

Winni
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: del on September 25, 2019, 11:09:13 pm
I recommend learning C in order to get a clear understanding of memory management. Then you can make your own simple classes that manage their own memory and that takes away 99% of the hassle. Compile it of course as C++. But you're still down in the weeds. You'll get an appreciation of what happens in constructors and destructors. Then you can jump around between C++ and Free Pascal and see what you like and etc. I would also stay away from Python - you'll just pick up so much mush that you'll have to unlearn later IMHO.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: RayoGlauco on September 26, 2019, 12:14:51 am
Pascal is a good language to learn the principles of programming. Before entering memory management, it is necessary to know what is a variable, a constant, the types of data, the basic control structures, the algorithms, and much more ...
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: garlar27 on September 26, 2019, 12:56:58 am
Pascal is a good language to learn the principles of programming. Before entering memory management, it is necessary to know what is a variable, a constant, the types of data, the basic control structures, the algorithms, and much more ...
I would like to add: You also need a compiler which help you to improve and is "your friend" and don't let you make silly mistakes or screw every thing up because of a wrong -type assignment.


I recommend learning C in order to get a clear understanding of memory management. Then you can make your own simple classes that manage their own memory and that takes away 99% of the hassle. Compile it of course as C++. But you're still down in the weeds. You'll get an appreciation of what happens in constructors and destructors. Then you can jump around between C++ and Free Pascal and see what you like and etc. I would also stay away from Python - you'll just pick up so much mush that you'll have to unlearn later IMHO.
Besides, C has the most shitty syntax you can find among the popular languages.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: winni on September 26, 2019, 01:44:22 am
Yes, garlar27 - it is a shitty syntax. So many useless comments in curly bracketts ........
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: jamie on September 26, 2019, 01:54:50 am
It can't be that bad since fpc has adopted some of it.

+=,. -= *= etc..

 I kind of wish it would adopt a few other things from C but you can't have it all I guess.

 Like for example a compound assignment and Boolean test..

Code: Pascal  [Select]
  1.  If (SomeVar := AFunction(ParamsIfNeede)) = ? then
  2.  
An Assignment and Testing the results in a short line.
See the beauty in that  :D

Also I wish for a C escape string on the fly..

 If the string starts with " then it must finalize in a " and this can become a C style string where C escape code can be used etc..

 Oh well, Guess I better stop dreaming..
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: del on September 26, 2019, 01:59:01 am
I would like to add: You also need a compiler which help you to improve and is "your friend" and don't let you make silly mistakes or screw every thing up because of a wrong -type assignment.

Besides, C has the most shitty syntax you can find among the popular languages.

Well ... I can't complain about Pascal. It spoiled me with its nested functions. So I use lambdas to get the same effect in C++. However, it would be nice if Pascal and its compilers enforced case sensitivity with variable, function, and type names. Code can look pretty ratty if the same entity shows up in a bazillion different forms, and the loose namespace can surprisingly take you to all kinds of unanticipated places.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: jamie on September 26, 2019, 02:02:55 am
Oh please don't do that, that is one of the things that takes me hours to debug having same names with different cases...

 If I make a mistake with a Case in Pascal I don't need to worry bout it not compiling or it calling the wrong identifier.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Thaddy on September 26, 2019, 08:27:50 am
I fully agree.  Case insensitivity is one very productive feature of Pascal over C/C++ and other curly bracket languages.
+10
Code: C  [Select]
  1. /* wanna get nuts?  valid code... */
  2. int ieee;
  3. int Ieee;
  4. int ieeE;
  5. int iEee;
  6. int IEEe;
  7. /* even... */
  8. int INT;
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: PascalDragon on September 26, 2019, 09:02:50 am
(Though just to stay on topic, I still say the C librarys in FreePascal would be rather cool / handy and did get a little excited about it when I had the idea. >.>
...Also, so far, 6hrs till I can get towed. OMG x.x)
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. And once he has some understanding of the basics he can find libraries himself if needed.

I recommend learning C in order to get a clear understanding of memory management. Then you can make your own simple classes that manage their own memory and that takes away 99% of the hassle. Compile it of course as C++. But you're still down in the weeds. You'll get an appreciation of what happens in constructors and destructors. Then you can jump around between C++ and Free Pascal and see what you like and etc. I would also stay away from Python - you'll just pick up so much mush that you'll have to unlearn later IMHO.
The same basics regarding memory management can be learned with FPC as well.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: RayoGlauco on September 26, 2019, 11:06:59 am
Well ... I can't complain about Pascal. It spoiled me with its nested functions. So I use lambdas to get the same effect in C++. However, it would be nice if Pascal and its compilers enforced case sensitivity with variable, function, and type names. Code can look pretty ratty if the same entity shows up in a bazillion different forms, and the loose namespace can surprisingly take you to all kinds of unanticipated places.

Visual Basic editor has a feature I do like: when you type an identifier, it gets converted to the case of its declaration. I think this may be good for the Lazarus editor (i.e.: if you declare MyVariable:integer and then you write MYVARIABLE := 2; then it is automatically changed to MyVariable := 2; .
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: howardpc on September 26, 2019, 02:09:54 pm
The Lazarus editor already has this capability.
If you declare
Code: Pascal  [Select]
  1. MyVar: Integer;
and then type myv followed by Ctrl-Space, the name is filled in with the capitalisation from its declaration.
It is not (yet) automatic, i.e. it does need you to press Ctrl-Space.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: garlar27 on September 26, 2019, 05:45:01 pm
It can't be that bad since fpc has adopted some of it.

+=,. -= *= etc..

 I kind of wish it would adopt a few other things from C but you can't have it all I guess.

 Like for example a compound assignment and Boolean test..

Code: Pascal  [Select]
  1.  If (SomeVar := AFunction(ParamsIfNeede)) = ? then
  2.  
An Assignment and Testing the results in a short line.
See the beauty in that  :D
It looks awful for me...
That's one of the many problems with C syntax

It may be "nice" when coding but not when debugging or during maintenance...
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: marcov on September 26, 2019, 05:54:35 pm
It can't be that bad since fpc has adopted some of it.

+=,. -= *= etc..

FPC hasn't adopted those. They are only supported for conversion purposes, and were never intended to be used in newly written pascal code.
 
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Thaddy on September 26, 2019, 05:56:36 pm
Question marks are an integral part of the C language,,,,, This is legal:
Code: C  [Select]
  1. const int n = (x != 0) ? 10 : 20;
Does it have logic? (note const..)
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Thaddy on September 26, 2019, 05:59:33 pm
It can't be that bad since fpc has adopted some of it.

+=,. -= *= etc..

FPC hasn't adopted those. They are only supported for conversion purposes, and were never intended to be used in newly written pascal code.
To that I agree, but why never marked such use as deprecated... I once asked for =+ (first assign, then increment =++ as opposed to ++= )etc... which was refused. I think {$coperators on} should be - well - deprecated.
They are not complete and misleading.
On occasion I also misuse the feature, though.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: howardpc on September 26, 2019, 06:14:07 pm
It can't be that bad since fpc has adopted some of it.

+=,. -= *= etc..

FPC hasn't adopted those. They are only supported for conversion purposes, and were never intended to be used in newly written pascal code.
If that is really the case, how do you account for the 33,403 occurrences of += counted today in a trawl of recent FPC sources (never mind use of Cisms in the LCL)?
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: winni on September 26, 2019, 07:15:13 pm
The very most of the  33,403 occurrences of += are in the makefiles. This is not Pascal. Did you know???? And some are in some quick-and-dirty testfiles.

That is the truth.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Thaddy on September 26, 2019, 07:22:54 pm
Of course he knows.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: howardpc on September 26, 2019, 07:57:41 pm
The very most of the  33,403 occurrences of += are in the makefiles. This is not Pascal. Did you know???? And some are in some quick-and-dirty testfiles.

That is the truth.
The 33,403 figure comes from counting "+=" in *.pas, *.inc, *.pp in directories under fpcsrc.
Are any of those makefiles?
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: winni on September 26, 2019, 08:07:29 pm
Yes - do you think I'm dump? From a lot of subdirectories which all contain makefiles.

Do you want to see the whole output of grep? I think it does not fit in the 250k limit for attachments.

I can't stand those Donald-Trump-Boris-Johnson-News!

Winni
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: winni on September 26, 2019, 08:35:35 pm
Here is the file against the fake news.

What I did:

* A recursive grep start at fpcsrc/3.0.4/
* Let grep count the occurances per file
* A numeric sort on the second field: the count
* wrote it to a file
* manually deleted all files with count 0

Now it's only 28k and is attached.

Winni

Oops - as I just saw: I included all binaries files. Lazy me.

Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: del on September 26, 2019, 08:49:11 pm
Question marks are an integral part of the C language,,,,, This is legal:
Code: C  [Select]
  1. const int n = (x != 0) ? 10 : 20;
Does it have logic? (note const..)

What you see is what you get.

Quote
batch.cpp: In function ‘bool BatchScale(CppOpsFrame*, const string&, const wxArrayString&, wxGauge*, DispPan*)’:
batch.cpp:148:29: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized]
 
 Compilation finished successfully.

x -226405976
n 10

And don't even THINK about changing that "n".

Quote
batch.cpp: In function ‘bool BatchScale(CppOpsFrame*, const string&, const wxArrayString&, wxGauge*, DispPan*)’:
batch.cpp:148:7: error: assignment of read-only variable ‘n’
  n += 5;
       ^
 Compilation failed.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Thaddy on September 26, 2019, 09:16:31 pm
N would be not n. My first example shows that, and NO Visual Studio will not help as somebody wrongly suggested.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: howardpc on September 26, 2019, 09:58:49 pm
@winni
I think we are talking about apples and oranges.
The figure I quoted arises from counting += in only 3 kinds of fpcsrc files: .pas, .inc, .pp
You are counting in files such as .fpc, .fpcmake, makefile*, .msg, .dat, .js, .ppu, .it, .png, .tga, .res ,.pdf and others too it seems.

These are all beside the point. My point is solely about Pascal source files in the fpc source tree, which abound in their use of +=.
I am not trying to promote the use of Cisms. I merely point out that they were introduced by an FPC developer who clearly convinced the rest of the core team of the value of this abbreviated syntax as an extension to traditional Pascal.
And either he or other developers make free use of the syntax now it is there.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: winni on September 26, 2019, 10:07:30 pm
No, we are  not  talking about apple and oranges. We are talking about the ability of counting to ten.

Look at my list - and then count all your 3 pascal files. What will be left? A little of nothing. Your 30.000 and something occurances of += derive all from the makefiles.

I'll make you a list of the occurances in the *.pas,*pp and *.inc file. Small file. Will be back soon.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: del on September 26, 2019, 10:15:16 pm
N would be not n. My first example shows that, and NO Visual Studio will not help as somebody wrongly suggested.
OK. To answer your question: Yes. It does have logic. The compiler did as you expected it would do. It even gave you the courtesy of a warning. You're conditionally initializing a constant. The slop comes in when parts of the condition are not initialized and left to chance. I would not want Visual Studio or anything else to "help" me other than give the warning. That's the problem with Python - too much "help", and Heaven forbid if one indent slips and blows up pages of code.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: winni on September 26, 2019, 10:49:38 pm
@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!
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: howardpc on September 27, 2019, 12:16:00 am
I take your points about files containing += in string literals and comments, which should not be counted, and one or two files skewing the overall total of raw += instances.
Looking in detail at the results and removing comment and literal cases, I now count a total of 34 files containing a total of 263  "real" uses of +=,
along with the three "wild" tests/webtbs files which between them house a further 33,074 instances of +=. So use of += is not as widespread in the sources as it first appeared to me.

The "wild" test files do show the usefulness of the syntax in some circumstances, though. Using traditional syntax would inflate those test files considerably, perhaps enough to adversely affect download time of the svn tree.
My search result file, omitting the 33,074 instances (which make the file too large to attach here) is attached.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: winni on September 27, 2019, 12:33:56 am
So you get the same small result as me: 2 pages.

Then I take my 61 files and compare it with whole fpsrc directory tree. In my case I find 15943 files.

61 / 15943  results in 0,38 %.  Engineers call this noise.

So we can call this "C Noise" and turn the page.

Winni
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: del on September 27, 2019, 01:23:43 am
Wow. Don't mean to jack the thread (too late, it's jacked) but I use *= a lot more (39 times) than += (0 times) on this project I'm working on. I'm quoting my config options below. This all originated in some Delphi stuff I did a bazillion years ago. I didn't deliberately enable the C Style operators (I love them) but I get to use them.

So what is it in my configuration file that's enabling +=, -=, *=, and /=? There are a couple of places in the project option pages where you can click on a checkbox to enable these operators. One the "Parsing" page it's checked (-Sc, {$COPERATORS ON}). Then on the "Custom Operations" page the -Sc box is unchecked. I think we're already more than pregnant with this capability. Could we not just call it good and give it our blessing?

Quote
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
  <CompilerOptions>
    <Version Value="11"/>
    <PathDelim Value="\"/>
    <SearchPaths>
      <IncludeFiles Value="$(ProjOutDir)"/>
    </SearchPaths>
    <Parsing>
      <SyntaxOptions>
        <SyntaxMode Value="Delphi"/>
      </SyntaxOptions>
    </Parsing>
    <CodeGeneration>
      <SmartLinkUnit Value="True"/>
      <TargetOS Value="linux"/>
      <Optimizations>
        <OptimizationLevel Value="3"/>
      </Optimizations>
    </CodeGeneration>
    <Linking>
      <Debugging>
        <GenerateDebugInfo Value="False"/>
      </Debugging>
      <LinkSmart Value="True"/>
    </Linking>
    <Other>
      <ConfigFile>
        <StopAfterErrCount Value="4"/>
      </ConfigFile>
      <CompilerMessages>
        <IgnoredMessages idx5058="True" idx5057="True" idx5037="True" idx5036="True"/>
      </CompilerMessages>
      <CustomOptions Value="-dBorland -dVer150 -dDelphi7 -dCompiler6_Up -dPUREPASCAL"/>
    </Other>
  </CompilerOptions>
</CONFIG>
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: winni on September 27, 2019, 01:51:38 am
No. I remind that there are so simple things like inc(), dec() and (sqr().
But if we want to continue to discuss how to foozle the pascal language:
I prefer Label and Goto. One can make a lot of nonsense with it.
And it brings back that good old 80s Basic feeling. Gosub nowhere ....

Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: del on September 27, 2019, 07:57:01 am
Thank you. I was hoping for something more "dry" and more narrowly addressed to the specifics of my question.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: RayoGlauco on September 27, 2019, 09:12:50 am
Many years ago, I discovered C and its abbreviated expressions, as is that    const int n = (x! = 0)? 10: 20;    that appeared in a previous post. I liked it, because you can do many things using a few text. But now, I look at this expression, and I need some time to understand what it means. Not good for debugging.

In this regard, I remember that when I was studying at the university, I saw an announcement of a "dirty programming in C" contest. It was about making an absolutely unintelligible C program that did some useful task.  :o
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Handoko on September 27, 2019, 10:31:26 am
Pascal encourages good programming practices, their source codes usually are relatively easy to read and debug. C/C++ are great too. Unfortunately their users 'prefer' to type less and use more symbolic characters. And they are proud of it.

The world's smallest chess program written in C:
https://en.wikipedia.org/wiki/International_Obfuscated_C_Code_Contest#Toledo_Nanochess (https://en.wikipedia.org/wiki/International_Obfuscated_C_Code_Contest#Toledo_Nanochess)

That's why I prefer Pascal.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: marcov on September 27, 2019, 10:39:10 am
It can't be that bad since fpc has adopted some of it.

+=,. -= *= etc..

FPC hasn't adopted those. They are only supported for conversion purposes, and were never intended to be used in newly written pascal code.
If that is really the case, how do you account for the 33,403 occurrences of += counted today in a trawl of recent FPC sources (never mind use of Cisms in the LCL)?

A very good reason to not implement more such abominations on "narrow use" arguments and be very careful with such language features in the first place. If the cat is out of the bag, things intended for rare, specific cases will suddenly be mainstream.

Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: del on September 27, 2019, 11:29:18 am
OK philosophy is fine, but:


Just sayin'.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: marcov 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)

Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: del on September 27, 2019, 12:18:32 pm
Excellent. Answered.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: marcov 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.

Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: PascalDragon 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. :-[
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Thaddy 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!
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: del 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.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: PascalDragon on October 01, 2019, 09:13:14 am
Ergo: you already done your job!
Thanks for checking :D
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Thaddy 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.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: avra 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
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: winni on October 04, 2019, 01:27:19 am
@avra

Sounds good! I will give it a try.

Thanx

Winni
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: avra 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.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Firewrath 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.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Thaddy 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.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: 440bx 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.)

Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Thaddy on October 10, 2019, 09:43:30 am
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.
I completely agree, but I hope your "basic" stuff is written with a pinch of salt... - know it is -
E.g. said Erik van Bilsen - that wrote Chet - wrote complete video codecs in Pascal! and wrote a series in The Delphi Magazine on that. I have an MP3 codec fully implemented in Pascal (not by me alone!). And well written, easy to use data abstractions are nay absent in C and plentiful in Object Pascal.  Those are not "really" basic... The power of C is that it can do basic stuff really good... :D .... If you have time on hand...
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: 440bx on October 10, 2019, 10:17:51 am
I hope your "basic" stuff is written with a pinch of salt...
Yes.  Even now, I'm not sure how to characterize the difference between the libraries available in Pascal and C.  Because there are many more written in C, there are some very specialized ones (e.g, "not basic") not available in Pascal.  That said, there are some very specialized libraries written in Pascal, just not as many.

Put another way, hopefully a bit more accurate, the probability (keyword: probability) of finding a very specialized library written in C is higher than finding it written in Pascal.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: marcov on October 10, 2019, 11:04:17 am
Put another way, hopefully a bit more accurate, the probability (keyword: probability) of finding a very specialized library written in C is higher than finding it written in Pascal.

The probability that something works directly, specially on windows, however is much lower. If I could only could get back the days spent on cygwn/mingw installations, autoconf/make and configure scripts, and debugging makefiles........

Which is why these kind of discussions are ridiculous anyway. They overfocus on language and ignore the rest. But in the rest quite often there are problems too, and maybe even bigger ones. (like C code being *nix only etc)
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: 440bx on October 10, 2019, 11:55:48 am
They overfocus on language and ignore the rest. But in the rest quite often there are problems too, and maybe even bigger ones. (like C code being *nix only etc)
I agree.  Even if the mythical, always perfect, C to Pascal translator existed, such a translator would know the languages but, without the translator taking into consideration the environment/OS the program will run on, it is rather unlikely to produce a perfectly functioning program and,  that doesn't even take into account that C compilers have a number of "implementation defined" behaviors.

That's why I consider translators of one kind or another simply "saves typing" tools, because the programmer has to understand what is at the starting and ending points.  Without that understanding, the result is rather unlikely to be desirable regardless of how or what did the translation/porting.


A suggestion to the OP, focus on learning programming first, Pascal is an excellent choice for that - maybe even the best - worry about libraries later.

Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: marcov on October 10, 2019, 12:00:29 pm
I agree.  Even if the mythical, always perfect, C to Pascal translator existed,

It will never exist, since macro expressions are partially defined by usage.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Thaddy on October 10, 2019, 12:52:17 pm
It will never exist, since macro expressions are partially defined by usage.
macro's can be expanded. cpp -E will do that for you, generating huge files, but with proper judgement the expanded macro's can be copied out.
But I agree that still needs (quite a lot, sometimes, manual labor)
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: marcov on October 10, 2019, 01:33:37 pm
It will never exist, since macro expressions are partially defined by usage.
macro's can be expanded. cpp -E will do that for you, generating huge files, but with proper judgement the expanded macro's can be copied out.

But more important, that means the resulting program is no longer independent of the headers. Which usually means it is useless.
 
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Thaddy on October 10, 2019, 04:47:09 pm
But more important, that means the resulting program is no longer independent of the headers. Which usually means it is useless.
???? I use that for umpteen years: expanded macro's are easier to make into Pascal functions.
For which h2pas creates a prototype...
The advantage is NOT that you can automate it, but you can cut the crap: it makes you see the trees from the wood.
(KHRONOS macro's come to mind as a good anti pattern)
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: del on October 10, 2019, 06:58:28 pm
I wouldn't recommend that a newby learn C cuz it's not object oriented. Learn the Free Pascal. If you need small scale stuff, translate it. If you need ffmpeg then you're probably gonna have to get your feet wet with C++. But Free Pascal will give you as much, if not more, of a head start in that direction than would the humbly procedural C. IMHO.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Akira1364 on October 10, 2019, 07:22:44 pm
C definitely does not have particularly "good" libraries, IMO, due to the rather limited overall nature of the language.

C++ (which makes far more sense as a language to compare to Pascal as FPC currently implements it) on the other hand, sure.

As far as the question mark operator people were talking about earlier, it literally just does the same thing as "IfThen".

So this code:

Code: C  [Select]
  1. const int n = (x != 0) ? 10 : 20;

is basically equivalent to:

Code: Pascal  [Select]
  1. n := ifthen(x <> 0, 10, 20);

The one advantage of the C version is that it can be used in constant expressions since it's part of the language and not a function, while obviously "IfThen" can't.

That said, the compiler patch by Ryan Joseph for generic constants which is currently awaiting review does make stuff like that possible, which is one of the reasons I'm particularly looking forward to the feature.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: 440bx on October 10, 2019, 07:28:43 pm
I wouldn't recommend that a newby learn C cuz it's not object oriented.
It's quite likely no one will agree with this but, just to let the OP know that not everyone is a "passenger" on the OOP bandwagon, I would recommend staying away from OOP. 

As I recommended in the first post I made in this thread, get the book "Oh Pascal" by Doug Cooper, no OOP in there and, for someone interested in seeing what a program is supposed to be, get Per Brinch Hansen's "On Pascal Compilers", a beginner will probably have to read it several times before being able to fully appreciate it but, the effort is really worth it.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: del on October 10, 2019, 07:45:11 pm
C definitely does not have particularly "good" libraries, IMO, due to the rather limited overall nature of the language.
I just used one this morning for geographical transformations. Love those "structs". It's like Fortran - there's stuff out there. They didn't throw it all away.
 :D
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: PascalDragon on October 11, 2019, 09:23:28 am
C definitely does not have particularly "good" libraries, IMO, due to the rather limited overall nature of the language.

C++ (which makes far more sense as a language to compare to Pascal as FPC currently implements it) on the other hand, sure.

As far as the question mark operator people were talking about earlier, it literally just does the same thing as "IfThen".

So this code:

Code: C  [Select]
  1. const int n = (x != 0) ? 10 : 20;

is basically equivalent to:

Code: Pascal  [Select]
  1. n := ifthen(x <> 0, 10, 20);

The one advantage of the C version is that it can be used in constant expressions since it's part of the language and not a function, while obviously "IfThen" can't.

That said, the compiler patch by Ryan Joseph for generic constants which is currently awaiting review does make stuff like that possible, which is one of the reasons I'm particularly looking forward to the feature.
Even then there is a difference: the ternary operator works like an if-expression that only the part taken is evaluated. The IfThen function itself will never support that. Nor will it any function that can be written in Pascal. This needs a compiler provided functionality (like the IfThen intrinsic that I had added a year or two back, but had to remove again after other devs complained).
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Akira1364 on October 11, 2019, 08:04:56 pm
This needs a compiler provided functionality (like the IfThen intrinsic that I had added a year or two back, but had to remove again after other devs complained).

Not sure I remember that. How did it work exactly? And what were the complaints?

Would it have allowed something similar to this, with constant record members, if combined with Ryan's generic constants patch?

Code: C  [Select]
  1. #include <iostream>
  2.  
  3. template <const auto A, const auto B>
  4. struct Min
  5. {
  6.   static constexpr auto Result = (A < B) ? A : B;
  7. };
  8.  
  9. int main()
  10. {
  11.   std::cout << Min<-9, 5>::Result;
  12. }

Seems like it would have been quite useful if so.

The fact that you can't really express that many things as compile-time constants is basically the one aspect of other languages that I find myself missing in Pascal.

Edit: Just went back and found the mailing list thread... it seems like the intrinsic was basically just an objectively better version of the normal function version?

There's various things you cannot use the normal function version for at all, like this:

Code: Pascal  [Select]
  1. program Example;
  2.  
  3. {$mode Delphi}
  4.  
  5. uses Classes, SysUtils;
  6.  
  7. const Value = (2 * 3) div 3;
  8.  
  9. begin
  10.   // ↓↓↓ Creates a TObject no matter what, and thus a memory leak!
  11.   // ↓↓↓ Why would anyone *want* it to work like that if it could be avoided with the intrinsic version?
  12.   with IfThen<TObject>(Value = 2, TPersistent.Create, TObject.Create) do begin
  13.     WriteLn(ClassName);
  14.     Free;
  15.   end;
  16. end.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: marcov on October 11, 2019, 08:27:57 pm
But more important, that means the resulting program is no longer independent of the headers. Which usually means it is useless.
???? I use that for umpteen years: expanded macro's are easier to make into Pascal functions.

Yes, but if the macro's are ifdefed (e.g. for differing targets) it won't work. And the times that I tried that was usually the case.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: PascalDragon on October 14, 2019, 09:46:20 am
This needs a compiler provided functionality (like the IfThen intrinsic that I had added a year or two back, but had to remove again after other devs complained).

Not sure I remember that. How did it work exactly? And what were the complaints?

Would it have allowed something similar to this, with constant record members, if combined with Ryan's generic constants patch?

Code: C  [Select]
  1. #include <iostream>
  2.  
  3. template <const auto A, const auto B>
  4. struct Min
  5. {
  6.   static constexpr auto Result = (A < B) ? A : B;
  7. };
  8.  
  9. int main()
  10. {
  11.   std::cout << Min<-9, 5>::Result;
  12. }

Seems like it would have been quite useful if so.

The fact that you can't really express that many things as compile-time constants is basically the one aspect of other languages that I find myself missing in Pascal.

Edit: Just went back and found the mailing list thread... it seems like the intrinsic was basically just an objectively better version of the normal function version?

There's various things you cannot use the normal function version for at all, like this:

Code: Pascal  [Select]
  1. program Example;
  2.  
  3. {$mode Delphi}
  4.  
  5. uses Classes, SysUtils;
  6.  
  7. const Value = (2 * 3) div 3;
  8.  
  9. begin
  10.   // ↓↓↓ Creates a TObject no matter what, and thus a memory leak!
  11.   // ↓↓↓ Why would anyone *want* it to work like that if it could be avoided with the intrinsic version?
  12.   with IfThen<TObject>(Value = 2, TPersistent.Create, TObject.Create) do begin
  13.     WriteLn(ClassName);
  14.     Free;
  15.   end;
  16. end.

The generic IfThen function came after this discussion.

And the intrinsic would have determined the result type of the IfThen expression based on the result of the if-clause. Same as the ternary operator does in C.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Thaddy on October 14, 2019, 11:23:52 am
Yes! (I wrote it..)
You can also have a specialization to TFunction, btw, so it can be used for code paths. (The docs for 3.2 contains an example)
The auto part is also covered partially by using smart pointers from other contributors (it is not in the rtl, but there are contributions)
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Akira1364 on October 17, 2019, 09:34:54 pm
This needs a compiler provided functionality (like the IfThen intrinsic that I had added a year or two back, but had to remove again after other devs complained).

Not sure I remember that. How did it work exactly? And what were the complaints?

Would it have allowed something similar to this, with constant record members, if combined with Ryan's generic constants patch?

Code: C  [Select]
  1. #include <iostream>
  2.  
  3. template <const auto A, const auto B>
  4. struct Min
  5. {
  6.   static constexpr auto Result = (A < B) ? A : B;
  7. };
  8.  
  9. int main()
  10. {
  11.   std::cout << Min<-9, 5>::Result;
  12. }

Seems like it would have been quite useful if so.

The fact that you can't really express that many things as compile-time constants is basically the one aspect of other languages that I find myself missing in Pascal.

Edit: Just went back and found the mailing list thread... it seems like the intrinsic was basically just an objectively better version of the normal function version?

There's various things you cannot use the normal function version for at all, like this:

Code: Pascal  [Select]
  1. program Example;
  2.  
  3. {$mode Delphi}
  4.  
  5. uses Classes, SysUtils;
  6.  
  7. const Value = (2 * 3) div 3;
  8.  
  9. begin
  10.   // ↓↓↓ Creates a TObject no matter what, and thus a memory leak!
  11.   // ↓↓↓ Why would anyone *want* it to work like that if it could be avoided with the intrinsic version?
  12.   with IfThen<TObject>(Value = 2, TPersistent.Create, TObject.Create) do begin
  13.     WriteLn(ClassName);
  14.     Free;
  15.   end;
  16. end.

The generic IfThen function came after this discussion.

And the intrinsic would have determined the result type of the IfThen expression based on the result of the if-clause. Same as the ternary operator does in C.

I found another patch you made around that time that implements it as a language construct along the lines of "variable := if boolean then a() else b()", and found that it still works fine with trunk FPC and indeed allows stuff not possible with IfThen as it exists now.

What was wrong with that version, exactly? Doesn't it solve the problems people had with the "function-like" implementation?
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: PascalDragon on October 18, 2019, 09:44:28 am
I found another patch you made around that time that implements it as a language construct along the lines of "variable := if boolean then a() else b()", and found that it still works fine with trunk FPC and indeed allows stuff not possible with IfThen as it exists now.

What was wrong with that version, exactly? Doesn't it solve the problems people had with the "function-like" implementation?
There were core devs that disliked the use of the if-statement as an expression. :-X
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: marcov on October 18, 2019, 11:21:25 am
What was wrong with that version, exactly? Doesn't it solve the problems people had with the "function-like" implementation?

Readability, more difficult to the point error generation (something that C/C++ obviously doesn't care about)
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: PascalDragon on October 19, 2019, 01:25:38 pm
What was wrong with that version, exactly? Doesn't it solve the problems people had with the "function-like" implementation?

Readability, more difficult to the point error generation (something that C/C++ obviously doesn't care about)
What do you mean with "point error generation"?
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Firewrath on October 24, 2019, 12:22:48 am
Not to step on the other interesting discussions going on in this thread, but...
I just want to say I never thought C was in anyways better then Pascal. I have been 'playing' at programming off and on for years and Lazarus/FreePascal has been the easiest language for me to grasp and the one I've done the most with, ...though it has so far just been a bunch of stuff for my personal use. :P

440bx hit the nail on the head, so to speak, as to why I was asking about converting C:
It is a fact that there are many more libraries written for one thing or another in C than in Pascal.
IE: more stuff to play with.
And I know people have done a lot of cool stuff with Pascal and there's a lot of packages in the package manager I can mess with, but my home PC, where I do my programming, lacks internet access, sooo.... yeah. :P


Other then that, I think my mistake was that I kept reading that Pascal is "C based" and thinking that conversion should be ...easier then it is.
So after this thread, I get it now, "C like" instead of "C based".  :D

Also...
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.

Actually, I think I'm happy enough as a hobbyist programmer, I doubt I'll ever get the skills to be really good at it, muchless doing it for a job, but I'm good enough that it serves my purposes.
So Lazarus/FreePascal is great for that. ^-^

My friend though, I really don't know how far he wants to take it. I'll hopefully find out soon enough, seems I've been missing him the past few weeks with everything going on. He wasn't going to start learning till Dec. when he's taking time off work. So there's time to find out.

But Thanks for all the advice / opinions, plus all the other stuff, it's been helpful and interesting to read. ^-^
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: del on October 25, 2019, 05:29:50 am
The big draw IMHO is NOT Pascal (meh) but Lazarus. Lazarus is the kind of experience you want to have developing on Linux. However - the price you pay for this experience (and its dependency on Pascal) is that you eventually run into brick walls. And as impressive as the BGRABitmapPak is, there are times when you need more -

https://opencv.org/

Your options are to either acquaint yourself with C++, or one of the two languages that wrap it: Python or Java. IIRC Delphi made an attempt to wrap some of the old C based OpenCV stuff. But that's all majorly deprecated.  So if Python and Java can wrap OpenCV, why can't Pascal?


But anything is possible in the mysterious world of open source.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Thaddy on October 25, 2019, 06:31:02 am
There are multiple opencv bindings available. Which one are you referring to?
https://github.com/Laex/Delphi-OpenCV is the most prominent but there is also https://cviptools.siue.edu/downloads.php which has a COM interface layer that is accessible from FPC. The latter is based on opencv., afaik. I also saw some others.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: 440bx on October 25, 2019, 07:34:58 am
Lazarus is the kind of experience you want to have developing on Linux. However - the price you pay for this experience (and its dependency on Pascal) is that you eventually run into brick walls.
A RAD environment isn't a magic lamp you click that makes a genie appear to write the code for you (and even if it were, legend goes you only get three (3) lines of code.)  In programming, walls aren't made of bricks, they are made of ignorance bound together by sticky lack of effort.

Your options are to either acquaint yourself with C++, or one of the two languages that wrap it: Python or Java.
The other option is, learn Pascal really well.  You can write Python and Java in Free Pascal (provided you know the language.)

why can't Pascal?

  • Small community.
  • Tiny subset of small community that knows C++.
  • Tiny subset of tiny subset of small community that is actually incentivized to explore this.
You forgot the most important reason and that is, because there are people who _expect_ other people to do the work for them instead of doing it themselves.  If you find the current binding to OpenCV lacking in some ways, you could make them everything they should be (in your opinion.)  The obvious problems are, 1. it requires work on your part and 2. it also requires you to learn Pascal well enough to do it.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: del on October 25, 2019, 10:26:17 am
There are multiple opencv bindings available. Which one are you referring to?
https://github.com/Laex/Delphi-OpenCV is the most prominent but there is also https://cviptools.siue.edu/downloads.php which has a COM interface layer that is accessible from FPC. The latter is based on opencv., afaik. I also saw some others.
IplImage is the old deprecated C interface. And we know how much you guys love C. Not that you like C++ any better, but those who do will benefit from virtually care-free memory management and the latest and greatest features / functionality that the OpenCV library has to offer.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: del on October 25, 2019, 10:34:26 am
Lazarus is the kind of experience you want to have developing on Linux. However - the price you pay for this experience (and its dependency on Pascal) is that you eventually run into brick walls.
A RAD environment isn't a magic lamp you click that makes a genie appear to write the code for you (and even if it were, legend goes you only get three (3) lines of code.)  In programming, walls aren't made of bricks, they are made of ignorance bound together by sticky lack of effort.

Your options are to either acquaint yourself with C++, or one of the two languages that wrap it: Python or Java.
The other option is, learn Pascal really well.  You can write Python and Java in Free Pascal (provided you know the language.)

why can't Pascal?

  • Small community.
  • Tiny subset of small community that knows C++.
  • Tiny subset of tiny subset of small community that is actually incentivized to explore this.
You forgot the most important reason and that is, because there are people who _expect_ other people to do the work for them instead of doing it themselves.  If you find the current binding to OpenCV lacking in some ways, you could make them everything they should be (in your opinion.)  The obvious problems are, 1. it requires work on your part and 2. it also requires you to learn Pascal well enough to do it.

I get it. Lazarus is great, and if you want OpenCV (modern) you gotta role your own. Not a big shock - cuz I pretty much said the same thing. We agree to agree. Forget Python (a necessary evil where I work), and Java (never had any use for it, and probably never will).
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: marcov on October 25, 2019, 10:36:16 am
What do you mean with "point error generation"?

I said "to the point error generation".   If you have an in-expression if, the whole errorhandling situation changes. The IF might suddenly associate with the next begin end block etc.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: marcov on October 25, 2019, 10:38:02 am
Your options are to either acquaint yourself with C++, or one of the two languages that wrap it: Python or Java. IIRC Delphi made an attempt to wrap some of the old C based OpenCV stuff. But that's all majorly deprecated.  So if Python and Java can wrap OpenCV, why can't Pascal?

I do fulltime vision in Delphi, and I simply rarely need OpenCV (in 2010 iirc I looked at the KLT stuff, but that is about it).  Too generic (often first makes some extra datastructure like path vectors before doing the actual operation) , too slow specially for the base algorithms. Anything but base generally is too slow anyway.

It felt like targeted at e.g. PHD students to quickly get something done
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: del on October 25, 2019, 10:58:28 am
Your options are to either acquaint yourself with C++, or one of the two languages that wrap it: Python or Java. IIRC Delphi made an attempt to wrap some of the old C based OpenCV stuff. But that's all majorly deprecated.  So if Python and Java can wrap OpenCV, why can't Pascal?

I do fulltime vision in Delphi, and I simply rarely need OpenCV (in 2010 iirc I looked at the KLT stuff, but that is about it).  Too generic (often first makes some extra datastructure like path vectors before doing the actual operation) , too slow specially for the base algorithms. Anything but base generally is too slow anyway.

It felt like targeted at e.g. PHD students to quickly get something done
That reminds me of the old joke: "IDL was invented so Forestry Majors could code". Today we replace "IDL" with "Python". What sold me on OpenCV was stuff like optical flow and the fact that it engaged OpenCL and you could literally watch the temperatures rise on your cores and GPU's, not to mention hearing the fans rev up to deal with all the extra heat. It's just cool stuff - but of course I'm not on a crusade to push it. YMMV.
 :)
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: garlar27 on October 25, 2019, 07:46:16 pm
Code: Pascal  [Select]
  1. program Example;
  2.  
  3. {$mode Delphi}
  4.  
  5. uses Classes, SysUtils;
  6.  
  7. const Value = (2 * 3) div 3;
  8.  
  9. begin
  10.   // ↓↓↓ Creates a TObject no matter what, and thus a memory leak!
  11.   // ↓↓↓ Why would anyone *want* it to work like that if it could be avoided with the intrinsic version?
  12.   with IfThen<TObject>(Value = 2, TPersistent.Create, TObject.Create) do begin
  13.     WriteLn(ClassName);
  14.     Free;
  15.   end;
  16. end.
Maybe is a little late but you shouldn't use IfThen that way because both constructors are executed before entering the function. Thus you have two objects instantiated before executing the function's code.

for instance this code will generate an AV getting AnEditor.Name:
Code: Pascal  [Select]
  1. procedure TForm1.SomeAction;
  2. var
  3.    AnEditor: TEdit;
  4. begin
  5.    AnEditor:= nil;
  6.    Caption := 'Editor Name: '+ IfThen(Assigned(AnEditor), AnEditor.Name, 'nil');
  7. end;
  8.  
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Akira1364 on October 25, 2019, 10:19:34 pm
Maybe is a little late but you shouldn't use IfThen that way because both constructors are executed before entering the function. Thus you have two objects instantiated before executing the function's code.

That was the whole point of my post. I was contrasting it with the intrinsic version, which apparently did not work that way.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: Thaddy on October 26, 2019, 09:19:19 am
Furthermore, although you create the objects you have no reference for them: , parameter names are not references. They take an existing reference. That example creates objects that have no reference elsewhere, so you can not even free them...
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: PascalDragon on October 26, 2019, 10:58:34 am
What do you mean with "point error generation"?

I said "to the point error generation".   If you have an in-expression if, the whole errorhandling situation changes. The IF might suddenly associate with the next begin end block etc.
In case of the if-expression that would not be the case, because an expression is either separated from the next one by a semicolon or is followed by an end, thus there would be no possibility to associate it with a begin. Also that expression could not contain beginend-blocks as those are not expressions.

What is indeed problematic (but that's a general problem of the ternary operators) is how "greedy" it is (especially the else-branch):
Code: Pascal  [Select]
  1.   a := if y > 4 then y else z + 4;
  2.   // can be evaluated either as
  3.   if y > 4 then
  4.     a := y
  5.   else
  6.     a := z + 4;
  7.   // or
  8.   if y > 4 then
  9.     a := y
  10.   else
  11.     a := z;
  12.   a := a + 4;
  13.  

What the if-expression solves better than the ternary operator is the "reach" of the tested expression. In C(++) I always struggle with this:
Code: C  [Select]
  1. a = z + y > 4 ? y : z;
Does this test y > 4 or z + y > 4? In the end I stuff that into brackets to avoid any misconceptions:
Code: C  [Select]
  1. a = z + (y > 4) ? y : z;
  2. // or even
  3. a = z + (y > 4 ? y : z);
Using the if-keyword this potential ambiguity (potential, because the compiler follows a clear rule there; one just has to remember it) is avoided.
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: valdir.marcos on December 20, 2019, 07:56:15 am
I don't know any really working C to Pascal Transpiler. Seems to be too complicated on code level.
But I found something antique in the deepth of my harddisk:
In 1989 Derek from Knowledge Software wrote a C to Turbo Pascal Transpiler! No classes, no objects. It is a DOS program! Try it!
I don't know any really working C to Pascal Transpiler.
Try this one: https://github.com/WouterVanNifterick/C-To-Delphi
Talking about transpiler seems to be an old subject:
https://pdfs.semanticscholar.org/699c/a65ee0f28b85e1301e11ad9a59d4adac760d.pdf
Title: Re: Has anyone converted the C librarys to FreePascal?
Post by: valdir.marcos on December 20, 2019, 08:30:28 am
I don't know any really working C to Pascal Transpiler. Seems to be too complicated on code level.
But I found something antique in the deepth of my harddisk:
In 1989 Derek from Knowledge Software wrote a C to Turbo Pascal Transpiler! No classes, no objects. It is a DOS program! Try it!
I don't know any really working C to Pascal Transpiler.
Try this one: https://github.com/WouterVanNifterick/C-To-Delphi
Talking about transpiler seems to be an old subject:
https://pdfs.semanticscholar.org/699c/a65ee0f28b85e1301e11ad9a59d4adac760d.pdf
The PDF file.
A Modern Approach to Teaching Computer Translation Manuel E. Bermudez, University of Florida, Gainesville, FL, USA, 2001.

An even older book about:
"An introduction to a practical system for generating mechanical translators for programming languages"

A compiler generator Hardcover – January 1, 1970
by William M. McKeeman, James J. Horning, David B. Wortman
https://www.amazon.com/compiler-generator-William-M-McKeeman/dp/B007EUHZ2U

https://www.cs.toronto.edu/XPL/contents.html