Przeglądaj źródła

use int64 and float64

blueloveTH 3 lat temu
rodzic
commit
252bf167b1
5 zmienionych plików z 33 dodań i 30 usunięć
  1. 2 2
      src/compiler.h
  2. 3 3
      src/iter.h
  3. 8 5
      src/obj.h
  4. 9 9
      src/pocketpy.h
  5. 11 11
      src/vm.h

+ 2 - 2
src/compiler.h

@@ -151,9 +151,9 @@ public:
                 // here is m.length()-1, since the first char is eaten by lexToken()
                 for(int j=0; j<m.length()-1; j++) parser->eatChar();
                 if (m[2].matched) {
-                    parser->setNextToken(TK("@num"), vm->PyFloat(std::stof(m[0])));
+                    parser->setNextToken(TK("@num"), vm->PyFloat(std::stod(m[0])));
                 } else {
-                    parser->setNextToken(TK("@num"), vm->PyInt(std::stoi(m[0])));
+                    parser->setNextToken(TK("@num"), vm->PyInt(std::stoll(m[0])));
                 }  
             }
         }catch(std::exception& e){

+ 3 - 3
src/iter.h

@@ -2,11 +2,11 @@
 
 #include "obj.h"
 
-typedef std::function<PyVar (int)> _PyIntFn;
+typedef std::function<PyVar (_Int)> _PyIntFn;
 
 class RangeIterator : public _Iterator {
 private:
-    int current;
+    _Int current;
     _Range r;
     _PyIntFn fn;
 public:
@@ -32,7 +32,7 @@ public:
 
 class VectorIterator : public _Iterator {
 private:
-    int index = 0;
+    size_t index = 0;
     const PyVarList* vec;
 public:
     VectorIterator(PyVar _ref) : _Iterator(_ref) {

+ 8 - 5
src/obj.h

@@ -10,6 +10,9 @@
 
 #include "str.h"
 
+typedef int64_t _Int;
+typedef double _Float;
+
 class PyObject;
 class CodeObject;
 class BasePointer;
@@ -47,14 +50,14 @@ struct BoundedMethod {
 };
 
 struct _Range {
-    int start = 0;
-    int stop = -1;
-    int step = 1;
+    _Int start = 0;
+    _Int stop = -1;
+    _Int step = 1;
 };
 
 struct _Slice {
     int start = 0;
-    int stop = 2147483647;
+    int stop = 2147483647;  // contain types always use int32 as index, no support for int64
 
     void normalize(int len){
         if(start < 0) start += len;
@@ -76,7 +79,7 @@ public:
     _Iterator(PyVar _ref) : _ref(_ref) {}
 };
 
-typedef std::variant<int,float,bool,_Str,PyVarList,_CppFunc,_Func,std::shared_ptr<_Iterator>,BoundedMethod,_Range,_Slice,_Pointer> _Value;
+typedef std::variant<_Int,_Float,bool,_Str,PyVarList,_CppFunc,_Func,std::shared_ptr<_Iterator>,BoundedMethod,_Range,_Slice,_Pointer> _Value;
 
 #define UNREACHABLE() throw std::runtime_error("Unreachable code")
 

+ 9 - 9
src/pocketpy.h

@@ -3,9 +3,9 @@
 #include "vm.h"
 #include "compiler.h"
 
-inline int _round(float f){
-    if(f > 0) return (int)(f + 0.5);
-    return (int)(f - 0.5);
+inline _Int _round(_Float f){
+    if(f > 0) return (_Int)(f + 0.5);
+    return (_Int)(f - 0.5);
 }
 
 #define BIND_NUM_ARITH_OPT(name, op)                                                                    \
@@ -66,7 +66,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
     });
 
     _vm->bindBuiltinFunc("chr", [](VM* vm, PyVarList args) {
-        int i = vm->PyInt_AS_C(args.at(0));
+        _Int i = vm->PyInt_AS_C(args.at(0));
         if (i < 0 || i > 128) vm->valueError("chr() arg not in range(128)");
         return vm->PyStr(_Str(1, (char)i));
     });
@@ -78,7 +78,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
     _vm->bindBuiltinFunc("ord", [](VM* vm, PyVarList args) {
         _Str s = vm->PyStr_AS_C(args.at(0));
         if (s.size() != 1) vm->typeError("ord() expected an ASCII character");
-        return vm->PyInt((int)s[0]);
+        return vm->PyInt((_Int)s[0]);
     });
 
     _vm->bindBuiltinFunc("dir", [](VM* vm, PyVarList args) {
@@ -111,7 +111,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
 
     _vm->bindMethod("range", "__iter__", [](VM* vm, PyVarList args) {
         vm->__checkType(args.at(0), vm->_tp_range);
-        auto iter = std::make_shared<RangeIterator>(args[0], [=](int val){return vm->PyInt(val);});
+        auto iter = std::make_shared<RangeIterator>(args[0], [=](_Int val){return vm->PyInt(val);});
         return vm->PyIter(iter);
     });
 
@@ -131,7 +131,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
         if(args[0]->isType(vm->_tp_int) && args[1]->isType(vm->_tp_int)){
             return vm->PyInt(_round(pow(vm->PyInt_AS_C(args[0]), vm->PyInt_AS_C(args[1]))));
         }else{
-            return vm->PyFloat((float)pow(vm->numToFloat(args[0]), vm->numToFloat(args[1])));
+            return vm->PyFloat((_Float)pow(vm->numToFloat(args[0]), vm->numToFloat(args[1])));
         }
     });
 
@@ -160,7 +160,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
 
     /************ PyFloat ************/
     _vm->bindMethod("float", "__neg__", [](VM* vm, PyVarList args) {
-        return vm->PyFloat(-1.0f * vm->PyFloat_AS_C(args[0]));
+        return vm->PyFloat(-1.0 * vm->PyFloat_AS_C(args[0]));
     });
 
     _vm->bindMethod("float", "__repr__", [](VM* vm, PyVarList args) {
@@ -418,7 +418,7 @@ void __runCodeBuiltins(VM* vm, const char* src){
 void __addModuleTime(VM* vm){
     PyVar mod = vm->newModule("time");
     vm->bindFunc(mod, "time", [](VM* vm, PyVarList args) {
-        return vm->PyInt((int)std::time(nullptr));
+        return vm->PyFloat((_Float)std::time(nullptr));
     });
 }
 

+ 11 - 11
src/vm.h

@@ -316,7 +316,7 @@ public:
         PyVar tp = obj->attribs[__class__];
         if(tp == _tp_bool) return obj;
         if(tp == _tp_int) return PyBool(PyInt_AS_C(obj) != 0);
-        if(tp == _tp_float) return PyBool(PyFloat_AS_C(obj) != 0.0f);
+        if(tp == _tp_float) return PyBool(PyFloat_AS_C(obj) != 0.0);
         PyVarOrNull len_fn = getAttr(obj, "__len__", false);
         if(len_fn != nullptr){
             PyVar ret = call(len_fn, {});
@@ -533,9 +533,9 @@ public:
         return isIntOrFloat(obj1) && isIntOrFloat(obj2);
     }
 
-    float numToFloat(const PyVar& obj){
+    _Float numToFloat(const PyVar& obj){
         if (obj->isType(_tp_int)){
-            return (float)PyInt_AS_C(obj);
+            return (_Float)PyInt_AS_C(obj);
         }else if(obj->isType(_tp_float)){
             return PyFloat_AS_C(obj);
         }
@@ -556,8 +556,8 @@ public:
     PyVar _tp_function, _tp_native_function, _tp_native_iterator, _tp_bounded_method;
     PyVar _tp_slice, _tp_range, _tp_module, _tp_pointer;
 
-    __DEF_PY_AS_C(Int, int, _tp_int)
-    __DEF_PY_AS_C(Float, float, _tp_float)
+    __DEF_PY_AS_C(Int, _Int, _tp_int)
+    __DEF_PY_AS_C(Float, _Float, _tp_float)
     DEF_NATIVE(Str, _Str, _tp_str)
     DEF_NATIVE(List, PyVarList, _tp_list)
     DEF_NATIVE(Tuple, PyVarList, _tp_tuple)
@@ -569,8 +569,8 @@ public:
     DEF_NATIVE(Slice, _Slice, _tp_slice)
     DEF_NATIVE(Pointer, _Pointer, _tp_pointer)
     
-    inline PyVar PyInt(int i) { return newNumber(_tp_int, i); }
-    inline PyVar PyFloat(float f) { return newNumber(_tp_float, f); }
+    inline PyVar PyInt(_Int i) { return newNumber(_tp_int, i); }
+    inline PyVar PyFloat(_Float f) { return newNumber(_tp_float, f); }
     inline bool PyBool_AS_C(PyVar obj){return obj == True;}
     inline PyVar PyBool(bool value){return value ? True : False;}
 
@@ -620,15 +620,15 @@ public:
         }
     }
 
-    int hash(const PyVar& obj){
+    _Int hash(const PyVar& obj){
         if (obj->isType(_tp_int)) return PyInt_AS_C(obj);
         if (obj->isType(_tp_bool)) return PyBool_AS_C(obj) ? 1 : 0;
         if (obj->isType(_tp_float)){
-            float val = PyFloat_AS_C(obj);
-            return (int)std::hash<float>()(val);
+            _Float val = PyFloat_AS_C(obj);
+            return (_Int)std::hash<_Float>()(val);
         }
         if (obj->isType(_tp_str)) return PyStr_AS_C(obj).hash();
-        if (obj->isType(_tp_type)) return (int64_t)obj.get();
+        if (obj->isType(_tp_type)) return (_Int)obj.get();
         typeError("unhashable type: " + obj->getTypeName());
         return 0;
     }