From 595188496d3983215a5f898e61be0e75402111d8 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 27 Feb 2023 21:22:18 +0800 Subject: [PATCH] up --- src/cffi.h | 8 +++---- src/compiler.h | 2 +- src/pocketpy.h | 64 +++++++++++++++++++++++++------------------------- src/vm.h | 50 +++++---------------------------------- 4 files changed, 43 insertions(+), 81 deletions(-) diff --git a/src/cffi.h b/src/cffi.h index c574135b..6d3e2847 100644 --- a/src/cffi.h +++ b/src/cffi.h @@ -143,8 +143,8 @@ struct Pointer{ switch(ctype.index){ case C_TYPE("char_"): return py_object(vm, ref()); case C_TYPE("int_"): return py_object(vm, ref()); - case C_TYPE("float_"): return vm->PyFloat(ref()); - case C_TYPE("double_"): return vm->PyFloat(ref()); + case C_TYPE("float_"): return py_object(vm, ref()); + case C_TYPE("double_"): return py_object(vm, ref()); case C_TYPE("bool_"): return vm->PyBool(ref()); case C_TYPE("void_"): vm->ValueError("cannot get void*"); break; case C_TYPE("int8_"): return py_object(vm, ref()); @@ -166,8 +166,8 @@ struct Pointer{ switch(ctype.index){ case C_TYPE("char_"): ref() = py_cast_v(vm, val); break; case C_TYPE("int_"): ref() = py_cast_v(vm, val); break; - case C_TYPE("float_"): ref() = vm->PyFloat_AS_C(val); break; - case C_TYPE("double_"): ref() = vm->PyFloat_AS_C(val); break; + case C_TYPE("float_"): ref() = py_cast_v(vm, val); break; + case C_TYPE("double_"): ref() = py_cast_v(vm, val); break; case C_TYPE("bool_"): ref() = vm->PyBool_AS_C(val); break; case C_TYPE("void_"): vm->ValueError("cannot set void*"); break; case C_TYPE("int8_"): ref() = py_cast_v(vm, val); break; diff --git a/src/compiler.h b/src/compiler.h index 8fac8002..ec0ac260 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -171,7 +171,7 @@ private: if (m[1].matched) base = 16; if (m[2].matched) { if(base == 16) SyntaxError("hex literal should not contain a dot"); - parser->set_next_token(TK("@num"), vm->PyFloat(S_TO_FLOAT(m[0], &size))); + parser->set_next_token(TK("@num"), py_object(vm, S_TO_FLOAT(m[0], &size))); } else { parser->set_next_token(TK("@num"), py_object(vm, S_TO_INT(m[0], &size, base))); } diff --git a/src/pocketpy.h b/src/pocketpy.h index ecc00117..1a6d92c2 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -22,9 +22,9 @@ CodeObject_ VM::compile(Str source, Str filename, CompileMode mode) { #define BIND_NUM_ARITH_OPT(name, op) \ _vm->_bind_methods<1>({"int","float"}, #name, [](VM* vm, Args& args){ \ if(is_both_int(args[0], args[1])){ \ - return py_object(vm, vm->_PyInt_AS_C(args[0]) op vm->_PyInt_AS_C(args[1])); \ + return py_object(vm, _py_cast_v(vm, args[0]) op _py_cast_v(vm, args[1])); \ }else{ \ - return vm->PyFloat(vm->num_to_float(args[0]) op vm->num_to_float(args[1])); \ + return py_object(vm, vm->num_to_float(args[0]) op vm->num_to_float(args[1])); \ } \ }); @@ -35,7 +35,7 @@ CodeObject_ VM::compile(Str source, Str filename, CompileMode mode) { vm->TypeError("unsupported operand type(s) for " #op ); \ } \ if(is_both_int(args[0], args[1])) \ - return vm->PyBool(vm->_PyInt_AS_C(args[0]) op vm->_PyInt_AS_C(args[1])); \ + return vm->PyBool(_py_cast_v(vm, args[0]) op _py_cast_v(vm, args[1])); \ return vm->PyBool(vm->num_to_float(args[0]) op vm->num_to_float(args[1])); \ }); @@ -185,13 +185,13 @@ void init_builtins(VM* _vm) { _vm->_bind_methods<1>({"int", "float"}, "__truediv__", [](VM* vm, Args& args) { f64 rhs = vm->num_to_float(args[1]); if (rhs == 0) vm->ZeroDivisionError(); - return vm->PyFloat(vm->num_to_float(args[0]) / rhs); + return py_object(vm, vm->num_to_float(args[0]) / rhs); }); _vm->_bind_methods<1>({"int", "float"}, "__pow__", [](VM* vm, Args& args) { if(is_both_int(args[0], args[1])){ - i64 lhs = vm->_PyInt_AS_C(args[0]); - i64 rhs = vm->_PyInt_AS_C(args[1]); + i64 lhs = _py_cast_v(vm, args[0]); + i64 rhs = _py_cast_v(vm, args[1]); bool flag = false; if(rhs < 0) {flag = true; rhs = -rhs;} i64 ret = 1; @@ -200,17 +200,17 @@ void init_builtins(VM* _vm) { lhs *= lhs; rhs >>= 1; } - if(flag) return vm->PyFloat((f64)(1.0 / ret)); + if(flag) return py_object(vm, (f64)(1.0 / ret)); return py_object(vm, ret); }else{ - return vm->PyFloat((f64)std::pow(vm->num_to_float(args[0]), vm->num_to_float(args[1]))); + return py_object(vm, (f64)std::pow(vm->num_to_float(args[0]), vm->num_to_float(args[1]))); } }); /************ PyInt ************/ _vm->bind_static_method<1>("int", "__new__", [](VM* vm, Args& args) { if (is_type(args[0], vm->tp_int)) return args[0]; - if (is_type(args[0], vm->tp_float)) return py_object(vm, (i64)vm->PyFloat_AS_C(args[0])); + if (is_type(args[0], vm->tp_float)) return py_object(vm, (i64)py_cast_v(vm, args[0])); if (is_type(args[0], vm->tp_bool)) return py_object(vm, vm->_PyBool_AS_C(args[0]) ? 1 : 0); if (is_type(args[0], vm->tp_str)) { const Str& s = vm->PyStr_AS_C(args[0]); @@ -255,16 +255,16 @@ void init_builtins(VM* _vm) { /************ PyFloat ************/ _vm->bind_static_method<1>("float", "__new__", [](VM* vm, Args& args) { - if (is_type(args[0], vm->tp_int)) return vm->PyFloat((f64)py_cast_v(vm, args[0])); + if (is_type(args[0], vm->tp_int)) return py_object(vm, (f64)py_cast_v(vm, args[0])); if (is_type(args[0], vm->tp_float)) return args[0]; - if (is_type(args[0], vm->tp_bool)) return vm->PyFloat(vm->_PyBool_AS_C(args[0]) ? 1.0 : 0.0); + if (is_type(args[0], vm->tp_bool)) return py_object(vm, vm->_PyBool_AS_C(args[0]) ? 1.0 : 0.0); if (is_type(args[0], vm->tp_str)) { const Str& s = vm->PyStr_AS_C(args[0]); - if(s == "inf") return vm->PyFloat(INFINITY); - if(s == "-inf") return vm->PyFloat(-INFINITY); + if(s == "inf") return py_object(vm, INFINITY); + if(s == "-inf") return py_object(vm, -INFINITY); try{ f64 val = S_TO_FLOAT(s); - return vm->PyFloat(val); + return py_object(vm, val); }catch(std::invalid_argument&){ vm->ValueError("invalid literal for float(): '" + s + "'"); } @@ -274,7 +274,7 @@ void init_builtins(VM* _vm) { }); _vm->bind_method<0>("float", "__repr__", [](VM* vm, Args& args) { - f64 val = vm->PyFloat_AS_C(args[0]); + f64 val = py_cast_v(vm, args[0]); if(std::isinf(val) || std::isnan(val)) return vm->PyStr(std::to_string(val)); StrStream ss; ss << std::setprecision(std::numeric_limits::max_digits10-1-2) << val; @@ -284,7 +284,7 @@ void init_builtins(VM* _vm) { }); _vm->bind_method<0>("float", "__json__", [](VM* vm, Args& args) { - f64 val = vm->PyFloat_AS_C(args[0]); + f64 val = py_cast_v(vm, args[0]); if(std::isinf(val) || std::isnan(val)) vm->ValueError("cannot jsonify 'nan' or 'inf'"); return vm->PyStr(std::to_string(val)); }); @@ -557,7 +557,7 @@ void add_module_time(VM* vm){ PyVar mod = vm->new_module("time"); vm->bind_func<0>(mod, "time", [](VM* vm, Args& args) { auto now = std::chrono::high_resolution_clock::now(); - return vm->PyFloat(std::chrono::duration_cast(now.time_since_epoch()).count() / 1000000.0); + return py_object(vm, std::chrono::duration_cast(now.time_since_epoch()).count() / 1000000.0); }); } @@ -587,21 +587,21 @@ void add_module_json(VM* vm){ void add_module_math(VM* vm){ PyVar mod = vm->new_module("math"); - vm->setattr(mod, "pi", vm->PyFloat(3.1415926535897932384)); - vm->setattr(mod, "e" , vm->PyFloat(2.7182818284590452354)); + vm->setattr(mod, "pi", py_object(vm, 3.1415926535897932384)); + vm->setattr(mod, "e" , py_object(vm, 2.7182818284590452354)); - vm->bind_func<1>(mod, "log", CPP_LAMBDA(vm->PyFloat(std::log(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "log10", CPP_LAMBDA(vm->PyFloat(std::log10(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "log2", CPP_LAMBDA(vm->PyFloat(std::log2(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "sin", CPP_LAMBDA(vm->PyFloat(std::sin(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "cos", CPP_LAMBDA(vm->PyFloat(std::cos(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "tan", CPP_LAMBDA(vm->PyFloat(std::tan(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "log", CPP_LAMBDA(py_object(vm, std::log(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "log10", CPP_LAMBDA(py_object(vm, std::log10(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "log2", CPP_LAMBDA(py_object(vm, std::log2(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "sin", CPP_LAMBDA(py_object(vm, std::sin(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "cos", CPP_LAMBDA(py_object(vm, std::cos(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "tan", CPP_LAMBDA(py_object(vm, std::tan(vm->num_to_float(args[0]))))); vm->bind_func<1>(mod, "isnan", CPP_LAMBDA(vm->PyBool(std::isnan(vm->num_to_float(args[0]))))); vm->bind_func<1>(mod, "isinf", CPP_LAMBDA(vm->PyBool(std::isinf(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "fabs", CPP_LAMBDA(vm->PyFloat(std::fabs(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "fabs", CPP_LAMBDA(py_object(vm, std::fabs(vm->num_to_float(args[0]))))); vm->bind_func<1>(mod, "floor", CPP_LAMBDA(py_object(vm, (i64)std::floor(vm->num_to_float(args[0]))))); vm->bind_func<1>(mod, "ceil", CPP_LAMBDA(py_object(vm, (i64)std::ceil(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "sqrt", CPP_LAMBDA(vm->PyFloat(std::sqrt(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "sqrt", CPP_LAMBDA(py_object(vm, std::sqrt(vm->num_to_float(args[0]))))); } void add_module_dis(VM* vm){ @@ -763,7 +763,7 @@ void add_module_random(VM* vm){ return vm->None; }); - vm->bind_func<0>(mod, "random", CPP_LAMBDA(vm->PyFloat(std::rand() / (f64)RAND_MAX))); + vm->bind_func<0>(mod, "random", CPP_LAMBDA(py_object(vm, std::rand() / (f64)RAND_MAX))); vm->bind_func<2>(mod, "randint", [](VM* vm, Args& args) { i64 a = py_cast_v(vm, args[0]); i64 b = py_cast_v(vm, args[1]); @@ -772,10 +772,10 @@ void add_module_random(VM* vm){ }); vm->bind_func<2>(mod, "uniform", [](VM* vm, Args& args) { - f64 a = vm->PyFloat_AS_C(args[0]); - f64 b = vm->PyFloat_AS_C(args[1]); + f64 a = py_cast_v(vm, args[0]); + f64 b = py_cast_v(vm, args[1]); if(a > b) std::swap(a, b); - return vm->PyFloat(a + (b - a) * std::rand() / (f64)RAND_MAX); + return py_object(vm, a + (b - a) * std::rand() / (f64)RAND_MAX); }); CodeObject_ code = vm->compile(kRandomCode, "random.py", EXEC_MODE); @@ -976,7 +976,7 @@ extern "C" { char* packet = strdup(ss.str().c_str()); switch(ret_code){ case 'i': return py_object(vm, f_int(packet)); - case 'f': return vm->PyFloat(f_float(packet)); + case 'f': return py_object(vm, f_float(packet)); case 'b': return vm->PyBool(f_bool(packet)); case 's': { char* p = f_str(packet); diff --git a/src/vm.h b/src/vm.h index 38339707..e1eaa4b6 100644 --- a/src/vm.h +++ b/src/vm.h @@ -40,7 +40,6 @@ public: PyVar _py_op_call; PyVar _py_op_yield; std::vector _all_types; - // PyVar _ascii_str_pool[128]; PyVar run_frame(Frame* frame); @@ -428,8 +427,6 @@ public: bind_func(builtins, funcName, fn); } - - int normalized_index(int index, int size){ if(index < 0) index += size; if(index < 0 || index >= size){ @@ -526,43 +523,7 @@ public: return static_cast(obj->value()); } - inline const Str& PyStr_AS_C(const PyVar& obj) { - check_type(obj, tp_str); - return OBJ_GET(Str, obj); - } - inline PyVar PyStr(const Str& value) { - // some BUGs here - // if(value.size() == 1){ - // char c = value.c_str()[0]; - // if(c >= 0) return _ascii_str_pool[(int)c]; - // } - return new_object(tp_str, value); - } - - inline i64 _PyInt_AS_C(const PyVar& obj){ - return obj.bits >> 2; - } - - inline PyVar PyFloat(f64 value) { - i64 bits = __8B(value)._int; - bits = (bits >> 2) << 2; - bits |= 0b10; - return PyVar(reinterpret_cast(bits)); - } - - inline f64 PyFloat_AS_C(const PyVar& obj){ - check_type(obj, tp_float); - i64 bits = obj.bits; - bits = (bits >> 2) << 2; - return __8B(bits)._float; - } - - inline f64 _PyFloat_AS_C(const PyVar& obj){ - i64 bits = obj.bits; - bits = (bits >> 2) << 2; - return __8B(bits)._float; - } - + DEF_NATIVE(Str, Str, tp_str) DEF_NATIVE(List, List, tp_list) DEF_NATIVE(Tuple, Tuple, tp_tuple) DEF_NATIVE(Function, Function, tp_function) @@ -825,6 +786,7 @@ template<> bool _py_cast_v(VM* vm, const PyVar& obj){ return obj == vm->True; } +DEF_NATIVE_2(Str, tp_str) DEF_NATIVE_2(List, tp_list) DEF_NATIVE_2(Tuple, tp_tuple) DEF_NATIVE_2(Function, tp_function) @@ -840,7 +802,7 @@ PyVar VM::num_negated(const PyVar& obj){ if (is_int(obj)){ return py_object(this, -py_cast_v(this, obj)); }else if(is_float(obj)){ - return py_object(this, -PyFloat_AS_C(obj)); + return py_object(this, -py_cast_v(this, obj)); } TypeError("expected 'int' or 'float', got " + OBJ_NAME(_t(obj)).escape(true)); return nullptr; @@ -848,7 +810,7 @@ PyVar VM::num_negated(const PyVar& obj){ f64 VM::num_to_float(const PyVar& obj){ if(is_float(obj)){ - return PyFloat_AS_C(obj); + return py_cast_v(this, obj); } else if (is_int(obj)){ return (f64)py_cast_v(this, obj); } @@ -860,7 +822,7 @@ const PyVar& VM::asBool(const PyVar& obj){ if(is_type(obj, tp_bool)) return obj; if(obj == None) return False; if(is_type(obj, tp_int)) return PyBool(py_cast_v(this, obj) != 0); - if(is_type(obj, tp_float)) return PyBool(PyFloat_AS_C(obj) != 0.0); + if(is_type(obj, tp_float)) return PyBool(py_cast_v(this, obj) != 0.0); PyVarOrNull len_fn = getattr(obj, __len__, false); if(len_fn != nullptr){ PyVar ret = call(len_fn); @@ -884,7 +846,7 @@ i64 VM::hash(const PyVar& obj){ if (is_type(obj, tp_type)) return obj.bits; if (is_type(obj, tp_bool)) return _PyBool_AS_C(obj) ? 1 : 0; if (is_float(obj)){ - f64 val = PyFloat_AS_C(obj); + f64 val = py_cast_v(this, obj); return (i64)std::hash()(val); } TypeError("unhashable type: " + OBJ_NAME(_t(obj)).escape(true));