diff --git a/src/public/cast.c b/src/public/cast.c index f4aa4e63..fede7ccc 100644 --- a/src/public/cast.c +++ b/src/public/cast.c @@ -5,10 +5,12 @@ #include "pocketpy/interpreter/vm.h" int64_t py_toint(const py_Ref self){ + assert(self->type == tp_int); return self->_i64; } double py_tofloat(const py_Ref self){ + assert(self->type == tp_float); return self->_f64; } @@ -29,15 +31,18 @@ bool py_castfloat(const py_Ref self, double* out){ } bool py_tobool(const py_Ref self){ + assert(self->type == tp_bool); return self->extra; } const char* py_tostr(const py_Ref self){ + assert(self->type == tp_str); py_Str* ud = PyObject__value(self->_obj); return py_Str__data(ud); } const char* py_tostrn(const py_Ref self, int* out){ + assert(self->type == tp_str); py_Str* ud = PyObject__value(self->_obj); *out = ud->size; return py_Str__data(ud); diff --git a/src2/main.c b/src2/main.c index ea1e1e15..06343b84 100644 --- a/src2/main.c +++ b/src2/main.c @@ -25,7 +25,7 @@ int main(int argc, char** argv) { #endif py_initialize(); - const char* source = "[1+2, 'a']"; + const char* source = "[1/2, 'a']"; py_Ref r0 = py_reg(0); if(py_eval(source, r0)){ @@ -35,9 +35,9 @@ int main(int argc, char** argv) { // handle the result py_Ref _0 = py_list__getitem(r0, 0); py_Ref _1 = py_list__getitem(r0, 1); - int _L0 = py_toint(_0); + float _L0 = py_tofloat(_0); const char* _L1 = py_tostr(_1); - printf("%d, %s\n", _L0, _L1); + printf("%f, %s\n", _L0, _L1); } py_finalize();