Bitwise»Forums
Florian Morel
2 posts
Style question regarding Homework day 2-3
Edited by Florian Morel on Reason: Initial post
Hi there!

First of all, thank you Per for Bitwise, it is an amazing project to follow and I learned a lot already. Keep up the good work!

I have a style-related question.
While creating my solution for day 2-3, I found that I have a lot of enum redundancy (especially for operators) between type.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// In the lexer, I have an enum for every token possible, operator included
typedef enum TokenType {
    TOKENTYPE_ADD,
    TOKENTYPE_MIN,
    TOKENTYPE_MUL,
    //... you get the point
} TokenType;

//In my expression parser, I have a very similar structure for Operator Expression
typedef enum OpExprType {
    OPEXPRTYPE_ADD,
    OPEXPRTYPE_MIN,
    OPEXPRTYPE_MUL,
    //...
} OpExprType;


I almost have a direct mapping between TokenType and OpExprType and it feels really redundant. It's not the first time I find this kind of structure, I was wondering if it is just the way it is, or if there are some technique to simplify that.

Thank you for you time!
Charles Nicholson
3 posts
None
Style question regarding Homework day 2-3
Edited by Charles Nicholson on
Just off the top of my head you could do a macro:

With Macros
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#define basicReplace(NAME)\
    NAME_ADD, \
    NAME_MIN, \
    ..., \
    NAME_LASTSHAREDTOKEN, \

....
....

typedef enum TokenType {
    basicReplace(TOKENTYPE)
    TOKENTYPE_NOTSHARED
    //... you get the point
} TokenType;


typedef enum OpExprType {
    basicReplace(OPEXPRTYPE),
    OPEXPRTYPE_SPECIAL,
    OPEXPRTYPE_3242,
    //...
} OpExprType;