• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Implemented Enumerations

PixelPaladin

New member
CX Code Contributor
3rd Party Module Dev
Joined
Aug 27, 2017
Messages
110
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

  • custom_trans.zip
    51.1 KB · Views: 288
I'm a huge fan of enums, will look into your example. :)
 
It makes sense. I get by with big blocks of named constants, but enums would be a nice addition.
 
Definitely could do with enumerations.
 
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!
 
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

  • custom_trans_clean.zip
    10.2 KB · Views: 297
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?
 
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
 
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?
 
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.
 
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...
 
Why should it produce an error? The first solution is pretty flexible i think.
 
Yeah I second that. Why should this produce an error?
 
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.
 
Alright, but for enumerations I think it's ok to have them in the same line?
 
Back
Top Bottom