mirror of
				https://github.com/pocketpy/pocketpy
				synced 2025-10-26 22:40:17 +00:00 
			
		
		
		
	up
This commit is contained in:
		
							parent
							
								
									fa22cdb3b2
								
							
						
					
					
						commit
						595188496d
					
				| @ -143,8 +143,8 @@ struct Pointer{ | |||||||
|         switch(ctype.index){ |         switch(ctype.index){ | ||||||
|             case C_TYPE("char_"): return py_object(vm, ref<char>()); |             case C_TYPE("char_"): return py_object(vm, ref<char>()); | ||||||
|             case C_TYPE("int_"): return py_object(vm, ref<int>()); |             case C_TYPE("int_"): return py_object(vm, ref<int>()); | ||||||
|             case C_TYPE("float_"): return vm->PyFloat(ref<float>()); |             case C_TYPE("float_"): return py_object(vm, ref<float>()); | ||||||
|             case C_TYPE("double_"): return vm->PyFloat(ref<double>()); |             case C_TYPE("double_"): return py_object(vm, ref<double>()); | ||||||
|             case C_TYPE("bool_"): return vm->PyBool(ref<bool>()); |             case C_TYPE("bool_"): return vm->PyBool(ref<bool>()); | ||||||
|             case C_TYPE("void_"): vm->ValueError("cannot get void*"); break; |             case C_TYPE("void_"): vm->ValueError("cannot get void*"); break; | ||||||
|             case C_TYPE("int8_"): return py_object(vm, ref<int8_t>()); |             case C_TYPE("int8_"): return py_object(vm, ref<int8_t>()); | ||||||
| @ -166,8 +166,8 @@ struct Pointer{ | |||||||
|         switch(ctype.index){ |         switch(ctype.index){ | ||||||
|             case C_TYPE("char_"): ref<char>() = py_cast_v<i64>(vm, val); break; |             case C_TYPE("char_"): ref<char>() = py_cast_v<i64>(vm, val); break; | ||||||
|             case C_TYPE("int_"): ref<int>() = py_cast_v<i64>(vm, val); break; |             case C_TYPE("int_"): ref<int>() = py_cast_v<i64>(vm, val); break; | ||||||
|             case C_TYPE("float_"): ref<float>() = vm->PyFloat_AS_C(val); break; |             case C_TYPE("float_"): ref<float>() = py_cast_v<f64>(vm, val); break; | ||||||
|             case C_TYPE("double_"): ref<double>() = vm->PyFloat_AS_C(val); break; |             case C_TYPE("double_"): ref<double>() = py_cast_v<f64>(vm, val); break; | ||||||
|             case C_TYPE("bool_"): ref<bool>() = vm->PyBool_AS_C(val); break; |             case C_TYPE("bool_"): ref<bool>() = vm->PyBool_AS_C(val); break; | ||||||
|             case C_TYPE("void_"): vm->ValueError("cannot set void*"); break; |             case C_TYPE("void_"): vm->ValueError("cannot set void*"); break; | ||||||
|             case C_TYPE("int8_"): ref<int8_t>() = py_cast_v<i64>(vm, val); break; |             case C_TYPE("int8_"): ref<int8_t>() = py_cast_v<i64>(vm, val); break; | ||||||
|  | |||||||
| @ -171,7 +171,7 @@ private: | |||||||
|                 if (m[1].matched) base = 16; |                 if (m[1].matched) base = 16; | ||||||
|                 if (m[2].matched) { |                 if (m[2].matched) { | ||||||
|                     if(base == 16) SyntaxError("hex literal should not contain a dot"); |                     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 { |                 } else { | ||||||
|                     parser->set_next_token(TK("@num"), py_object(vm, S_TO_INT(m[0], &size, base))); |                     parser->set_next_token(TK("@num"), py_object(vm, S_TO_INT(m[0], &size, base))); | ||||||
|                 } |                 } | ||||||
|  | |||||||
| @ -22,9 +22,9 @@ CodeObject_ VM::compile(Str source, Str filename, CompileMode mode) { | |||||||
| #define BIND_NUM_ARITH_OPT(name, op)                                                                    \ | #define BIND_NUM_ARITH_OPT(name, op)                                                                    \ | ||||||
|     _vm->_bind_methods<1>({"int","float"}, #name, [](VM* vm, Args& args){                         \ |     _vm->_bind_methods<1>({"int","float"}, #name, [](VM* vm, Args& args){                         \ | ||||||
|         if(is_both_int(args[0], args[1])){                                                              \ |         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<i64>(vm, args[0]) op _py_cast_v<i64>(vm, args[1]));                     \ | ||||||
|         }else{                                                                                          \ |         }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 );                                     \ |             vm->TypeError("unsupported operand type(s) for " #op );                                     \ | ||||||
|         }                                                                                               \ |         }                                                                                               \ | ||||||
|         if(is_both_int(args[0], args[1]))                                                               \ |         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<i64>(vm, args[0]) op _py_cast_v<i64>(vm, args[1]));                    \ | ||||||
|         return vm->PyBool(vm->num_to_float(args[0]) op vm->num_to_float(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) { |     _vm->_bind_methods<1>({"int", "float"}, "__truediv__", [](VM* vm, Args& args) { | ||||||
|         f64 rhs = vm->num_to_float(args[1]); |         f64 rhs = vm->num_to_float(args[1]); | ||||||
|         if (rhs == 0) vm->ZeroDivisionError(); |         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) { |     _vm->_bind_methods<1>({"int", "float"}, "__pow__", [](VM* vm, Args& args) { | ||||||
|         if(is_both_int(args[0], args[1])){ |         if(is_both_int(args[0], args[1])){ | ||||||
|             i64 lhs = vm->_PyInt_AS_C(args[0]); |             i64 lhs = _py_cast_v<i64>(vm, args[0]); | ||||||
|             i64 rhs = vm->_PyInt_AS_C(args[1]); |             i64 rhs = _py_cast_v<i64>(vm, args[1]); | ||||||
|             bool flag = false; |             bool flag = false; | ||||||
|             if(rhs < 0) {flag = true; rhs = -rhs;} |             if(rhs < 0) {flag = true; rhs = -rhs;} | ||||||
|             i64 ret = 1; |             i64 ret = 1; | ||||||
| @ -200,17 +200,17 @@ void init_builtins(VM* _vm) { | |||||||
|                 lhs *= lhs; |                 lhs *= lhs; | ||||||
|                 rhs >>= 1; |                 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); |             return py_object(vm, ret); | ||||||
|         }else{ |         }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 ************/ |     /************ PyInt ************/ | ||||||
|     _vm->bind_static_method<1>("int", "__new__", [](VM* vm, Args& args) { |     _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_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<f64>(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_bool)) return py_object(vm, vm->_PyBool_AS_C(args[0]) ? 1 : 0); | ||||||
|         if (is_type(args[0], vm->tp_str)) { |         if (is_type(args[0], vm->tp_str)) { | ||||||
|             const Str& s = vm->PyStr_AS_C(args[0]); |             const Str& s = vm->PyStr_AS_C(args[0]); | ||||||
| @ -255,16 +255,16 @@ void init_builtins(VM* _vm) { | |||||||
| 
 | 
 | ||||||
|     /************ PyFloat ************/ |     /************ PyFloat ************/ | ||||||
|     _vm->bind_static_method<1>("float", "__new__", [](VM* vm, Args& args) { |     _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<i64>(vm, args[0])); |         if (is_type(args[0], vm->tp_int)) return py_object(vm, (f64)py_cast_v<i64>(vm, args[0])); | ||||||
|         if (is_type(args[0], vm->tp_float)) return 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)) { |         if (is_type(args[0], vm->tp_str)) { | ||||||
|             const Str& s = vm->PyStr_AS_C(args[0]); |             const Str& s = vm->PyStr_AS_C(args[0]); | ||||||
|             if(s == "inf") return vm->PyFloat(INFINITY); |             if(s == "inf") return py_object(vm, INFINITY); | ||||||
|             if(s == "-inf") return vm->PyFloat(-INFINITY); |             if(s == "-inf") return py_object(vm, -INFINITY); | ||||||
|             try{ |             try{ | ||||||
|                 f64 val = S_TO_FLOAT(s); |                 f64 val = S_TO_FLOAT(s); | ||||||
|                 return vm->PyFloat(val); |                 return py_object(vm, val); | ||||||
|             }catch(std::invalid_argument&){ |             }catch(std::invalid_argument&){ | ||||||
|                 vm->ValueError("invalid literal for float(): '" + s + "'"); |                 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) { |     _vm->bind_method<0>("float", "__repr__", [](VM* vm, Args& args) { | ||||||
|         f64 val = vm->PyFloat_AS_C(args[0]); |         f64 val = py_cast_v<f64>(vm, args[0]); | ||||||
|         if(std::isinf(val) || std::isnan(val)) return vm->PyStr(std::to_string(val)); |         if(std::isinf(val) || std::isnan(val)) return vm->PyStr(std::to_string(val)); | ||||||
|         StrStream ss; |         StrStream ss; | ||||||
|         ss << std::setprecision(std::numeric_limits<f64>::max_digits10-1-2) << val; |         ss << std::setprecision(std::numeric_limits<f64>::max_digits10-1-2) << val; | ||||||
| @ -284,7 +284,7 @@ void init_builtins(VM* _vm) { | |||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     _vm->bind_method<0>("float", "__json__", [](VM* vm, Args& args) { |     _vm->bind_method<0>("float", "__json__", [](VM* vm, Args& args) { | ||||||
|         f64 val = vm->PyFloat_AS_C(args[0]); |         f64 val = py_cast_v<f64>(vm, args[0]); | ||||||
|         if(std::isinf(val) || std::isnan(val)) vm->ValueError("cannot jsonify 'nan' or 'inf'"); |         if(std::isinf(val) || std::isnan(val)) vm->ValueError("cannot jsonify 'nan' or 'inf'"); | ||||||
|         return vm->PyStr(std::to_string(val)); |         return vm->PyStr(std::to_string(val)); | ||||||
|     }); |     }); | ||||||
| @ -557,7 +557,7 @@ void add_module_time(VM* vm){ | |||||||
|     PyVar mod = vm->new_module("time"); |     PyVar mod = vm->new_module("time"); | ||||||
|     vm->bind_func<0>(mod, "time", [](VM* vm, Args& args) { |     vm->bind_func<0>(mod, "time", [](VM* vm, Args& args) { | ||||||
|         auto now = std::chrono::high_resolution_clock::now(); |         auto now = std::chrono::high_resolution_clock::now(); | ||||||
|         return vm->PyFloat(std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count() / 1000000.0); |         return py_object(vm, std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count() / 1000000.0); | ||||||
|     }); |     }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -587,21 +587,21 @@ void add_module_json(VM* vm){ | |||||||
| 
 | 
 | ||||||
| void add_module_math(VM* vm){ | void add_module_math(VM* vm){ | ||||||
|     PyVar mod = vm->new_module("math"); |     PyVar mod = vm->new_module("math"); | ||||||
|     vm->setattr(mod, "pi", vm->PyFloat(3.1415926535897932384)); |     vm->setattr(mod, "pi", py_object(vm, 3.1415926535897932384)); | ||||||
|     vm->setattr(mod, "e" , vm->PyFloat(2.7182818284590452354)); |     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, "log", CPP_LAMBDA(py_object(vm, 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, "log10", CPP_LAMBDA(py_object(vm, 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, "log2", CPP_LAMBDA(py_object(vm, 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, "sin", CPP_LAMBDA(py_object(vm, 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, "cos", CPP_LAMBDA(py_object(vm, 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, "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, "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, "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, "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, "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){ | void add_module_dis(VM* vm){ | ||||||
| @ -763,7 +763,7 @@ void add_module_random(VM* vm){ | |||||||
|         return vm->None; |         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) { |     vm->bind_func<2>(mod, "randint", [](VM* vm, Args& args) { | ||||||
|         i64 a = py_cast_v<i64>(vm, args[0]); |         i64 a = py_cast_v<i64>(vm, args[0]); | ||||||
|         i64 b = py_cast_v<i64>(vm, args[1]); |         i64 b = py_cast_v<i64>(vm, args[1]); | ||||||
| @ -772,10 +772,10 @@ void add_module_random(VM* vm){ | |||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     vm->bind_func<2>(mod, "uniform", [](VM* vm, Args& args) { |     vm->bind_func<2>(mod, "uniform", [](VM* vm, Args& args) { | ||||||
|         f64 a = vm->PyFloat_AS_C(args[0]); |         f64 a = py_cast_v<f64>(vm, args[0]); | ||||||
|         f64 b = vm->PyFloat_AS_C(args[1]); |         f64 b = py_cast_v<f64>(vm, args[1]); | ||||||
|         if(a > b) std::swap(a, b); |         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); |     CodeObject_ code = vm->compile(kRandomCode, "random.py", EXEC_MODE); | ||||||
| @ -976,7 +976,7 @@ extern "C" { | |||||||
|             char* packet = strdup(ss.str().c_str()); |             char* packet = strdup(ss.str().c_str()); | ||||||
|             switch(ret_code){ |             switch(ret_code){ | ||||||
|                 case 'i': return py_object(vm, f_int(packet)); |                 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 'b': return vm->PyBool(f_bool(packet)); | ||||||
|                 case 's': { |                 case 's': { | ||||||
|                     char* p = f_str(packet); |                     char* p = f_str(packet); | ||||||
|  | |||||||
							
								
								
									
										50
									
								
								src/vm.h
									
									
									
									
									
								
							
							
						
						
									
										50
									
								
								src/vm.h
									
									
									
									
									
								
							| @ -40,7 +40,6 @@ public: | |||||||
|     PyVar _py_op_call; |     PyVar _py_op_call; | ||||||
|     PyVar _py_op_yield; |     PyVar _py_op_yield; | ||||||
|     std::vector<PyVar> _all_types; |     std::vector<PyVar> _all_types; | ||||||
|     // PyVar _ascii_str_pool[128];
 |  | ||||||
| 
 | 
 | ||||||
|     PyVar run_frame(Frame* frame); |     PyVar run_frame(Frame* frame); | ||||||
| 
 | 
 | ||||||
| @ -428,8 +427,6 @@ public: | |||||||
|         bind_func<ARGC>(builtins, funcName, fn); |         bind_func<ARGC>(builtins, funcName, fn); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     int normalized_index(int index, int size){ |     int normalized_index(int index, int size){ | ||||||
|         if(index < 0) index += size; |         if(index < 0) index += size; | ||||||
|         if(index < 0 || index >= size){ |         if(index < 0 || index >= size){ | ||||||
| @ -526,43 +523,7 @@ public: | |||||||
|         return static_cast<BaseIter*>(obj->value()); |         return static_cast<BaseIter*>(obj->value()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     inline const Str& PyStr_AS_C(const PyVar& obj) { |     DEF_NATIVE(Str, Str, tp_str) | ||||||
|         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<int*>(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(List, List, tp_list) |     DEF_NATIVE(List, List, tp_list) | ||||||
|     DEF_NATIVE(Tuple, Tuple, tp_tuple) |     DEF_NATIVE(Tuple, Tuple, tp_tuple) | ||||||
|     DEF_NATIVE(Function, Function, tp_function) |     DEF_NATIVE(Function, Function, tp_function) | ||||||
| @ -825,6 +786,7 @@ template<> bool _py_cast_v<bool>(VM* vm, const PyVar& obj){ | |||||||
|     return obj == vm->True; |     return obj == vm->True; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | DEF_NATIVE_2(Str, tp_str) | ||||||
| DEF_NATIVE_2(List, tp_list) | DEF_NATIVE_2(List, tp_list) | ||||||
| DEF_NATIVE_2(Tuple, tp_tuple) | DEF_NATIVE_2(Tuple, tp_tuple) | ||||||
| DEF_NATIVE_2(Function, tp_function) | DEF_NATIVE_2(Function, tp_function) | ||||||
| @ -840,7 +802,7 @@ PyVar VM::num_negated(const PyVar& obj){ | |||||||
|     if (is_int(obj)){ |     if (is_int(obj)){ | ||||||
|         return py_object(this, -py_cast_v<i64>(this, obj)); |         return py_object(this, -py_cast_v<i64>(this, obj)); | ||||||
|     }else if(is_float(obj)){ |     }else if(is_float(obj)){ | ||||||
|         return py_object(this, -PyFloat_AS_C(obj)); |         return py_object(this, -py_cast_v<f64>(this, obj)); | ||||||
|     } |     } | ||||||
|     TypeError("expected 'int' or 'float', got " + OBJ_NAME(_t(obj)).escape(true)); |     TypeError("expected 'int' or 'float', got " + OBJ_NAME(_t(obj)).escape(true)); | ||||||
|     return nullptr; |     return nullptr; | ||||||
| @ -848,7 +810,7 @@ PyVar VM::num_negated(const PyVar& obj){ | |||||||
| 
 | 
 | ||||||
| f64 VM::num_to_float(const PyVar& obj){ | f64 VM::num_to_float(const PyVar& obj){ | ||||||
|     if(is_float(obj)){ |     if(is_float(obj)){ | ||||||
|         return PyFloat_AS_C(obj); |         return py_cast_v<f64>(this, obj); | ||||||
|     } else if (is_int(obj)){ |     } else if (is_int(obj)){ | ||||||
|         return (f64)py_cast_v<i64>(this, obj); |         return (f64)py_cast_v<i64>(this, obj); | ||||||
|     } |     } | ||||||
| @ -860,7 +822,7 @@ const PyVar& VM::asBool(const PyVar& obj){ | |||||||
|     if(is_type(obj, tp_bool)) return obj; |     if(is_type(obj, tp_bool)) return obj; | ||||||
|     if(obj == None) return False; |     if(obj == None) return False; | ||||||
|     if(is_type(obj, tp_int)) return PyBool(py_cast_v<i64>(this, obj) != 0); |     if(is_type(obj, tp_int)) return PyBool(py_cast_v<i64>(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<f64>(this, obj) != 0.0); | ||||||
|     PyVarOrNull len_fn = getattr(obj, __len__, false); |     PyVarOrNull len_fn = getattr(obj, __len__, false); | ||||||
|     if(len_fn != nullptr){ |     if(len_fn != nullptr){ | ||||||
|         PyVar ret = call(len_fn); |         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_type)) return obj.bits; | ||||||
|     if (is_type(obj, tp_bool)) return _PyBool_AS_C(obj) ? 1 : 0; |     if (is_type(obj, tp_bool)) return _PyBool_AS_C(obj) ? 1 : 0; | ||||||
|     if (is_float(obj)){ |     if (is_float(obj)){ | ||||||
|         f64 val = PyFloat_AS_C(obj); |         f64 val = py_cast_v<f64>(this, obj); | ||||||
|         return (i64)std::hash<f64>()(val); |         return (i64)std::hash<f64>()(val); | ||||||
|     } |     } | ||||||
|     TypeError("unhashable type: " +  OBJ_NAME(_t(obj)).escape(true)); |     TypeError("unhashable type: " +  OBJ_NAME(_t(obj)).escape(true)); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user