Skip to content

Commit f5a0a7d

Browse files
committed
Merge remote-tracking branch 'upstream/master' into containment
2 parents 189c8e1 + ca318bb commit f5a0a7d

21 files changed

Lines changed: 2332 additions & 405 deletions

logo/vector-logo-3.png

111 KB
Loading

logo/vector-logo-inkscape_master.svg

Lines changed: 2133 additions & 248 deletions
Loading

py/builtin.c

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// args[0] is function from class body
1919
// args[1] is class name
2020
// args[2:] are base objects
21-
mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) {
21+
static mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) {
2222
assert(2 <= n_args);
2323

2424
// we differ from CPython: we set the new __locals__ object here
@@ -62,14 +62,16 @@ mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) {
6262

6363
MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__);
6464

65-
mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
65+
static mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
6666
if (o != mp_const_none) {
6767
mp_obj_print(o);
6868
printf("\n");
6969
}
7070
return mp_const_none;
7171
}
7272

73+
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin___repl_print___obj, mp_builtin___repl_print__);
74+
7375
mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
7476
if (MP_OBJ_IS_SMALL_INT(o_in)) {
7577
mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o_in);
@@ -97,7 +99,9 @@ mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
9799
}
98100
}
99101

100-
mp_obj_t mp_builtin_all(mp_obj_t o_in) {
102+
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs);
103+
104+
static mp_obj_t mp_builtin_all(mp_obj_t o_in) {
101105
mp_obj_t iterable = rt_getiter(o_in);
102106
mp_obj_t item;
103107
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
@@ -108,7 +112,9 @@ mp_obj_t mp_builtin_all(mp_obj_t o_in) {
108112
return mp_const_true;
109113
}
110114

111-
mp_obj_t mp_builtin_any(mp_obj_t o_in) {
115+
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all);
116+
117+
static mp_obj_t mp_builtin_any(mp_obj_t o_in) {
112118
mp_obj_t iterable = rt_getiter(o_in);
113119
mp_obj_t item;
114120
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
@@ -119,15 +125,19 @@ mp_obj_t mp_builtin_any(mp_obj_t o_in) {
119125
return mp_const_false;
120126
}
121127

122-
mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
128+
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any);
129+
130+
static mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
123131
if (mp_obj_is_callable(o_in)) {
124132
return mp_const_true;
125133
} else {
126134
return mp_const_false;
127135
}
128136
}
129137

130-
mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
138+
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
139+
140+
static mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
131141
int ord = mp_obj_get_int(o_in);
132142
if (0 <= ord && ord <= 0x10ffff) {
133143
char *str = m_new(char, 2);
@@ -139,7 +149,9 @@ mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
139149
}
140150
}
141151

142-
mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
152+
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr);
153+
154+
static mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
143155
if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) {
144156
mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);
145157
mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in);
@@ -152,6 +164,8 @@ mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
152164
}
153165
}
154166

167+
MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_divmod_obj, mp_builtin_divmod);
168+
155169
static mp_obj_t mp_builtin_hash(mp_obj_t o_in) {
156170
// TODO hash will generally overflow small integer; can we safely truncate it?
157171
return mp_obj_new_int(mp_obj_hash(o_in));
@@ -165,7 +179,7 @@ static mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
165179

166180
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter);
167181

168-
mp_obj_t mp_builtin_len(mp_obj_t o_in) {
182+
static mp_obj_t mp_builtin_len(mp_obj_t o_in) {
169183
mp_obj_t len = mp_obj_len_maybe(o_in);
170184
if (len == NULL) {
171185
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "object of type '%s' has no len()", mp_obj_get_type_str(o_in)));
@@ -174,7 +188,9 @@ mp_obj_t mp_builtin_len(mp_obj_t o_in) {
174188
}
175189
}
176190

177-
mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) {
191+
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_builtin_len);
192+
193+
static mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) {
178194
if (n_args == 1) {
179195
// given an iterable
180196
mp_obj_t iterable = rt_getiter(args[0]);
@@ -201,7 +217,9 @@ mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) {
201217
}
202218
}
203219

204-
mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) {
220+
MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_max_obj, 1, mp_builtin_max);
221+
222+
static mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) {
205223
if (n_args == 1) {
206224
// given an iterable
207225
mp_obj_t iterable = rt_getiter(args[0]);
@@ -228,6 +246,8 @@ mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) {
228246
}
229247
}
230248

249+
MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_min_obj, 1, mp_builtin_min);
250+
231251
static mp_obj_t mp_builtin_next(mp_obj_t o) {
232252
mp_obj_t ret = rt_iternext(o);
233253
if (ret == mp_const_stop_iteration) {
@@ -239,7 +259,7 @@ static mp_obj_t mp_builtin_next(mp_obj_t o) {
239259

240260
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
241261

242-
mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
262+
static mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
243263
const char *str = qstr_str(mp_obj_get_qstr(o_in));
244264
if (strlen(str) == 1) {
245265
return mp_obj_new_int(str[0]);
@@ -248,15 +268,19 @@ mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
248268
}
249269
}
250270

251-
mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args) {
271+
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
272+
273+
static mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args) {
274+
assert(2 <= n_args && n_args <= 3);
252275
switch (n_args) {
253276
case 2: return rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]);
254-
case 3: return rt_binary_op(RT_BINARY_OP_MODULO, rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]), args[2]); // TODO optimise...
255-
default: nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "pow expected at most 3 arguments, got %d", n_args));
277+
default: return rt_binary_op(RT_BINARY_OP_MODULO, rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]), args[2]); // TODO optimise...
256278
}
257279
}
258280

259-
mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) {
281+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow);
282+
283+
static mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) {
260284
for (int i = 0; i < n_args; i++) {
261285
if (i > 0) {
262286
printf(" ");
@@ -273,21 +297,25 @@ mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) {
273297
return mp_const_none;
274298
}
275299

276-
mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args) {
300+
MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_print_obj, 0, mp_builtin_print);
301+
302+
static mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args) {
303+
assert(1 <= n_args && n_args <= 3);
277304
switch (n_args) {
278305
case 1: return mp_obj_new_range(0, mp_obj_get_int(args[0]), 1);
279306
case 2: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), 1);
280-
case 3: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2]));
281-
default: nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "range expected at most 3 arguments, got %d", n_args));
307+
default: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2]));
282308
}
283309
}
284310

285-
mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
311+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_range_obj, 1, 3, mp_builtin_range);
312+
313+
static mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
314+
assert(1 <= n_args && n_args <= 2);
286315
mp_obj_t value;
287316
switch (n_args) {
288317
case 1: value = mp_obj_new_int(0); break;
289-
case 2: value = args[1]; break;
290-
default: nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "sum expected at most 2 arguments, got %d", n_args));
318+
default: value = args[1]; break;
291319
}
292320
mp_obj_t iterable = rt_getiter(args[0]);
293321
mp_obj_t item;
@@ -296,3 +324,5 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
296324
}
297325
return value;
298326
}
327+
328+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum);

py/builtin.h

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
// TODO convert all these to objects using MP_DECLARE and MP_DEFINE
1+
mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args);
22

33
MP_DECLARE_CONST_FUN_OBJ(mp_builtin___build_class___obj);
4-
mp_obj_t mp_builtin___import__(int n, mp_obj_t *args);
5-
mp_obj_t mp_builtin___repl_print__(mp_obj_t o);
6-
mp_obj_t mp_builtin_abs(mp_obj_t o_in);
7-
mp_obj_t mp_builtin_all(mp_obj_t o_in);
8-
mp_obj_t mp_builtin_any(mp_obj_t o_in);
9-
mp_obj_t mp_builtin_callable(mp_obj_t o_in);
10-
mp_obj_t mp_builtin_chr(mp_obj_t o_in);
11-
mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in);
4+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin___repl_print___obj);
5+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_abs_obj);
6+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_all_obj);
7+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_any_obj);
8+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_callable_obj);
9+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_chr_obj);
10+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_divmod_obj);
1211
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hash_obj);
1312
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_isinstance_obj);
1413
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_issubclass_obj);
1514
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_iter_obj);
16-
mp_obj_t mp_builtin_len(mp_obj_t o_in);
17-
mp_obj_t mp_builtin_list(int n_args, const mp_obj_t *args);
18-
mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args);
19-
mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args);
15+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_len_obj);
16+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_list_obj);
17+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_max_obj);
18+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_min_obj);
2019
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_next_obj);
21-
mp_obj_t mp_builtin_ord(mp_obj_t o_in);
22-
mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args);
23-
mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args);
24-
mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args);
25-
mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args);
20+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_ord_obj);
21+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_pow_obj);
22+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_print_obj);
23+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_range_obj);
24+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj);

py/builtinimport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "map.h"
1919
#include "builtin.h"
2020

21-
mp_obj_t mp_builtin___import__(int n, mp_obj_t *args) {
21+
mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) {
2222
/*
2323
printf("import:\n");
2424
for (int i = 0; i < n; i++) {

py/mpconfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ typedef long long mp_longint_impl_t;
9191
#define BITS_PER_BYTE (8)
9292
#define BITS_PER_WORD (BITS_PER_BYTE * BYTES_PER_WORD)
9393
// machine_int_t value with most significant bit set
94-
#define WORD_MSBIT_HIGH (1 << (BYTES_PER_WORD * 8 - 1))
94+
#define WORD_MSBIT_HIGH (((machine_uint_t)1) << (BYTES_PER_WORD * 8 - 1))
9595

9696
// printf format spec to use for machine_int_t and friends
9797
#ifndef INT_FMT

py/obj.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,15 @@ bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
108108
return val == 0;
109109
} else if (o2 == mp_const_true) {
110110
return val == 1;
111-
} else {
112-
return false;
111+
} else if (MP_OBJ_IS_TYPE(o2, &int_type)) {
112+
// If o2 is long int, dispatch to its virtual methods
113+
mp_obj_base_t *o = o2;
114+
if (o->type->binary_op != NULL) {
115+
mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o2, o1);
116+
return r == mp_const_true ? true : false;
117+
}
113118
}
119+
return false;
114120
}
115121
} else if (MP_OBJ_IS_QSTR(o1) || MP_OBJ_IS_QSTR(o2)) {
116122
return false;

py/objfun.c

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -98,45 +98,21 @@ const mp_obj_type_t fun_native_type = {
9898
.call_n_kw = fun_native_call_n_kw,
9999
};
100100

101-
mp_obj_t rt_make_function_0(mp_fun_0_t fun) {
101+
// fun must have the correct signature for n_args fixed arguments
102+
mp_obj_t rt_make_function_n(int n_args, void *fun) {
102103
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
103104
o->base.type = &fun_native_type;
104-
o->n_args_min = 0;
105-
o->n_args_max = 0;
106-
o->fun = fun;
107-
return o;
108-
}
109-
110-
mp_obj_t rt_make_function_1(mp_fun_1_t fun) {
111-
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
112-
o->base.type = &fun_native_type;
113-
o->n_args_min = 1;
114-
o->n_args_max = 1;
115-
o->fun = fun;
116-
return o;
117-
}
118-
119-
mp_obj_t rt_make_function_2(mp_fun_2_t fun) {
120-
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
121-
o->base.type = &fun_native_type;
122-
o->n_args_min = 2;
123-
o->n_args_max = 2;
124-
o->fun = fun;
125-
return o;
126-
}
127-
128-
mp_obj_t rt_make_function_3(mp_fun_3_t fun) {
129-
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
130-
o->base.type = &fun_native_type;
131-
o->n_args_min = 3;
132-
o->n_args_max = 3;
105+
o->is_kw = false;
106+
o->n_args_min = n_args;
107+
o->n_args_max = n_args;
133108
o->fun = fun;
134109
return o;
135110
}
136111

137112
mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) {
138113
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
139114
o->base.type = &fun_native_type;
115+
o->is_kw = false;
140116
o->n_args_min = n_args_min;
141117
o->n_args_max = ~((machine_uint_t)0);
142118
o->fun = fun;
@@ -147,6 +123,7 @@ mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) {
147123
mp_obj_t rt_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun) {
148124
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
149125
o->base.type = &fun_native_type;
126+
o->is_kw = false;
150127
o->n_args_min = n_args_min;
151128
o->n_args_max = n_args_max;
152129
o->fun = fun;

py/objint.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,7 @@
88
#include "mpconfig.h"
99
#include "mpqstr.h"
1010
#include "obj.h"
11-
12-
typedef struct _mp_obj_int_t {
13-
mp_obj_base_t base;
14-
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
15-
mp_longint_impl_t val;
16-
#endif
17-
} mp_obj_int_t;
18-
19-
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in);
20-
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);
11+
#include "objint.h"
2112

2213
// This dispatcher function is expected to be independent of the implementation
2314
// of long int
@@ -54,11 +45,13 @@ void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj
5445
// This is called only for non-SMALL_INT
5546
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
5647
assert(0);
48+
return mp_const_none;
5749
}
5850

5951
// This is called only with strings whose value doesn't fit in SMALL_INT
6052
mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
6153
assert(0);
54+
return mp_const_none;
6255
}
6356

6457
mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
@@ -69,6 +62,7 @@ mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
6962
}
7063
// TODO: Raise exception
7164
assert(0);
65+
return mp_const_none;
7266
}
7367

7468
mp_obj_t mp_obj_new_int(machine_int_t value) {
@@ -77,5 +71,6 @@ mp_obj_t mp_obj_new_int(machine_int_t value) {
7771
}
7872
// TODO: Raise exception
7973
assert(0);
74+
return mp_const_none;
8075
}
8176
#endif

py/objint.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
typedef struct _mp_obj_int_t {
2+
mp_obj_base_t base;
3+
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
4+
mp_longint_impl_t val;
5+
#endif
6+
} mp_obj_int_t;
7+
8+
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in);
9+
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);

0 commit comments

Comments
 (0)