Lazarus

Free Pascal => Beginners => Topic started by: origedit on May 07, 2025, 04:11:48 pm

Title: [solved] Constructing an array indexed by an enumerated type
Post by: origedit on May 07, 2025, 04:11:48 pm
I'm new to Pascal, trying it out with a program to solve a simple task. I encountered a problem about syntax without a satisfying solution. Given an enumeration:
Code: Pascal  [Select][+][-]
  1. type Symbol = (LParen, RParen, Add, Subtract, Multiply, Divide, Power);
I would need a few arrays associating a symbol with its priority, character, and later, a procedure.
Code: Pascal  [Select][+][-]
  1. const Priority: Array[Symbol] of Integer = (-1, -1, 1, 1, 2, 2, 3);
The problem is, this definition isn't clear. In C, i can write
Code: C  [Select][+][-]
  1. {..., lparen = -1, ...}
to make the association apparent. I looked at FP's manual and found nothing about how this can be done, It seems that such feature is possible for records though. Any help?
Title: Re: Constructing an array indexed by an enumerated type
Post by: 440bx on May 07, 2025, 04:50:44 pm
FPC allows you to declare the enumeration just as in C. i.e.:
Code: Pascal  [Select][+][-]
  1. type Symbol = (LParen = -1, RParen = 1, Add = 1, Subtract = 1, Multiply = 2, Divide = 2, Power = 3);
  2.  

HTH.
Title: Re: Constructing an array indexed by an enumerated type
Post by: origedit on May 07, 2025, 05:06:40 pm
Sorry, this isn't what i'm looking for. To make a concrete example, the model is analogous to this:

Code: C  [Select][+][-]
  1. enum { lefpar = 0, rigpar, plus, minus, multiply, divide };
  2.  
  3. void (*funcs[])() = {
  4.         lefpar = 0,
  5.         rigpar = rigpar_f,
  6.         plus = plus_f,
  7.         minus = minus_f,
  8.         multiply = multiply_f,
  9.         divide = divide_f
  10. };
  11. const int precs[] = {
  12.         lefpar = 0,
  13.         rigpar = 0,
  14.         plus = 1,
  15.         minus = 1,
  16.         multiply = 2,
  17.         divide = 2
  18. };
Title: Re: Constructing an array indexed by an enumerated type
Post by: Thaddy on May 07, 2025, 05:46:16 pm
That should not even work in C, since you have duplicates in the enum and you can not determine the procedure to call.
It seems you are trying to use the enum as a weight. That is the wrong approach.
I will see if I can come up with something better, but right now dinner is served.
Btw the syntax suggested by 440bx works if it is only a weight you are after.
Title: Re: Constructing an array indexed by an enumerated type
Post by: origedit on May 07, 2025, 06:01:12 pm
It seems you are trying to use the enum as a weight. That is the wrong approach.

That's the thing! What I'm trying to do is to give identifiers to IDs of symbols. The IDs would be used to index several arrays.

400bx gave a a solution for assigning identifiers to the weights, not to the indices. That isn't what i'm looking for.
Title: Re: Constructing an array indexed by an enumerated type
Post by: Fibonacci on May 07, 2025, 06:22:29 pm
Code: Pascal  [Select][+][-]
  1. function plus_f(a, b: integer): integer;
  2. begin
  3.   result := a+b;
  4. end;
  5.  
  6. type
  7.   symbols = (lefpar = 0, rigpar, plus, minus, multiply, divide);
  8.  
  9. const
  10.   priors: array[symbols] of integer = (-1, -1, 1, 1, 2, 3);
  11.   precs: array[symbols] of integer = (0, 0, 1, 1, 2, 2);
  12.   funcs: array[symbols] of pointer = (nil, nil, @plus_f, nil, nil, nil);
  13.  
  14. type
  15.   tplus_f = function(a, b: integer): integer;
  16.  
  17. begin
  18.   writeln(tplus_f(funcs[plus])(10, 5));
  19.   readln;
  20. end.



Code: Pascal  [Select][+][-]
  1. function plus_f(a, b: integer): integer;
  2. begin
  3.   result := a+b;
  4. end;
  5.  
  6. type
  7.   symbols = (lefpar = 0, rigpar, plus, minus, multiply, divide);
  8.  
  9.   tx = record
  10.     prior, prec: integer;
  11.     func: pointer;
  12.   end;
  13.  
  14. const
  15.   x: array[symbols] of tx = (
  16.     {lefpar} (),
  17.     {rigpar} (),
  18.     {plus} (
  19.       prior: -1;
  20.       prec: 0;
  21.       func: @plus_f;
  22.     ),
  23.     {minus} (),
  24.     {multiply} (),
  25.     {divide} ()
  26.   );
  27.  
  28. type
  29.   tplus_f = function(a, b: integer): integer;
  30.  
  31. begin
  32.   writeln(tplus_f(x[plus].func)(10, 5));
  33.   readln;
  34. end.
Title: Re: Constructing an array indexed by an enumerated type
Post by: Martin_fr on May 07, 2025, 06:28:08 pm
It seems you are trying to use the enum as a weight. That is the wrong approach.

That's the thing! What I'm trying to do is to give identifiers to IDs of symbols. The IDs would be used to index several arrays.

400bx gave a a solution for assigning identifiers to the weights, not to the indices. That isn't what i'm looking for.

In a constant array declaration you can not specify the indexes. They are only known by the position of the element.

You can however always add them as comment. Though of course the compiler wont complain if you get them wrong.

Or you can assign them in code
Title: Re: Constructing an array indexed by an enumerated type
Post by: origedit on May 07, 2025, 06:42:33 pm
That's a shame. i expected Pascal to have this feature. Thanks for your patience.
TinyPortal © 2005-2018