Skip to content

Commit 13e64f0

Browse files
committed
Merge remote-tracking branch 'upstream/master' into containment
2 parents 813edf6 + 34f813e commit 13e64f0

64 files changed

Lines changed: 1632 additions & 249 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@
1919
######################
2020
*.swp
2121

22+
# Build directory
23+
######################
24+
build/

logo/micropythonpowered-art.png

29.8 KB
Loading

logo/vector-logo-2.png

1.85 KB
Loading

logo/vector-logo-inkscape_master.svg

Lines changed: 10 additions & 4 deletions
Loading

py/compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@ void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
13161316
void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
13171317
int l_end = comp_next_label(comp);
13181318
c_if_cond(comp, pns->nodes[0], true, l_end);
1319-
EMIT(load_id, MP_QSTR_AssertionError);
1319+
EMIT(load_global, MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
13201320
if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
13211321
// assertion message
13221322
compile_node(comp, pns->nodes[1]);

py/emitpass1.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ static void emit_pass1_load_id(emit_t *emit, qstr qstr) {
4545
bool added;
4646
id_info_t *id = scope_find_or_add_id(emit->scope, qstr, &added);
4747
if (added) {
48-
if (qstr == MP_QSTR_AssertionError) {
49-
// TODO how much of a hack is this?
50-
id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
51-
} else if (strcmp(qstr_str(qstr), "super") == 0 && emit->scope->kind == SCOPE_FUNCTION) {
48+
if (strcmp(qstr_str(qstr), "super") == 0 && emit->scope->kind == SCOPE_FUNCTION) {
5249
// special case, super is a global, and also counts as use of __class__
5350
id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
5451
id_info_t *id2 = scope_find_local_in_parent(emit->scope, emit->qstr___class__);

py/gc.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,8 @@
88

99
#if MICROPY_ENABLE_GC
1010

11-
// a machine word is big enough to hold a pointer
12-
/*
13-
#define BYTES_PER_WORD (8)
14-
typedef unsigned long machine_uint_t;
15-
*/
1611
typedef unsigned char byte;
1712

18-
#define BITS_PER_BYTE (8)
19-
#define BITS_PER_WORD (BITS_PER_BYTE * BYTES_PER_WORD)
2013
#define WORDS_PER_BLOCK (4)
2114
#define BYTES_PER_BLOCK (WORDS_PER_BLOCK * BYTES_PER_WORD)
2215
#define STACK_SIZE (64) // tunable; minimum is 1

py/lexer.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,15 @@ static void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
299299
// backslash (outside string literals) must appear just before a physical newline
300300
next_char(lex);
301301
if (!is_physical_newline(lex)) {
302-
// TODO SyntaxError
303-
assert(0);
302+
// SyntaxError: unexpected character after line continuation character
303+
tok->src_name = lex->name;
304+
tok->src_line = lex->line;
305+
tok->src_column = lex->column;
306+
tok->kind = MP_TOKEN_BAD_LINE_CONTINUATION;
307+
vstr_reset(&lex->vstr);
308+
tok->str = vstr_str(&lex->vstr);
309+
tok->len = 0;
310+
return;
304311
} else {
305312
next_char(lex);
306313
}

py/lexer.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@ typedef enum _mp_token_kind_t {
1010
MP_TOKEN_INVALID,
1111
MP_TOKEN_DEDENT_MISMATCH,
1212
MP_TOKEN_LONELY_STRING_OPEN,
13+
MP_TOKEN_BAD_LINE_CONTINUATION,
1314

14-
MP_TOKEN_NEWLINE, // 4
15-
MP_TOKEN_INDENT, // 5
16-
MP_TOKEN_DEDENT, // 6
15+
MP_TOKEN_NEWLINE, // 5
16+
MP_TOKEN_INDENT, // 6
17+
MP_TOKEN_DEDENT, // 7
1718

18-
MP_TOKEN_NAME, // 7
19+
MP_TOKEN_NAME, // 8
1920
MP_TOKEN_NUMBER,
2021
MP_TOKEN_STRING,
2122
MP_TOKEN_BYTES,
2223

2324
MP_TOKEN_ELLIPSIS,
2425

25-
MP_TOKEN_KW_FALSE, // 12
26+
MP_TOKEN_KW_FALSE, // 13
2627
MP_TOKEN_KW_NONE,
2728
MP_TOKEN_KW_TRUE,
2829
MP_TOKEN_KW_AND,
@@ -31,7 +32,7 @@ typedef enum _mp_token_kind_t {
3132
MP_TOKEN_KW_BREAK,
3233
MP_TOKEN_KW_CLASS,
3334
MP_TOKEN_KW_CONTINUE,
34-
MP_TOKEN_KW_DEF, // 21
35+
MP_TOKEN_KW_DEF, // 22
3536
MP_TOKEN_KW_DEL,
3637
MP_TOKEN_KW_ELIF,
3738
MP_TOKEN_KW_ELSE,
@@ -41,7 +42,7 @@ typedef enum _mp_token_kind_t {
4142
MP_TOKEN_KW_FROM,
4243
MP_TOKEN_KW_GLOBAL,
4344
MP_TOKEN_KW_IF,
44-
MP_TOKEN_KW_IMPORT, // 31
45+
MP_TOKEN_KW_IMPORT, // 32
4546
MP_TOKEN_KW_IN,
4647
MP_TOKEN_KW_IS,
4748
MP_TOKEN_KW_LAMBDA,
@@ -51,12 +52,12 @@ typedef enum _mp_token_kind_t {
5152
MP_TOKEN_KW_PASS,
5253
MP_TOKEN_KW_RAISE,
5354
MP_TOKEN_KW_RETURN,
54-
MP_TOKEN_KW_TRY, // 41
55+
MP_TOKEN_KW_TRY, // 42
5556
MP_TOKEN_KW_WHILE,
5657
MP_TOKEN_KW_WITH,
5758
MP_TOKEN_KW_YIELD,
5859

59-
MP_TOKEN_OP_PLUS, // 45
60+
MP_TOKEN_OP_PLUS, // 46
6061
MP_TOKEN_OP_MINUS,
6162
MP_TOKEN_OP_STAR,
6263
MP_TOKEN_OP_DBL_STAR,
@@ -66,7 +67,7 @@ typedef enum _mp_token_kind_t {
6667
MP_TOKEN_OP_LESS,
6768
MP_TOKEN_OP_DBL_LESS,
6869
MP_TOKEN_OP_MORE,
69-
MP_TOKEN_OP_DBL_MORE, // 55
70+
MP_TOKEN_OP_DBL_MORE, // 56
7071
MP_TOKEN_OP_AMPERSAND,
7172
MP_TOKEN_OP_PIPE,
7273
MP_TOKEN_OP_CARET,
@@ -76,7 +77,7 @@ typedef enum _mp_token_kind_t {
7677
MP_TOKEN_OP_DBL_EQUAL,
7778
MP_TOKEN_OP_NOT_EQUAL,
7879

79-
MP_TOKEN_DEL_PAREN_OPEN, // 64
80+
MP_TOKEN_DEL_PAREN_OPEN, // 65
8081
MP_TOKEN_DEL_PAREN_CLOSE,
8182
MP_TOKEN_DEL_BRACKET_OPEN,
8283
MP_TOKEN_DEL_BRACKET_CLOSE,
@@ -86,7 +87,7 @@ typedef enum _mp_token_kind_t {
8687
MP_TOKEN_DEL_COLON,
8788
MP_TOKEN_DEL_PERIOD,
8889
MP_TOKEN_DEL_SEMICOLON,
89-
MP_TOKEN_DEL_AT, // 74
90+
MP_TOKEN_DEL_AT, // 75
9091
MP_TOKEN_DEL_EQUAL,
9192
MP_TOKEN_DEL_PLUS_EQUAL,
9293
MP_TOKEN_DEL_MINUS_EQUAL,
@@ -96,7 +97,7 @@ typedef enum _mp_token_kind_t {
9697
MP_TOKEN_DEL_PERCENT_EQUAL,
9798
MP_TOKEN_DEL_AMPERSAND_EQUAL,
9899
MP_TOKEN_DEL_PIPE_EQUAL,
99-
MP_TOKEN_DEL_CARET_EQUAL, // 84
100+
MP_TOKEN_DEL_CARET_EQUAL, // 85
100101
MP_TOKEN_DEL_DBL_MORE_EQUAL,
101102
MP_TOKEN_DEL_DBL_LESS_EQUAL,
102103
MP_TOKEN_DEL_DBL_STAR_EQUAL,

py/map.c

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,44 +132,77 @@ void mp_set_init(mp_set_t *set, int n) {
132132
set->table = m_new0(mp_obj_t, set->alloc);
133133
}
134134

135-
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found) {
136-
int hash = mp_obj_hash(index);
137-
assert(set->alloc); /* FIXME: if alloc is ever 0 when doing a lookup, this'll fail: */
138-
int pos = hash % set->alloc;
135+
static void mp_set_rehash(mp_set_t *set) {
136+
int old_alloc = set->alloc;
137+
mp_obj_t *old_table = set->table;
138+
set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1);
139+
set->used = 0;
140+
set->table = m_new0(mp_obj_t, set->alloc);
141+
for (int i = 0; i < old_alloc; i++) {
142+
if (old_table[i] != NULL) {
143+
mp_set_lookup(set, old_table[i], true);
144+
}
145+
}
146+
m_del(mp_obj_t, old_table, old_alloc);
147+
}
148+
149+
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
150+
int hash;
151+
int pos;
152+
if (set->alloc == 0) {
153+
if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
154+
mp_set_rehash(set);
155+
} else {
156+
return NULL;
157+
}
158+
}
159+
if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
160+
hash = 0;
161+
pos = 0;
162+
} else {
163+
hash = mp_obj_hash(index);;
164+
pos = hash % set->alloc;
165+
}
139166
for (;;) {
140167
mp_obj_t elem = set->table[pos];
141168
if (elem == MP_OBJ_NULL) {
142169
// not in table
143-
if (add_if_not_found) {
170+
if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
144171
if (set->used + 1 >= set->alloc) {
145172
// not enough room in table, rehash it
146-
int old_alloc = set->alloc;
147-
mp_obj_t *old_table = set->table;
148-
set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1);
149-
set->used = 0;
150-
set->table = m_new(mp_obj_t, set->alloc);
151-
for (int i = 0; i < old_alloc; i++) {
152-
if (old_table[i] != NULL) {
153-
mp_set_lookup(set, old_table[i], true);
154-
}
155-
}
156-
m_del(mp_obj_t, old_table, old_alloc);
173+
mp_set_rehash(set);
157174
// restart the search for the new element
158175
pos = hash % set->alloc;
159176
} else {
160177
set->used += 1;
161178
set->table[pos] = index;
162179
return index;
163180
}
181+
} else if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
182+
pos++;
164183
} else {
165184
return MP_OBJ_NULL;
166185
}
167-
} else if (mp_obj_equal(elem, index)) {
186+
} else if (lookup_kind & MP_MAP_LOOKUP_FIRST || mp_obj_equal(elem, index)) {
168187
// found it
188+
if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
189+
set->used--;
190+
set->table[pos] = NULL;
191+
}
169192
return elem;
170193
} else {
171194
// not yet found, keep searching in this table
172195
pos = (pos + 1) % set->alloc;
173196
}
174197
}
175198
}
199+
200+
void mp_set_clear(mp_set_t *set) {
201+
set->used = 0;
202+
machine_uint_t a = set->alloc;
203+
set->alloc = 0;
204+
set->table = m_renew(mp_obj_t, set->table, a, set->alloc);
205+
for (uint i=0; i<set->alloc; i++) {
206+
set->table[i] = NULL;
207+
}
208+
}

0 commit comments

Comments
 (0)