blueloveTH 1 год назад
Родитель
Сommit
68bc6ee269
4 измененных файлов с 27 добавлено и 34 удалено
  1. 12 8
      src/compiler/compiler.c
  2. 13 22
      src/interpreter/ceval.c
  3. 2 0
      src2/main.c
  4. 0 4
      tests/00_tmp.py

+ 12 - 8
src/compiler/compiler.c

@@ -1076,6 +1076,11 @@ typedef struct AttribExpr {
     py_Name name;
 } AttribExpr;
 
+void AttribExpr__dtor(Expr* self_) {
+    AttribExpr* self = (AttribExpr*)self_;
+    vtdelete(self->child);
+}
+
 void AttribExpr__emit_(Expr* self_, Ctx* ctx) {
     AttribExpr* self = (AttribExpr*)self_;
     vtemit_(self->child, ctx);
@@ -1117,6 +1122,7 @@ AttribExpr* AttribExpr__new(int line, Expr* child, py_Name name) {
                               .emit_store = AttribExpr__emit_store,
                               .emit_inplace = AttribExpr__emit_inplace,
                               .emit_istore = AttribExpr__emit_istore,
+                              .dtor = AttribExpr__dtor,
                               .is_attrib = true};
     static_assert_expr_size(AttribExpr);
     AttribExpr* self = PoolExpr_alloc();
@@ -1218,7 +1224,6 @@ void Ctx__dtor(Ctx* self) {
     for(int i = 0; i < self->s_expr.count; i++) {
         vtdelete(c11__getitem(Expr*, &self->s_expr, i));
     }
-    c11_vector__clear(&self->s_expr);
     c11_vector__dtor(&self->s_expr);
     c11_smallmap_n2i__dtor(&self->global_names);
     c11_smallmap_s2n__dtor(&self->co_consts_string_dedup_map);
@@ -1379,8 +1384,8 @@ void Ctx__emit_store_name(Ctx* self, NameScope scope, py_Name name, int line) {
 void Ctx__s_emit_top(Ctx* self) {
     Expr* top = c11_vector__back(Expr*, &self->s_expr);
     vtemit_(top, self);
-    c11_vector__pop(&self->s_expr);
     vtdelete(top);
+    c11_vector__pop(&self->s_expr);
 }
 
 // push
@@ -1394,15 +1399,16 @@ int Ctx__s_size(Ctx* self) { return self->s_expr.count; }
 
 // pop -> delete
 void Ctx__s_pop(Ctx* self) {
-    vtdelete(c11_vector__back(Expr*, &self->s_expr));
+    Expr* top = c11_vector__back(Expr*, &self->s_expr);
+    vtdelete(top);
     c11_vector__pop(&self->s_expr);
 }
 
 // pop move
 Expr* Ctx__s_popx(Ctx* self) {
-    Expr* e = c11_vector__back(Expr*, &self->s_expr);
+    Expr* top = c11_vector__back(Expr*, &self->s_expr);
     c11_vector__pop(&self->s_expr);
-    return e;
+    return top;
 }
 
 /* compiler.c */
@@ -2313,10 +2319,8 @@ Error* pk_compile(pk_SourceData_ src, CodeObject* out) {
     CodeObject__ctor(out, src, c11_string__sv(src->filename));
     err = Compiler__compile(&compiler, out);
     if(err) {
-        // if error occurs, dispose the code object
+        // dispose the code object if error occurs
         CodeObject__dtor(out);
-    } else {
-        assert(out->codes.count);
     }
     Compiler__dtor(&compiler);
     return err;

+ 13 - 22
src/interpreter/ceval.c

@@ -5,7 +5,6 @@
 #include "pocketpy/pocketpy.h"
 #include <stdbool.h>
 
-
 static bool stack_binaryop(pk_VM* self, py_Name op, py_Name rop);
 
 #define DISPATCH()                                                                                 \
@@ -45,7 +44,7 @@ static bool stack_binaryop(pk_VM* self, py_Name op, py_Name rop);
 
 #define vectorcall_opcall(argc, kwargc)                                                            \
     do {                                                                                           \
-        pk_FrameResult res = pk_VM__vectorcall(self, (argc), (kwargc), true);                                \
+        pk_FrameResult res = pk_VM__vectorcall(self, (argc), (kwargc), true);                      \
         switch(res) {                                                                              \
             case RES_RETURN: PUSH(&self->last_retval); break;                                      \
             case RES_CALL:                                                                         \
@@ -86,7 +85,7 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
 #if 1
         c11_sbuf buf;
         c11_sbuf__ctor(&buf);
-        for(py_Ref p = self->stack.begin; p != SP(); p++){
+        for(py_Ref p = self->stack.begin; p != SP(); p++) {
             c11_sbuf__write_cstr(&buf, py_tpname(p->type));
             if(p != TOP()) c11_sbuf__write_cstr(&buf, ", ");
         }
@@ -260,16 +259,14 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
             case OP_LOAD_METHOD: {
                 // [self]
                 bool ok = py_getunboundmethod(TOP(), byte.arg, TOP(), SP());
-                if(ok){
+                if(ok) {
                     // [unbound, self]
                     SP()++;
-                }else{
+                } else {
                     // fallback to getattr
                     int res = py_getattr(TOP(), byte.arg, TOP());
-                    if(res != 1){
-                        if(res == 0){
-                            AttributeError(TOP(), byte.arg);
-                        }
+                    if(res != 1) {
+                        if(res == 0) { AttributeError(TOP(), byte.arg); }
                         goto __ERROR;
                     }
                 }
@@ -436,7 +433,7 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
                 assert(f != NULL);
                 py_TValue tmp = *TOP();
                 *TOP() = *f;              // [complex]
-                py_newnil(SP()++);       // [complex, NULL]
+                py_newnil(SP()++);        // [complex, NULL]
                 py_newint(SP()++, 0);     // [complex, NULL, 0]
                 *SP()++ = tmp;            // [complex, NULL, 0, x]
                 vectorcall_opcall(2, 0);  // [complex(x)]
@@ -668,18 +665,12 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
                 py_newbool(TOP(), !res);
                 DISPATCH();
             }
-                // case OP_UNARY_STAR: TOP() = VAR(StarWrapper(byte.arg, TOP())); DISPATCH();
-                // case OP_UNARY_INVERT: {
-                //     PyVar _0;
-                //     auto _ti = _tp_info(TOP());
-                //     if(_ti->m__invert__)
-                //         _0 = _ti->m__invert__(this, TOP());
-                //     else
-                //         _0 = call_method(TOP(), __invert__);
-                //     TOP() = _0;
-                //     DISPATCH();
-                // }
-
+            // case OP_UNARY_STAR: TOP() = VAR(StarWrapper(byte.arg, TOP())); DISPATCH();
+            case OP_UNARY_INVERT: {
+                if(!py_callmagic(__invert__, 1, TOP())) goto __ERROR;
+                *TOP() = self->last_retval;
+                DISPATCH();
+            }
             default: PK_UNREACHABLE();
         }
 

+ 2 - 0
src2/main.c

@@ -59,6 +59,8 @@ int main(int argc, char** argv) {
     py_finalize();
     free(source);
 
+    return 0;
+    
 __HELP:
     printf("Usage: pocketpy [filename]\n");
     return 0;

+ 0 - 4
tests/00_tmp.py

@@ -98,7 +98,3 @@ assert (-4)**13 == -67108864
 assert ~3 == -4
 assert ~-3 == 2
 assert ~0 == -1
-
-# test __str__, __repr__
-assert str(1) == '1'
-assert repr(1) == '1'