add assert

This commit is contained in:
blueloveTH 2024-06-30 23:01:26 +08:00
parent 8bbb9fc5f3
commit 8270d9035b
2 changed files with 8 additions and 3 deletions

View File

@ -5,10 +5,12 @@
#include "pocketpy/interpreter/vm.h" #include "pocketpy/interpreter/vm.h"
int64_t py_toint(const py_Ref self){ int64_t py_toint(const py_Ref self){
assert(self->type == tp_int);
return self->_i64; return self->_i64;
} }
double py_tofloat(const py_Ref self){ double py_tofloat(const py_Ref self){
assert(self->type == tp_float);
return self->_f64; return self->_f64;
} }
@ -29,15 +31,18 @@ bool py_castfloat(const py_Ref self, double* out){
} }
bool py_tobool(const py_Ref self){ bool py_tobool(const py_Ref self){
assert(self->type == tp_bool);
return self->extra; return self->extra;
} }
const char* py_tostr(const py_Ref self){ const char* py_tostr(const py_Ref self){
assert(self->type == tp_str);
py_Str* ud = PyObject__value(self->_obj); py_Str* ud = PyObject__value(self->_obj);
return py_Str__data(ud); return py_Str__data(ud);
} }
const char* py_tostrn(const py_Ref self, int* out){ const char* py_tostrn(const py_Ref self, int* out){
assert(self->type == tp_str);
py_Str* ud = PyObject__value(self->_obj); py_Str* ud = PyObject__value(self->_obj);
*out = ud->size; *out = ud->size;
return py_Str__data(ud); return py_Str__data(ud);

View File

@ -25,7 +25,7 @@ int main(int argc, char** argv) {
#endif #endif
py_initialize(); py_initialize();
const char* source = "[1+2, 'a']"; const char* source = "[1/2, 'a']";
py_Ref r0 = py_reg(0); py_Ref r0 = py_reg(0);
if(py_eval(source, r0)){ if(py_eval(source, r0)){
@ -35,9 +35,9 @@ int main(int argc, char** argv) {
// handle the result // handle the result
py_Ref _0 = py_list__getitem(r0, 0); py_Ref _0 = py_list__getitem(r0, 0);
py_Ref _1 = py_list__getitem(r0, 1); py_Ref _1 = py_list__getitem(r0, 1);
int _L0 = py_toint(_0); float _L0 = py_tofloat(_0);
const char* _L1 = py_tostr(_1); const char* _L1 = py_tostr(_1);
printf("%d, %s\n", _L0, _L1); printf("%f, %s\n", _L0, _L1);
} }
py_finalize(); py_finalize();