Skip to content

Commit e9906ac

Browse files
committed
Add ellipsis object.
1 parent 4d4bc95 commit e9906ac

11 files changed

Lines changed: 59 additions & 12 deletions

File tree

py/bc0.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
// Micro Python byte-codes.
2+
// The comment at the end of the line (if it exists) tells the arguments to the byte-code.
3+
14
#define MP_BC_LOAD_CONST_FALSE (0x10)
25
#define MP_BC_LOAD_CONST_NONE (0x11)
36
#define MP_BC_LOAD_CONST_TRUE (0x12)
4-
#define MP_BC_LOAD_CONST_SMALL_INT (0x13) // 24-bit, in excess
5-
#define MP_BC_LOAD_CONST_INT (0x14) // qstr
6-
#define MP_BC_LOAD_CONST_DEC (0x15) // qstr
7-
#define MP_BC_LOAD_CONST_ID (0x16) // qstr
8-
#define MP_BC_LOAD_CONST_BYTES (0x17) // qstr
9-
#define MP_BC_LOAD_CONST_STRING (0x18) // qstr
7+
#define MP_BC_LOAD_CONST_ELLIPSIS (0x13)
8+
#define MP_BC_LOAD_CONST_SMALL_INT (0x14) // 24-bit, in excess
9+
#define MP_BC_LOAD_CONST_INT (0x15) // qstr
10+
#define MP_BC_LOAD_CONST_DEC (0x16) // qstr
11+
#define MP_BC_LOAD_CONST_ID (0x17) // qstr
12+
#define MP_BC_LOAD_CONST_BYTES (0x18) // qstr
13+
#define MP_BC_LOAD_CONST_STRING (0x19) // qstr
1014

1115
#define MP_BC_LOAD_FAST_0 (0x20)
1216
#define MP_BC_LOAD_FAST_1 (0x21)

py/emitbc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ static void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
249249
case MP_TOKEN_KW_FALSE: emit_write_byte_1(emit, MP_BC_LOAD_CONST_FALSE); break;
250250
case MP_TOKEN_KW_NONE: emit_write_byte_1(emit, MP_BC_LOAD_CONST_NONE); break;
251251
case MP_TOKEN_KW_TRUE: emit_write_byte_1(emit, MP_BC_LOAD_CONST_TRUE); break;
252+
case MP_TOKEN_ELLIPSIS: emit_write_byte_1(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
252253
default: assert(0);
253254
}
254255
}

py/grammar.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ DEF_RULE(import_stmt, nc, or(2), rule(import_name), rule(import_from))
113113
DEF_RULE(import_name, c(import_name), and(2), tok(KW_IMPORT), rule(dotted_as_names))
114114
DEF_RULE(import_from, c(import_from), and(4), tok(KW_FROM), rule(import_from_2), tok(KW_IMPORT), rule(import_from_3))
115115
DEF_RULE(import_from_2, nc, or(2), rule(dotted_name), rule(import_from_2b))
116-
DEF_RULE(import_from_2b, nc, and(2), rule(one_or_more_period_or_ellipses), opt_rule(dotted_name))
116+
DEF_RULE(import_from_2b, nc, and(2), rule(one_or_more_period_or_ellipsis), opt_rule(dotted_name))
117117
DEF_RULE(import_from_3, nc, or(3), tok(OP_STAR), rule(import_as_names_paren), rule(import_as_names))
118118
DEF_RULE(import_as_names_paren, nc, and(3), tok(DEL_PAREN_OPEN), rule(import_as_names), tok(DEL_PAREN_CLOSE))
119-
DEF_RULE(one_or_more_period_or_ellipses, nc, one_or_more, rule(period_or_ellipses))
120-
DEF_RULE(period_or_ellipses, nc, or(2), tok(DEL_PERIOD), tok(ELLIPSES))
119+
DEF_RULE(one_or_more_period_or_ellipsis, nc, one_or_more, rule(period_or_ellipsis))
120+
DEF_RULE(period_or_ellipsis, nc, or(2), tok(DEL_PERIOD), tok(ELLIPSIS))
121121
DEF_RULE(import_as_name, nc, and(2), tok(NAME), opt_rule(as_name))
122122
DEF_RULE(dotted_as_name, nc, and(2), rule(dotted_name), opt_rule(as_name))
123123
DEF_RULE(as_name, nc, and(2), tok(KW_AS), tok(NAME))
@@ -220,7 +220,7 @@ DEF_RULE(power_dbl_star, c(power_dbl_star), and(2), tok(OP_DBL_STAR), rule(facto
220220
// testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
221221
// trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
222222

223-
DEF_RULE(atom, nc, or(10), tok(NAME), tok(NUMBER), rule(atom_string), tok(ELLIPSES), tok(KW_NONE), tok(KW_TRUE), tok(KW_FALSE), rule(atom_paren), rule(atom_bracket), rule(atom_brace))
223+
DEF_RULE(atom, nc, or(10), tok(NAME), tok(NUMBER), rule(atom_string), tok(ELLIPSIS), tok(KW_NONE), tok(KW_TRUE), tok(KW_FALSE), rule(atom_paren), rule(atom_bracket), rule(atom_brace))
224224
DEF_RULE(atom_string, c(atom_string), one_or_more, rule(string_or_bytes))
225225
DEF_RULE(string_or_bytes, nc, or(2), tok(STRING), tok(BYTES))
226226
DEF_RULE(atom_paren, c(atom_paren), and(3), tok(DEL_PAREN_OPEN), opt_rule(atom_2b), tok(DEL_PAREN_CLOSE))

py/lexer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ static const uint8_t tok_enc_kind[] = {
239239
MP_TOKEN_OP_CARET, MP_TOKEN_DEL_CARET_EQUAL,
240240
MP_TOKEN_DEL_EQUAL, MP_TOKEN_OP_DBL_EQUAL,
241241
MP_TOKEN_OP_NOT_EQUAL,
242-
MP_TOKEN_DEL_PERIOD, MP_TOKEN_ELLIPSES,
242+
MP_TOKEN_DEL_PERIOD, MP_TOKEN_ELLIPSIS,
243243
};
244244

245245
// must have the same order as enum in lexer.h

py/lexer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ typedef enum _mp_token_kind_t {
2020
MP_TOKEN_STRING,
2121
MP_TOKEN_BYTES,
2222

23-
MP_TOKEN_ELLIPSES,
23+
MP_TOKEN_ELLIPSIS,
2424

2525
MP_TOKEN_KW_FALSE, // 12
2626
MP_TOKEN_KW_NONE,

py/mpqstrraw.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Q(native)
2020
Q(viper)
2121
Q(asm_thumb)
2222

23+
Q(Ellipsis)
2324
Q(StopIteration)
2425

2526
Q(AttributeError)

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ extern const mp_obj_type_t mp_const_type;
110110
extern const mp_obj_t mp_const_none;
111111
extern const mp_obj_t mp_const_false;
112112
extern const mp_obj_t mp_const_true;
113+
extern const mp_obj_t mp_const_ellipsis;
113114
extern const mp_obj_t mp_const_stop_iteration; // special object indicating end of iteration (not StopIteration exception!)
114115

115116
// Need to declare this here so we are not dependent on map.h

py/objslice.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,35 @@
99
#include "obj.h"
1010
#include "runtime0.h"
1111

12+
/******************************************************************************/
13+
/* ellipsis object, a singleton */
14+
15+
typedef struct _mp_obj_ellipsis_t {
16+
mp_obj_base_t base;
17+
} mp_obj_ellipsis_t;
18+
19+
void ellipsis_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
20+
print(env, "Ellipsis");
21+
}
22+
23+
const mp_obj_type_t ellipsis_type = {
24+
{ &mp_const_type },
25+
"ellipsis",
26+
ellipsis_print, // print
27+
NULL, // call_n
28+
NULL, // unary_op
29+
NULL, // binary_op
30+
NULL, // getiter
31+
NULL, // iternext
32+
{{NULL, NULL},}, // method list
33+
};
34+
35+
static const mp_obj_ellipsis_t ellipsis_obj = {{&ellipsis_type}};
36+
const mp_obj_t mp_const_ellipsis = (mp_obj_t)&ellipsis_obj;
37+
38+
/******************************************************************************/
39+
/* slice object */
40+
1241
#if MICROPY_ENABLE_SLICE
1342

1443
// TODO: This implements only variant of slice with 2 integer args only.

py/runtime.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ void rt_init(void) {
8484
mp_qstr_map_lookup(&map_builtins, MP_QSTR_SyntaxError, true)->value = mp_obj_new_exception(MP_QSTR_SyntaxError);
8585
mp_qstr_map_lookup(&map_builtins, MP_QSTR_ValueError, true)->value = mp_obj_new_exception(MP_QSTR_ValueError);
8686

87+
// built-in objects
88+
mp_qstr_map_lookup(&map_builtins, MP_QSTR_Ellipsis, true)->value = mp_const_ellipsis;
89+
8790
// built-in core functions
8891
mp_qstr_map_lookup(&map_builtins, MP_QSTR___build_class__, true)->value = rt_make_function_2(mp_builtin___build_class__);
8992
mp_qstr_map_lookup(&map_builtins, MP_QSTR___repl_print__, true)->value = rt_make_function_1(mp_builtin___repl_print__);

py/showbc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ void mp_show_byte_code(const byte *ip, int len) {
4646
printf("LOAD_CONST_TRUE");
4747
break;
4848

49+
case MP_BC_LOAD_CONST_ELLIPSIS:
50+
printf("LOAD_CONST_ELLIPSIS");
51+
break;
52+
4953
case MP_BC_LOAD_CONST_SMALL_INT:
5054
unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000;
5155
ip += 3;

0 commit comments

Comments
 (0)