Recent

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

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Has anyone converted the C librarys to FreePascal?
« Reply #90 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.  

Akira1364

  • Hero Member
  • *****
  • Posts: 561
Re: Has anyone converted the C librarys to FreePascal?
« Reply #91 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.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Has anyone converted the C librarys to FreePascal?
« Reply #92 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...
« Last Edit: October 26, 2019, 09:20:58 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Has anyone converted the C librarys to FreePascal?
« Reply #93 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.

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Has anyone converted the C librarys to FreePascal?
« Reply #94 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

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Has anyone converted the C librarys to FreePascal?
« Reply #95 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

 

TinyPortal © 2005-2018