diff options
author | Damien George <damien.p.george@gmail.com> | 2014-05-12 23:07:34 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-05-12 23:07:34 +0100 |
commit | 97f9a2813e5909f0025a2e5e746fff2187f29171 (patch) | |
tree | bae809e621083de39ee4fb6bf9b70edb23c7e08f /py/lexer.c | |
parent | 96f137b24a6c0b2e1e3c6f5fb8617b8a7f7896d6 (diff) |
py: Add support for __debug__ constant.
__debug__ now resolves to True or False. Its value needs to be set by
mp_set_debug().
TODO: call mp_set_debug in unix/ port.
TODO: optimise away "if False:" statements in compiler.
Diffstat (limited to 'py/lexer.c')
-rw-r--r-- | py/lexer.c | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/py/lexer.c b/py/lexer.c index f736ef303..e2dfea78c 100644 --- a/py/lexer.c +++ b/py/lexer.c @@ -64,6 +64,13 @@ struct _mp_lexer_t { mp_token_t tok_cur; }; +// debug flag for __debug__ constant +STATIC mp_token_kind_t mp_debug_value; + +void mp_set_debug(bool value) { + mp_debug_value = value ? MP_TOKEN_KW_TRUE : MP_TOKEN_KW_FALSE; +} + // TODO replace with a call to a standard function bool str_strn_equal(const char *str, const char *strn, int len) { uint i = 0; @@ -303,7 +310,7 @@ STATIC const char *tok_kw[] = { "while", "with", "yield", - NULL, + "__debug__", }; STATIC int hex_digit(unichar c) { @@ -687,9 +694,18 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs // check for keywords if (tok->kind == MP_TOKEN_NAME) { - for (int i = 0; tok_kw[i] != NULL; i++) { + // We check for __debug__ here and convert it to its value. This is so + // the parser gives a syntax error on, eg, x.__debug__. Otherwise, we + // need to check for this special token in many places in the compiler. + // TODO improve speed of these string comparisons + //for (int i = 0; tok_kw[i] != NULL; i++) { + for (int i = 0; i < ARRAY_SIZE(tok_kw); i++) { if (str_strn_equal(tok_kw[i], tok->str, tok->len)) { - tok->kind = MP_TOKEN_KW_FALSE + i; + if (i == ARRAY_SIZE(tok_kw) - 1) { + tok->kind = mp_debug_value; + } else { + tok->kind = MP_TOKEN_KW_FALSE + i; + } break; } } |