Przeglądaj źródła

solving unused stack args

lyon1998 4 lat temu
rodzic
commit
dc42b5bc4d

+ 3 - 3
package/PikaStdLib/PikaStdData.py

@@ -29,7 +29,7 @@ class List(TinyObj):
         pass
 
     # support list[]  = val
-    def __set__():
+    def __set__(obj: any, key: any, val: any, obj_str: str):
         pass
 
     # support val = list[] 
@@ -59,7 +59,7 @@ class Dict(TinyObj):
         pass
 
     # support dict[]  = val
-    def __set__():
+    def __set__(obj: any, key: any, val: any, obj_str: str):
         pass
 
     # support val = dict[] 
@@ -81,7 +81,7 @@ class String(TinyObj):
         pass
 
     # support string[]  = val
-    def __set__():
+    def __set__(obj: any, key: any, val: any, obj_str: str):
         pass
 
     # support val = string[] 

+ 3 - 3
port/linux/package/pikascript/PikaStdData.py

@@ -29,7 +29,7 @@ class List(TinyObj):
         pass
 
     # support list[]  = val
-    def __set__():
+    def __set__(obj: any, key: any, val: any, obj_str: str):
         pass
 
     # support val = list[] 
@@ -59,7 +59,7 @@ class Dict(TinyObj):
         pass
 
     # support dict[]  = val
-    def __set__():
+    def __set__(obj: any, key: any, val: any, obj_str: str):
         pass
 
     # support val = dict[] 
@@ -81,7 +81,7 @@ class String(TinyObj):
         pass
 
     # support string[]  = val
-    def __set__():
+    def __set__(obj: any, key: any, val: any, obj_str: str):
         pass
 
     # support val = string[] 

+ 39 - 0
port/linux/test/parse-test.cpp

@@ -2675,3 +2675,42 @@ TEST(parser, plus_equ_) {
     args_deinit(buffs);
     EXPECT_EQ(pikaMemNow(), 0);
 }
+
+TEST(parser, class_demo_3) {
+    pikaMemInfo.heapUsedMax = 0;
+    Args* buffs = New_strBuff();
+    char* lines = (char*)
+        "class people:\n"
+        "    def speak(self):\n"
+        "        print('i am a people')\n"
+        " \n"
+        "class student(people):\n"
+        "    def speak(self):\n"
+        "        print('i am a student')\n"
+        " \n"
+        "p = people()\n"
+        "s = student()\n"
+        "p.speak()\n"
+        "s.speak()\n"
+        ;
+    printf("%s", lines);
+    char* pikaAsm = Parser_multiLineToAsm(buffs, (char*)lines);
+    printf("%s", pikaAsm);
+    args_deinit(buffs);
+    EXPECT_EQ(pikaMemNow(), 0);
+}
+
+TEST(parser, a_a) {
+    pikaMemInfo.heapUsedMax = 0;
+    Args* buffs = New_strBuff();
+    char* lines = (char*)
+        "a = 1\n"
+        "a\n"
+        "a\n"
+        ;
+    printf("%s", lines);
+    char* pikaAsm = Parser_multiLineToAsm(buffs, (char*)lines);
+    printf("%s", pikaAsm);
+    args_deinit(buffs);
+    EXPECT_EQ(pikaMemNow(), 0);
+}

+ 1 - 1
port/linux/test/sysObj-test.cpp

@@ -31,7 +31,7 @@ TEST(sysObj, noMethod) {
     // printf("sysout = %s\r\n", sysOut);
     // ASSERT_EQ(1, strEqu((char*)"[error] runner: method no found.", sysOut));
     EXPECT_STREQ(log_buff[4],
-                 "[error] runner: method 'printttt' no found.\r\n");
+                 "[error] name 'printttt' is not defined\r\n");
     // ASSERT_EQ(2, errCode);
     // obj_deinit(globals);
     obj_deinit(obj);

+ 0 - 1
src/PikaObj.c

@@ -355,7 +355,6 @@ Arg* newFreeObjArg(NewFun newObjFun) {
 }
 
 Arg* obj_newObjInPackage(NewFun newObjFun) {
-    // return arg_setMetaObj("", "", newObjFun);
     return newFreeObjArg(newObjFun);
 }
 

+ 18 - 2
src/PikaVM.c

@@ -185,7 +185,7 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, VMState* vs, char* data) {
     PikaObj* method_host_obj;
     Arg* method_arg;
     Method method_ptr;
-    ArgType method_type = ARG_TYPE_NONE;
+    ArgType method_type = ARG_TYPE_NULL;
     char* method_dec;
     char* type_list;
     char* sys_out;
@@ -218,7 +218,8 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, VMState* vs, char* data) {
     if (NULL == method_arg) {
         /* error, method no found */
         VMState_setErrorCode(vs, 2);
-        __platform_printf("[error] runner: method '%s' no found.\r\n", data);
+        __platform_printf("[error] name '%s' is not defined\r\n", data);
+
         goto RUN_exit;
     }
     /* get method Ptr */
@@ -1118,6 +1119,19 @@ void byteCodeFrame_print(ByteCodeFrame* self) {
                       self->const_pool.size + self->instruct_array.size);
 }
 
+void VMState_solveUnusedStack(VMState* vs) {
+    uint8_t top = stack_getTop(&(vs->stack));
+    for (int i = 0; i < top; i++) {
+        Arg* arg = stack_popArg(&(vs->stack));
+        ArgType type = arg_getType(arg);
+        if (type == ARG_TYPE_VOID) {
+            arg_deinit(arg);
+            continue;
+        }
+        arg_deinit(arg);
+    }
+}
+
 VMParameters* pikaVM_runByteCodeWithState(PikaObj* self,
                                           VMParameters* locals,
                                           VMParameters* globals,
@@ -1139,6 +1153,7 @@ VMParameters* pikaVM_runByteCodeWithState(PikaObj* self,
         }
         InstructUnit* this_ins_unit = VMState_getInstructNow(&vs);
         if (instructUnit_getIsNewLine(this_ins_unit)) {
+            VMState_solveUnusedStack(&vs);
             stack_reset(&(vs.stack));
         }
         vs.pc = pikaVM_runInstructUnit(self, &vs, this_ins_unit);
@@ -1169,6 +1184,7 @@ VMParameters* pikaVM_runByteCodeWithState(PikaObj* self,
             vs.error_code = 0;
         }
     }
+    VMState_solveUnusedStack(&vs);
     stack_deinit(&(vs.stack));
     return locals;
 }

+ 4 - 4
src/dataArg.c

@@ -95,7 +95,7 @@ uint8_t* content_deinit(uint8_t* self) {
 uint8_t* content_setContent(uint8_t* self, uint8_t* content, uint16_t size) {
     if (NULL == self) {
         /* malloc */
-        return content_init("", ARG_TYPE_NONE, content, size, NULL);
+        return content_init("", ARG_TYPE_VOID, content, size, NULL);
     }
 
     /* only copy */
@@ -116,7 +116,7 @@ uint8_t* content_setContent(uint8_t* self, uint8_t* content, uint16_t size) {
 
 uint8_t* content_setNameHash(uint8_t* self, Hash nameHash) {
     if (NULL == self) {
-        return content_init_hash(nameHash, ARG_TYPE_NONE, NULL, 0, NULL);
+        return content_init_hash(nameHash, ARG_TYPE_VOID, NULL, 0, NULL);
     }
     __arg* arg = (__arg*)self;
     arg->name_hash = nameHash;
@@ -144,7 +144,7 @@ ArgType content_getType(uint8_t* self) {
 }
 
 Arg* arg_newContent(Arg* self, uint32_t size) {
-    uint8_t* newContent = content_init("", ARG_TYPE_NONE, NULL, size, NULL);
+    uint8_t* newContent = content_init("", ARG_TYPE_VOID, NULL, size, NULL);
     arg_freeContent(self);
     return newContent;
 }
@@ -257,7 +257,7 @@ Hash arg_getNameHash(Arg* self) {
 
 ArgType arg_getType(Arg* self) {
     if (NULL == self) {
-        return ARG_TYPE_NONE;
+        return ARG_TYPE_NULL;
     }
     return content_getType(self);
 }

+ 2 - 0
src/dataArg.h

@@ -32,7 +32,9 @@
 
 typedef uint32_t Hash;
 typedef enum {
+    ARG_TYPE_UNDEF = 0,
     ARG_TYPE_NONE,
+    ARG_TYPE_VOID,
     ARG_TYPE_NULL,
     ARG_TYPE_INT,
     ARG_TYPE_FLOAT,

+ 1 - 1
src/dataArgs.c

@@ -130,7 +130,7 @@ ArgType args_getType(Args* self, char* name) {
     Arg* arg = NULL;
     arg = args_getArg(self, name);
     if (NULL == arg) {
-        return ARG_TYPE_NONE;
+        return ARG_TYPE_NULL;
     }
     return arg_getType(arg);
 }