Recent

Author Topic: [CLOSED] Enum without name?  (Read 3086 times)

guest65405

  • Guest
[CLOSED] Enum without name?
« on: April 02, 2020, 06:45:59 pm »
The full syntax of C enum is with name. But I saw numerous of C libraries use enum without name. When translating to Pascal with h2pas I have to edit these header and give name to each of the enums. Should we follow the footstep of C and allow nameless enum so porting of C headers will be more easier? I think it could be done with a compiler directive like {$NAMELESS_ENUM ON}.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8465
  • FPC developer.
Re: Enum without name?
« Reply #1 on: April 02, 2020, 07:19:19 pm »

Could you give an example? And why can't it be fixed in a header convertor (did you try?). Language changes should really be the last resort.

guest65405

  • Guest
Re: Enum without name?
« Reply #2 on: April 03, 2020, 03:49:40 am »

Could you give an example? And why can't it be fixed in a header convertor (did you try?). Language changes should really be the last resort.

Here you are. A bunch of them:

Code: C  [Select][+][-]
  1. enum {                        /* bitmap type */
  2.  CD_RGB,                      /* these definitions are compatible with the IM library */
  3.  CD_MAP,
  4.  CD_RGBA = 0x100
  5. };
  6.  
  7. enum {                         /* bitmap data */
  8.  CD_IRED,
  9.  CD_IGREEN,
  10.  CD_IBLUE,
  11.  CD_IALPHA,
  12.  CD_INDEX,
  13.  CD_COLORS
  14. };
  15.  
  16. enum {                          /* status report */
  17.  CD_ERROR = -1,
  18.  CD_OK    =  0
  19. };
  20.  
  21. enum {                          /* clip mode */
  22.  CD_CLIPOFF,
  23.  CD_CLIPAREA,
  24.  CD_CLIPPOLYGON,
  25.  CD_CLIPREGION,
  26.  CD_CLIPPATH
  27. };
  28.  
  29. enum {                          /* region combine mode */
  30.  CD_UNION,
  31.  CD_INTERSECT,
  32.  CD_DIFFERENCE,
  33.  CD_NOTINTERSECT
  34. };
  35.  
  36. enum {                          /* polygon mode (begin...end) */
  37.  CD_FILL,
  38.  CD_OPEN_LINES,
  39.  CD_CLOSED_LINES,
  40.  CD_CLIP,
  41.  CD_BEZIER,
  42.  CD_REGION,
  43.  CD_PATH
  44. };
  45.  
  46. #define CD_POLYCUSTOM 10
  47.  
  48. enum {                          /* path actions */
  49.  CD_PATH_NEW,
  50.  CD_PATH_MOVETO,
  51.  CD_PATH_LINETO,
  52.  CD_PATH_ARC,
  53.  CD_PATH_CURVETO,
  54.  CD_PATH_CLOSE,
  55.  CD_PATH_FILL,
  56.  CD_PATH_STROKE,
  57.  CD_PATH_FILLSTROKE,
  58.  CD_PATH_CLIP
  59. };
  60.  
  61. enum {                          /* fill mode */
  62.  CD_EVENODD,
  63.  CD_WINDING
  64. };
  65.  
  66. enum {                          /* line join  */
  67.  CD_MITER,
  68.  CD_BEVEL,
  69.  CD_ROUND
  70. };  
  71.  
  72. enum {                          /* line cap  */
  73.  CD_CAPFLAT,  
  74.  CD_CAPSQUARE,
  75.  CD_CAPROUND
  76. };  
  77.  
  78. enum {                          /* background opacity mode */
  79.  CD_OPAQUE,
  80.  CD_TRANSPARENT
  81. };
  82.  
  83. enum {                          /* write mode */
  84.  CD_REPLACE,
  85.  CD_XOR,
  86.  CD_NOT_XOR
  87. };
  88.  
  89. enum {                          /* color allocation mode (palette) */
  90.  CD_POLITE,
  91.  CD_FORCE
  92. };
  93.  
  94. enum {                          /* line style */
  95.  CD_CONTINUOUS,
  96.  CD_DASHED,
  97.  CD_DOTTED,
  98.  CD_DASH_DOT,
  99.  CD_DASH_DOT_DOT,
  100.  CD_CUSTOM
  101. };
  102.  
  103. enum {                          /* marker type */
  104.  CD_PLUS,
  105.  CD_STAR,
  106.  CD_CIRCLE,
  107.  CD_X,
  108.  CD_BOX,
  109.  CD_DIAMOND,
  110.  CD_HOLLOW_CIRCLE,
  111.  CD_HOLLOW_BOX,
  112.  CD_HOLLOW_DIAMOND
  113. };
  114.  
  115. enum {                          /* hatch type */
  116.  CD_HORIZONTAL,
  117.  CD_VERTICAL,
  118.  CD_FDIAGONAL,
  119.  CD_BDIAGONAL,
  120.  CD_CROSS,
  121.  CD_DIAGCROSS
  122. };
  123.  
  124. enum {                          /* interior style */
  125.  CD_SOLID,
  126.  CD_HATCH,
  127.  CD_STIPPLE,
  128.  CD_PATTERN,
  129.  CD_HOLLOW,
  130.  CD_CUSTOMPATTERN     /* used only in ContextPlus drivers */
  131. };
  132.  
  133. enum {                          /* text alignment */
  134.  CD_NORTH,
  135.  CD_SOUTH,
  136.  CD_EAST,
  137.  CD_WEST,
  138.  CD_NORTH_EAST,
  139.  CD_NORTH_WEST,
  140.  CD_SOUTH_EAST,
  141.  CD_SOUTH_WEST,
  142.  CD_CENTER,
  143.  CD_BASE_LEFT,
  144.  CD_BASE_CENTER,
  145.  CD_BASE_RIGHT
  146. };
  147.  
  148. enum {                          /* style */
  149.  CD_PLAIN  = 0,
  150.  CD_BOLD   = 1,
  151.  CD_ITALIC = 2,
  152.  CD_UNDERLINE = 4,
  153.  CD_STRIKEOUT = 8
  154. };

h2pas will fail with them all and give a bunch of error messages.

I have to give names to these enums to make h2pas happy:

Code: C  [Select][+][-]
  1. enum CD_BITMAP_TYPE {                        /* bitmap type */
  2.  CD_RGB,                      /* these definitions are compatible with the IM library */
  3.  CD_MAP,
  4.  CD_RGBA = 0x100
  5. };
  6.  
  7. enum CD_BITMAP_DATA {                         /* bitmap data */
  8.  CD_IRED,
  9.  CD_IGREEN,
  10.  CD_IBLUE,
  11.  CD_IALPHA,
  12.  CD_INDEX,
  13.  CD_COLORS
  14. };
  15.  
  16. enum CD_STATUS {                          /* status report */
  17.  CD_ERROR = -1,
  18.  CD_OK    =  0
  19. };
  20.  
  21. enum CLIP_MODE {                          /* clip mode */
  22.  CD_CLIPOFF,
  23.  CD_CLIPAREA,
  24.  CD_CLIPPOLYGON,
  25.  CD_CLIPREGION,
  26.  CD_CLIPPATH
  27. };
  28.  
  29. enum REGION_COMBINE_MODE {                          /* region combine mode */
  30.  CD_UNION,
  31.  CD_INTERSECT,
  32.  CD_DIFFERENCE,
  33.  CD_NOTINTERSECT
  34. };
  35.  
  36. enum POLYGON_MODE {                          /* polygon mode (begin...end) */
  37.  CD_FILL,
  38.  CD_OPEN_LINES,
  39.  CD_CLOSED_LINES,
  40.  CD_CLIP,
  41.  CD_BEZIER,
  42.  CD_REGION,
  43.  CD_PATH
  44. };
  45.  
  46. #define CD_POLYCUSTOM 10
  47.  
  48. enum CD_PATH {                          /* path actions */
  49.  CD_PATH_NEW,
  50.  CD_PATH_MOVETO,
  51.  CD_PATH_LINETO,
  52.  CD_PATH_ARC,
  53.  CD_PATH_CURVETO,
  54.  CD_PATH_CLOSE,
  55.  CD_PATH_FILL,
  56.  CD_PATH_STROKE,
  57.  CD_PATH_FILLSTROKE,
  58.  CD_PATH_CLIP
  59. };
  60.  
  61. enum FILL_MODE {                          /* fill mode */
  62.  CD_EVENODD,
  63.  CD_WINDING
  64. };
  65.  
  66. enum LINE_JOIN {                          /* line join  */
  67.  CD_MITER,
  68.  CD_BEVEL,
  69.  CD_ROUND
  70. };  
  71.  
  72. enum LINE_CAP {                          /* line cap  */
  73.  CD_CAPFLAT,  
  74.  CD_CAPSQUARE,
  75.  CD_CAPROUND
  76. };  
  77.  
  78. enum BG_OPACITY_MODE {                          /* background opacity mode */
  79.  CD_OPAQUE,
  80.  CD_TRANSPARENT
  81. };
  82.  
  83. enum WRITE_MODE {                          /* write mode */
  84.  CD_REPLACE,
  85.  CD_XOR,
  86.  CD_NOT_XOR
  87. };
  88.  
  89. enum COLOR_ALLOC_MODE {                          /* color allocation mode (palette) */
  90.  CD_POLITE,
  91.  CD_FORCE
  92. };
  93.  
  94. enum LINE_STYLE {                          /* line style */
  95.  CD_CONTINUOUS,
  96.  CD_DASHED,
  97.  CD_DOTTED,
  98.  CD_DASH_DOT,
  99.  CD_DASH_DOT_DOT,
  100.  CD_CUSTOM
  101. };
  102.  
  103. enum MARKER_TYPE {                          /* marker type */
  104.  CD_PLUS,
  105.  CD_STAR,
  106.  CD_CIRCLE,
  107.  CD_X,
  108.  CD_BOX,
  109.  CD_DIAMOND,
  110.  CD_HOLLOW_CIRCLE,
  111.  CD_HOLLOW_BOX,
  112.  CD_HOLLOW_DIAMOND
  113. };
  114.  
  115. enum HATCH_TYPE {                          /* hatch type */
  116.  CD_HORIZONTAL,
  117.  CD_VERTICAL,
  118.  CD_FDIAGONAL,
  119.  CD_BDIAGONAL,
  120.  CD_CROSS,
  121.  CD_DIAGCROSS
  122. };
  123.  
  124. enum INTERIOR_TYPE {                          /* interior style */
  125.  CD_SOLID,
  126.  CD_HATCH,
  127.  CD_STIPPLE,
  128.  CD_PATTERN,
  129.  CD_HOLLOW,
  130.  CD_CUSTOMPATTERN     /* used only in ContextPlus drivers */
  131. };
  132.  
  133. enum TEXT_ALLIGNMENT {                          /* text alignment */
  134.  CD_NORTH,
  135.  CD_SOUTH,
  136.  CD_EAST,
  137.  CD_WEST,
  138.  CD_NORTH_EAST,
  139.  CD_NORTH_WEST,
  140.  CD_SOUTH_EAST,
  141.  CD_SOUTH_WEST,
  142.  CD_CENTER,
  143.  CD_BASE_LEFT,
  144.  CD_BASE_CENTER,
  145.  CD_BASE_RIGHT
  146. };
  147.  
  148. enum STYLE {                          /* style */
  149.  CD_PLAIN  = 0,
  150.  CD_BOLD   = 1,
  151.  CD_ITALIC = 2,
  152.  CD_UNDERLINE = 4,
  153.  CD_STRIKEOUT = 8
  154. };

You see. These enums are too many. Give name to each of them is a nightmare.

Update: To be more clear, even if I use -e to convert all enums to constants h2pas still fails. Give names to these enums is mandatory to make h2pas proceed with translating the file.

bytebites

  • Sr. Member
  • ****
  • Posts: 281
Re: Enum without name?
« Reply #3 on: April 03, 2020, 07:10:32 am »
Better way is that C-language removes nameless enum.

guest65405

  • Guest
Re: Enum without name?
« Reply #4 on: April 03, 2020, 08:15:26 am »
Better way is that C-language removes nameless enum.

We have to port C libraries, not the other way around.

You could try contact the ISO committee. But if they listen to you and actually add it to the next C standard, libraries will not jump to these newer standard immediately, though. Most of C libraries target C99 for portability.

Thaddy

  • Hero Member
  • *****
  • Posts: 10190
Re: Enum without name?
« Reply #5 on: April 03, 2020, 08:40:57 am »
h2pas comes with full sources including a grammar. You will just need to change the grammar  :D O:-) and feed it through plex/pyacc, then recompile.
You will need to resolve the ambiguity you just introduced by hand, though. See lex/yacc documentation on how to solve that.

When you've done all that, plz submit a patch.... ;D O:-)

It is probably enough to change just the lexer part, although you will need to add a name generator to keep the rest working. (Also useful for nameless parameters, that are also not supported)
/utils/h2pas is the sub-directory
I have looked at it and it should be doable for beginners that invest a little time in lex/yacc examples.
« Last Edit: April 03, 2020, 09:12:38 am by Thaddy »
I am more like donkey than shrek

guest65405

  • Guest
Re: Enum without name?
« Reply #6 on: April 03, 2020, 09:01:15 am »
h2pas comes with full sources including a grammar. You will just need to change the grammar  :D O:-) and feed it through plex/pyacc, then recompile.
You will need to resolve the ambiguity you just introduced by hand, though. See lex/yacc documentation on how to solve that.

When you've done all that, plz submit a patch.... ;D O:-)

(It is probably enough to change just the lexer part.)
/utils/h2pas is the sub-directory

I hope I have the knowledge to do so. But indeed I don't.

I translate my frequently used libraries into Pascal and started to migrate. When I learned programming, I learned with C, C99 to be precise. Just basic programming, though. There are many things in C I still don't know.

guest65405

  • Guest
Re: Enum without name?
« Reply #7 on: April 03, 2020, 09:15:17 am »
It seemed my proposal is not plausible. I will stop discussing it here and back to work.

 

TinyPortal © 2005-2018