Ver Fonte

Merge branch 'main' into sandbox

blueloveTH há 1 semana atrás
pai
commit
ce62204443

+ 1 - 1
README.md

@@ -251,7 +251,7 @@ Website: [https://fdtd.io](https://fdtd.io)
 
 ## Star History
 
-[![Star History Chart](https://api.star-history.com/svg?repos=pocketpy/pocketpy&type=Date)](https://www.star-history.com/#pocketpy/pocketpy&Date)
+[![Star History Chart](https://star-history.dera.page/svg?repos=pocketpy/pocketpy&type=Date)](https://star-history.dera.page/#pocketpy/pocketpy&Date)
 
 
 ## License

+ 1 - 0
include/pocketpy/compiler/lexer.h

@@ -168,6 +168,7 @@ enum Precedence {
 };
 
 Error* Lexer__process(SourceData_ src, Token** out_tokens, int* out_length);
+void destruct_tokens(Token* tokens, int length);
 
 #define Token__sv(self)                                                                            \
     (c11_sv) { (self)->start, (self)->length }

+ 1 - 1
include/pocketpy/objects/codeobject.h

@@ -129,7 +129,7 @@ void FuncDecl__add_kwarg(FuncDecl* self, py_Name name, const py_TValue* value);
 void FuncDecl__add_starred_arg(FuncDecl* self, py_Name name);
 void FuncDecl__add_starred_kwarg(FuncDecl* self, py_Name name);
 void FuncDecl__gc_mark(const FuncDecl* self, c11_vector* p_stack);
-void FuncDecl__dtor(FuncDecl* self);
+void FuncDecl__dtor(void* p);
 
 // runtime function
 typedef struct Function {

+ 5 - 0
include/typings/vmath.pyi

@@ -141,6 +141,11 @@ class vec2i(_vecI['vec2i']):
     def with_x(self, x: int) -> vec2i: ...
     def with_y(self, y: int) -> vec2i: ...
 
+    def l1_norm(self) -> int: ...
+    def l2_norm(self) -> float: ...
+    def max_norm(self) -> int: ...
+    def length(self) -> float: ...
+
     def __init__(self, x: int, y: int) -> None: ...
 
 

+ 1 - 0
python/typing.py

@@ -40,6 +40,7 @@ Iterator = _PLACEHOLDER
 Hashable = _PLACEHOLDER
 
 TypeVar = _PLACEHOLDER
+ParamSpec = _PLACEHOLDER
 Self = _PLACEHOLDER
 
 Protocol = object

Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
src/common/_generated.c


+ 3 - 2
src/common/sourcedata.c

@@ -43,7 +43,8 @@ static void SourceData__ctor(struct SourceData* self,
     c11_vector__push(const char*, &self->line_starts, self->source->data);
 }
 
-static void SourceData__dtor(struct SourceData* self) {
+static void SourceData__dtor(void* p) {
+    struct SourceData* self = p;
     c11_string__delete(self->filename);
     if(self->source) c11_string__delete(self->source);
     c11_vector__dtor(&self->line_starts);
@@ -56,7 +57,7 @@ SourceData_ SourceData__rcnew(const char* source,
     SourceData_ self = PK_MALLOC(sizeof(struct SourceData));
     SourceData__ctor(self, source, filename, mode, is_dynamic);
     self->rc.count = 1;
-    self->rc.dtor = (void (*)(void*))SourceData__dtor;
+    self->rc.dtor = SourceData__dtor;
     return self;
 }
 

+ 13 - 8
src/compiler/compiler.c

@@ -1399,12 +1399,7 @@ static void Compiler__ctor(Compiler* self, SourceData_ src, Token* tokens, int t
 
 static void Compiler__dtor(Compiler* self) {
     // free tokens
-    for(int i = 0; i < self->tokens_length; i++) {
-        if(self->tokens[i].value.index == TokenValue_STR) {
-            // PK_FREE internal string
-            c11_string__delete(self->tokens[i].value._str);
-        }
-    }
+    destruct_tokens(self->tokens, self->tokens_length);
     PK_FREE(self->tokens);
     // free contexts
     c11__foreach(Ctx, &self->contexts, ctx) Ctx__dtor(ctx);
@@ -2417,8 +2412,14 @@ static Error* consume_pep695_py312(Compiler* self) {
     Error* err;
     if(match(TK_LBRACKET)) {
         do {
-            consume(TK_ID);
-            if(match(TK_COLON)) check(consume_type_hints(self));
+            if(match(TK_POW)) {
+                // **P
+                consume(TK_ID);
+            } else {
+                // T: int
+                consume(TK_ID);
+                if(match(TK_COLON)) check(consume_type_hints(self));
+            }
         } while(match(TK_COMMA));
         consume(TK_RBRACKET);
     }
@@ -2491,6 +2492,10 @@ static Error* compile_class(Compiler* self, int decorators) {
             check(EXPR(self));
             has_base = true;  // [base]
         }
+        while(match(TK_COMMA)) {
+            // ignore extra bases
+            check(consume_type_hints(self));
+        }
         consume(TK_RPAREN);
     }
     if(!has_base) {

+ 11 - 0
src/compiler/lexer.c

@@ -628,6 +628,7 @@ Error* Lexer__process(SourceData_ src, Token** out_tokens, int* out_length) {
     while(!eof) {
         void* err = lex_one_token(&lexer, &eof, false);
         if(err) {
+            destruct_tokens(lexer.nexts.data, lexer.nexts.length);
             Lexer__dtor(&lexer);
             return err;
         }
@@ -639,6 +640,16 @@ Error* Lexer__process(SourceData_ src, Token** out_tokens, int* out_length) {
     return NULL;
 }
 
+void destruct_tokens(Token* tokens, int length) {
+    // free tokens
+    for(int i = 0; i < length; i++) {
+        if(tokens[i].value.index == TokenValue_STR) {
+            // PK_FREE internal string
+            c11_string__delete(tokens[i].value._str);
+        }
+    }
+}
+
 const char* TokenSymbols[] = {
     "@eof",
     "@eol",

+ 2 - 1
src/modules/pickle.c

@@ -409,13 +409,14 @@ static bool pkl__write_object(PickleObject* buf, py_TValue* obj) {
                 if(!py_call(f_reduce, 1, obj)) return false;
                 // expected: (callable, args)
                 py_Ref reduced = py_retval();
-                if(!py_istuple(reduced)) { return TypeError("__reduce__ must return a tuple"); }
+                if(!py_istuple(reduced)) return TypeError("__reduce__ must return a tuple");
                 if(py_tuple_len(reduced) != 2) {
                     return TypeError("__reduce__ must return a tuple of length 2");
                 }
                 if(!pkl__write_object(buf, py_tuple_getitem(reduced, 0))) return false;
                 pkl__emit_op(buf, PKL_NIL);
                 py_Ref args_tuple = py_tuple_getitem(reduced, 1);
+                if(!py_istuple(args_tuple)) return TypeError("__reduce__ args must be a tuple");
                 int args_length = py_tuple_len(args_tuple);
                 for(int i = 0; i < args_length; i++) {
                     if(!pkl__write_object(buf, py_tuple_getitem(args_tuple, i))) return false;

+ 1 - 15
src/modules/time.c

@@ -10,7 +10,6 @@
 #endif
 
 #include "pocketpy/pocketpy.h"
-#include "pocketpy/common/threads.h"
 #include <assert.h>
 
 #define NANOS_PER_SEC 1000000000
@@ -18,20 +17,8 @@
 #ifndef __circle__
 
 int64_t time_ns() {
-#ifdef _WIN32
-    FILETIME system_time;
-    ULARGE_INTEGER large;
-
-    GetSystemTimePreciseAsFileTime(&system_time);
-    large.u.LowPart = system_time.dwLowDateTime;
-    large.u.HighPart = system_time.dwHighDateTime;
-    /* 11,644,473,600,000,000,000: number of nanoseconds between
-       the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
-       days). */
-    return (large.QuadPart - 116444736000000000) * 100;
-#else
     struct timespec tms;
-#ifdef CLOCK_REALTIME
+#if !defined(_WIN32) && defined(CLOCK_REALTIME)
     clock_gettime(CLOCK_REALTIME, &tms);
 #else
     /* The C11 way */
@@ -42,7 +29,6 @@ int64_t time_ns() {
     /* Add full nanoseconds */
     nanos += tms.tv_nsec;
     return nanos;
-#endif
 }
 
 int64_t time_monotonic_ns() {

+ 29 - 0
src/modules/vmath.c

@@ -336,6 +336,31 @@ DEF_VECTOR_INT_OPS(2)
 DEF_VECTOR_INT_OPS(3)
 DEF_VECTOR_INT_OPS(4)
 
+// vec2i l1_norm, l2_norm, max_norm
+static bool vec2i_l1_norm(int argc, py_Ref argv) {
+    PY_CHECK_ARGC(1);
+    c11_vec2i v = py_tovec2i(argv);
+    int norm = abs(v.x) + abs(v.y);
+    py_newint(py_retval(), norm);
+    return true;
+}
+
+static bool vec2i_l2_norm(int argc, py_Ref argv) {
+    PY_CHECK_ARGC(1);
+    c11_vec2i v = py_tovec2i(argv);
+    double norm = dmath_sqrt(v.x * v.x + v.y * v.y);
+    py_newfloat(py_retval(), norm);
+    return true;
+}
+
+static bool vec2i_max_norm(int argc, py_Ref argv) {
+    PY_CHECK_ARGC(1);
+    c11_vec2i v = py_tovec2i(argv);
+    int norm = c11__max(abs(v.x), abs(v.y));
+    py_newint(py_retval(), norm);
+    return true;
+}
+
 static bool vec2i__hash__(int argc, py_Ref argv) {
     PY_CHECK_ARGC(1);
     c11_vec2i v = py_tovec2i(argv);
@@ -1260,6 +1285,10 @@ void pk__add_module_vmath() {
     py_bindmethod(vec2i, "with_x", vec2i__with_x);
     py_bindmethod(vec2i, "with_y", vec2i__with_y);
     py_bindmethod(vec2i, "dot", vec2i_dot);
+    py_bindmethod(vec2i, "l1_norm", vec2i_l1_norm);
+    py_bindmethod(vec2i, "l2_norm", vec2i_l2_norm);
+    py_bindmethod(vec2i, "max_norm", vec2i_max_norm);
+    py_bindmethod(vec2i, "length", vec2i_l2_norm);
 
     // clang-format off
     py_newvec2i(_const(vec2i, "ZERO"), (c11_vec2i){{0, 0}});

+ 3 - 2
src/objects/codeobject.c

@@ -17,7 +17,8 @@ bool Bytecode__is_forward_jump(const Bytecode* self) {
            (op == OP_FOR_ITER || op == OP_FOR_ITER_YIELD_VALUE);
 }
 
-void FuncDecl__dtor(FuncDecl* self) {
+void FuncDecl__dtor(void* p) {
+    FuncDecl* self = p;
     CodeObject__dtor(&self->code);
     c11_vector__dtor(&self->args);
     c11_vector__dtor(&self->kwargs);
@@ -28,7 +29,7 @@ void FuncDecl__dtor(FuncDecl* self) {
 FuncDecl_ FuncDecl__rcnew(SourceData_ src, c11_sv name) {
     FuncDecl* self = PK_MALLOC(sizeof(FuncDecl));
     self->rc.count = 1;
-    self->rc.dtor = (void (*)(void*))FuncDecl__dtor;
+    self->rc.dtor = FuncDecl__dtor;
     CodeObject__ctor(&self->code, src, name);
 
     c11_vector__ctor(&self->args, sizeof(int32_t));

+ 1 - 1
src/objects/codeobject_ser.c

@@ -323,7 +323,7 @@ static void FuncDecl__serialize(c11_serializer* s,
 static FuncDecl_ FuncDecl__deserialize(c11_deserializer* d, SourceData_ embedded_src) {
     FuncDecl_ self = PK_MALLOC(sizeof(FuncDecl));
     self->rc.count = 1;
-    self->rc.dtor = (void (*)(void*))FuncDecl__dtor;
+    self->rc.dtor = FuncDecl__dtor;
 
     c11_vector__ctor(&self->args, sizeof(int32_t));
     c11_vector__ctor(&self->kwargs, sizeof(FuncDeclKwArg));

+ 1 - 1
src/public/CodeExecution.c

@@ -22,7 +22,7 @@ static bool _py_compile(CodeObject* out,
     SourceData_ src = SourceData__rcnew(source, filename, mode, is_dynamic);
     Error* err = pk_compile(src, out);
     if(err) {
-        py_exception(tp_SyntaxError, err->msg);
+        py_exception(tp_SyntaxError, "%s", err->msg);
         py_BaseException__stpush(NULL, &vm->unhandled_exc, err->src, err->lineno, NULL);
         PK_DECREF(src);
 

+ 6 - 0
tests/803_vmath.py

@@ -425,3 +425,9 @@ for i in range(-1000, 10000):
     e[vec2i(i, 12)] = i
     e[vec2i(i, 11)] = i
     e[vec2i(i, 13)] = i
+
+# test vec2i
+v = vec2i(-3, 4)
+assert v.l1_norm() == 7
+assert v.l2_norm() == 5.0
+assert v.max_norm() == 4

Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff