Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FreePascal language support #376

Open
laheller opened this issue Jan 20, 2025 · 6 comments
Open

FreePascal language support #376

laheller opened this issue Jan 20, 2025 · 6 comments

Comments

@laheller
Copy link

This is not a real issue, just a question, whether there is a plan to support also the FreePascal programming language, e. g. to create an Astronomy FreePascal unit (library)?

@cosinekitty
Copy link
Owner

I am not planning on adding any more languages at this point. Adding new language support is possible, but it's a lot more work than most people realize until they try to do it. For me, it would be the equivalent of a full-time job for about 6 weeks. For a volunteer new to the project, it would take a lot longer. I would be willing to help guide someone, but usually when I explain all the things that need to be done, people decide they don't want to do it.

@laheller
Copy link
Author

I am not planning on adding any more languages at this point. Adding new language support is possible, but it's a lot more work than most people realize until they try to do it. For me, it would be the equivalent of a full-time job for about 6 weeks. For a volunteer new to the project, it would take a lot longer. I would be willing to help guide someone, but usually when I explain all the things that need to be done, people decide they don't want to do it.

OK, understand.
An optional solution could be to just create a so called wrapper code for FreePascal which simply calls the functions from DLL file compiled from the Astronomy C-language part. I already did something similar in my other project so I think I will at least try.
If anything working, will let you know.

@laheller
Copy link
Author

@cosinekitty

Seems to be working. I slightly modified the C header file astronomy.h, added:

#ifdef _WIN32
#define _PREFIX __declspec(dllexport)
#else
#define _PREFIX
#endif // _WIN32

Then for each function just added the symbol _PREFIX, example:

_PREFIX astro_time_t Astronomy_MakeTime(int year, int month, int day, int hour, int minute, double second);

This way after build in Visual Studio the result library (astronomy.dll file) exports all the functions which can be called from any other language which support external functions.

The wrapper Pascal unit is then:

unit Astronomy;

{$mode objfpc}{$H+}

interface

uses
  Classes
  { you can add units after this };

const
  LibraryName = 'astronomy'; // should refer the astronomy.dll file without extension

type
  AstroTime = packed record
    UT: Double;
    TT: Double;
    Psi: Double;
    Eps: Double;
    ST: Double;
  end;

function Astronomy_MakeTime(Year: Int32; Month: Int32; Day: Int32; Hour: Int32; Minute: Int32; Second: Double): AstroTime; cdecl; external LibraryName;

implementation

end.

To call the wrapper function from Pascal code:

program Demo;

{$mode objfpc}{$H+}

uses
  Classes, SysUtils, Astronomy;

var
  at1: AstroTime;

begin
  at1 := Astronomy_MakeTime(2025, 1, 20, 22, 20, 0.5);
  WriteLn('Astro time: ' + FloatToStr(at1.UT));
end.

It works as expected. This way we can wrap in Pascal the whole C-language functionality.

@cosinekitty
Copy link
Owner

That is really cool! I used to use Turbo Pascal back in the day, which later became Delphi. FreePascal looks very similar, right down to the {$ ...} compiler directives. I miss the elegant way modules worked, and how blazingly fast compiles were...

@laheller
Copy link
Author

@cosinekitty

The reason I use FreePascal is that it's actively developed, it's free and it supports Windows/Linux/MacOS platforms.
BTW I am working on the Astronomy FreePascal wrapper unit (module). Also some demo code. You can expect a PR soon.

@laheller
Copy link
Author

@cosinekitty

Here we go, there is a PR for FreePascal language support:
#377

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants