summaryrefslogtreecommitdiffstatshomepage
path: root/py/objfloat.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-01-04 20:21:15 +0000
committerDamien George <damien.p.george@gmail.com>2014-01-04 20:21:15 +0000
commit71c5181a8dfa69ba9f5ca322a3aba0660be2e166 (patch)
tree77aae5a9008f269276bda9c433235f7e11b57ed4 /py/objfloat.c
parente9906ac3d771a312b05d76e42aee8e806dd0d128 (diff)
downloadmicropython-71c5181a8dfa69ba9f5ca322a3aba0660be2e166.tar.gz
micropython-71c5181a8dfa69ba9f5ca322a3aba0660be2e166.zip
Convert Python types to proper Python type hierarchy.
Now much more inline with how CPython does types.
Diffstat (limited to 'py/objfloat.c')
-rw-r--r--py/objfloat.c26
1 files changed, 23 insertions, 3 deletions
diff --git a/py/objfloat.c b/py/objfloat.c
index f151fe25a0..8fc925e0b6 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,