Skip to content

Commit 97f9a28

Browse files
committed
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.
1 parent 96f137b commit 97f9a28

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

py/lexer.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ struct _mp_lexer_t {
6464
mp_token_t tok_cur;
6565
};
6666

67+
// debug flag for __debug__ constant
68+
STATIC mp_token_kind_t mp_debug_value;
69+
70+
void mp_set_debug(bool value) {
71+
mp_debug_value = value ? MP_TOKEN_KW_TRUE : MP_TOKEN_KW_FALSE;
72+
}
73+
6774
// TODO replace with a call to a standard function
6875
bool str_strn_equal(const char *str, const char *strn, int len) {
6976
uint i = 0;
@@ -303,7 +310,7 @@ STATIC const char *tok_kw[] = {
303310
"while",
304311
"with",
305312
"yield",
306-
NULL,
313+
"__debug__",
307314
};
308315

309316
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
687694

688695
// check for keywords
689696
if (tok->kind == MP_TOKEN_NAME) {
690-
for (int i = 0; tok_kw[i] != NULL; i++) {
697+
// We check for __debug__ here and convert it to its value. This is so
698+
// the parser gives a syntax error on, eg, x.__debug__. Otherwise, we
699+
// need to check for this special token in many places in the compiler.
700+
// TODO improve speed of these string comparisons
701+
//for (int i = 0; tok_kw[i] != NULL; i++) {
702+
for (int i = 0; i < ARRAY_SIZE(tok_kw); i++) {
691703
if (str_strn_equal(tok_kw[i], tok->str, tok->len)) {
692-
tok->kind = MP_TOKEN_KW_FALSE + i;
704+
if (i == ARRAY_SIZE(tok_kw) - 1) {
705+
tok->kind = mp_debug_value;
706+
} else {
707+
tok->kind = MP_TOKEN_KW_FALSE + i;
708+
}
693709
break;
694710
}
695711
}

py/runtime.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,14 @@ const mp_obj_module_t mp_module___main__ = {
6969
};
7070

7171
void mp_init(void) {
72-
// call port specific initialization if any
72+
// call port specific initialization if any
7373
#ifdef MICROPY_PORT_INIT_FUNC
7474
MICROPY_PORT_INIT_FUNC;
7575
#endif
7676

77+
// __debug__ enabled by default
78+
mp_set_debug(true);
79+
7780
mp_emit_glue_init();
7881

7982
// init global module stuff

py/runtime.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ typedef struct _mp_arg_t {
5454
void mp_init(void);
5555
void mp_deinit(void);
5656

57+
void mp_set_debug(bool value); // sets the value of __debug__; see lexer.c
58+
5759
void mp_arg_check_num(uint n_args, uint n_kw, uint n_args_min, uint n_args_max, bool takes_kw);
5860
void mp_arg_parse_all(uint n_pos, const mp_obj_t *pos, mp_map_t *kws, uint n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);
5961
void mp_arg_parse_all_kw_array(uint n_pos, uint n_kw, const mp_obj_t *args, uint n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);

0 commit comments

Comments
 (0)