Bitwise » Forums » Style question regarding Homework day 2-3
Maid
Florian Morel
3 posts
#14749 Style question regarding Homework day 2-3
6 months, 3 weeks ago Edited by Florian Morel on March 28, 2018, 1:27 p.m. 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!
LaneRendell
Charles Nicholson
4 posts

None

#14752 Style question regarding Homework day 2-3
6 months, 3 weeks ago Edited by Charles Nicholson on March 29, 2018, 1:54 a.m.

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;


None