From c90c0f68a2f1ee8563497a5380a16e71d18d702a Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 4 Jan 2014 01:57:00 +0200 Subject: Move INT_FMT, etc. declaration into global mpconfig.h . This in particular makes it available for stm port. --- py/mpconfig.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'py') diff --git a/py/mpconfig.h b/py/mpconfig.h index 17c5a770c..44095bd10 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -4,6 +4,20 @@ #include +#ifndef INT_FMT +// printf format spec to use for machine_int_t and friends +#ifdef __LP64__ +// Archs where machine_int_t == long, long != int +#define UINT_FMT "%lu" +#define INT_FMT "%ld" +#else +// Archs where machine_int_t == int +#define UINT_FMT "%u" +#define INT_FMT "%d" +#endif +#endif //INT_FMT + + // Any options not explicitly set in mpconfigport.h will get default // values below. -- cgit v1.2.3 From 1c6de11f772afae9b4155f8a654cadd05125a2de Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 3 Jan 2014 02:41:17 +0200 Subject: Add basic implementation of slice object. So far, only start and stop integer indexes are supported. Step is not supported, as well as objects of arbitrary types. --- py/obj.h | 5 +++++ py/objslice.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 py/objslice.c (limited to 'py') diff --git a/py/obj.h b/py/obj.h index 7b4b0656f..16c7c36dd 100644 --- a/py/obj.h +++ b/py/obj.h @@ -144,6 +144,7 @@ mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items); mp_obj_t mp_obj_new_list_reverse(uint n, mp_obj_t *items); mp_obj_t mp_obj_new_dict(int n_args); mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items); +mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step); mp_obj_t mp_obj_new_bound_meth(mp_obj_t self, mp_obj_t meth); mp_obj_t mp_obj_new_class(struct _mp_map_t *class_locals); mp_obj_t mp_obj_new_instance(mp_obj_t clas); @@ -214,6 +215,10 @@ mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value); // set void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item); +// slice +extern const mp_obj_type_t slice_type; +void mp_obj_slice_get(mp_obj_t self_in, machine_int_t *start, machine_int_t *stop, machine_int_t *step); + // functions typedef struct _mp_obj_fun_native_t { // need this so we can define const objects (to go in ROM) mp_obj_base_t base; diff --git a/py/objslice.c b/py/objslice.c new file mode 100644 index 000000000..619899b23 --- /dev/null +++ b/py/objslice.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +#include "nlr.h" +#include "misc.h" +#include "mpconfig.h" +#include "obj.h" +#include "runtime0.h" + +#if MICROPY_ENABLE_SLICE + +// TODO: This implements only variant of slice with 2 integer args only. +// CPython supports 3rd arg (step), plus args can be arbitrary Python objects. +typedef struct _mp_obj_slice_t { + mp_obj_base_t base; + machine_int_t start; + machine_int_t stop; +} mp_obj_slice_t; + +void slice_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) { + mp_obj_slice_t *o = o_in; + print(env, "slice(" INT_FMT ", " INT_FMT ")", o->start, o->stop); +} + +const mp_obj_type_t slice_type = { + { &mp_const_type }, + "slice", + slice_print, + NULL, // call_n + NULL, // unary_op + NULL, // binary_op + NULL, // getiter + NULL, // iternext + { { NULL, NULL }, }, // method list +}; + +// TODO: Make sure to handle "empty" values, which are signified by None in CPython +mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) { + assert(ostep == NULL); + machine_int_t start = mp_obj_get_int(ostart); + machine_int_t stop = mp_obj_get_int(ostop); + mp_obj_slice_t *o = m_new(mp_obj_slice_t, 1); + o->base.type = &slice_type; + o->start = start; + o->stop = stop; + return (mp_obj_t)o; +} + +void mp_obj_slice_get(mp_obj_t self_in, machine_int_t *start, machine_int_t *stop, machine_int_t *step) { + assert(MP_OBJ_IS_TYPE(self_in, &slice_type)); + mp_obj_slice_t *self = self_in; + *start = self->start; + *stop = self->stop; + *step = 1; +} + +#endif -- cgit v1.2.3 From ded0a1efa5744235fbb4cd07c96598018a3661eb Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 3 Jan 2014 02:48:56 +0200 Subject: Implement BUILD_SLICE opcode (2-arg version). --- py/vm.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'py') diff --git a/py/vm.c b/py/vm.c index c549e2b49..35dcbea52 100644 --- a/py/vm.c +++ b/py/vm.c @@ -410,6 +410,18 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t ** sp++; break; + case MP_BC_BUILD_SLICE: + DECODE_UINT; + if (unum == 2) { + obj2 = POP(); + obj1 = TOP(); + SET_TOP(mp_obj_new_slice(obj1, obj2, NULL)); + } else { + printf("3-argument slice is not supported\n"); + assert(0); + } + break; + case MP_BC_UNPACK_SEQUENCE: DECODE_UINT; rt_unpack_sequence(sp[0], unum, sp - unum + 1); -- cgit v1.2.3 From 31ba60f8364a4009ddc3d45fee90c84b43d88d2c Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 3 Jan 2014 02:51:16 +0200 Subject: str: Initial implementation of string slicing. Only step=1 and non-negative indexes are supported so far. --- py/objstr.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'py') diff --git a/py/objstr.c b/py/objstr.c index 48abf4951..46adabcec 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -29,7 +29,21 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { case RT_BINARY_OP_SUBSCR: // string access // XXX a massive hack! - return mp_obj_new_int(lhs_str[mp_obj_get_int(rhs_in)]); + + // TODO: need predicate to check for int-like type (bools are such for example) + // ["no", "yes"][1 == 2] is common idiom + if (MP_OBJ_IS_SMALL_INT(rhs_in)) { + // TODO: This implements byte string access for single index so far + return mp_obj_new_int(lhs_str[mp_obj_get_int(rhs_in)]); + } else if (MP_OBJ_IS_TYPE(rhs_in, &slice_type)) { + int start, stop, step; + mp_obj_slice_get(rhs_in, &start, &stop, &step); + assert(step == 1); + return mp_obj_new_str(qstr_from_strn_copy(lhs_str + start, stop - start)); + } else { + // Throw TypeError here + assert(0); + } case RT_BINARY_OP_ADD: case RT_BINARY_OP_INPLACE_ADD: -- cgit v1.2.3 From cd22627f781080fb245dd6999f2158c8099379b0 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 3 Jan 2014 03:01:12 +0200 Subject: Enable slice support in config. --- py/mpconfig.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'py') diff --git a/py/mpconfig.h b/py/mpconfig.h index 44095bd10..56495d915 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -25,3 +25,9 @@ #ifndef MICROPY_MEM_STATS #define MICROPY_MEM_STATS (1) #endif + +// Whether to support slice object and correspondingly +// slice subscript operators +#ifndef MICROPY_ENABLE_SLICE +#define MICROPY_ENABLE_SLICE (1) +#endif -- cgit v1.2.3 From 59800afae9f74571eea5bb463a28fd9bd8251150 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 3 Jan 2014 23:35:32 +0200 Subject: slice: Implement special handling of omitted start/stop indexes. --- py/objslice.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'py') diff --git a/py/objslice.c b/py/objslice.c index 619899b23..03607e4c3 100644 --- a/py/objslice.c +++ b/py/objslice.c @@ -39,8 +39,23 @@ const mp_obj_type_t slice_type = { // TODO: Make sure to handle "empty" values, which are signified by None in CPython mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) { assert(ostep == NULL); - machine_int_t start = mp_obj_get_int(ostart); - machine_int_t stop = mp_obj_get_int(ostop); + machine_int_t start = 0, stop = 0; + if (ostart != mp_const_none) { + start = mp_obj_get_int(ostart); + } + if (ostop != mp_const_none) { + stop = mp_obj_get_int(ostop); + if (stop == 0) { + // [x:0] is a special case - in our slice object, stop = 0 means + // "end of sequence". Fortunately, [x:0] is an empty seqence for + // any x (including negative). [x:x] is also always empty sequence. + // but x also can be 0. But note that b""[x:x] is b"" for any x (i.e. + // no IndexError, at least in Python 3.3.3). So, we just use -1's to + // signify that. -1 is catchy "special" number in case someone will + // try to print [x:0] slice ever. + start = stop = -1; + } + } mp_obj_slice_t *o = m_new(mp_obj_slice_t, 1); o->base.type = &slice_type; o->start = start; -- cgit v1.2.3 From decad08ef57aa3cf3960ce65e29b194cb97c6d22 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 3 Jan 2014 23:36:56 +0200 Subject: str: Handle non-positive slice indexes. --- py/objstr.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'py') diff --git a/py/objstr.c b/py/objstr.c index 46adabcec..54dd087a4 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -39,6 +39,13 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { int start, stop, step; mp_obj_slice_get(rhs_in, &start, &stop, &step); assert(step == 1); + int len = strlen(lhs_str); + if (start < 0) { + start = len + start; + } + if (stop <= 0) { + stop = len + stop; + } return mp_obj_new_str(qstr_from_strn_copy(lhs_str + start, stop - start)); } else { // Throw TypeError here -- cgit v1.2.3 From e606cb656165aff2424fb6ca45f09d606246d073 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 4 Jan 2014 01:34:23 +0200 Subject: slice: Allow building with MICROPY_ENABLE_SLICE=0. --- py/objstr.c | 2 ++ py/vm.c | 2 ++ 2 files changed, 4 insertions(+) (limited to 'py') diff --git a/py/objstr.c b/py/objstr.c index 54dd087a4..8e3e9d902 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -35,6 +35,7 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { if (MP_OBJ_IS_SMALL_INT(rhs_in)) { // TODO: This implements byte string access for single index so far return mp_obj_new_int(lhs_str[mp_obj_get_int(rhs_in)]); +#if MICROPY_ENABLE_SLICE } else if (MP_OBJ_IS_TYPE(rhs_in, &slice_type)) { int start, stop, step; mp_obj_slice_get(rhs_in, &start, &stop, &step); @@ -47,6 +48,7 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { stop = len + stop; } return mp_obj_new_str(qstr_from_strn_copy(lhs_str + start, stop - start)); +#endif } else { // Throw TypeError here assert(0); diff --git a/py/vm.c b/py/vm.c index 35dcbea52..382780640 100644 --- a/py/vm.c +++ b/py/vm.c @@ -410,6 +410,7 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t ** sp++; break; +#if MICROPY_ENABLE_SLICE case MP_BC_BUILD_SLICE: DECODE_UINT; if (unum == 2) { @@ -421,6 +422,7 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t ** assert(0); } break; +#endif case MP_BC_UNPACK_SEQUENCE: DECODE_UINT; -- cgit v1.2.3 From f8b9d3c41addea79851c355f014db9f0f256cdaf Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 4 Jan 2014 01:38:26 +0200 Subject: str: Throw TypeError for invalid index type and clean up comments. --- py/objstr.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'py') diff --git a/py/objstr.c b/py/objstr.c index 8e3e9d902..6a0721d45 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -27,13 +27,11 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { const char *lhs_str = qstr_str(lhs->qstr); switch (op) { case RT_BINARY_OP_SUBSCR: - // string access - // XXX a massive hack! - // TODO: need predicate to check for int-like type (bools are such for example) // ["no", "yes"][1 == 2] is common idiom if (MP_OBJ_IS_SMALL_INT(rhs_in)) { // TODO: This implements byte string access for single index so far + // TODO: Handle negative indexes. return mp_obj_new_int(lhs_str[mp_obj_get_int(rhs_in)]); #if MICROPY_ENABLE_SLICE } else if (MP_OBJ_IS_TYPE(rhs_in, &slice_type)) { @@ -50,8 +48,9 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { return mp_obj_new_str(qstr_from_strn_copy(lhs_str + start, stop - start)); #endif } else { - // Throw TypeError here - assert(0); + // Message doesn't match CPython, but we don't have so much bytes as they + // to spend them on verbose wording + nlr_jump(mp_obj_new_exception_msg(rt_q_TypeError, "index must be int")); } case RT_BINARY_OP_ADD: -- cgit v1.2.3 From c8d1384fc0c7aafa5dee3445ece20f4e43dfa9c1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 4 Jan 2014 01:06:10 +0000 Subject: Fix int -> machine_int_t; add print to slice test. --- py/objstr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'py') diff --git a/py/objstr.c b/py/objstr.c index 6a0721d45..59547e3cd 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -35,7 +35,7 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { return mp_obj_new_int(lhs_str[mp_obj_get_int(rhs_in)]); #if MICROPY_ENABLE_SLICE } else if (MP_OBJ_IS_TYPE(rhs_in, &slice_type)) { - int start, stop, step; + machine_int_t start, stop, step; mp_obj_slice_get(rhs_in, &start, &stop, &step); assert(step == 1); int len = strlen(lhs_str); -- cgit v1.2.3 From 6ee1e383d6b95d0bb5f2902ec91b8d831e4b5803 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 4 Jan 2014 03:47:34 +0200 Subject: str slice: Trim slice indexes to be in range. --- py/objstr.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'py') diff --git a/py/objstr.c b/py/objstr.c index 59547e3cd..54e6f3770 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -41,9 +41,20 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { int len = strlen(lhs_str); if (start < 0) { start = len + start; + if (start < 0) { + start = 0; + } + } else if (start > len) { + start = len; } if (stop <= 0) { stop = len + stop; + // CPython returns empty string in such case + if (stop < 0) { + stop = start; + } + } else if (stop > len) { + stop = len; } return mp_obj_new_str(qstr_from_strn_copy(lhs_str + start, stop - start)); #endif -- cgit v1.2.3 From e67ed5d285bb2ae7b83eb8be3ee488ab08fa4db1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 4 Jan 2014 13:55:24 +0000 Subject: Improve configurability for native x64/thumb emitter. With MICROPY_EMIT_X64 and MICROPY_EMIT_THUMB disabled, the respective emitters and assemblers will not be included in the code. This can significantly reduce binary size for unix version. --- py/asmthumb.c | 5 +++++ py/asmx64.c | 6 ++++++ py/compile.c | 7 ++++++- py/emitcpy.c | 1 + py/emitnative.c | 4 ++-- 5 files changed, 20 insertions(+), 3 deletions(-) (limited to 'py') diff --git a/py/asmthumb.c b/py/asmthumb.c index ee8041ac9..ba95d80c6 100644 --- a/py/asmthumb.c +++ b/py/asmthumb.c @@ -7,6 +7,9 @@ #include "mpconfig.h" #include "asmthumb.h" +// wrapper around everything in this file +#if MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB + #define UNSIGNED_FIT8(x) (((x) & 0xffffff00) == 0) #define UNSIGNED_FIT16(x) (((x) & 0xffff0000) == 0) #define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80) @@ -447,3 +450,5 @@ void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp asm_thumb_write_op16(as, OP_SVC(fun_id)); } } + +#endif // MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB diff --git a/py/asmx64.c b/py/asmx64.c index c425034ba..ed9ca80f5 100644 --- a/py/asmx64.c +++ b/py/asmx64.c @@ -6,6 +6,10 @@ #include "misc.h" #include "asmx64.h" +#include "mpconfig.h" + +// wrapper around everything in this file +#if MICROPY_EMIT_X64 #if defined(__OpenBSD__) || defined(__MACH__) #define MAP_ANONYMOUS MAP_ANON @@ -620,3 +624,5 @@ void asm_x64_call_ind(asm_x64_t* as, void *ptr, int temp_r64) { asm_x64_write_word32(as, ptr - (void*)(as->code_base + as->code_offset + 4)); */ } + +#endif // MICROPY_EMIT_X64 diff --git a/py/compile.c b/py/compile.c index 68ac20804..8db7c6d94 100644 --- a/py/compile.c +++ b/py/compile.c @@ -3083,11 +3083,13 @@ mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl) { // compile pass 2 and 3 #if !MICROPY_EMIT_CPYTHON emit_t *emit_bc = NULL; +#if MICROPY_EMIT_NATIVE emit_t *emit_native = NULL; #endif #if MICROPY_EMIT_INLINE_THUMB emit_inline_asm_t *emit_inline_thumb = NULL; #endif +#endif // !MICROPY_EMIT_CPYTHON for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) { if (false) { // dummy @@ -3115,6 +3117,8 @@ mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl) { comp->emit_method_table = &emit_cpython_method_table; #else switch (s->emit_options) { + +#if MICROPY_EMIT_NATIVE case EMIT_OPT_NATIVE_PYTHON: case EMIT_OPT_VIPER: #if MICROPY_EMIT_X64 @@ -3131,6 +3135,7 @@ mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl) { comp->emit = emit_native; comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER); break; +#endif // MICROPY_EMIT_NATIVE default: if (emit_bc == NULL) { @@ -3140,7 +3145,7 @@ mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl) { comp->emit_method_table = &emit_bc_method_table; break; } -#endif +#endif // !MICROPY_EMIT_CPYTHON // compile pass 2 and pass 3 compile_scope(comp, s, PASS_2); diff --git a/py/emitcpy.c b/py/emitcpy.c index 652617cc8..7b2d50fb7 100644 --- a/py/emitcpy.c +++ b/py/emitcpy.c @@ -13,6 +13,7 @@ #include "runtime0.h" #include "emit.h" +// wrapper around everything in this file #if MICROPY_EMIT_CPYTHON struct _emit_t { diff --git a/py/emitnative.c b/py/emitnative.c index a29922d96..cc00c5731 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -34,7 +34,7 @@ #include "runtime.h" // wrapper around everything in this file -#if N_X64 || N_THUMB +#if (MICROPY_EMIT_X64 && N_X64) || (MICROPY_EMIT_THUMB && N_THUMB) #if N_X64 @@ -1319,4 +1319,4 @@ const emit_method_table_t EXPORT_FUN(method_table) = { emit_native_yield_from, }; -#endif // N_X64 || N_THUMB +#endif // (MICROPY_EMIT_X64 && N_X64) || (MICROPY_EMIT_THUMB && N_THUMB) -- cgit v1.2.3 From eb7bfcb28697f6fb2d4d933bc39233aa15423a20 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 4 Jan 2014 15:57:35 +0000 Subject: Split qstr into pools, and put initial pool in ROM. Qstr's are now split into a linked-list of qstr pools. This has 2 benefits: the first pool can be in ROM (huge benefit, since we no longer use RAM for the core qstrs), and subsequent pools use m_new for the next pool instead of m_renew (thus avoiding a huge single table for all the qstrs). Still would be better to use a hash table, but this scheme takes us part of the way (eventually convert the pools to hash tables). Also fixed bug with import. Also improved the way the module code is referenced (not magic number 1 anymore). --- py/builtin.c | 25 +++++---- py/builtinimport.c | 5 +- py/compile.c | 76 +++++++++----------------- py/mpqstr.h | 13 +++++ py/mpqstrraw.h | 63 +++++++++++++++++++++ py/obj.c | 13 +++-- py/objclass.c | 7 ++- py/objdict.c | 3 +- py/objfun.c | 11 ++-- py/objgenerator.c | 3 +- py/objinstance.c | 3 +- py/objlist.c | 3 +- py/objstr.c | 7 ++- py/qstr.c | 101 ++++++++++++++++++++++++++-------- py/runtime.c | 157 +++++++++++++++++++++-------------------------------- py/runtime.h | 15 ----- py/runtime0.h | 2 +- 17 files changed, 286 insertions(+), 221 deletions(-) create mode 100644 py/mpqstr.h create mode 100644 py/mpqstrraw.h (limited to 'py') diff --git a/py/builtin.c b/py/builtin.c index d29a2bf8c..b565ed464 100644 --- a/py/builtin.c +++ b/py/builtin.c @@ -8,6 +8,7 @@ #include "nlr.h" #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "obj.h" #include "runtime0.h" #include "runtime.h" @@ -91,7 +92,7 @@ mp_obj_t mp_builtin_bool(int n_args, const mp_obj_t *args) { switch (n_args) { case 0: return mp_const_false; case 1: if (rt_is_true(args[0])) { return mp_const_true; } else { return mp_const_false; } - default: nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "bool() takes at most 1 argument (%d given)", (void*)(machine_int_t)n_args)); + default: nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "bool() takes at most 1 argument (%d given)", (void*)(machine_int_t)n_args)); } } @@ -147,7 +148,7 @@ mp_obj_t mp_builtin_chr(mp_obj_t o_in) { str[1] = '\0'; return mp_obj_new_str(qstr_from_str_take(str, 2)); } else { - nlr_jump(mp_obj_new_exception_msg(rt_q_ValueError, "chr() arg not in range(0x110000)")); + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "chr() arg not in range(0x110000)")); } } @@ -165,7 +166,7 @@ mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) { revs_args[0] = MP_OBJ_NEW_SMALL_INT(i1 % i2); return rt_build_tuple(2, revs_args); } else { - nlr_jump(mp_obj_new_exception_msg_2_args(rt_q_TypeError, "unsupported operand type(s) for divmod(): '%s' and '%s'", mp_obj_get_type_str(o1_in), mp_obj_get_type_str(o2_in))); + nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_TypeError, "unsupported operand type(s) for divmod(): '%s' and '%s'", mp_obj_get_type_str(o1_in), mp_obj_get_type_str(o2_in))); } } @@ -235,7 +236,7 @@ mp_obj_t mp_builtin_len(mp_obj_t o_in) { } else if (MP_OBJ_IS_TYPE(o_in, &dict_type)) { len = mp_obj_dict_len(o_in); } else { - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "object of type '%s' has no len()", mp_obj_get_type_str(o_in))); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "object of type '%s' has no len()", mp_obj_get_type_str(o_in))); } return MP_OBJ_NEW_SMALL_INT(len); } @@ -254,7 +255,7 @@ mp_obj_t mp_builtin_list(int n_args, const mp_obj_t *args) { } return list; } - default: nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "list() takes at most 1 argument (%d given)", (void*)(machine_int_t)n_args)); + default: nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "list() takes at most 1 argument (%d given)", (void*)(machine_int_t)n_args)); } } @@ -270,7 +271,7 @@ mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) { } } if (max_obj == NULL) { - nlr_jump(mp_obj_new_exception_msg(rt_q_ValueError, "max() arg is an empty sequence")); + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "max() arg is an empty sequence")); } return max_obj; } else { @@ -297,7 +298,7 @@ mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) { } } if (min_obj == NULL) { - nlr_jump(mp_obj_new_exception_msg(rt_q_ValueError, "min() arg is an empty sequence")); + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "min() arg is an empty sequence")); } return min_obj; } else { @@ -315,7 +316,7 @@ mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) { static mp_obj_t mp_builtin_next(mp_obj_t o) { mp_obj_t ret = rt_iternext(o); if (ret == mp_const_stop_iteration) { - nlr_jump(mp_obj_new_exception(qstr_from_str_static("StopIteration"))); + nlr_jump(mp_obj_new_exception(MP_QSTR_StopIteration)); } else { return ret; } @@ -328,7 +329,7 @@ mp_obj_t mp_builtin_ord(mp_obj_t o_in) { if (strlen(str) == 1) { return mp_obj_new_int(str[0]); } else { - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "ord() expected a character, but string of length %d found", (void*)(machine_int_t)strlen(str))); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "ord() expected a character, but string of length %d found", (void*)(machine_int_t)strlen(str))); } } @@ -336,7 +337,7 @@ mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args) { switch (n_args) { case 2: return rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]); 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... - default: nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "pow expected at most 3 arguments, got %d", (void*)(machine_int_t)n_args)); + default: nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "pow expected at most 3 arguments, got %d", (void*)(machine_int_t)n_args)); } } @@ -362,7 +363,7 @@ mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args) { case 1: return mp_obj_new_range(0, mp_obj_get_int(args[0]), 1); case 2: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), 1); 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])); - default: nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "range expected at most 3 arguments, got %d", (void*)(machine_int_t)n_args)); + default: nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "range expected at most 3 arguments, got %d", (void*)(machine_int_t)n_args)); } } @@ -391,7 +392,7 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { switch (n_args) { case 1: value = mp_obj_new_int(0); break; case 2: value = args[1]; break; - default: nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "sum expected at most 2 arguments, got %d", (void*)(machine_int_t)n_args)); + default: nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "sum expected at most 2 arguments, got %d", (void*)(machine_int_t)n_args)); } mp_obj_t iterable = rt_getiter(args[0]); mp_obj_t item; diff --git a/py/builtinimport.c b/py/builtinimport.c index 47dbf2121..90a0fc339 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -58,7 +58,9 @@ mp_obj_t mp_builtin___import__(int n, mp_obj_t *args) { return mp_const_none; } - if (!mp_compile(pn, false)) { + mp_obj_t module_fun = mp_compile(pn, false); + + if (module_fun == mp_const_none) { // TODO handle compile error correctly rt_locals_set(old_locals); rt_globals_set(old_globals); @@ -66,7 +68,6 @@ mp_obj_t mp_builtin___import__(int n, mp_obj_t *args) { } // complied successfully, execute it - mp_obj_t module_fun = rt_make_function_from_id(1); // TODO we should return from mp_compile the unique_code_id for the module nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { rt_call_function_0(module_fun); diff --git a/py/compile.c b/py/compile.c index 8db7c6d94..f8fa2cb2c 100644 --- a/py/compile.c +++ b/py/compile.c @@ -7,6 +7,7 @@ #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "lexer.h" #include "parse.h" #include "scope.h" @@ -38,20 +39,6 @@ typedef enum { #define EMIT_OPT_ASM_THUMB (4) typedef struct _compiler_t { - qstr qstr___class__; - qstr qstr___locals__; - qstr qstr___name__; - qstr qstr___module__; - qstr qstr___qualname__; - qstr qstr___doc__; - qstr qstr_assertion_error; - qstr qstr_micropython; - qstr qstr_byte_code; - qstr qstr_native; - qstr qstr_viper; - qstr qstr_asm_thumb; - qstr qstr_range; - bool is_repl; pass_kind_t pass; bool had_error; // try to keep compiler clean from nlr @@ -202,7 +189,7 @@ static int comp_next_label(compiler_t *comp) { } static scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) { - scope_t *scope = scope_new(kind, pn, rt_get_unique_code_id(kind == SCOPE_MODULE), emit_options); + scope_t *scope = scope_new(kind, pn, rt_get_unique_code_id(), emit_options); scope->parent = comp->scope_cur; scope->next = NULL; if (comp->scope_head == NULL) { @@ -903,7 +890,7 @@ qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint // returns true if it was a built-in decorator (even if the built-in had an error) static bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_node_t *name_nodes, uint *emit_options) { - if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != comp->qstr_micropython) { + if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) { return false; } @@ -913,16 +900,16 @@ static bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_ } qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]); - if (attr == comp->qstr_byte_code) { + if (attr == MP_QSTR_byte_code) { *emit_options = EMIT_OPT_BYTE_CODE; #if MICROPY_EMIT_NATIVE - } else if (attr == comp->qstr_native) { + } else if (attr == MP_QSTR_native) { *emit_options = EMIT_OPT_NATIVE_PYTHON; - } else if (attr == comp->qstr_viper) { + } else if (attr == MP_QSTR_viper) { *emit_options = EMIT_OPT_VIPER; #endif #if MICROPY_EMIT_INLINE_THUMB - } else if (attr == comp->qstr_asm_thumb) { + } else if (attr == MP_QSTR_asm_thumb) { *emit_options = EMIT_OPT_ASM_THUMB; #endif } else { @@ -1329,7 +1316,7 @@ void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { int l_end = comp_next_label(comp); c_if_cond(comp, pns->nodes[0], true, l_end); - EMIT(load_id, comp->qstr_assertion_error); + EMIT(load_id, MP_QSTR_assertion_error); if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) { // assertion message compile_node(comp, pns->nodes[1]); @@ -1495,7 +1482,7 @@ void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { // for viper it will be much, much faster if (/*comp->scope_cur->emit_options == EMIT_OPT_VIPER &&*/ MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_power)) { mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1]; - if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == comp->qstr_range && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren) && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) { + if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren) && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) { mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0]; mp_parse_node_t *args; int n_args = list_get(&pn_range_args, PN_arglist, &args); @@ -1743,7 +1730,7 @@ void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) { if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) { // for REPL, evaluate then print the expression - EMIT(load_id, qstr_from_str_static("__repl_print__")); + EMIT(load_id, MP_QSTR___repl_print__); compile_node(comp, pns->nodes[0]); EMIT(call_function, 1, 0, false, false); EMIT(pop_top); @@ -2683,7 +2670,7 @@ void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) { if (kind == MP_PARSE_NODE_STRING) { compile_node(comp, pns->nodes[0]); // a doc string // store doc string - EMIT(store_id, comp->qstr___doc__); + EMIT(store_id, MP_QSTR___doc__); } } } @@ -2796,35 +2783,35 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) { if (comp->pass == PASS_1) { bool added; - id_info_t *id_info = scope_find_or_add_id(scope, comp->qstr___class__, &added); + id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added); assert(added); id_info->kind = ID_INFO_KIND_LOCAL; - id_info = scope_find_or_add_id(scope, comp->qstr___locals__, &added); + id_info = scope_find_or_add_id(scope, MP_QSTR___locals__, &added); assert(added); id_info->kind = ID_INFO_KIND_LOCAL; id_info->param = true; scope->num_params = 1; // __locals__ is the parameter } - EMIT(load_id, comp->qstr___locals__); + EMIT(load_id, MP_QSTR___locals__); EMIT(store_locals); - EMIT(load_id, comp->qstr___name__); - EMIT(store_id, comp->qstr___module__); + EMIT(load_id, MP_QSTR___name__); + EMIT(store_id, MP_QSTR___module__); EMIT(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name - EMIT(store_id, comp->qstr___qualname__); + EMIT(store_id, MP_QSTR___qualname__); check_for_doc_string(comp, pns->nodes[2]); compile_node(comp, pns->nodes[2]); // 2 is class body - id_info_t *id = scope_find(scope, comp->qstr___class__); + id_info_t *id = scope_find(scope, MP_QSTR___class__); assert(id != NULL); if (id->kind == ID_INFO_KIND_LOCAL) { EMIT(load_const_tok, MP_TOKEN_KW_NONE); } else { #if MICROPY_EMIT_CPYTHON - EMIT(load_closure, comp->qstr___class__, 0); // XXX check this is the correct local num + EMIT(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num #else - EMIT(load_fast, comp->qstr___class__, 0); // XXX check this is the correct local num + EMIT(load_fast, MP_QSTR___class__, 0); // XXX check this is the correct local num #endif } EMIT(return_value); @@ -2917,7 +2904,7 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) { scope->num_locals = 0; for (int i = 0; i < scope->id_info_len; i++) { id_info_t *id = &scope->id_info[i]; - if (scope->kind == SCOPE_CLASS && id->qstr == comp->qstr___class__) { + if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) { // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL continue; } @@ -3021,20 +3008,6 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) { mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl) { compiler_t *comp = m_new(compiler_t, 1); - comp->qstr___class__ = qstr_from_str_static("__class__"); - comp->qstr___locals__ = qstr_from_str_static("__locals__"); - comp->qstr___name__ = qstr_from_str_static("__name__"); - comp->qstr___module__ = qstr_from_str_static("__module__"); - comp->qstr___qualname__ = qstr_from_str_static("__qualname__"); - comp->qstr___doc__ = qstr_from_str_static("__doc__"); - comp->qstr_assertion_error = qstr_from_str_static("AssertionError"); - comp->qstr_micropython = qstr_from_str_static("micropython"); - comp->qstr_byte_code = qstr_from_str_static("byte_code"); - comp->qstr_native = qstr_from_str_static("native"); - comp->qstr_viper = qstr_from_str_static("viper"); - comp->qstr_asm_thumb = qstr_from_str_static("asm_thumb"); - comp->qstr_range = qstr_from_str_static("range"); - comp->is_repl = is_repl; comp->had_error = false; @@ -3048,10 +3021,10 @@ mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl) { pn = fold_constants(pn); // set the outer scope - scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE); + scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE); // compile pass 1 - comp->emit = emit_pass1_new(comp->qstr___class__); + comp->emit = emit_pass1_new(MP_QSTR___class__); comp->emit_method_table = &emit_pass1_method_table; comp->emit_inline_asm = NULL; comp->emit_inline_asm_method_table = NULL; @@ -3162,10 +3135,11 @@ mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl) { } else { #if MICROPY_EMIT_CPYTHON // can't create code, so just return true + (void)module_scope; // to suppress warning that module_scope is unused return mp_const_true; #else // return function that executes the outer module - return rt_make_function_from_id(1); + return rt_make_function_from_id(module_scope->unique_code_id); #endif } } diff --git a/py/mpqstr.h b/py/mpqstr.h new file mode 100644 index 000000000..1440fb3b8 --- /dev/null +++ b/py/mpqstr.h @@ -0,0 +1,13 @@ +// See mpqstrraw.h for a list of qstr's that are available as constants. +// Reference them as MP_QSTR_xxxx. +// +// Note: it would be possible to define MP_QSTR_xxx as qstr_from_str_static("xxx") +// for qstrs that are referenced this way, but you don't want to have them in ROM. + +enum { + MP_QSTR_nil = 0, +#define Q(id) MP_QSTR_##id, +#include "mpqstrraw.h" +#undef Q + MP_QSTR_number_of, +} category_t; diff --git a/py/mpqstrraw.h b/py/mpqstrraw.h new file mode 100644 index 000000000..85cf1ff7b --- /dev/null +++ b/py/mpqstrraw.h @@ -0,0 +1,63 @@ +// All the qstr definitions in this file are available as constants. +// That is, they are in ROM and you can reference them simple as MP_QSTR_xxxx. + +Q(__build_class__) +Q(__class__) +Q(__doc__) +Q(__init__) +Q(__locals__) +Q(__main__) +Q(__module__) +Q(__name__) +Q(__next__) +Q(__qualname__) +Q(__repl_print__) + +Q(assertion_error) +Q(micropython) +Q(byte_code) +Q(native) +Q(viper) +Q(asm_thumb) + +Q(StopIteration) + +Q(AttributeError) +Q(IndexError) +Q(KeyError) +Q(NameError) +Q(TypeError) +Q(SyntaxError) +Q(ValueError) + +Q(abs) +Q(all) +Q(any) +Q(bool) +Q(callable) +Q(chr) +Q(complex) +Q(dict) +Q(divmod) +Q(float) +Q(hash) +Q(int) +Q(iter) +Q(len) +Q(list) +Q(max) +Q(min) +Q(next) +Q(ord) +Q(pow) +Q(print) +Q(range) +Q(set) +Q(sum) +Q(type) + +Q(append) +Q(pop) +Q(sort) +Q(join) +Q(format) diff --git a/py/obj.c b/py/obj.c index d88d0ac3d..2b834ffe4 100644 --- a/py/obj.c +++ b/py/obj.c @@ -7,6 +7,7 @@ #include "nlr.h" #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "obj.h" #include "runtime0.h" #include "runtime.h" @@ -144,7 +145,7 @@ machine_float_t mp_obj_get_float(mp_obj_t arg) { } else if (MP_OBJ_IS_TYPE(arg, &float_type)) { return mp_obj_float_get(arg); } else { - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "can't convert %s to float", mp_obj_get_type_str(arg))); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "can't convert %s to float", mp_obj_get_type_str(arg))); } } @@ -164,7 +165,7 @@ void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) { } else if (MP_OBJ_IS_TYPE(arg, &complex_type)) { mp_obj_complex_get(arg, real, imag); } else { - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "can't convert %s to complex", mp_obj_get_type_str(arg))); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "can't convert %s to complex", mp_obj_get_type_str(arg))); } } #endif @@ -188,11 +189,11 @@ mp_obj_t *mp_obj_get_array_fixed_n(mp_obj_t o_in, machine_int_t n) { mp_obj_list_get(o_in, &seq_len, &seq_items); } if (seq_len != n) { - nlr_jump(mp_obj_new_exception_msg_2_args(rt_q_IndexError, "requested length %d but object has length %d", (void*)n, (void*)(machine_uint_t)seq_len)); + nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_IndexError, "requested length %d but object has length %d", (void*)n, (void*)(machine_uint_t)seq_len)); } return seq_items; } else { - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "object '%s' is not a tuple or list", mp_obj_get_type_str(o_in))); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "object '%s' is not a tuple or list", mp_obj_get_type_str(o_in))); } } @@ -204,10 +205,10 @@ uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index) i += len; } if (i < 0 || i >= len) { - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_IndexError, "%s index out of range", type->name)); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_IndexError, "%s index out of range", type->name)); } return i; } else { - nlr_jump(mp_obj_new_exception_msg_2_args(rt_q_TypeError, "%s indices must be integers, not %s", type->name, mp_obj_get_type_str(index))); + nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_TypeError, "%s indices must be integers, not %s", type->name, mp_obj_get_type_str(index))); } } diff --git a/py/objclass.c b/py/objclass.c index 203923a72..f223c5ff2 100644 --- a/py/objclass.c +++ b/py/objclass.c @@ -6,6 +6,7 @@ #include "nlr.h" #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "obj.h" #include "runtime.h" #include "map.h" @@ -25,7 +26,7 @@ mp_obj_t class_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { mp_obj_t o = mp_obj_new_instance(self_in); // look for __init__ function - mp_map_elem_t *init_fn = mp_qstr_map_lookup(self->locals, qstr_from_str_static("__init__"), false); + mp_map_elem_t *init_fn = mp_qstr_map_lookup(self->locals, MP_QSTR___init__, false); if (init_fn != NULL) { // call __init__ function @@ -40,13 +41,13 @@ mp_obj_t class_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { m_del(mp_obj_t, args2, n_args + 1); } if (init_ret != mp_const_none) { - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "__init__() should return None, not '%s'", mp_obj_get_type_str(init_ret))); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "__init__() should return None, not '%s'", mp_obj_get_type_str(init_ret))); } } else { // TODO if (n_args != 0) { - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "function takes 0 positional arguments but %d were given", (void*)(machine_int_t)n_args)); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "function takes 0 positional arguments but %d were given", (void*)(machine_int_t)n_args)); } } diff --git a/py/objdict.c b/py/objdict.c index 50ce27904..acf1a9f80 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -6,6 +6,7 @@ #include "nlr.h" #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "obj.h" #include "runtime0.h" #include "runtime.h" @@ -42,7 +43,7 @@ mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { // dict load mp_map_elem_t *elem = mp_map_lookup_helper(&o->map, rhs_in, false); if (elem == NULL) { - nlr_jump(mp_obj_new_exception_msg(rt_q_KeyError, "")); + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "")); } else { return elem->value; } diff --git a/py/objfun.c b/py/objfun.c index e998bd28d..c4783867a 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -6,6 +6,7 @@ #include "nlr.h" #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "obj.h" #include "map.h" #include "runtime.h" @@ -24,7 +25,7 @@ mp_obj_t fun_native_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { // check number of arguments if (n_args != self->n_args_min) { - nlr_jump(mp_obj_new_exception_msg_2_args(rt_q_TypeError, "function takes %d positional arguments but %d were given", (const char*)(machine_int_t)self->n_args_min, (const char*)(machine_int_t)n_args)); + nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_TypeError, "function takes %d positional arguments but %d were given", (const char*)(machine_int_t)self->n_args_min, (const char*)(machine_int_t)n_args)); } // dispatch function call @@ -47,9 +48,9 @@ mp_obj_t fun_native_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { // function takes a variable number of arguments if (n_args < self->n_args_min) { - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "() missing %d required positional arguments: ", (const char*)(machine_int_t)(self->n_args_min - n_args))); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "() missing %d required positional arguments: ", (const char*)(machine_int_t)(self->n_args_min - n_args))); } else if (n_args > self->n_args_max) { - nlr_jump(mp_obj_new_exception_msg_2_args(rt_q_TypeError, " expected at most %d arguments, got %d", (void*)(machine_int_t)self->n_args_max, (void*)(machine_int_t)n_args)); + nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_TypeError, " expected at most %d arguments, got %d", (void*)(machine_int_t)self->n_args_max, (void*)(machine_int_t)n_args)); } // TODO really the args need to be passed in as a Python tuple, as the form f(*[1,2]) can be used to pass var args @@ -141,7 +142,7 @@ mp_obj_t fun_bc_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { mp_obj_fun_bc_t *self = self_in; if (n_args != self->n_args) { - nlr_jump(mp_obj_new_exception_msg_2_args(rt_q_TypeError, "function takes %d positional arguments but %d were given", (const char*)(machine_int_t)self->n_args, (const char*)(machine_int_t)n_args)); + nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_TypeError, "function takes %d positional arguments but %d were given", (const char*)(machine_int_t)self->n_args, (const char*)(machine_int_t)n_args)); } // optimisation: allow the compiler to optimise this tail call for @@ -250,7 +251,7 @@ mp_obj_t fun_asm_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { mp_obj_fun_asm_t *self = self_in; if (n_args != self->n_args) { - nlr_jump(mp_obj_new_exception_msg_2_args(rt_q_TypeError, "function takes %d positional arguments but %d were given", (const char*)(machine_int_t)self->n_args, (const char*)(machine_int_t)n_args)); + nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_TypeError, "function takes %d positional arguments but %d were given", (const char*)(machine_int_t)self->n_args, (const char*)(machine_int_t)n_args)); } machine_uint_t ret; diff --git a/py/objgenerator.c b/py/objgenerator.c index ecdd72dc6..849212269 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -6,6 +6,7 @@ #include "nlr.h" #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "obj.h" #include "runtime.h" #include "bc.h" @@ -29,7 +30,7 @@ mp_obj_t gen_wrap_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { const byte *bc_code; mp_obj_fun_bc_get(self_fun, &bc_n_args, &bc_n_state, &bc_code); if (n_args != bc_n_args) { - nlr_jump(mp_obj_new_exception_msg_2_args(rt_q_TypeError, "function takes %d positional arguments but %d were given", (const char*)(machine_int_t)bc_n_args, (const char*)(machine_int_t)n_args)); + nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_TypeError, "function takes %d positional arguments but %d were given", (const char*)(machine_int_t)bc_n_args, (const char*)(machine_int_t)n_args)); } return mp_obj_new_gen_instance(bc_code, self->n_state, n_args, args); diff --git a/py/objinstance.c b/py/objinstance.c index e5d23af2d..6cfcdf6c1 100644 --- a/py/objinstance.c +++ b/py/objinstance.c @@ -6,6 +6,7 @@ #include "nlr.h" #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "obj.h" #include "runtime.h" #include "map.h" @@ -44,7 +45,7 @@ mp_obj_t mp_obj_instance_load_attr(mp_obj_t self_in, qstr attr) { return elem->value; } } - nlr_jump(mp_obj_new_exception_msg_2_args(rt_q_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(self_in), qstr_str(attr))); + nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(self_in), qstr_str(attr))); } void mp_obj_instance_load_method(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { diff --git a/py/objlist.c b/py/objlist.c index e37105798..30d3a41e4 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -6,6 +6,7 @@ #include "nlr.h" #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "obj.h" #include "runtime0.h" #include "runtime.h" @@ -81,7 +82,7 @@ static mp_obj_t list_pop(int n_args, const mp_obj_t *args) { assert(MP_OBJ_IS_TYPE(args[0], &list_type)); mp_obj_list_t *self = args[0]; if (self->len == 0) { - nlr_jump(mp_obj_new_exception_msg(rt_q_IndexError, "pop from empty list")); + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "pop from empty list")); } uint index = mp_get_index(self->base.type, self->len, n_args == 1 ? mp_obj_new_int(-1) : args[1]); mp_obj_t ret = self->items[index]; diff --git a/py/objstr.c b/py/objstr.c index 54e6f3770..03a761863 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -7,6 +7,7 @@ #include "nlr.h" #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "obj.h" #include "runtime0.h" #include "runtime.h" @@ -61,7 +62,7 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { } else { // Message doesn't match CPython, but we don't have so much bytes as they // to spend them on verbose wording - nlr_jump(mp_obj_new_exception_msg(rt_q_TypeError, "index must be int")); + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "index must be int")); } case RT_BINARY_OP_ADD: @@ -134,7 +135,7 @@ mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) { return mp_obj_new_str(qstr_from_str_take(joined_str, required_len + 1)); bad_arg: - nlr_jump(mp_obj_new_exception_msg(rt_q_TypeError, "?str.join expecting a list of str's")); + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "?str.join expecting a list of str's")); } void vstr_printf_wrapper(void *env, const char *fmt, ...) { @@ -158,7 +159,7 @@ mp_obj_t str_format(int n_args, const mp_obj_t *args) { vstr_add_char(vstr, '{'); } else if (*str == '}') { if (arg_i >= n_args) { - nlr_jump(mp_obj_new_exception_msg(rt_q_IndexError, "tuple index out of range")); + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "tuple index out of range")); } mp_obj_print_helper(vstr_printf_wrapper, vstr, args[arg_i]); arg_i++; diff --git a/py/qstr.c b/py/qstr.c index 0dd8a04b7..0ed7aa9a2 100644 --- a/py/qstr.c +++ b/py/qstr.c @@ -2,55 +2,110 @@ #include #include "misc.h" +#include "mpqstr.h" -static int qstrs_alloc; -static int qstrs_len; -static const char **qstrs; +// NOTE: we are using linear arrays to store and search for qstr's (unique strings, interned strings) +// ultimately we will replace this with a static hash table of some kind +// also probably need to include the length in the string data, to allow null bytes in the string + +#if 0 // print debugging info +#include +#define DEBUG_printf(args...) printf(args) +#else // don't print debugging info +#define DEBUG_printf(args...) (void)0 +#endif + +typedef struct _qstr_pool_t { + struct _qstr_pool_t *prev; + uint total_prev_len; + uint alloc; + uint len; + const char *qstrs[]; +} qstr_pool_t; + +const static qstr_pool_t const_pool = { + NULL, // no previous pool + 0, // no previous pool + 10, // set so that the first dynamically allocated pool is twice this size; must be <= the len (just below) + MP_QSTR_number_of, // corresponds to number of strings in array just below + { + "nil", // must be first, since 0 qstr is nil +#define Q(id) #id, +#include "mpqstrraw.h" +#undef Q + }, +}; + +static qstr_pool_t *last_pool = (qstr_pool_t*)&const_pool; // we won't modify the const_pool since it has no allocated room left void qstr_init(void) { - qstrs_alloc = 400; - qstrs_len = 1; - qstrs = m_new(const char*, qstrs_alloc); - qstrs[0] = "nil"; + // nothing to do! } static qstr qstr_add(const char *str) { - if (qstrs_len >= qstrs_alloc) { - qstrs = m_renew(const char*, qstrs, qstrs_alloc, qstrs_alloc * 2); - qstrs_alloc *= 2; + DEBUG_printf("QSTR: add %s\n", str); + + // make sure we have room in the pool for a new qstr + if (last_pool->len >= last_pool->alloc) { + qstr_pool_t *pool = m_new_obj_var(qstr_pool_t, const char*, last_pool->alloc * 2); + pool->prev = last_pool; + pool->total_prev_len = last_pool->total_prev_len + last_pool->len; + pool->alloc = last_pool->alloc * 2; + pool->len = 0; + last_pool = pool; + DEBUG_printf("QSTR: allocate new pool of size %d\n", last_pool->alloc); } - qstrs[qstrs_len++] = str; - return qstrs_len - 1; + + // add the new qstr + last_pool->qstrs[last_pool->len++] = str; + + // return id for the newly-added qstr + return last_pool->total_prev_len + last_pool->len - 1; } qstr qstr_from_str_static(const char *str) { - for (int i = 0; i < qstrs_len; i++) { - if (strcmp(qstrs[i], str) == 0) { - return i; + for (qstr_pool_t *pool = last_pool; pool != NULL; pool = pool->prev) { + for (const char **qstr = pool->qstrs, **qstr_top = pool->qstrs + pool->len; qstr < qstr_top; qstr++) { + if (strcmp(*qstr, str) == 0) { + return pool->total_prev_len + (qstr - pool->qstrs); + } } } return qstr_add(str); } qstr qstr_from_str_take(char *str, int alloc_len) { - for (int i = 0; i < qstrs_len; i++) { - if (strcmp(qstrs[i], str) == 0) { - m_del(char, str, alloc_len); - return i; + for (qstr_pool_t *pool = last_pool; pool != NULL; pool = pool->prev) { + for (const char **qstr = pool->qstrs, **qstr_top = pool->qstrs + pool->len; qstr < qstr_top; qstr++) { + if (strcmp(*qstr, str) == 0) { + m_del(char, str, alloc_len); + return pool->total_prev_len + (qstr - pool->qstrs); + } } } return qstr_add(str); } qstr qstr_from_strn_copy(const char *str, int len) { - for (int i = 0; i < qstrs_len; i++) { - if (strncmp(qstrs[i], str, len) == 0 && qstrs[i][len] == '\0') { - return i; + for (qstr_pool_t *pool = last_pool; pool != NULL; pool = pool->prev) { + for (const char **qstr = pool->qstrs, **qstr_top = pool->qstrs + pool->len; qstr < qstr_top; qstr++) { + if (strncmp(*qstr, str, len) == 0 && (*qstr)[len] == '\0') { + return pool->total_prev_len + (qstr - pool->qstrs); + } } } return qstr_add(strndup(str, len)); } +// convert qstr id to pointer to its string const char *qstr_str(qstr qstr) { - return qstrs[qstr]; + // search + for (qstr_pool_t *pool = last_pool; pool != NULL; pool = pool->prev) { + if (qstr >= pool->total_prev_len) { + return pool->qstrs[qstr - pool->total_prev_len]; + } + } + + // not found, return nil + return const_pool.qstrs[0]; } diff --git a/py/runtime.c b/py/runtime.c index 3144321f3..a1f9ee3b7 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -11,6 +11,7 @@ #include "nlr.h" #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "obj.h" #include "runtime0.h" #include "runtime.h" @@ -27,22 +28,6 @@ #define DEBUG_OP_printf(args...) (void)0 #endif -// TODO make these predefined so they don't take up RAM -qstr rt_q_append; -qstr rt_q_pop; -qstr rt_q_sort; -qstr rt_q_join; -qstr rt_q_format; -qstr rt_q___build_class__; -qstr rt_q___next__; -qstr rt_q_AttributeError; -qstr rt_q_IndexError; -qstr rt_q_KeyError; -qstr rt_q_NameError; -qstr rt_q_TypeError; -qstr rt_q_SyntaxError; -qstr rt_q_ValueError; - // locals and globals need to be pointers because they can be the same in outer module scope static mp_map_t *map_locals; static mp_map_t *map_globals; @@ -83,74 +68,58 @@ FILE *fp_write_code = NULL; #endif void rt_init(void) { - rt_q_append = qstr_from_str_static("append"); - rt_q_pop = qstr_from_str_static("pop"); - rt_q_sort = qstr_from_str_static("sort"); - rt_q_join = qstr_from_str_static("join"); - rt_q_format = qstr_from_str_static("format"); - rt_q___build_class__ = qstr_from_str_static("__build_class__"); - rt_q___next__ = qstr_from_str_static("__next__"); - rt_q_AttributeError = qstr_from_str_static("AttributeError"); - rt_q_IndexError = qstr_from_str_static("IndexError"); - rt_q_KeyError = qstr_from_str_static("KeyError"); - rt_q_NameError = qstr_from_str_static("NameError"); - rt_q_TypeError = qstr_from_str_static("TypeError"); - rt_q_SyntaxError = qstr_from_str_static("SyntaxError"); - rt_q_ValueError = qstr_from_str_static("ValueError"); - // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New()) map_locals = map_globals = mp_map_new(MP_MAP_QSTR, 1); - mp_qstr_map_lookup(map_globals, qstr_from_str_static("__name__"), true)->value = mp_obj_new_str(qstr_from_str_static("__main__")); + mp_qstr_map_lookup(map_globals, MP_QSTR___name__, true)->value = mp_obj_new_str(MP_QSTR___main__); // init built-in hash table mp_map_init(&map_builtins, MP_MAP_QSTR, 3); // built-in exceptions (TODO, make these proper classes) - mp_qstr_map_lookup(&map_builtins, rt_q_AttributeError, true)->value = mp_obj_new_exception(rt_q_AttributeError); - mp_qstr_map_lookup(&map_builtins, rt_q_IndexError, true)->value = mp_obj_new_exception(rt_q_IndexError); - mp_qstr_map_lookup(&map_builtins, rt_q_KeyError, true)->value = mp_obj_new_exception(rt_q_KeyError); - mp_qstr_map_lookup(&map_builtins, rt_q_NameError, true)->value = mp_obj_new_exception(rt_q_NameError); - mp_qstr_map_lookup(&map_builtins, rt_q_TypeError, true)->value = mp_obj_new_exception(rt_q_TypeError); - mp_qstr_map_lookup(&map_builtins, rt_q_SyntaxError, true)->value = mp_obj_new_exception(rt_q_SyntaxError); - mp_qstr_map_lookup(&map_builtins, rt_q_ValueError, true)->value = mp_obj_new_exception(rt_q_ValueError); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_AttributeError, true)->value = mp_obj_new_exception(MP_QSTR_AttributeError); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_IndexError, true)->value = mp_obj_new_exception(MP_QSTR_IndexError); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_KeyError, true)->value = mp_obj_new_exception(MP_QSTR_KeyError); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_NameError, true)->value = mp_obj_new_exception(MP_QSTR_NameError); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_TypeError, true)->value = mp_obj_new_exception(MP_QSTR_TypeError); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_SyntaxError, true)->value = mp_obj_new_exception(MP_QSTR_SyntaxError); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_ValueError, true)->value = mp_obj_new_exception(MP_QSTR_ValueError); // built-in core functions - mp_qstr_map_lookup(&map_builtins, rt_q___build_class__, true)->value = rt_make_function_2(mp_builtin___build_class__); - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("__repl_print__"), true)->value = rt_make_function_1(mp_builtin___repl_print__); + mp_qstr_map_lookup(&map_builtins, MP_QSTR___build_class__, true)->value = rt_make_function_2(mp_builtin___build_class__); + mp_qstr_map_lookup(&map_builtins, MP_QSTR___repl_print__, true)->value = rt_make_function_1(mp_builtin___repl_print__); // built-in user functions - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("abs"), true)->value = rt_make_function_1(mp_builtin_abs); - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("all"), true)->value = rt_make_function_1(mp_builtin_all); - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("any"), true)->value = rt_make_function_1(mp_builtin_any); - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("bool"), true)->value = rt_make_function_var(0, mp_builtin_bool); - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("callable"), true)->value = rt_make_function_1(mp_builtin_callable); - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("chr"), true)->value = rt_make_function_1(mp_builtin_chr); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_abs, true)->value = rt_make_function_1(mp_builtin_abs); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_all, true)->value = rt_make_function_1(mp_builtin_all); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_any, true)->value = rt_make_function_1(mp_builtin_any); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_bool, true)->value = rt_make_function_var(0, mp_builtin_bool); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_callable, true)->value = rt_make_function_1(mp_builtin_callable); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_chr, true)->value = rt_make_function_1(mp_builtin_chr); #if MICROPY_ENABLE_FLOAT - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("complex"), true)->value = (mp_obj_t)&mp_builtin_complex_obj; + mp_qstr_map_lookup(&map_builtins, MP_QSTR_complex, true)->value = (mp_obj_t)&mp_builtin_complex_obj; #endif - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("dict"), true)->value = rt_make_function_0(mp_builtin_dict); - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("divmod"), true)->value = rt_make_function_2(mp_builtin_divmod); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_dict, true)->value = rt_make_function_0(mp_builtin_dict); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_divmod, true)->value = rt_make_function_2(mp_builtin_divmod); #if MICROPY_ENABLE_FLOAT - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("float"), true)->value = (mp_obj_t)&mp_builtin_float_obj; + mp_qstr_map_lookup(&map_builtins, MP_QSTR_float, true)->value = (mp_obj_t)&mp_builtin_float_obj; #endif - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("hash"), true)->value = (mp_obj_t)&mp_builtin_hash_obj; - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("int"), true)->value = (mp_obj_t)&mp_builtin_int_obj; - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("iter"), true)->value = (mp_obj_t)&mp_builtin_iter_obj; - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("len"), true)->value = rt_make_function_1(mp_builtin_len); - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("list"), true)->value = rt_make_function_var(0, mp_builtin_list); - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("max"), true)->value = rt_make_function_var(1, mp_builtin_max); - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("min"), true)->value = rt_make_function_var(1, mp_builtin_min); - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("next"), true)->value = (mp_obj_t)&mp_builtin_next_obj; - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("ord"), true)->value = rt_make_function_1(mp_builtin_ord); - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("pow"), true)->value = rt_make_function_var(2, mp_builtin_pow); - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("print"), true)->value = rt_make_function_var(0, mp_builtin_print); - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("range"), true)->value = rt_make_function_var(1, mp_builtin_range); - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("set"), true)->value = (mp_obj_t)&mp_builtin_set_obj; - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("sum"), true)->value = rt_make_function_var(1, mp_builtin_sum); - mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("type"), true)->value = (mp_obj_t)&mp_builtin_type_obj; - - - next_unique_code_id = 2; // 1 is reserved for the __main__ module scope + mp_qstr_map_lookup(&map_builtins, MP_QSTR_hash, true)->value = (mp_obj_t)&mp_builtin_hash_obj; + mp_qstr_map_lookup(&map_builtins, MP_QSTR_int, true)->value = (mp_obj_t)&mp_builtin_int_obj; + mp_qstr_map_lookup(&map_builtins, MP_QSTR_iter, true)->value = (mp_obj_t)&mp_builtin_iter_obj; + mp_qstr_map_lookup(&map_builtins, MP_QSTR_len, true)->value = rt_make_function_1(mp_builtin_len); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_list, true)->value = rt_make_function_var(0, mp_builtin_list); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_max, true)->value = rt_make_function_var(1, mp_builtin_max); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_min, true)->value = rt_make_function_var(1, mp_builtin_min); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_next, true)->value = (mp_obj_t)&mp_builtin_next_obj; + mp_qstr_map_lookup(&map_builtins, MP_QSTR_ord, true)->value = rt_make_function_1(mp_builtin_ord); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_pow, true)->value = rt_make_function_var(2, mp_builtin_pow); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_print, true)->value = rt_make_function_var(0, mp_builtin_print); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_range, true)->value = rt_make_function_var(1, mp_builtin_range); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_set, true)->value = (mp_obj_t)&mp_builtin_set_obj; + mp_qstr_map_lookup(&map_builtins, MP_QSTR_sum, true)->value = rt_make_function_var(1, mp_builtin_sum); + mp_qstr_map_lookup(&map_builtins, MP_QSTR_type, true)->value = (mp_obj_t)&mp_builtin_type_obj; + + next_unique_code_id = 1; // 0 indicates "no code" unique_codes = NULL; #ifdef WRITE_CODE @@ -166,12 +135,8 @@ void rt_deinit(void) { #endif } -int rt_get_unique_code_id(bool is_main_module) { - if (is_main_module) { - return 1; - } else { - return next_unique_code_id++; - } +int rt_get_unique_code_id(void) { + return next_unique_code_id++; } static void alloc_unique_codes(void) { @@ -186,7 +151,7 @@ static void alloc_unique_codes(void) { void rt_assign_byte_code(int unique_code_id, byte *code, uint len, int n_args, int n_locals, int n_stack, bool is_generator) { alloc_unique_codes(); - assert(unique_code_id < next_unique_code_id); + assert(1 <= unique_code_id && unique_code_id < next_unique_code_id); unique_codes[unique_code_id].kind = MP_CODE_BYTE; unique_codes[unique_code_id].n_args = n_args; unique_codes[unique_code_id].n_locals = n_locals; @@ -355,7 +320,7 @@ mp_obj_t rt_load_const_dec(qstr qstr) { } } if (*s != 0) { - nlr_jump(mp_obj_new_exception_msg(rt_q_SyntaxError, "invalid syntax for number")); + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_SyntaxError, "invalid syntax for number")); } if (exp_neg) { exp_val = -exp_val; @@ -373,7 +338,7 @@ mp_obj_t rt_load_const_dec(qstr qstr) { return mp_obj_new_float(dec_val); } #else - nlr_jump(mp_obj_new_exception_msg(rt_q_SyntaxError, "decimal numbers not supported")); + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_SyntaxError, "decimal numbers not supported")); #endif } @@ -391,7 +356,7 @@ mp_obj_t rt_load_name(qstr qstr) { if (elem == NULL) { elem = mp_qstr_map_lookup(&map_builtins, qstr, false); if (elem == NULL) { - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_NameError, "name '%s' is not defined", qstr_str(qstr))); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_NameError, "name '%s' is not defined", qstr_str(qstr))); } } } @@ -405,7 +370,7 @@ mp_obj_t rt_load_global(qstr qstr) { if (elem == NULL) { elem = mp_qstr_map_lookup(&map_builtins, qstr, false); if (elem == NULL) { - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_NameError, "name '%s' is not defined", qstr_str(qstr))); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_NameError, "name '%s' is not defined", qstr_str(qstr))); } } return elem->value; @@ -413,9 +378,9 @@ mp_obj_t rt_load_global(qstr qstr) { mp_obj_t rt_load_build_class(void) { DEBUG_OP_printf("load_build_class\n"); - mp_map_elem_t *elem = mp_qstr_map_lookup(&map_builtins, rt_q___build_class__, false); + mp_map_elem_t *elem = mp_qstr_map_lookup(&map_builtins, MP_QSTR___build_class__, false); if (elem == NULL) { - nlr_jump(mp_obj_new_exception_msg(rt_q_NameError, "name '__build_class__' is not defined")); + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_NameError, "name '__build_class__' is not defined")); } return elem->value; } @@ -465,7 +430,7 @@ mp_obj_t rt_unary_op(int op, mp_obj_t arg) { } } // TODO specify in error message what the operator is - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "bad operand type for unary operator: '%s'", o->type->name)); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "bad operand type for unary operator: '%s'", o->type->name)); } } @@ -544,7 +509,7 @@ mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { } // TODO specify in error message what the operator is - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "unsupported operand type for binary operator: '%s'", mp_obj_get_type_str(lhs))); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "unsupported operand type for binary operator: '%s'", mp_obj_get_type_str(lhs))); } mp_obj_t rt_compare_op(int op, mp_obj_t lhs, mp_obj_t rhs) { @@ -693,13 +658,13 @@ mp_obj_t rt_call_function_n(mp_obj_t fun_in, int n_args, const mp_obj_t *args) { DEBUG_OP_printf("calling function %p(n_args=%d, args=%p)\n", fun_in, n_args, args); if (MP_OBJ_IS_SMALL_INT(fun_in)) { - nlr_jump(mp_obj_new_exception_msg(rt_q_TypeError, "'int' object is not callable")); + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not callable")); } else { mp_obj_base_t *fun = fun_in; if (fun->type->call_n != NULL) { return fun->type->call_n(fun_in, n_args, args); } else { - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "'%s' object is not callable", fun->type->name)); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "'%s' object is not callable", fun->type->name)); } } } @@ -756,14 +721,14 @@ void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) { mp_obj_list_get(seq_in, &seq_len, &seq_items); } if (seq_len < num) { - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_ValueError, "need more than %d values to unpack", (void*)(machine_uint_t)seq_len)); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_ValueError, "need more than %d values to unpack", (void*)(machine_uint_t)seq_len)); } else if (seq_len > num) { - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_ValueError, "too many values to unpack (expected %d)", (void*)(machine_uint_t)num)); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_ValueError, "too many values to unpack (expected %d)", (void*)(machine_uint_t)num)); } memcpy(items, seq_items, num * sizeof(mp_obj_t)); } else { // TODO call rt_getiter and extract via rt_iternext - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(seq_in))); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(seq_in))); } } @@ -807,12 +772,12 @@ mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) { } no_attr: - nlr_jump(mp_obj_new_exception_msg_2_args(rt_q_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr))); + nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr))); } void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) { DEBUG_OP_printf("load method %s\n", qstr_str(attr)); - if (MP_OBJ_IS_TYPE(base, &gen_instance_type) && attr == rt_q___next__) { + if (MP_OBJ_IS_TYPE(base, &gen_instance_type) && attr == MP_QSTR___next__) { dest[1] = (mp_obj_t)&mp_builtin_next_obj; dest[0] = base; return; @@ -850,7 +815,7 @@ void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) { mp_map_t *globals = mp_obj_module_get_globals(base); mp_qstr_map_lookup(globals, attr, true)->value = value; } else { - nlr_jump(mp_obj_new_exception_msg_2_args(rt_q_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr))); + nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr))); } } @@ -869,26 +834,26 @@ void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) { mp_obj_t rt_getiter(mp_obj_t o_in) { if (MP_OBJ_IS_SMALL_INT(o_in)) { - nlr_jump(mp_obj_new_exception_msg(rt_q_TypeError, "'int' object is not iterable")); + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not iterable")); } else { mp_obj_base_t *o = o_in; if (o->type->getiter != NULL) { return o->type->getiter(o_in); } else { - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "'%s' object is not iterable", o->type->name)); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "'%s' object is not iterable", o->type->name)); } } } mp_obj_t rt_iternext(mp_obj_t o_in) { if (MP_OBJ_IS_SMALL_INT(o_in)) { - nlr_jump(mp_obj_new_exception_msg(rt_q_TypeError, "? 'int' object is not iterable")); + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "? 'int' object is not iterable")); } else { mp_obj_base_t *o = o_in; if (o->type->iternext != NULL) { return o->type->iternext(o_in); } else { - nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "? '%s' object is not iterable", o->type->name)); + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "? '%s' object is not iterable", o->type->name)); } } } diff --git a/py/runtime.h b/py/runtime.h index cf9180275..96f1671f6 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -1,18 +1,3 @@ -extern qstr rt_q_append; -extern qstr rt_q_pop; -extern qstr rt_q_sort; -extern qstr rt_q_join; -extern qstr rt_q_format; -extern qstr rt_q___build_class__; -extern qstr rt_q___next__; -extern qstr rt_q_AttributeError; -extern qstr rt_q_IndexError; -extern qstr rt_q_KeyError; -extern qstr rt_q_NameError; -extern qstr rt_q_TypeError; -extern qstr rt_q_SyntaxError; -extern qstr rt_q_ValueError; - int rt_is_true(mp_obj_t arg); mp_obj_t rt_load_const_dec(qstr qstr); diff --git a/py/runtime0.h b/py/runtime0.h index 8ec2c058f..97dbe5ddb 100644 --- a/py/runtime0.h +++ b/py/runtime0.h @@ -81,7 +81,7 @@ extern void *const rt_fun_table[RT_F_NUMBER_OF]; void rt_init(void); void rt_deinit(void); -int rt_get_unique_code_id(bool is_main_module); +int rt_get_unique_code_id(void); void rt_assign_byte_code(int unique_code_id, byte *code, uint len, int n_args, int n_locals, int n_stack, bool is_generator); void rt_assign_native_code(int unique_code_id, void *f, uint len, int n_args); void rt_assign_inline_asm_code(int unique_code_id, void *f, uint len, int n_args); -- cgit v1.2.3 From e9906ac3d771a312b05d76e42aee8e806dd0d128 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 4 Jan 2014 18:44:46 +0000 Subject: Add ellipsis object. --- py/bc0.h | 16 ++++++++++------ py/emitbc.c | 1 + py/grammar.h | 8 ++++---- py/lexer.c | 2 +- py/lexer.h | 2 +- py/mpqstrraw.h | 1 + py/obj.h | 1 + py/objslice.c | 29 +++++++++++++++++++++++++++++ py/runtime.c | 3 +++ py/showbc.c | 4 ++++ py/vm.c | 4 ++++ 11 files changed, 59 insertions(+), 12 deletions(-) (limited to 'py') diff --git a/py/bc0.h b/py/bc0.h index 773e23d2e..0a4a49ce6 100644 --- a/py/bc0.h +++ b/py/bc0.h @@ -1,12 +1,16 @@ +// Micro Python byte-codes. +// The comment at the end of the line (if it exists) tells the arguments to the byte-code. + #define MP_BC_LOAD_CONST_FALSE (0x10) #define MP_BC_LOAD_CONST_NONE (0x11) #define MP_BC_LOAD_CONST_TRUE (0x12) -#define MP_BC_LOAD_CONST_SMALL_INT (0x13) // 24-bit, in excess -#define MP_BC_LOAD_CONST_INT (0x14) // qstr -#define MP_BC_LOAD_CONST_DEC (0x15) // qstr -#define MP_BC_LOAD_CONST_ID (0x16) // qstr -#define MP_BC_LOAD_CONST_BYTES (0x17) // qstr -#define MP_BC_LOAD_CONST_STRING (0x18) // qstr +#define MP_BC_LOAD_CONST_ELLIPSIS (0x13) +#define MP_BC_LOAD_CONST_SMALL_INT (0x14) // 24-bit, in excess +#define MP_BC_LOAD_CONST_INT (0x15) // qstr +#define MP_BC_LOAD_CONST_DEC (0x16) // qstr +#define MP_BC_LOAD_CONST_ID (0x17) // qstr +#define MP_BC_LOAD_CONST_BYTES (0x18) // qstr +#define MP_BC_LOAD_CONST_STRING (0x19) // qstr #define MP_BC_LOAD_FAST_0 (0x20) #define MP_BC_LOAD_FAST_1 (0x21) diff --git a/py/emitbc.c b/py/emitbc.c index dc1988582..c0ec2469a 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -249,6 +249,7 @@ static void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) { case MP_TOKEN_KW_FALSE: emit_write_byte_1(emit, MP_BC_LOAD_CONST_FALSE); break; case MP_TOKEN_KW_NONE: emit_write_byte_1(emit, MP_BC_LOAD_CONST_NONE); break; case MP_TOKEN_KW_TRUE: emit_write_byte_1(emit, MP_BC_LOAD_CONST_TRUE); break; + case MP_TOKEN_ELLIPSIS: emit_write_byte_1(emit, MP_BC_LOAD_CONST_ELLIPSIS); break; default: assert(0); } } diff --git a/py/grammar.h b/py/grammar.h index b1faab79e..4d53bd3fc 100644 --- a/py/grammar.h +++ b/py/grammar.h @@ -113,11 +113,11 @@ DEF_RULE(import_stmt, nc, or(2), rule(import_name), rule(import_from)) DEF_RULE(import_name, c(import_name), and(2), tok(KW_IMPORT), rule(dotted_as_names)) DEF_RULE(import_from, c(import_from), and(4), tok(KW_FROM), rule(import_from_2), tok(KW_IMPORT), rule(import_from_3)) DEF_RULE(import_from_2, nc, or(2), rule(dotted_name), rule(import_from_2b)) -DEF_RULE(import_from_2b, nc, and(2), rule(one_or_more_period_or_ellipses), opt_rule(dotted_name)) +DEF_RULE(import_from_2b, nc, and(2), rule(one_or_more_period_or_ellipsis), opt_rule(dotted_name)) DEF_RULE(import_from_3, nc, or(3), tok(OP_STAR), rule(import_as_names_paren), rule(import_as_names)) DEF_RULE(import_as_names_paren, nc, and(3), tok(DEL_PAREN_OPEN), rule(import_as_names), tok(DEL_PAREN_CLOSE)) -DEF_RULE(one_or_more_period_or_ellipses, nc, one_or_more, rule(period_or_ellipses)) -DEF_RULE(period_or_ellipses, nc, or(2), tok(DEL_PERIOD), tok(ELLIPSES)) +DEF_RULE(one_or_more_period_or_ellipsis, nc, one_or_more, rule(period_or_ellipsis)) +DEF_RULE(period_or_ellipsis, nc, or(2), tok(DEL_PERIOD), tok(ELLIPSIS)) DEF_RULE(import_as_name, nc, and(2), tok(NAME), opt_rule(as_name)) DEF_RULE(dotted_as_name, nc, and(2), rule(dotted_name), opt_rule(as_name)) 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 // testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] ) // trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME -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)) +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)) DEF_RULE(atom_string, c(atom_string), one_or_more, rule(string_or_bytes)) DEF_RULE(string_or_bytes, nc, or(2), tok(STRING), tok(BYTES)) DEF_RULE(atom_paren, c(atom_paren), and(3), tok(DEL_PAREN_OPEN), opt_rule(atom_2b), tok(DEL_PAREN_CLOSE)) diff --git a/py/lexer.c b/py/lexer.c index 4df91b036..d4205236c 100644 --- a/py/lexer.c +++ b/py/lexer.c @@ -239,7 +239,7 @@ static const uint8_t tok_enc_kind[] = { MP_TOKEN_OP_CARET, MP_TOKEN_DEL_CARET_EQUAL, MP_TOKEN_DEL_EQUAL, MP_TOKEN_OP_DBL_EQUAL, MP_TOKEN_OP_NOT_EQUAL, - MP_TOKEN_DEL_PERIOD, MP_TOKEN_ELLIPSES, + MP_TOKEN_DEL_PERIOD, MP_TOKEN_ELLIPSIS, }; // must have the same order as enum in lexer.h diff --git a/py/lexer.h b/py/lexer.h index 27244fde9..3cb48ce9e 100644 --- a/py/lexer.h +++ b/py/lexer.h @@ -20,7 +20,7 @@ typedef enum _mp_token_kind_t { MP_TOKEN_STRING, MP_TOKEN_BYTES, - MP_TOKEN_ELLIPSES, + MP_TOKEN_ELLIPSIS, MP_TOKEN_KW_FALSE, // 12 MP_TOKEN_KW_NONE, diff --git a/py/mpqstrraw.h b/py/mpqstrraw.h index 85cf1ff7b..e73bd828e 100644 --- a/py/mpqstrraw.h +++ b/py/mpqstrraw.h @@ -20,6 +20,7 @@ Q(native) Q(viper) Q(asm_thumb) +Q(Ellipsis) Q(StopIteration) Q(AttributeError) diff --git a/py/obj.h b/py/obj.h index 16c7c36dd..f0ba6999e 100644 --- a/py/obj.h +++ b/py/obj.h @@ -110,6 +110,7 @@ extern const mp_obj_type_t mp_const_type; extern const mp_obj_t mp_const_none; extern const mp_obj_t mp_const_false; extern const mp_obj_t mp_const_true; +extern const mp_obj_t mp_const_ellipsis; extern const mp_obj_t mp_const_stop_iteration; // special object indicating end of iteration (not StopIteration exception!) // Need to declare this here so we are not dependent on map.h diff --git a/py/objslice.c b/py/objslice.c index 03607e4c3..d99325fd7 100644 --- a/py/objslice.c +++ b/py/objslice.c @@ -9,6 +9,35 @@ #include "obj.h" #include "runtime0.h" +/******************************************************************************/ +/* ellipsis object, a singleton */ + +typedef struct _mp_obj_ellipsis_t { + mp_obj_base_t base; +} mp_obj_ellipsis_t; + +void ellipsis_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { + print(env, "Ellipsis"); +} + +const mp_obj_type_t ellipsis_type = { + { &mp_const_type }, + "ellipsis", + ellipsis_print, // print + NULL, // call_n + NULL, // unary_op + NULL, // binary_op + NULL, // getiter + NULL, // iternext + {{NULL, NULL},}, // method list +}; + +static const mp_obj_ellipsis_t ellipsis_obj = {{&ellipsis_type}}; +const mp_obj_t mp_const_ellipsis = (mp_obj_t)&ellipsis_obj; + +/******************************************************************************/ +/* slice object */ + #if MICROPY_ENABLE_SLICE // TODO: This implements only variant of slice with 2 integer args only. diff --git a/py/runtime.c b/py/runtime.c index a1f9ee3b7..72881067d 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -84,6 +84,9 @@ void rt_init(void) { mp_qstr_map_lookup(&map_builtins, MP_QSTR_SyntaxError, true)->value = mp_obj_new_exception(MP_QSTR_SyntaxError); mp_qstr_map_lookup(&map_builtins, MP_QSTR_ValueError, true)->value = mp_obj_new_exception(MP_QSTR_ValueError); + // built-in objects + mp_qstr_map_lookup(&map_builtins, MP_QSTR_Ellipsis, true)->value = mp_const_ellipsis; + // built-in core functions mp_qstr_map_lookup(&map_builtins, MP_QSTR___build_class__, true)->value = rt_make_function_2(mp_builtin___build_class__); mp_qstr_map_lookup(&map_builtins, MP_QSTR___repl_print__, true)->value = rt_make_function_1(mp_builtin___repl_print__); diff --git a/py/showbc.c b/py/showbc.c index a3bfa2833..eb7d41b24 100644 --- a/py/showbc.c +++ b/py/showbc.c @@ -46,6 +46,10 @@ void mp_show_byte_code(const byte *ip, int len) { printf("LOAD_CONST_TRUE"); break; + case MP_BC_LOAD_CONST_ELLIPSIS: + printf("LOAD_CONST_ELLIPSIS"); + break; + case MP_BC_LOAD_CONST_SMALL_INT: unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000; ip += 3; diff --git a/py/vm.c b/py/vm.c index 382780640..8e7ef7485 100644 --- a/py/vm.c +++ b/py/vm.c @@ -99,6 +99,10 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t ** PUSH(mp_const_true); break; + case MP_BC_LOAD_CONST_ELLIPSIS: + PUSH(mp_const_ellipsis); + break; + case MP_BC_LOAD_CONST_SMALL_INT: unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000; ip += 3; -- cgit v1.2.3 From 5d02e2d6fa3afcd8aa9ecb475b0c4599da6ca03d Mon Sep 17 00:00:00 2001 From: Mikael Eiman Date: Sat, 4 Jan 2014 20:15:04 +0100 Subject: OSX: fixes to make nlrx64.S with Apple's clang --- py/nlrx64.S | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'py') diff --git a/py/nlrx64.S b/py/nlrx64.S index 6d0e2118f..37d313778 100644 --- a/py/nlrx64.S +++ b/py/nlrx64.S @@ -5,9 +5,14 @@ .text /* uint nlr_push(rdi=nlr_buf_t *nlr) */ +#ifndef __llvm__ .globl nlr_push .type nlr_push, @function nlr_push: +#else + .globl _nlr_push +_nlr_push: +#endif movq (%rsp), %rax # load return %rip movq %rax, 16(%rdi) # store %rip into nlr_buf movq %rbp, 24(%rdi) # store %rbp into nlr_buf @@ -22,22 +27,32 @@ nlr_push: movq %rdi, nlr_top(%rip) # stor new nlr_buf (to make linked list) xorq %rax, %rax # return 0, normal return ret # return - .size nlr_push, .-nlr_push +// .size nlr_push, .-nlr_push /* void nlr_pop() */ +#ifndef __llvm__ .globl nlr_pop .type nlr_pop, @function nlr_pop: +#else + .globl _nlr_pop +_nlr_pop: +#endif movq nlr_top(%rip), %rax # get nlr_top into %rax movq (%rax), %rax # load prev nlr_buf movq %rax, nlr_top(%rip) # store prev nlr_buf (to unlink list) ret # return - .size nlr_pop, .-nlr_pop +// .size nlr_pop, .-nlr_pop /* void nlr_jump(rdi=uint val) */ +#ifndef __llvm__ .globl nlr_jump .type nlr_jump, @function nlr_jump: +#else + .globl _nlr_jump + _nlr_jump: +#endif movq %rdi, %rax # put return value in %rax movq nlr_top(%rip), %rdi # get nlr_top into %rdi movq %rax, 8(%rdi) # store return value @@ -55,8 +70,12 @@ nlr_jump: xorq %rax, %rax # clear return register inc %al # increase to make 1, non-local return ret # return +#ifndef __llvm__ .size nlr_jump, .-nlr_jump +#endif +#ifndef __llvm__ .local nlr_top +#endif .comm nlr_top,8,8 #endif -- cgit v1.2.3 From f53cdd947cacbe273bcef0c68ced83162a16a24a Mon Sep 17 00:00:00 2001 From: Mikael Eiman Date: Sat, 4 Jan 2014 20:19:19 +0100 Subject: OSX: fixes to make nlrx64.S with Apple's clang (forgot a few places) --- py/nlrx64.S | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'py') diff --git a/py/nlrx64.S b/py/nlrx64.S index 37d313778..20a3704fd 100644 --- a/py/nlrx64.S +++ b/py/nlrx64.S @@ -27,7 +27,9 @@ _nlr_push: movq %rdi, nlr_top(%rip) # stor new nlr_buf (to make linked list) xorq %rax, %rax # return 0, normal return ret # return -// .size nlr_push, .-nlr_push +#ifndef __llvm__ + .size nlr_push, .-nlr_push +#endif /* void nlr_pop() */ #ifndef __llvm__ @@ -42,7 +44,9 @@ _nlr_pop: movq (%rax), %rax # load prev nlr_buf movq %rax, nlr_top(%rip) # store prev nlr_buf (to unlink list) ret # return -// .size nlr_pop, .-nlr_pop +#ifndef __llvm__ + .size nlr_pop, .-nlr_pop +#endif /* void nlr_jump(rdi=uint val) */ #ifndef __llvm__ -- cgit v1.2.3 From d67091371da5fed82547988864577928e8468b8e Mon Sep 17 00:00:00 2001 From: Mikael Eiman Date: Sat, 4 Jan 2014 20:27:13 +0100 Subject: OSX: fixes to make nlrx64.S with Apple's clang (switched to Apple-specific define instead of __llvm__) --- py/nlrx64.S | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'py') diff --git a/py/nlrx64.S b/py/nlrx64.S index 20a3704fd..441817b4f 100644 --- a/py/nlrx64.S +++ b/py/nlrx64.S @@ -5,7 +5,7 @@ .text /* uint nlr_push(rdi=nlr_buf_t *nlr) */ -#ifndef __llvm__ +#ifndef __apple_build_version__ .globl nlr_push .type nlr_push, @function nlr_push: @@ -27,12 +27,12 @@ _nlr_push: movq %rdi, nlr_top(%rip) # stor new nlr_buf (to make linked list) xorq %rax, %rax # return 0, normal return ret # return -#ifndef __llvm__ +#ifndef __apple_build_version__ .size nlr_push, .-nlr_push #endif /* void nlr_pop() */ -#ifndef __llvm__ +#ifndef __apple_build_version__ .globl nlr_pop .type nlr_pop, @function nlr_pop: @@ -44,12 +44,12 @@ _nlr_pop: movq (%rax), %rax # load prev nlr_buf movq %rax, nlr_top(%rip) # store prev nlr_buf (to unlink list) ret # return -#ifndef __llvm__ +#ifndef __apple_build_version__ .size nlr_pop, .-nlr_pop #endif /* void nlr_jump(rdi=uint val) */ -#ifndef __llvm__ +#ifndef __apple_build_version__ .globl nlr_jump .type nlr_jump, @function nlr_jump: @@ -74,11 +74,11 @@ nlr_jump: xorq %rax, %rax # clear return register inc %al # increase to make 1, non-local return ret # return -#ifndef __llvm__ +#ifndef __apple_build_version__ .size nlr_jump, .-nlr_jump #endif -#ifndef __llvm__ +#ifndef __apple_build_version__ .local nlr_top #endif .comm nlr_top,8,8 -- cgit v1.2.3 From 71c5181a8dfa69ba9f5ca322a3aba0660be2e166 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 4 Jan 2014 20:21:15 +0000 Subject: Convert Python types to proper Python type hierarchy. Now much more inline with how CPython does types. --- py/builtin.c | 126 +----------------------------------------------------- py/compile.c | 2 +- py/emitpass1.c | 5 ++- py/mpqstrraw.h | 5 ++- py/obj.c | 12 +++--- py/obj.h | 7 +++ py/objbool.c | 14 +++++- py/objboundmeth.c | 1 + py/objcell.c | 1 + py/objclass.c | 1 + py/objclosure.c | 1 + py/objcomplex.c | 45 ++++++++++++++++++- py/objdict.c | 11 ++++- py/objexcept.c | 1 + py/objfloat.c | 26 +++++++++-- py/objfun.c | 3 ++ py/objgenerator.c | 2 + py/objinstance.c | 1 + py/objlist.c | 25 +++++++++++ py/objmodule.c | 1 + py/objnone.c | 1 + py/objrange.c | 2 + py/objset.c | 28 +++++++++++- py/objslice.c | 2 + py/objstr.c | 1 + py/objtuple.c | 76 +++++++++++++++++++++++++++----- py/objtype.c | 21 +++++++-- py/runtime.c | 29 +++++++------ 28 files changed, 277 insertions(+), 173 deletions(-) (limited to 'py') diff --git a/py/builtin.c b/py/builtin.c index b565ed464..6babc7669 100644 --- a/py/builtin.c +++ b/py/builtin.c @@ -88,14 +88,6 @@ mp_obj_t mp_builtin_any(mp_obj_t o_in) { return mp_const_false; } -mp_obj_t mp_builtin_bool(int n_args, const mp_obj_t *args) { - switch (n_args) { - case 0: return mp_const_false; - case 1: if (rt_is_true(args[0])) { return mp_const_true; } else { return mp_const_false; } - default: nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "bool() takes at most 1 argument (%d given)", (void*)(machine_int_t)n_args)); - } -} - mp_obj_t mp_builtin_callable(mp_obj_t o_in) { if (mp_obj_is_callable(o_in)) { return mp_const_true; @@ -104,42 +96,6 @@ mp_obj_t mp_builtin_callable(mp_obj_t o_in) { } } -#if MICROPY_ENABLE_FLOAT -mp_obj_t mp_builtin_complex(int n_args, const mp_obj_t *args) { - assert(0 <= n_args && n_args <= 2); - - if (n_args == 0) { - return mp_obj_new_complex(0, 0); - } else if (n_args == 1) { - // TODO allow string as first arg and parse it - if (MP_OBJ_IS_TYPE(args[0], &complex_type)) { - return args[0]; - } else { - return mp_obj_new_complex(mp_obj_get_float(args[0]), 0); - } - } else { - mp_float_t real, imag; - if (MP_OBJ_IS_TYPE(args[0], &complex_type)) { - mp_obj_get_complex(args[0], &real, &imag); - } else { - real = mp_obj_get_float(args[0]); - imag = 0; - } - if (MP_OBJ_IS_TYPE(args[1], &complex_type)) { - mp_float_t real2, imag2; - mp_obj_get_complex(args[1], &real2, &imag2); - real -= imag2; - imag += real2; - } else { - imag += mp_obj_get_float(args[1]); - } - return mp_obj_new_complex(real, imag); - } -} - -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_complex_obj, 0, 2, mp_builtin_complex); -#endif - mp_obj_t mp_builtin_chr(mp_obj_t o_in) { int ord = mp_obj_get_int(o_in); if (0 <= ord && ord <= 0x10ffff) { @@ -152,11 +108,6 @@ mp_obj_t mp_builtin_chr(mp_obj_t o_in) { } } -mp_obj_t mp_builtin_dict(void) { - // TODO create from an iterable! - return rt_build_map(0); -} - mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) { if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) { mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in); @@ -170,25 +121,6 @@ mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) { } } -#if MICROPY_ENABLE_FLOAT -static mp_obj_t mp_builtin_float(int n_args, const mp_obj_t *args) { - assert(0 <= n_args && n_args <= 1); - - if (n_args == 0) { - return mp_obj_new_float(0); - } else { - // TODO allow string as arg and parse it - if (MP_OBJ_IS_TYPE(args[0], &float_type)) { - return args[0]; - } else { - return mp_obj_new_float(mp_obj_get_float(args[0])); - } - } -} - -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_float_obj, 0, 1, mp_builtin_float); -#endif - static mp_obj_t mp_builtin_hash(mp_obj_t o_in) { // TODO hash will generally overflow small integer; can we safely truncate it? return mp_obj_new_int(mp_obj_hash(o_in)); @@ -196,23 +128,6 @@ static mp_obj_t mp_builtin_hash(mp_obj_t o_in) { MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hash_obj, mp_builtin_hash); -static mp_obj_t mp_builtin_int(int n_args, const mp_obj_t *args) { - assert(0 <= n_args && n_args <= 2); - - if (n_args == 0) { - return MP_OBJ_NEW_SMALL_INT(0); - } else if (n_args == 1) { - // TODO if arg is a string then parse it - return mp_obj_new_int(mp_obj_get_int(args[0])); - } else { // n_args == 2 - // TODO, parse with given base - assert(0); - return MP_OBJ_NEW_SMALL_INT(0); - } -} - -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_int_obj, 0, 2, mp_builtin_int); - static mp_obj_t mp_builtin_iter(mp_obj_t o_in) { return rt_getiter(o_in); } @@ -241,24 +156,6 @@ mp_obj_t mp_builtin_len(mp_obj_t o_in) { return MP_OBJ_NEW_SMALL_INT(len); } -mp_obj_t mp_builtin_list(int n_args, const mp_obj_t *args) { - switch (n_args) { - case 0: return rt_build_list(0, NULL); - case 1: - { - // make list from iterable - mp_obj_t iterable = rt_getiter(args[0]); - mp_obj_t list = rt_build_list(0, NULL); - mp_obj_t item; - while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) { - rt_list_append(list, item); - } - return list; - } - default: nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "list() takes at most 1 argument (%d given)", (void*)(machine_int_t)n_args)); - } -} - mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) { if (n_args == 1) { // given an iterable @@ -367,26 +264,6 @@ mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args) { } } -static mp_obj_t mp_builtin_set(int n_args, const mp_obj_t *args) { - assert(0 <= n_args && n_args <= 1); - - if (n_args == 0) { - // return a new, empty set - return mp_obj_new_set(0, NULL); - } else { - // 1 argument, an iterable from which we make a new set - mp_obj_t set = mp_obj_new_set(0, NULL); - mp_obj_t iterable = rt_getiter(args[0]); - mp_obj_t item; - while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) { - mp_obj_set_store(set, item); - } - return set; - } -} - -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_set_obj, 0, 1, mp_builtin_set); - mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { mp_obj_t value; switch (n_args) { @@ -405,8 +282,7 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { static mp_obj_t mp_builtin_type(mp_obj_t o_in) { // TODO implement the 3 argument version of type() if (MP_OBJ_IS_SMALL_INT(o_in)) { - // TODO implement int-type - return mp_const_none; + return (mp_obj_t)&int_type; } else { mp_obj_base_t *o = o_in; return (mp_obj_t)o->type; diff --git a/py/compile.c b/py/compile.c index f8fa2cb2c..0e1989031 100644 --- a/py/compile.c +++ b/py/compile.c @@ -1316,7 +1316,7 @@ void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { int l_end = comp_next_label(comp); c_if_cond(comp, pns->nodes[0], true, l_end); - EMIT(load_id, MP_QSTR_assertion_error); + EMIT(load_id, MP_QSTR_AssertionError); if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) { // assertion message compile_node(comp, pns->nodes[1]); diff --git a/py/emitpass1.c b/py/emitpass1.c index 1c11241e0..f78ec7e27 100644 --- a/py/emitpass1.c +++ b/py/emitpass1.c @@ -7,6 +7,7 @@ #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "lexer.h" #include "parse.h" #include "scope.h" @@ -44,9 +45,9 @@ static void emit_pass1_load_id(emit_t *emit, qstr qstr) { bool added; id_info_t *id = scope_find_or_add_id(emit->scope, qstr, &added); if (added) { - if (strcmp(qstr_str(qstr), "AssertionError") == 0) { - id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT; + if (qstr == MP_QSTR_AssertionError) { // TODO how much of a hack is this? + id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT; } else if (strcmp(qstr_str(qstr), "super") == 0 && emit->scope->kind == SCOPE_FUNCTION) { // special case, super is a global, and also counts as use of __class__ id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT; diff --git a/py/mpqstrraw.h b/py/mpqstrraw.h index e73bd828e..fe74c3e92 100644 --- a/py/mpqstrraw.h +++ b/py/mpqstrraw.h @@ -13,7 +13,6 @@ Q(__next__) Q(__qualname__) Q(__repl_print__) -Q(assertion_error) Q(micropython) Q(byte_code) Q(native) @@ -23,12 +22,13 @@ Q(asm_thumb) Q(Ellipsis) Q(StopIteration) +Q(AssertionError) Q(AttributeError) Q(IndexError) Q(KeyError) Q(NameError) -Q(TypeError) Q(SyntaxError) +Q(TypeError) Q(ValueError) Q(abs) @@ -55,6 +55,7 @@ Q(print) Q(range) Q(set) Q(sum) +Q(tuple) Q(type) Q(append) diff --git a/py/obj.c b/py/obj.c index 2b834ffe4..77580e1fe 100644 --- a/py/obj.c +++ b/py/obj.c @@ -13,10 +13,6 @@ #include "runtime.h" #include "map.h" -mp_obj_t mp_obj_new_int(machine_int_t value) { - return MP_OBJ_NEW_SMALL_INT(value); -} - const char *mp_obj_get_type_str(mp_obj_t o_in) { if (MP_OBJ_IS_SMALL_INT(o_in)) { return "int"; @@ -128,9 +124,13 @@ machine_int_t mp_obj_get_int(mp_obj_t arg) { return 1; } else if (MP_OBJ_IS_SMALL_INT(arg)) { return MP_OBJ_SMALL_INT_VALUE(arg); +#if MICROPY_ENABLE_FLOAT + } else if (MP_OBJ_IS_TYPE(arg, &float_type)) { + // TODO work out if this should be floor, ceil or trunc + return (machine_int_t)mp_obj_float_get(arg); +#endif } else { - assert(0); - return 0; + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "can't convert %s to int", mp_obj_get_type_str(arg))); } } diff --git a/py/obj.h b/py/obj.h index f0ba6999e..03e67dd47 100644 --- a/py/obj.h +++ b/py/obj.h @@ -58,6 +58,7 @@ typedef mp_obj_t (*mp_fun_t)(void); typedef mp_obj_t (*mp_fun_var_t)(int n, const mp_obj_t *); typedef void (*mp_print_fun_t)(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o); +typedef mp_obj_t (*mp_make_new_fun_t)(mp_obj_t type_in, int n_args, const mp_obj_t *args); // args are in reverse order in the array typedef mp_obj_t (*mp_call_n_fun_t)(mp_obj_t fun, int n_args, const mp_obj_t *args); // args are in reverse order in the array typedef mp_obj_t (*mp_unary_op_fun_t)(int op, mp_obj_t); typedef mp_obj_t (*mp_binary_op_fun_t)(int op, mp_obj_t, mp_obj_t); @@ -71,6 +72,7 @@ struct _mp_obj_type_t { mp_obj_base_t base; const char *name; mp_print_fun_t print; + mp_make_new_fun_t make_new; // to make an instance of the type mp_call_n_fun_t call_n; mp_unary_op_fun_t unary_op; // can return NULL if op not supported @@ -110,6 +112,7 @@ extern const mp_obj_type_t mp_const_type; extern const mp_obj_t mp_const_none; extern const mp_obj_t mp_const_false; extern const mp_obj_t mp_const_true; +extern const mp_obj_t mp_const_empty_tuple; extern const mp_obj_t mp_const_ellipsis; extern const mp_obj_t mp_const_stop_iteration; // special object indicating end of iteration (not StopIteration exception!) @@ -180,6 +183,9 @@ extern const mp_obj_type_t bool_type; mp_obj_t mp_obj_cell_get(mp_obj_t self_in); void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj); +// int +extern const mp_obj_type_t int_type; + // exception extern const mp_obj_type_t exception_type; qstr mp_obj_exception_get_type(mp_obj_t self_in); @@ -214,6 +220,7 @@ uint mp_obj_dict_len(mp_obj_t self_in); mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value); // set +extern const mp_obj_type_t set_type; void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item); // slice diff --git a/py/objbool.c b/py/objbool.c index 9b53ffae9..1d94ee03b 100644 --- a/py/objbool.c +++ b/py/objbool.c @@ -4,14 +4,16 @@ #include "nlr.h" #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "obj.h" +#include "runtime.h" typedef struct _mp_obj_bool_t { mp_obj_base_t base; bool value; } mp_obj_bool_t; -void bool_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { +static void bool_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { mp_obj_bool_t *self = self_in; if (self->value) { print(env, "True"); @@ -20,10 +22,20 @@ void bool_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_ob } } +// args are reverse in the array +static mp_obj_t bool_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) { + switch (n_args) { + case 0: return mp_const_false; + case 1: if (rt_is_true(args[0])) { return mp_const_true; } else { return mp_const_false; } + default: nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "bool takes at most 1 argument, %d given", (void*)(machine_int_t)n_args)); + } +} + const mp_obj_type_t bool_type = { { &mp_const_type }, "bool", bool_print, // print + bool_make_new, // make_new NULL, // call_n NULL, // unary_op NULL, // binary_op diff --git a/py/objboundmeth.c b/py/objboundmeth.c index 8bd238c65..264c2effd 100644 --- a/py/objboundmeth.c +++ b/py/objboundmeth.c @@ -37,6 +37,7 @@ const mp_obj_type_t bound_meth_type = { { &mp_const_type }, "bound_method", NULL, // print + NULL, // make_new bound_meth_call_n, // call_n NULL, // unary_op NULL, // binary_op diff --git a/py/objcell.c b/py/objcell.c index cba198057..daaa340a7 100644 --- a/py/objcell.c +++ b/py/objcell.c @@ -27,6 +27,7 @@ const mp_obj_type_t cell_type = { { &mp_const_type }, "cell", NULL, // print + NULL, // make_new NULL, // call_n NULL, // unary_op NULL, // binary_op diff --git a/py/objclass.c b/py/objclass.c index f223c5ff2..197dfa726 100644 --- a/py/objclass.c +++ b/py/objclass.c @@ -64,6 +64,7 @@ const mp_obj_type_t class_type = { { &mp_const_type }, "class", NULL, // print + NULL, // make_new class_call_n, // call_n NULL, // unary_op NULL, // binary_op diff --git a/py/objclosure.c b/py/objclosure.c index e3354d42d..550bb5067 100644 --- a/py/objclosure.c +++ b/py/objclosure.c @@ -36,6 +36,7 @@ const mp_obj_type_t closure_type = { { &mp_const_type }, "closure", NULL, // print + NULL, // make_new closure_call_n, // call_n NULL, // unary_op NULL, // binary_op diff --git a/py/objcomplex.c b/py/objcomplex.c index ab9c14677..5408d71cf 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -6,6 +6,7 @@ #include "nlr.h" #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "obj.h" #include "runtime0.h" #include "map.h" @@ -29,7 +30,46 @@ void complex_print(void (*print)(void *env, const char *fmt, ...), void *env, mp } } -mp_obj_t complex_unary_op(int op, mp_obj_t o_in) { +// args are reverse in the array +static mp_obj_t complex_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) { + switch (n_args) { + case 0: + return mp_obj_new_complex(0, 0); + + case 1: + // TODO allow string as first arg and parse it + if (MP_OBJ_IS_TYPE(args[0], &complex_type)) { + return args[0]; + } else { + return mp_obj_new_complex(mp_obj_get_float(args[0]), 0); + } + + case 2: + { + mp_float_t real, imag; + if (MP_OBJ_IS_TYPE(args[1], &complex_type)) { + mp_obj_get_complex(args[1], &real, &imag); + } else { + real = mp_obj_get_float(args[1]); + imag = 0; + } + if (MP_OBJ_IS_TYPE(args[0], &complex_type)) { + mp_float_t real2, imag2; + mp_obj_get_complex(args[0], &real2, &imag2); + real -= imag2; + imag += real2; + } else { + imag += mp_obj_get_float(args[0]); + } + return mp_obj_new_complex(real, imag); + } + + default: + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "complex takes at most 2 arguments, %d given", (void*)(machine_int_t)n_args)); + } +} + +static mp_obj_t complex_unary_op(int op, mp_obj_t o_in) { mp_obj_complex_t *o = o_in; switch (op) { case RT_UNARY_OP_NOT: if (o->real != 0 || o->imag != 0) { return mp_const_true;} else { return mp_const_false; } @@ -39,7 +79,7 @@ mp_obj_t complex_unary_op(int op, mp_obj_t o_in) { } } -mp_obj_t complex_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { +static mp_obj_t complex_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { mp_float_t lhs_real, lhs_imag, rhs_real, rhs_imag; mp_obj_complex_get(lhs_in, &lhs_real, &lhs_imag); mp_obj_complex_get(rhs_in, &rhs_real, &rhs_imag); @@ -79,6 +119,7 @@ const mp_obj_type_t complex_type = { { &mp_const_type }, "complex", complex_print, // print + complex_make_new, // make_new NULL, // call_n complex_unary_op, // unary_op complex_binary_op, // binary_op diff --git a/py/objdict.c b/py/objdict.c index acf1a9f80..3737f5eab 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -17,7 +17,7 @@ typedef struct _mp_obj_dict_t { mp_map_t map; } mp_obj_dict_t; -void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { +static void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { mp_obj_dict_t *self = self_in; bool first = true; print(env, "{"); @@ -35,7 +35,13 @@ void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_ob print(env, "}"); } -mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { +// args are reverse in the array +static mp_obj_t dict_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) { + // TODO create from an iterable! + return rt_build_map(0); +} + +static mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { mp_obj_dict_t *o = lhs_in; switch (op) { case RT_BINARY_OP_SUBSCR: @@ -58,6 +64,7 @@ const mp_obj_type_t dict_type = { { &mp_const_type }, "dict", dict_print, // print + dict_make_new, // make_new NULL, // call_n NULL, // unary_op dict_binary_op, // binary_op diff --git a/py/objexcept.c b/py/objexcept.c index e735852c3..ec03b9bf1 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -39,6 +39,7 @@ const mp_obj_type_t exception_type = { { &mp_const_type }, "exception", exception_print, // print + NULL, // make_new NULL, // call_n NULL, // unary_op NULL, // binary_op diff --git a/py/objfloat.c b/py/objfloat.c index f151fe25a..8fc925e0b 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -6,6 +6,7 @@ #include "nlr.h" #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "obj.h" #include "runtime0.h" @@ -18,12 +19,30 @@ typedef struct _mp_obj_float_t { mp_obj_t mp_obj_new_float(mp_float_t value); -void float_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) { +static void float_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) { mp_obj_float_t *o = o_in; print(env, "%.8g", o->value); } -mp_obj_t float_unary_op(int op, mp_obj_t o_in) { +static mp_obj_t float_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) { + switch (n_args) { + case 0: + return mp_obj_new_float(0); + + case 1: + // TODO allow string as arg and parse it + if (MP_OBJ_IS_TYPE(args[0], &float_type)) { + return args[0]; + } else { + return mp_obj_new_float(mp_obj_get_float(args[0])); + } + + default: + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "float takes at most 1 argument, %d given", (void*)(machine_int_t)n_args)); + } +} + +static mp_obj_t float_unary_op(int op, mp_obj_t o_in) { mp_obj_float_t *o = o_in; switch (op) { case RT_UNARY_OP_NOT: if (o->value != 0) { return mp_const_true;} else { return mp_const_false; } @@ -33,7 +52,7 @@ mp_obj_t float_unary_op(int op, mp_obj_t o_in) { } } -mp_obj_t float_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { +static mp_obj_t float_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { if (MP_OBJ_IS_TYPE(rhs_in, &complex_type)) { return complex_type.binary_op(op, lhs_in, rhs_in); } @@ -61,6 +80,7 @@ const mp_obj_type_t float_type = { { &mp_const_type }, "float", float_print, + float_make_new, // make_new NULL, // call_n float_unary_op, float_binary_op, diff --git a/py/objfun.c b/py/objfun.c index c4783867a..22b82a9ca 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -70,6 +70,7 @@ const mp_obj_type_t fun_native_type = { { &mp_const_type }, "function", NULL, // print + NULL, // make_new fun_native_call_n, // call_n NULL, // unary_op NULL, // binary_op @@ -162,6 +163,7 @@ const mp_obj_type_t fun_bc_type = { { &mp_const_type }, "function", NULL, // print + NULL, // make_new fun_bc_call_n, // call_n NULL, // unary_op NULL, // binary_op @@ -275,6 +277,7 @@ static const mp_obj_type_t fun_asm_type = { { &mp_const_type }, "function", NULL, // print + NULL, // make_new fun_asm_call_n, // call_n NULL, // unary_op NULL, // binary_op diff --git a/py/objgenerator.c b/py/objgenerator.c index 849212269..7fbca075e 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -40,6 +40,7 @@ const mp_obj_type_t gen_wrap_type = { { &mp_const_type }, "generator", NULL, // print + NULL, // make_new gen_wrap_call_n, // call_n NULL, // unary_op NULL, // binary_op @@ -94,6 +95,7 @@ const mp_obj_type_t gen_instance_type = { { &mp_const_type }, "generator", gen_instance_print, // print + NULL, // make_new NULL, // call_n NULL, // unary_op NULL, // binary_op diff --git a/py/objinstance.c b/py/objinstance.c index 6cfcdf6c1..fd8cada7c 100644 --- a/py/objinstance.c +++ b/py/objinstance.c @@ -93,6 +93,7 @@ const mp_obj_type_t instance_type = { { &mp_const_type }, "instance", NULL, // print + NULL, // make_new NULL, // call_n NULL, // unary_op NULL, // binary_op diff --git a/py/objlist.c b/py/objlist.c index 967df44fd..301882611 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -36,6 +36,29 @@ static void list_print(void (*print)(void *env, const char *fmt, ...), void *env print(env, "]"); } +static mp_obj_t list_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) { + switch (n_args) { + case 0: + // return a new, empty list + return rt_build_list(0, NULL); + + case 1: + { + // make list from iterable + mp_obj_t iterable = rt_getiter(args[0]); + mp_obj_t list = rt_build_list(0, NULL); + mp_obj_t item; + while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) { + rt_list_append(list, item); + } + return list; + } + + default: + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "list takes at most 1 argument, %d given", (void*)(machine_int_t)n_args)); + } +} + static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { mp_obj_list_t *o = lhs; switch (op) { @@ -165,6 +188,7 @@ const mp_obj_type_t list_type = { { &mp_const_type }, "list", list_print, // print + list_make_new, // make_new NULL, // call_n NULL, // unary_op list_binary_op, // binary_op @@ -242,6 +266,7 @@ static const mp_obj_type_t list_it_type = { { &mp_const_type }, "list_iterator", NULL, // print + NULL, // make_new NULL, // call_n NULL, // unary_op NULL, // binary_op diff --git a/py/objmodule.c b/py/objmodule.c index addab14b7..9263eb44f 100644 --- a/py/objmodule.c +++ b/py/objmodule.c @@ -25,6 +25,7 @@ const mp_obj_type_t module_type = { { &mp_const_type }, "module", module_print, // print + NULL, // make_new NULL, // call_n NULL, // unary_op NULL, // binary_op diff --git a/py/objnone.c b/py/objnone.c index f7b665e99..11dd4939b 100644 --- a/py/objnone.c +++ b/py/objnone.c @@ -18,6 +18,7 @@ const mp_obj_type_t none_type = { { &mp_const_type }, "NoneType", none_print, // print + NULL, // make_new NULL, // call_n NULL, // unary_op NULL, // binary_op diff --git a/py/objrange.c b/py/objrange.c index b7fd17fa0..b163f779b 100644 --- a/py/objrange.c +++ b/py/objrange.c @@ -26,6 +26,7 @@ static const mp_obj_type_t range_type = { { &mp_const_type} , "range", NULL, // print + NULL, // make_new NULL, // call_n NULL, // unary_op NULL, // binary_op @@ -70,6 +71,7 @@ static const mp_obj_type_t range_it_type = { { &mp_const_type }, "range_iterator", NULL, // print + NULL, // make_new NULL, // call_n NULL, // unary_op NULL, // binary_op diff --git a/py/objset.c b/py/objset.c index f225ca7f6..826f8bdc8 100644 --- a/py/objset.c +++ b/py/objset.c @@ -5,7 +5,9 @@ #include "nlr.h" #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "obj.h" +#include "runtime.h" #include "map.h" typedef struct _mp_obj_set_t { @@ -29,10 +31,34 @@ void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj print(env, "}"); } -static const mp_obj_type_t set_type = { +static mp_obj_t set_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) { + switch (n_args) { + case 0: + // return a new, empty set + return mp_obj_new_set(0, NULL); + + case 1: + { + // 1 argument, an iterable from which we make a new set + mp_obj_t set = mp_obj_new_set(0, NULL); + mp_obj_t iterable = rt_getiter(args[0]); + mp_obj_t item; + while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) { + mp_obj_set_store(set, item); + } + return set; + } + + default: + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "set takes at most 1 argument, %d given", (void*)(machine_int_t)n_args)); + } +} + +const mp_obj_type_t set_type = { { &mp_const_type }, "set", set_print, // print + set_make_new, // make_new NULL, // call_n NULL, // unary_op NULL, // binary_op diff --git a/py/objslice.c b/py/objslice.c index d99325fd7..a624be963 100644 --- a/py/objslice.c +++ b/py/objslice.c @@ -24,6 +24,7 @@ const mp_obj_type_t ellipsis_type = { { &mp_const_type }, "ellipsis", ellipsis_print, // print + NULL, // make_new NULL, // call_n NULL, // unary_op NULL, // binary_op @@ -58,6 +59,7 @@ const mp_obj_type_t slice_type = { "slice", slice_print, NULL, // call_n + NULL, // make_new NULL, // unary_op NULL, // binary_op NULL, // getiter diff --git a/py/objstr.c b/py/objstr.c index 03a761863..27c9440d0 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -179,6 +179,7 @@ const mp_obj_type_t str_type = { { &mp_const_type }, "str", str_print, // print + NULL, // make_new NULL, // call_n NULL, // unary_op str_binary_op, // binary_op diff --git a/py/objtuple.c b/py/objtuple.c index b98d6ede7..d55259d1a 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -1,13 +1,14 @@ #include #include -//#include #include #include "nlr.h" #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "obj.h" #include "runtime0.h" +#include "runtime.h" typedef struct _mp_obj_tuple_t { mp_obj_base_t base; @@ -20,7 +21,7 @@ static mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, int cur); /******************************************************************************/ /* tuple */ -void tuple_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) { +static void tuple_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) { mp_obj_tuple_t *o = o_in; print(env, "("); for (int i = 0; i < o->len; i++) { @@ -35,7 +36,48 @@ void tuple_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_o print(env, ")"); } -mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { +// args are in reverse order in the array +static mp_obj_t tuple_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) { + switch (n_args) { + case 0: + // return a empty tuple + return mp_const_empty_tuple; + + case 1: + { + // 1 argument, an iterable from which we make a new tuple + if (MP_OBJ_IS_TYPE(args[0], &tuple_type)) { + return args[0]; + } + + // TODO optimise for cases where we know the length of the iterator + + uint alloc = 4; + uint len = 0; + mp_obj_t *items = m_new(mp_obj_t, alloc); + + mp_obj_t iterable = rt_getiter(args[0]); + mp_obj_t item; + while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) { + if (len >= alloc) { + items = m_renew(mp_obj_t, items, alloc, alloc * 2); + alloc *= 2; + } + items[len++] = item; + } + + mp_obj_t tuple = mp_obj_new_tuple(len, items); + m_free(items, alloc); + + return tuple; + } + + default: + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "tuple takes at most 1 argument, %d given", (void*)(machine_int_t)n_args)); + } +} + +static mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { mp_obj_tuple_t *o = lhs; switch (op) { case RT_BINARY_OP_SUBSCR: @@ -50,20 +92,15 @@ mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { } } -mp_obj_t tuple_getiter(mp_obj_t o_in) { +static mp_obj_t tuple_getiter(mp_obj_t o_in) { return mp_obj_new_tuple_iterator(o_in, 0); } -void mp_obj_tuple_get(mp_obj_t self_in, uint *len, mp_obj_t **items) { - mp_obj_tuple_t *self = self_in; - *len = self->len; - *items = &self->items[0]; -} - const mp_obj_type_t tuple_type = { { &mp_const_type }, "tuple", tuple_print, // print + tuple_make_new, // make_new NULL, // call_n NULL, // unary_op tuple_binary_op, // binary_op @@ -72,7 +109,14 @@ const mp_obj_type_t tuple_type = { {{NULL, NULL},}, // method list }; +// the zero-length tuple +static const mp_obj_tuple_t empty_tuple_obj = {{&tuple_type}, 0}; +const mp_obj_t mp_const_empty_tuple = (mp_obj_t)&empty_tuple_obj; + mp_obj_t mp_obj_new_tuple(uint n, mp_obj_t *items) { + if (n == 0) { + return mp_const_empty_tuple; + } mp_obj_tuple_t *o = m_new_obj_var(mp_obj_tuple_t, mp_obj_t, n); o->base.type = &tuple_type; o->len = n; @@ -83,6 +127,9 @@ mp_obj_t mp_obj_new_tuple(uint n, mp_obj_t *items) { } mp_obj_t mp_obj_new_tuple_reverse(uint n, mp_obj_t *items) { + if (n == 0) { + return mp_const_empty_tuple; + } mp_obj_tuple_t *o = m_new_obj_var(mp_obj_tuple_t, mp_obj_t, n); o->base.type = &tuple_type; o->len = n; @@ -92,6 +139,12 @@ mp_obj_t mp_obj_new_tuple_reverse(uint n, mp_obj_t *items) { return o; } +void mp_obj_tuple_get(mp_obj_t self_in, uint *len, mp_obj_t **items) { + mp_obj_tuple_t *self = self_in; + *len = self->len; + *items = &self->items[0]; +} + /******************************************************************************/ /* tuple iterator */ @@ -101,7 +154,7 @@ typedef struct _mp_obj_tuple_it_t { machine_uint_t cur; } mp_obj_tuple_it_t; -mp_obj_t tuple_it_iternext(mp_obj_t self_in) { +static mp_obj_t tuple_it_iternext(mp_obj_t self_in) { mp_obj_tuple_it_t *self = self_in; if (self->cur < self->tuple->len) { mp_obj_t o_out = self->tuple->items[self->cur]; @@ -116,6 +169,7 @@ static const mp_obj_type_t tuple_it_type = { { &mp_const_type }, "tuple_iterator", NULL, // print + NULL, // make_new NULL, // call_n NULL, // unary_op NULL, // binary_op diff --git a/py/objtype.c b/py/objtype.c index 83ae48d2d..aeeaebb95 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -4,17 +4,30 @@ #include "nlr.h" #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "obj.h" -void type_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { - print(env, ""); +static void type_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { + mp_obj_type_t *self = self_in; + print(env, "", self->name); +} + +static mp_obj_t type_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { + mp_obj_type_t *self = self_in; + if (self->make_new != NULL) { + // TODO we need to init the object if it's an instance of a type + return self->make_new(self, n_args, args); + } else { + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "cannot create '%s' instances", self->name)); + } } const mp_obj_type_t mp_const_type = { { &mp_const_type }, - "", + "type", type_print, // print - NULL, // call_n + NULL, // make_new + type_call_n, // call_n NULL, // unary_op NULL, // binary_op NULL, // getiter diff --git a/py/runtime.c b/py/runtime.c index 72881067d..197c08b55 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -91,26 +91,31 @@ void rt_init(void) { mp_qstr_map_lookup(&map_builtins, MP_QSTR___build_class__, true)->value = rt_make_function_2(mp_builtin___build_class__); mp_qstr_map_lookup(&map_builtins, MP_QSTR___repl_print__, true)->value = rt_make_function_1(mp_builtin___repl_print__); - // built-in user functions + // built-in types + mp_qstr_map_lookup(&map_builtins, MP_QSTR_bool, true)->value = (mp_obj_t)&bool_type; +#if MICROPY_ENABLE_FLOAT + mp_qstr_map_lookup(&map_builtins, MP_QSTR_complex, true)->value = (mp_obj_t)&complex_type; +#endif + mp_qstr_map_lookup(&map_builtins, MP_QSTR_dict, true)->value = (mp_obj_t)&dict_type; +#if MICROPY_ENABLE_FLOAT + mp_qstr_map_lookup(&map_builtins, MP_QSTR_float, true)->value = (mp_obj_t)&float_type; +#endif + mp_qstr_map_lookup(&map_builtins, MP_QSTR_int, true)->value = (mp_obj_t)&int_type; + mp_qstr_map_lookup(&map_builtins, MP_QSTR_list, true)->value = (mp_obj_t)&list_type; + mp_qstr_map_lookup(&map_builtins, MP_QSTR_set, true)->value = (mp_obj_t)&set_type; + mp_qstr_map_lookup(&map_builtins, MP_QSTR_tuple, true)->value = (mp_obj_t)&tuple_type; + mp_qstr_map_lookup(&map_builtins, MP_QSTR_type, true)->value = (mp_obj_t)&mp_builtin_type_obj; // TODO + + // built-in user functions; TODO covert all to &mp_builtin_xxx's mp_qstr_map_lookup(&map_builtins, MP_QSTR_abs, true)->value = rt_make_function_1(mp_builtin_abs); mp_qstr_map_lookup(&map_builtins, MP_QSTR_all, true)->value = rt_make_function_1(mp_builtin_all); mp_qstr_map_lookup(&map_builtins, MP_QSTR_any, true)->value = rt_make_function_1(mp_builtin_any); - mp_qstr_map_lookup(&map_builtins, MP_QSTR_bool, true)->value = rt_make_function_var(0, mp_builtin_bool); mp_qstr_map_lookup(&map_builtins, MP_QSTR_callable, true)->value = rt_make_function_1(mp_builtin_callable); mp_qstr_map_lookup(&map_builtins, MP_QSTR_chr, true)->value = rt_make_function_1(mp_builtin_chr); -#if MICROPY_ENABLE_FLOAT - mp_qstr_map_lookup(&map_builtins, MP_QSTR_complex, true)->value = (mp_obj_t)&mp_builtin_complex_obj; -#endif - mp_qstr_map_lookup(&map_builtins, MP_QSTR_dict, true)->value = rt_make_function_0(mp_builtin_dict); mp_qstr_map_lookup(&map_builtins, MP_QSTR_divmod, true)->value = rt_make_function_2(mp_builtin_divmod); -#if MICROPY_ENABLE_FLOAT - mp_qstr_map_lookup(&map_builtins, MP_QSTR_float, true)->value = (mp_obj_t)&mp_builtin_float_obj; -#endif mp_qstr_map_lookup(&map_builtins, MP_QSTR_hash, true)->value = (mp_obj_t)&mp_builtin_hash_obj; - mp_qstr_map_lookup(&map_builtins, MP_QSTR_int, true)->value = (mp_obj_t)&mp_builtin_int_obj; mp_qstr_map_lookup(&map_builtins, MP_QSTR_iter, true)->value = (mp_obj_t)&mp_builtin_iter_obj; mp_qstr_map_lookup(&map_builtins, MP_QSTR_len, true)->value = rt_make_function_1(mp_builtin_len); - mp_qstr_map_lookup(&map_builtins, MP_QSTR_list, true)->value = rt_make_function_var(0, mp_builtin_list); mp_qstr_map_lookup(&map_builtins, MP_QSTR_max, true)->value = rt_make_function_var(1, mp_builtin_max); mp_qstr_map_lookup(&map_builtins, MP_QSTR_min, true)->value = rt_make_function_var(1, mp_builtin_min); mp_qstr_map_lookup(&map_builtins, MP_QSTR_next, true)->value = (mp_obj_t)&mp_builtin_next_obj; @@ -118,9 +123,7 @@ void rt_init(void) { mp_qstr_map_lookup(&map_builtins, MP_QSTR_pow, true)->value = rt_make_function_var(2, mp_builtin_pow); mp_qstr_map_lookup(&map_builtins, MP_QSTR_print, true)->value = rt_make_function_var(0, mp_builtin_print); mp_qstr_map_lookup(&map_builtins, MP_QSTR_range, true)->value = rt_make_function_var(1, mp_builtin_range); - mp_qstr_map_lookup(&map_builtins, MP_QSTR_set, true)->value = (mp_obj_t)&mp_builtin_set_obj; mp_qstr_map_lookup(&map_builtins, MP_QSTR_sum, true)->value = rt_make_function_var(1, mp_builtin_sum); - mp_qstr_map_lookup(&map_builtins, MP_QSTR_type, true)->value = (mp_obj_t)&mp_builtin_type_obj; next_unique_code_id = 1; // 0 indicates "no code" unique_codes = NULL; -- cgit v1.2.3 From 45b43c21c4f8e30dcff00c2429eddba20ca002aa Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 5 Jan 2014 01:50:45 +0000 Subject: Oops: add objint.c --- py/objint.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 py/objint.c (limited to 'py') diff --git a/py/objint.c b/py/objint.c new file mode 100644 index 000000000..5bc747e8f --- /dev/null +++ b/py/objint.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include + +#include "nlr.h" +#include "misc.h" +#include "mpconfig.h" +#include "mpqstr.h" +#include "obj.h" + +typedef struct _mp_obj_int_t { + mp_obj_base_t base; +} mp_obj_int_t; + +static mp_obj_t int_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) { + switch (n_args) { + case 0: + return MP_OBJ_NEW_SMALL_INT(0); + + case 1: + // TODO allow string as arg and parse it + return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0])); + + //case 2: + // TODO, parse with given base + + default: + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "int takes at most 2 arguments, %d given", (void*)(machine_int_t)n_args)); + } +} + +const mp_obj_type_t int_type = { + { &mp_const_type }, + "int", + NULL, + int_make_new, // make_new + NULL, // call_n + NULL, // unary_op + NULL, // binary_op + NULL, // getiter + NULL, // iternext + { { NULL, NULL }, }, // method list +}; + +mp_obj_t mp_obj_new_int(machine_int_t value) { + return MP_OBJ_NEW_SMALL_INT(value); +} -- cgit v1.2.3