Implemented Enumerations

PixelPaladin

Active Member
CX Code Contributor
3rd Party Module Dev
Joined
Aug 27, 2017
Location
Germany
I think there should exist some way to enumerate constants so I had a look at the compiler source code but it seems like it would be very complicated to add enums. However in C for example there are different ways to enumerate stuff:

Code:
enum foo{a, b, c=123};
or using typedef:
Code:
typedef enum {a, b, c=123} foo;
or:
Code:
enum {a, b, c=123};
Since the last one looks like it could be implemented I experimented with the compiler code and added an 'Enumerate' keyword.

So instead of writing
Code:
Const A:Int = 0
Const B:Int = 1
Const C:Int = 3
Const D:Int = F+100
Const E:Int = 22
Const F:Int = 23
… you can now write:
Code:
Enumerate A, B, C, D=F+100, E=22, F
(the resulting code should be the same)

It also works with all attributes (private, public, …) and line breaks behave like they should (they are allowed after operators and after commas). The generated code is the same since in the end it does nothing other than creating integer constants. Enumerations are possible within classes, interfaces, functions, methods And in global space.

Here is the source code for the custom trans tool:
 

Attachments

Gerry Quinn

Active Member
Joined
Jun 24, 2017
It makes sense. I get by with big blocks of named constants, but enums would be a nice addition.
 

muruba

Active Member
CX Code Contributor
3rd Party Module Dev
Patreon Silver
Joined
Jul 5, 2017
I am not sure how hard it is to translate the example below to different languages, but even if CX enums can be expressed as const strings under the hood (and not ordinals, so no arithmetic) would still be awesome!
 

PixelPaladin

Active Member
CX Code Contributor
3rd Party Module Dev
Joined
Aug 27, 2017
Location
Germany
I just had a look at parser.cxs and found that I forgot to remove some lines so I cleaned up the code a little. toker.cxs and parser.cxs should be the only modified files.
 

Attachments

MikeHart

Administrator
Staff member
Joined
Jun 19, 2017
Location
Germany
Can you do multiline enumerations or does it have to be a one line definition?

Can you do multiple enumerations? If yes, what is the starting value of the following ones?
 

PixelPaladin

Active Member
CX Code Contributor
3rd Party Module Dev
Joined
Aug 27, 2017
Location
Germany
Can you do multiline enumerations or does it have to be a one line definition?

Can you do multiple enumerations? If yes, what is the starting value of the following ones?
Yes, multiline enumerations are possible. Every enumeration starts at 0:
Code:
Enumerate
   Foo1, ' => 0
   Bar1, ' => 1
   Baz1  ' => 2
Enumerate
   Foo2, ' => 0
   Bar2, ' => 1
   Baz2  ' => 2
 

MikeHart

Administrator
Staff member
Joined
Jun 19, 2017
Location
Germany
I tested it last night and it works flawless to me at least on HTML5. But...

should there be an EndEnumerate at the end? Or named as an ENUM/ENDENUM block?

Right now, multiline enumerations look kinda odd to me. But maybe you guys are just fine with it like it is proposed by PixelPaladin?
 

PixelPaladin

Active Member
CX Code Contributor
3rd Party Module Dev
Joined
Aug 27, 2017
Location
Germany
I tested it last night and it works flawless to me at least on HTML5. But...

should there be an EndEnumerate at the end? Or named as an ENUM/ENDENUM block?

Right now, multiline enumerations look kinda odd to me. But maybe you guys are just fine with it like it is proposed by PixelPaladin?
It should work the same on all platforms since it only creates constant integer values.

I also had the idea to use 'End'/'End Enumerate' or write the values in brackets like an array or curly braces like in c/c++ (implemented it that way first). But in the end I think that my current solution is more consistent since there is no other case in cerberus where a list of values ends with the 'End' keyword. Just compare these:
Code:
Const A:Int = 0, B:Int = 1, C:Int = 2

Enumerate A, B, C

Const A:Int = 0,
      B:Int = 1,
      C:Int = 2

Enumerate A,
          B,
          C
The 'End' keyword would fit better for a 'type like' enumeration like I planned it first:
Code:
Enum WeekDay
    Monday
    Tuesday
    ...
    Sunday
End

Function GetWeekDay:WeekDay()
    ...
End
However I see no way to merge something like this into the current code.
 

PixelPaladin

Active Member
CX Code Contributor
3rd Party Module Dev
Joined
Aug 27, 2017
Location
Germany
Just noticed an inconsistency. The following code compiles without an error:
Code:
Enumerate
    Foo, Bar
This can be fixed by removing 'SkipEols' in 'ParseEnumStmt()' (in parser.cxs). Sorry for that...
 

PixelPaladin

Active Member
CX Code Contributor
3rd Party Module Dev
Joined
Aug 27, 2017
Location
Germany
Don't know – but it's the way everything else is implemented.
The following does not work:
Code:
Const
    Foo:Int, Bar:Int
it says "Syntax error - expecting identifier."
same goes for fields, locals and globals.
 
Top Bottom