From dc42b5bc4d2ecfd2fbaac3467a30d9d33509a236 Mon Sep 17 00:00:00 2001 From: lyon1998 Date: Thu, 31 Mar 2022 20:32:33 +0800 Subject: [PATCH] solving unused stack args --- package/PikaStdLib/PikaStdData.py | 6 +-- port/linux/package/pikascript/PikaStdData.py | 6 +-- port/linux/test/parse-test.cpp | 39 ++++++++++++++++++++ port/linux/test/sysObj-test.cpp | 2 +- src/PikaObj.c | 1 - src/PikaVM.c | 20 +++++++++- src/dataArg.c | 8 ++-- src/dataArg.h | 2 + src/dataArgs.c | 2 +- 9 files changed, 71 insertions(+), 15 deletions(-) diff --git a/package/PikaStdLib/PikaStdData.py b/package/PikaStdLib/PikaStdData.py index 2139f0219..32b6f6f45 100644 --- a/package/PikaStdLib/PikaStdData.py +++ b/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[] diff --git a/port/linux/package/pikascript/PikaStdData.py b/port/linux/package/pikascript/PikaStdData.py index 2139f0219..32b6f6f45 100644 --- a/port/linux/package/pikascript/PikaStdData.py +++ b/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[] diff --git a/port/linux/test/parse-test.cpp b/port/linux/test/parse-test.cpp index a69e944a0..708c6871c 100644 --- a/port/linux/test/parse-test.cpp +++ b/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); +} diff --git a/port/linux/test/sysObj-test.cpp b/port/linux/test/sysObj-test.cpp index 6ed671573..51f39fc1e 100644 --- a/port/linux/test/sysObj-test.cpp +++ b/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); diff --git a/src/PikaObj.c b/src/PikaObj.c index 082cbfdd7..c20200eb9 100644 --- a/src/PikaObj.c +++ b/src/PikaObj.c @@ -355,7 +355,6 @@ Arg* newFreeObjArg(NewFun newObjFun) { } Arg* obj_newObjInPackage(NewFun newObjFun) { - // return arg_setMetaObj("", "", newObjFun); return newFreeObjArg(newObjFun); } diff --git a/src/PikaVM.c b/src/PikaVM.c index f4ade3e66..ca398e4ac 100644 --- a/src/PikaVM.c +++ b/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; } diff --git a/src/dataArg.c b/src/dataArg.c index cf7de49a2..c86792839 100644 --- a/src/dataArg.c +++ b/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); } diff --git a/src/dataArg.h b/src/dataArg.h index 1c8a60cac..01ff5c29b 100644 --- a/src/dataArg.h +++ b/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, diff --git a/src/dataArgs.c b/src/dataArgs.c index c6817f859..905bbac3d 100644 --- a/src/dataArgs.c +++ b/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); }