Skip to content

Commit

Permalink
py/lexer: Add static assert that token enum values all fit in a byte.
Browse files Browse the repository at this point in the history
Signed-off-by: Damien George <[email protected]>
  • Loading branch information
dpgeorge committed Jul 18, 2024
1 parent e00d80d commit 96007e7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 3 additions & 1 deletion py/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ static const char *const tok_enc =
"=e=" // = ==
"!."; // start of special cases: != . ...

// TODO static assert that number of tokens is less than 256 so we can safely make this table with byte sized entries
static const uint8_t tok_enc_kind[] = {
MP_TOKEN_DEL_PAREN_OPEN, MP_TOKEN_DEL_PAREN_CLOSE,
MP_TOKEN_DEL_BRACKET_OPEN, MP_TOKEN_DEL_BRACKET_CLOSE,
Expand Down Expand Up @@ -774,6 +773,9 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
} else {
// search for encoded delimiter or operator

// assert that the token enum value fits in a byte, so they all fit in tok_enc_kind
MP_STATIC_ASSERT(MP_TOKEN_NUMBER_OF <= 256);

const char *t = tok_enc;
size_t tok_enc_index = 0;
for (; *t != 0 && !is_char(lex, *t); t += 1) {
Expand Down
2 changes: 2 additions & 0 deletions py/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_DEL_SEMICOLON,
MP_TOKEN_DEL_EQUAL,
MP_TOKEN_DEL_MINUS_MORE,

MP_TOKEN_NUMBER_OF,
} mp_token_kind_t;

// this data structure is exposed for efficiency
Expand Down

0 comments on commit 96007e7

Please sign in to comment.