blueloveTH 2 years ago
parent
commit
14433b1210
3 changed files with 30 additions and 23 deletions
  1. 23 18
      src/ceval.h
  2. 2 1
      src/compiler.h
  3. 5 4
      src/opcodes.h

+ 23 - 18
src/ceval.h

@@ -319,6 +319,7 @@ __NEXT_STEP:;
             if(item == nullptr) ValueError("not enough values to unpack");
             frame->push(item);
         }
+        // handle extra items
         if(byte.op == OP_UNPACK_EX){
             List extras;
             while(true){
@@ -332,25 +333,29 @@ __NEXT_STEP:;
         }
     }; DISPATCH();
     /*****************************************/
+    case OP_BEGIN_CLASS: {
+        StrName name = frame->co->names[byte.arg];
+        PyObject* super_cls = frame->popx();
+        if(super_cls == None) super_cls = _t(tp_object);
+        check_type(super_cls, tp_type);
+        PyObject* cls = new_type_object(frame->_module, name, OBJ_GET(Type, super_cls));
+        frame->push(cls);
+    } DISPATCH();
+    case OP_END_CLASS: {
+        PyObject* cls = frame->popx();
+        cls->attr()._try_perfect_rehash();
+    }; DISPATCH();
+    case OP_STORE_CLASS_ATTR: {
+        StrName name = frame->co->names[byte.arg];
+        PyObject* obj = frame->popx();
+        PyObject* cls = frame->top();
+        cls->attr().set(name, obj);
+    } DISPATCH();
+    /*****************************************/
+    /*****************************************/
+    /*****************************************/
     // case OP_SETUP_DECORATOR: DISPATCH();
-    // case OP_BEGIN_CLASS: {
-    //     StrName name = frame->co->names[byte.arg];
-    //     PyObject* clsBase = frame->popx();
-    //     if(clsBase == None) clsBase = _t(tp_object);
-    //     check_type(clsBase, tp_type);
-    //     PyObject* cls = new_type_object(frame->_module, name, OBJ_GET(Type, clsBase));
-    //     frame->push(cls);
-    // } DISPATCH();
-    // case OP_END_CLASS: {
-    //     PyObject* cls = frame->popx();
-    //     cls->attr()._try_perfect_rehash();
-    // }; DISPATCH();
-    // case OP_STORE_CLASS_ATTR: {
-    //     StrName name = frame->co->names[byte.arg];
-    //     PyObject* obj = frame->popx();
-    //     PyObject* cls = frame->top();
-    //     cls->attr().set(name, obj);
-    // } DISPATCH();
+
     // case OP_ASSERT: {
     //     PyObject* _msg = frame->pop_value(this);
     //     Str msg = CAST(Str, asStr(_msg));

+ 2 - 1
src/compiler.h

@@ -835,7 +835,8 @@ class Compiler {
                 ctx()->emit(OP_STORE_ATTR, index, prev().line);
             }
         }else{
-            ctx()->emit(OP_STORE_CLASS_ATTR, ctx()->add_name(decl->name), BC_KEEPLINE);
+            int index = ctx()->add_name(decl->name);
+            ctx()->emit(OP_STORE_CLASS_ATTR, index, prev().line);
         }
     }
 

+ 5 - 4
src/opcodes.h

@@ -18,10 +18,6 @@ OPCODE(YIELD_VALUE)
 
 OPCODE(SETUP_DECORATOR)
 
-OPCODE(BEGIN_CLASS)
-OPCODE(END_CLASS)
-OPCODE(STORE_CLASS_ATTR)
-
 /**************************/
 OPCODE(NO_OP)
 /**************************/
@@ -96,4 +92,9 @@ OPCODE(IMPORT_STAR)
 OPCODE(UNPACK_SEQUENCE)
 OPCODE(UNPACK_EX)
 /**************************/
+// TODO: examine this
+OPCODE(BEGIN_CLASS)
+OPCODE(END_CLASS)
+OPCODE(STORE_CLASS_ATTR)
+/**************************/
 #endif