blueloveTH hai 1 ano
pai
achega
18fe69d579

+ 1 - 0
include/pocketpy/interpreter/vm.h

@@ -96,6 +96,7 @@ py_Type pk_str__register();
 py_Type pk_bytes__register();
 py_Type pk_bytes__register();
 py_Type pk_list__register();
 py_Type pk_list__register();
 py_Type pk_function__register();
 py_Type pk_function__register();
+py_Type pk_nativefunc__register();
 
 
 py_TValue pk_builtins__register();
 py_TValue pk_builtins__register();
 
 

+ 12 - 0
src/interpreter/ceval.c

@@ -693,6 +693,18 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
                 *TOP() = self->last_retval;
                 *TOP() = self->last_retval;
                 DISPATCH();
                 DISPATCH();
             }
             }
+
+            ///////////
+            case OP_RAISE_ASSERT: {
+                if(byte.arg) {
+                    if(!py_str(TOP())) goto __ERROR;
+                    POP();
+                    py_exception("AssertionError", "%s", py_tostr(py_retval()));
+                } else {
+                    py_exception("AssertionError", "");
+                }
+                goto __ERROR;
+            }
             default: c11__unreachedable();
             default: c11__unreachedable();
         }
         }
 
 

+ 1 - 1
src/interpreter/vm.c

@@ -105,7 +105,7 @@ void pk_VM__ctor(pk_VM* self) {
     validate(tp_module, pk_VM__new_type(self, "module", tp_object, NULL, false));
     validate(tp_module, pk_VM__new_type(self, "module", tp_object, NULL, false));
 
 
     validate(tp_function, pk_function__register());
     validate(tp_function, pk_function__register());
-    validate(tp_nativefunc, pk_VM__new_type(self, "nativefunc", tp_object, NULL, false));
+    validate(tp_nativefunc, pk_nativefunc__register());
     validate(tp_bound_method, pk_VM__new_type(self, "bound_method", tp_object, NULL, false));
     validate(tp_bound_method, pk_VM__new_type(self, "bound_method", tp_object, NULL, false));
 
 
     validate(tp_super, pk_VM__new_type(self, "super", tp_object, NULL, false));
     validate(tp_super, pk_VM__new_type(self, "super", tp_object, NULL, false));

+ 13 - 0
src/public/modules.c

@@ -83,4 +83,17 @@ py_Type pk_function__register() {
     pk_TypeInfo* ti = c11__at(pk_TypeInfo, &vm->types, type);
     pk_TypeInfo* ti = c11__at(pk_TypeInfo, &vm->types, type);
     ti->dtor = (void (*)(void*))Function__dtor;
     ti->dtor = (void (*)(void*))Function__dtor;
     return type;
     return type;
+}
+
+static bool _py_nativefunc__repr(int argc, py_Ref argv) {
+    PY_CHECK_ARGC(1);
+    py_newstr(py_retval(), "<nativefunc object>");
+    return true;
+}
+
+py_Type pk_nativefunc__register() {
+    pk_VM* vm = pk_current_vm;
+    py_Type type = pk_VM__new_type(vm, "nativefunc", tp_object, NULL, false);
+    py_bindmagic(type, __repr__, _py_nativefunc__repr);
+    return type;
 }
 }

+ 19 - 8
src/public/py_number.c

@@ -1,4 +1,5 @@
 #include "pocketpy/interpreter/vm.h"
 #include "pocketpy/interpreter/vm.h"
+#include "pocketpy/common/sstream.h"
 #include "pocketpy/pocketpy.h"
 #include "pocketpy/pocketpy.h"
 
 
 #include <math.h>
 #include <math.h>
@@ -197,9 +198,12 @@ static bool _py_int__repr__(int argc, py_Ref argv) {
 static bool _py_float__repr__(int argc, py_Ref argv) {
 static bool _py_float__repr__(int argc, py_Ref argv) {
     PY_CHECK_ARGC(1);
     PY_CHECK_ARGC(1);
     py_f64 val = py_tofloat(&argv[0]);
     py_f64 val = py_tofloat(&argv[0]);
-    char buf[32];
-    int size = snprintf(buf, sizeof(buf), "%f", val);
-    py_newstrn(py_retval(), buf, size);
+    c11_sbuf buf;
+    c11_sbuf__ctor(&buf);
+    c11_sbuf__write_f64(&buf, val, -1);
+    c11_string* res = c11_sbuf__submit(&buf);
+    py_newstrn(py_retval(), res->data, res->size);
+    c11_string__delete(res);
     return true;
     return true;
 }
 }
 
 
@@ -341,11 +345,18 @@ static bool _py_float__new__(int argc, py_Ref argv) {
 
 
 // tp_bool
 // tp_bool
 static bool _py_bool__new__(int argc, py_Ref argv) {
 static bool _py_bool__new__(int argc, py_Ref argv) {
-    PY_CHECK_ARGC(1);
-    int res = py_bool(argv);
-    if(res == -1) return false;
-    py_newbool(py_retval(), res);
-    return true;
+    assert(argc > 0);
+    if(argc == 1){
+        py_newbool(py_retval(), false);
+        return true;
+    }
+    if(argc == 2){
+        int res = py_bool(py_arg(1));
+        if(res == -1) return false;
+        py_newbool(py_retval(), res);
+        return true;
+    }
+    return TypeError("bool() takes at most 1 argument");
 }
 }
 
 
 static bool _py_bool__hash__(int argc, py_Ref argv) {
 static bool _py_bool__hash__(int argc, py_Ref argv) {

+ 11 - 12
tests/02_float.py

@@ -53,14 +53,11 @@ assert eq(float("123"), 123.0)
 assert eq(float("123.456"), 123.456)
 assert eq(float("123.456"), 123.456)
 
 
 
 
-import math
-
 inf = float("inf")
 inf = float("inf")
 assert 1/0 == inf
 assert 1/0 == inf
 assert -1/0 == -inf
 assert -1/0 == -inf
 assert 1/inf == 0
 assert 1/inf == 0
 assert -1/inf == 0
 assert -1/inf == 0
-assert math.isnan(0/0)
 
 
 assert 2**-6000 == 0.0
 assert 2**-6000 == 0.0
 assert 2.0 ** 6000 == inf
 assert 2.0 ** 6000 == inf
@@ -74,7 +71,6 @@ assert eq(2 * .5, 1.0)
 assert eq(2 * (.5), 1.0)
 assert eq(2 * (.5), 1.0)
 assert eq(2 * (.5 + 1), 3.0)
 assert eq(2 * (.5 + 1), 3.0)
 
 
-
 assert 1e3 == 1000.0
 assert 1e3 == 1000.0
 assert 1e-3 == 0.001
 assert 1e-3 == 0.001
 assert -1e3 == -1000.0
 assert -1e3 == -1000.0
@@ -83,15 +79,18 @@ assert 1e0 == 1.0
 assert 1e-0 == 1.0
 assert 1e-0 == 1.0
 
 
 assert 2e3 == 2000.0
 assert 2e3 == 2000.0
-assert 2e3j == 2000j
 assert -2e-3 == -0.002
 assert -2e-3 == -0.002
-assert -2e-3j == -0.002j
-
 assert 3.4e-3 == 0.0034
 assert 3.4e-3 == 0.0034
 assert 3.4e+3 == 3400.0
 assert 3.4e+3 == 3400.0
 
 
-try:
-    float('-x13')
-    exit(1)
-except ValueError:
-    pass
+# import math
+# assert math.isnan(0/0)
+
+# assert 2e3j == 2000j
+# assert -2e-3j == -0.002j
+
+# try:
+#     float('-x13')
+#     exit(1)
+# except ValueError:
+#     pass

+ 9 - 1
tests/03_bool.py

@@ -12,6 +12,7 @@ assert True or False
 assert not False
 assert not False
 assert not (not True)
 assert not (not True)
 
 
+assert bool() == False
 assert bool(0) == False
 assert bool(0) == False
 assert bool(1) == True
 assert bool(1) == True
 assert bool([]) == False
 assert bool([]) == False
@@ -19,7 +20,14 @@ assert bool("abc") == True
 assert bool([1,2]) == True
 assert bool([1,2]) == True
 assert bool('') == False
 assert bool('') == False
 
 
-# extra compare for None
+# is operator
 assert None == None
 assert None == None
+assert None is None
 assert ... == ...
 assert ... == ...
+assert ... is ...
 assert NotImplemented == NotImplemented
 assert NotImplemented == NotImplemented
+assert NotImplemented is NotImplemented
+
+assert True is True
+assert False is False
+