Recent

Author Topic: const declaration  (Read 5648 times)

ccrause

  • Hero Member
  • *****
  • Posts: 996
Re: const declaration
« Reply #30 on: January 15, 2025, 03:18:56 pm »
How do I make it believe that it is ok? :)
function GetC is calculatable during compilation
A proposal to implement this can be seen here: https://gitlab.com/freepascal.org/fpc/source/-/merge_requests/645

If you want this to be implemented, support a real programmer: https://www.patreon.com/curiouskit

freemind001

  • Jr. Member
  • **
  • Posts: 51
Re: const declaration
« Reply #31 on: January 15, 2025, 03:53:17 pm »
For a real programmer that is easy. Very easy.

Just to implement this feature in the compiler first?
« Last Edit: January 15, 2025, 04:03:00 pm by freemind001 »

LV

  • Full Member
  • ***
  • Posts: 204
Re: const declaration
« Reply #32 on: January 15, 2025, 04:57:04 pm »
"Why would anybody ever DO that???"
i'm trying to make a wrapper for iunput.h in pascal for a personal project.

there are things like
Code: C  [Select][+][-]
  1. /* ioctl */
  2. #define UINPUT_IOCTL_BASE       'U'
  3. #define UI_DEV_CREATE           _IO(UINPUT_IOCTL_BASE, 1)
  4. #define UI_DEV_DESTROY          _IO(UINPUT_IOCTL_BASE, 2)
which seem to be not implementable in pascal, as you can't pass a value to macros.

As the project is for fun, talking about salary doesn't make sense. As well as discussing the knowledge of Pascal of forum members.
And yes, I agree, ChatGPT is pretty stupid when dealing with pascal. :)

Maybe
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. const
  4.   UINPUT_IOCTL_BASE = Ord('U');
  5.   UI_DEV_CREATE = (UINPUT_IOCTL_BASE shl 8) or 1;
  6.   UI_DEV_DESTROY = (UINPUT_IOCTL_BASE shl 8) or 2;
  7.  
  8. begin
  9.   WriteLn('UI_DEV_CREATE: ', UI_DEV_CREATE);
  10.   WriteLn('UI_DEV_DESTROY: ', UI_DEV_DESTROY);
  11. end.  
  12.  

ccrause

  • Hero Member
  • *****
  • Posts: 996
Re: const declaration
« Reply #33 on: January 15, 2025, 05:04:30 pm »
For a real programmer that is easy. Very easy.

Just to implement this feature in the compiler first?
More context is given by the preceding sentence:
Just write the macro as a function.
For a real programmer that is easy. Very easy.
I suspect Thaddy was making a general statement, not specifically addressing the topic of collapsing a pure function to a constant assignment at compile time.

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: const declaration
« Reply #34 on: January 15, 2025, 05:26:52 pm »
No, and Mark gave you the solution already.
It is simple: rewrite C macro's as functions. That makes them also type safe.
Some people think C style macro's lead to smaller code.  Absolute Nonsense.
Refrain from opiniated reactions if some really senior programmers already put you on the right track.
But I am sure they don't want the Trumps back...

freemind001

  • Jr. Member
  • **
  • Posts: 51
Re: const declaration
« Reply #35 on: January 15, 2025, 07:54:16 pm »
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$macro on}
  5.  
  6. uses
  7.   SysUtils;
  8.  
  9. function GetC(x, y: Integer): Integer; inline;
  10. begin
  11.   Result := x + y;
  12. end;
  13.  
  14. const
  15.   a = 1;
  16.   b = a + 1;
  17.  
  18. {$define c := GetC(a, b)}
  19.  
  20. begin
  21.   writeln(inttostr(c));
  22. end.                  
  23.  

Am I a real programmer?)
Is that what you mean? Looks rather ridiculous
« Last Edit: January 15, 2025, 09:59:16 pm by freemind001 »

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: const declaration
« Reply #36 on: January 16, 2025, 06:53:31 am »
{$define c := GetC(a, b)}
is not a valid fpc macro, that is the point.
fpc macro's do not take parameters.
programmers tend to read documentation.
What you want to happen is that a variable can be declared with a value that depends on a function result and that is not possible in Pascal.
This is only possible in the body of a program, procedure or function.

You also obviously did not even test your macro....disturbing....
Variable declaration can only be done with literal values, not with computed values.
« Last Edit: January 16, 2025, 07:10:04 am by Thaddy »
But I am sure they don't want the Trumps back...

WooBean

  • Sr. Member
  • ****
  • Posts: 280
Re: const declaration
« Reply #37 on: January 16, 2025, 09:31:32 am »
{$define c := GetC(a, b)}
is not a valid fpc macro, that is the point.
fpc macro's do not take parameters.
programmers tend to read documentation.
What you want to happen is that a variable can be declared with a value that depends on a function result and that is not possible in Pascal.
This is only possible in the body of a program, procedure or function.

You also obviously did not even test your macro....disturbing....
Variable declaration can only be done with literal values, not with computed values.

From aside I have tested freemind001's code. It works.

You could be right if parameteres of macro weren't const.

Very funny result at fight between owners of activity scores 16328 vs 41.

Of course it is a common victory.
Platforms: Win7/64, Linux Mint Ulyssa/64

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: const declaration
« Reply #38 on: January 16, 2025, 09:45:14 am »
How does it work?...... Show me....
But I am sure they don't want the Trumps back...

Khrys

  • Full Member
  • ***
  • Posts: 147
Re: const declaration
« Reply #39 on: January 16, 2025, 10:47:02 am »
How does it work?...... Show me....

I think you just missed that in the latest snippet, the macro isn't placed in the  const  section but inside the main function. It also doesn't take any arguments, it simply replaces  "c"  with the literal value  "GetC(a, b)"  which just so happens to work in that context. Apart from that, you're right of course - macros in FPC are very bare-bones compared to the C preprocessor.

Code: Pascal  [Select][+][-]
  1. {$define c := GetC(a, b)}
  2.  
  3. begin
  4.   writeln(inttostr(c));
  5. end.

freemind001

  • Jr. Member
  • **
  • Posts: 51
Re: const declaration
« Reply #40 on: January 16, 2025, 10:48:14 am »
If it didn't work, I wouldn't post it.
It compiles, and outputs the result 3.

Thaddy, I don't get you, you say that's very easy at first, a and then that it is impossible in pascal.

freemind001

  • Jr. Member
  • **
  • Posts: 51
Re: const declaration
« Reply #41 on: January 16, 2025, 10:58:11 am »
Can you please provide a snippet of code that implements
Code: C  [Select][+][-]
  1. #define UI_DEV_CREATE           _IO(UINPUT_IOCTL_BASE, 1)
in pascal?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12025
  • FPC developer.
Re: const declaration
« Reply #42 on: January 16, 2025, 11:27:52 am »
IOCTL macros are pure preprocessor stuff.  Not possible to convert.

Just write a manual convertor that outputs the values as hex or octal, that is what I did for e.g. rtl/freebsd/console.pp (but that was before FPC had octal literals)


MarkMLl

  • Hero Member
  • *****
  • Posts: 8176
Re: const declaration
« Reply #44 on: January 16, 2025, 01:23:19 pm »
Can you please provide a snippet of code that implements
Code: C  [Select][+][-]
  1. #define UI_DEV_CREATE           _IO(UINPUT_IOCTL_BASE, 1)
in pascal?

I've already given you examples of how to do it.

If you try to rely on FPC "not quite macros" when a better facility exists you WILL have problems: among others that they don't debug easily and that compiler messages can be misleading.

As I've already said, I'm not happy with Pascal's poor support for this sort of thing, but we're stuck with it. But you've been given answers, so kindly stop arguing and pretending that you know better than everybody else.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018