summaryrefslogtreecommitdiff
path: root/py/runtime.c
diff options
context:
space:
mode:
authorScott Shawcroft <scott.shawcroft@gmail.com>2017-10-24 22:31:16 -0700
committerScott Shawcroft <scott.shawcroft@gmail.com>2017-10-24 22:31:16 -0700
commit73c15dcf8b1b927757a595a867334b38355ddb8f (patch)
treeb1727518e47219b995611aa7e5c9da66e3df5075 /py/runtime.c
parentd43564f854a6a1202dfff550986b9343952d5d32 (diff)
parentf869d6b2e339c04469c6c9ea3fb2fabd7bbb2d8c (diff)
Merge commit 'f869d6b2e339c04469c6c9ea3fb2fabd7bbb2d8c' into nrf2_merge
This is prep for merging in the NRF5 pull request.
Diffstat (limited to 'py/runtime.c')
-rw-r--r--py/runtime.c35
1 files changed, 28 insertions, 7 deletions
diff --git a/py/runtime.c b/py/runtime.c
index 2d24f3f75..4af17dd09 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -31,8 +31,8 @@
#include "extmod/vfs.h"
-#include "py/mpstate.h"
-#include "py/nlr.h"
+// #include "py/mpstate.h"
+// #include "py/nlr.h"
#include "py/parsenum.h"
#include "py/compile.h"
#include "py/objstr.h"
@@ -42,7 +42,6 @@
#include "py/objmodule.h"
#include "py/objgenerator.h"
#include "py/smallint.h"
-#include "py/runtime0.h"
#include "py/runtime.h"
#include "py/builtin.h"
#include "py/stackctrl.h"
@@ -233,7 +232,7 @@ void mp_delete_global(qstr qst) {
mp_obj_dict_delete(MP_OBJ_FROM_PTR(mp_globals_get()), MP_OBJ_NEW_QSTR(qst));
}
-mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg) {
+mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg) {
DEBUG_OP_printf("unary " UINT_FMT " %p\n", op, arg);
if (op == MP_UNARY_OP_NOT) {
@@ -255,6 +254,15 @@ mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg) {
} else {
return MP_OBJ_NEW_SMALL_INT(-val);
}
+ case MP_UNARY_OP_ABS:
+ if (val >= 0) {
+ return arg;
+ } else if (val == MP_SMALL_INT_MIN) {
+ // check for overflow
+ return mp_obj_new_int(-val);
+ } else {
+ return MP_OBJ_NEW_SMALL_INT(-val);
+ }
default:
assert(op == MP_UNARY_OP_INVERT);
return MP_OBJ_NEW_SMALL_INT(~val);
@@ -285,7 +293,7 @@ mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg) {
}
}
-mp_obj_t mp_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) {
+mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) {
DEBUG_OP_printf("binary " UINT_FMT " %p %p\n", op, lhs, rhs);
// TODO correctly distinguish inplace operators for mutable objects
@@ -579,7 +587,20 @@ generic_binary_op:
}
}
- // TODO implement dispatch for reverse binary ops
+#if MICROPY_PY_REVERSE_SPECIAL_METHODS
+ if (op >= MP_BINARY_OP_OR && op <= MP_BINARY_OP_REVERSE_POWER) {
+ mp_obj_t t = rhs;
+ rhs = lhs;
+ lhs = t;
+ if (op <= MP_BINARY_OP_POWER) {
+ op += MP_BINARY_OP_REVERSE_OR - MP_BINARY_OP_OR;
+ goto generic_binary_op;
+ }
+
+ // Convert __rop__ back to __op__ for error message
+ op -= MP_BINARY_OP_REVERSE_OR - MP_BINARY_OP_OR;
+ }
+#endif
unsupported_op:
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
@@ -1476,7 +1497,7 @@ mp_obj_t mp_parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t parse_i
#endif // MICROPY_ENABLE_COMPILER
-NORETURN void *m_malloc_fail(size_t num_bytes) {
+NORETURN void m_malloc_fail(size_t num_bytes) {
DEBUG_printf("memory allocation failed, allocating %u bytes\n", (uint)num_bytes);
#if MICROPY_ENABLE_GC
if (gc_is_locked()) {