Recent

Author Topic: How to translate these macros to Pascal?  (Read 835 times)

guest65405

  • Guest
How to translate these macros to Pascal?
« on: March 29, 2020, 06:03:23 pm »
Code: C  [Select][+][-]
  1.                                                                 /* Macro returning the number of elements of an array   */
  2. #define NELEMS(a) (sizeof(a) / sizeof((a)[0]))
  3.  
  4.                                                                 /* Macro to check a pointer                                                             */
  5. #define PCHECK(p) if ((p) == NULL) SGL_Fatal(__FILE__, __LINE__, "ALLOCATION") ;
  6.  
  7.                                                                 /* Macro to check the result of a SGL function                  */
  8. #define CHKERR(m) { SGL_Error(__FILE__, __LINE__, (m)) ; }
  9.  
  10.                                                                 /* Macro to log an execution time                                               */
  11. #define GCLK(m) { double _t_t = SGL_Timer() ; m ; _t_t = SGL_Timer() - _t_t ; \
  12.                                         SGL_Log(STD, ":CLK: %-50.50s :%12.4f ms", #m, 1.e3 * _t_t) ;}

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8458
  • FPC developer.
Re: How to translate these macros to Pascal?
« Reply #1 on: March 29, 2020, 06:07:43 pm »
The first: you can't, unless you use an external macroprocessor. This was sometimes done before FPC had generics to generate type container classes automatically.

You can replace it by HIGH(A)-LOW(A)+1 but you can't turn it into a method or procedure. It might also be less universal. In many cases (but not all?) length() will also work.

The other three, should be doable with an inline function or a bunch of inline functions. The second is a reference (pointer type) parameter, the third string (since the second passes a string to SGL_Error, though it could be overloaded). The fourth performs a concatenation, also hard to do, but can be replaced with + at the cost of some runtime overhead.

Note that macro translation is not an automated process, the place of usage also contributes to the meaning of the macro. That's why translations of just the definition is guesswork.
« Last Edit: March 29, 2020, 06:30:37 pm by marcov »

jamie

  • Hero Member
  • *****
  • Posts: 3077
Re: How to translate these macros to Pascal?
« Reply #2 on: March 29, 2020, 06:09:23 pm »
The first one can be replace using the HIGH(MyArray); that returns the number of addressable elements in the array...

The rest are just functions you can write yourself, use Inline and hope for the best...  :)

SGL_FATAL can be replaced with an Exception handler...
 Raise Exception.Create(FileName+Line.tostring+'ALLOCATION');

and so on.
The only true wisdom is knowing you know nothing

ASerge

  • Hero Member
  • *****
  • Posts: 1613
Re: How to translate these macros to Pascal?
« Reply #3 on: March 29, 2020, 06:17:49 pm »
The first one can be replace using the HIGH(MyArray); that returns the number of addressable elements in the array...
You mean Length?

ASerge

  • Hero Member
  • *****
  • Posts: 1613
Re: How to translate these macros to Pascal?
« Reply #4 on: March 29, 2020, 06:22:10 pm »
Macros do not check parameters types, and are not safe. Use procedures and functions.
__FILE__ can be replaced with {$I %FILE%}
__LINE__ with {$I %LINE%}

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8458
  • FPC developer.
Re: How to translate these macros to Pascal?
« Reply #5 on: March 29, 2020, 06:30:59 pm »
I expanded my answer a bit. 

guest65405

  • Guest
Re: How to translate these macros to Pascal?
« Reply #6 on: March 29, 2020, 06:33:04 pm »
Full header file:

Code: C  [Select][+][-]
  1. #define NONE    0
  2. #define STD             1
  3. #define ERR             2
  4. #define FATAL   3
  5. #define QUIT    4
  6.  
  7. void SGL_Log(int mask, char *fmt,...) ;
  8. void SGL_Fatal(char *file, int line, char *diag) ;
  9. void SGL_Error(char *file, int line, int eresult) ;
  10.  
  11.                                                                 /* Macro returning the number of elements of an array   */
  12. #define NELEMS(a) (sizeof(a) / sizeof((a)[0]))
  13.  
  14.                                                                 /* Macro to check a pointer                                                             */
  15. #define PCHECK(p) if ((p) == NULL) SGL_Fatal(__FILE__, __LINE__, "ALLOCATION") ;
  16.  
  17.                                                                 /* Macro to check the result of a SGL function                  */
  18. #define CHKERR(m) { SGL_Error(__FILE__, __LINE__, (m)) ; }
  19.  
  20.                                                                 /* Macro to log an execution time                                               */
  21. #define GCLK(m) { double _t_t = SGL_Timer() ; m ; _t_t = SGL_Timer() - _t_t ; \
  22.                                         SGL_Log(STD, ":CLK: %-50.50s :%12.4f ms", #m, 1.e3 * _t_t) ;}

Please help. I'm creating binding for this library.

p/s: I think I could skip this header, but I don't know if other headers depend on it would break or not.

440bx

  • Hero Member
  • *****
  • Posts: 1876
Re: How to translate these macros to Pascal?
« Reply #7 on: March 29, 2020, 08:00:06 pm »
Please help. I'm creating binding for this library.

p/s: I think I could skip this header, but I don't know if other headers depend on it would break or not.
You've already gotten helpful responses but, I wanted to point something out. With the exception of the NELEMS(a), those macros will not be used in data definitions which means you don't need to translate them unless you're also porting the C code to Pascal.

It seems the only macro you need to concern yourself with is the NELEMS(a) macro and, even that one, it's more likely being used in code than in a data definition (though, it could be used in another data definition but, that case is somewhat rare.)

In a few words, correctly converting headers from C to Pascal requires a reasonable level of familiarity with C and Pascal.   Otherwise, you'll spend time converting things you don't even need in the target language (Pascal in this case.)
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

guest65405

  • Guest
Re: How to translate these macros to Pascal?
« Reply #8 on: March 31, 2020, 06:03:05 am »
Please help. I'm creating binding for this library.

p/s: I think I could skip this header, but I don't know if other headers depend on it would break or not.
You've already gotten helpful responses but, I wanted to point something out. With the exception of the NELEMS(a), those macros will not be used in data definitions which means you don't need to translate them unless you're also porting the C code to Pascal.

It seems the only macro you need to concern yourself with is the NELEMS(a) macro and, even that one, it's more likely being used in code than in a data definition (though, it could be used in another data definition but, that case is somewhat rare.)

In a few words, correctly converting headers from C to Pascal requires a reasonable level of familiarity with C and Pascal.   Otherwise, you'll spend time converting things you don't even need in the target language (Pascal in this case.)

In the original code, they do. He used them through out his library. At least it's what I think. I'm not sure, either. I will ask him. But he didn't reply to my two previous mails. The only thing I could do is waiting.

Thaddy

  • Hero Member
  • *****
  • Posts: 10187
Re: How to translate these macros to Pascal?
« Reply #9 on: March 31, 2020, 09:03:14 am »
If you have a C compiler like gnu installed, you can use cpp -E which will expand the macros, then examine the output on how those macro's are expanded to sourcecode.
That makes it easier to see what they are supposed to do.
Note the expanded output can be very large, so if possible copy out the macro's to a dummy header file and expand that with the cpp -E prepocessor command..
Note the dummy files also needs some basic C sourcecode that actual uses the macros, otherwise the more complex ones are not usable.

Type cpp --help to see what other options you might want to add.
-E means: "Preprocess only; do not compile, assemble or link." and expands macros to sourcecode. That expanded sourcecode is usually better suited to feed to h2pas and/or h2paspp

You will still need to be fairly proficient in C(++)  and the ability to liberally use copy/paste to create the dummy files.

But there remains the advantage that the dummy headers can optimally be machine generated and are usually correct too.: macros are typeless, but the expanded code is always typed and makes h2pas work better..
« Last Edit: March 31, 2020, 09:28:30 am by Thaddy »
I am more like donkey than shrek

PascalDragon

  • Hero Member
  • *****
  • Posts: 1694
  • Compiler Developer
Re: How to translate these macros to Pascal?
« Reply #10 on: March 31, 2020, 09:28:51 am »
Please help. I'm creating binding for this library.

p/s: I think I could skip this header, but I don't know if other headers depend on it would break or not.
You've already gotten helpful responses but, I wanted to point something out. With the exception of the NELEMS(a), those macros will not be used in data definitions which means you don't need to translate them unless you're also porting the C code to Pascal.

It seems the only macro you need to concern yourself with is the NELEMS(a) macro and, even that one, it's more likely being used in code than in a data definition (though, it could be used in another data definition but, that case is somewhat rare.)

In a few words, correctly converting headers from C to Pascal requires a reasonable level of familiarity with C and Pascal.   Otherwise, you'll spend time converting things you don't even need in the target language (Pascal in this case.)

In the original code, they do. He used them through out his library. At least it's what I think. I'm not sure, either. I will ask him. But he didn't reply to my two previous mails. The only thing I could do is waiting.

Using them inside the library and using them in code that uses the library is two different things. Normally libraries separate those headers into private/internal and public ones however.

guest65405

  • Guest
Re: How to translate these macros to Pascal?
« Reply #11 on: April 04, 2020, 09:42:11 am »
If you have a C compiler like gnu installed, you can use cpp -E which will expand the macros, then examine the output on how those macro's are expanded to sourcecode.

I tried. It will just omit all of the macros I wanted to translate.

guest65405

  • Guest
Re: How to translate these macros to Pascal?
« Reply #12 on: April 04, 2020, 09:43:03 am »
Using them inside the library and using them in code that uses the library is two different things. Normally libraries separate those headers into private/internal and public ones however.

Yes. He confirmed with me this debug header is not mandatory.

PascalDragon

  • Hero Member
  • *****
  • Posts: 1694
  • Compiler Developer
Re: How to translate these macros to Pascal?
« Reply #13 on: April 04, 2020, 10:44:38 am »
If you have a C compiler like gnu installed, you can use cpp -E which will expand the macros, then examine the output on how those macro's are expanded to sourcecode.

I tried. It will just omit all of the macros I wanted to translate.

The macros will only be expanded in locations they are used at, not by themselves. The result of cpp -E is that simply no more preprocessor directives exist anymore and it's a plain C/C++ source now (with all files included).

guest65405

  • Guest
Re: How to translate these macros to Pascal?
« Reply #14 on: April 04, 2020, 05:31:11 pm »
If you have a C compiler like gnu installed, you can use cpp -E which will expand the macros, then examine the output on how those macro's are expanded to sourcecode.

I tried. It will just omit all of the macros I wanted to translate.

The macros will only be expanded in locations they are used at, not by themselves. The result of cpp -E is that simply no more preprocessor directives exist anymore and it's a plain C/C++ source now (with all files included).

How stupid I am! I have used it wrong all the time!  %)

 

TinyPortal © 2005-2018