Commit 71c5181a authored by Damien George's avatar Damien George
Browse files

Convert Python types to proper Python type hierarchy.

Now much more inline with how CPython does types.
parent e9906ac3
......@@ -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;
......
......@@ -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]);
......
......@@ -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;
......
......@@ -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)
......
......@@ -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)));
}
}
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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,
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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 = {