From ab795de30c3198310d462c8bd614ff1ad6b3579f Mon Sep 17 00:00:00 2001 From: pikastech Date: Thu, 23 Jun 2022 15:37:04 +0800 Subject: [PATCH] pikaVM_runFile change to interpreter mode --- port/linux/boot/demo06-pikamain/main.c | 15 ++++++++++-- port/linux/make.sh | 1 + port/linux/package/pikascript/test_module1.py | 3 +++ port/linux/test/cJSON-test.cpp | 14 +++++------ port/linux/test/chinese-test.cpp | 2 +- port/linux/test/event-test.cpp | 2 +- port/linux/test/pikaMain-test.cpp | 2 +- port/linux/test/string-test.cpp | 2 +- src/PikaCompiler.c | 18 +++++++++++---- src/PikaCompiler.h | 1 + src/PikaObj.c | 7 ++++++ src/PikaObj.h | 1 + src/PikaVM.c | 23 ++++++++++++++++--- src/PikaVM.h | 3 ++- 14 files changed, 73 insertions(+), 21 deletions(-) diff --git a/port/linux/boot/demo06-pikamain/main.c b/port/linux/boot/demo06-pikamain/main.c index 2a0f21133..8a209e333 100644 --- a/port/linux/boot/demo06-pikamain/main.c +++ b/port/linux/boot/demo06-pikamain/main.c @@ -1,7 +1,18 @@ /* this demo shows the usage of method */ #include +#include "PikaMain.h" +#include "PikaVM.h" #include "pikaScript.h" -int main() { - pikaScriptShell(pikaScriptInit()); +int main(int argc, char* argv[]) { + if (1 == argc) { + pikaScriptShell(pikaScriptInit()); + return 0; + } + if (2 == argc) { + PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain); + pikaVM_runFile(pikaMain, argv[1]); + obj_deinit(pikaMain); + return 0; + } } diff --git a/port/linux/make.sh b/port/linux/make.sh index eeb00e86a..5fef7e807 100644 --- a/port/linux/make.sh +++ b/port/linux/make.sh @@ -1,2 +1,3 @@ sh api-make.sh sh only_make.sh +cp ./build/boot/demo06-pikamain/pikascript_demo06-pikamain package/pikascript/pika diff --git a/port/linux/package/pikascript/test_module1.py b/port/linux/package/pikascript/test_module1.py index 06c96693e..c8c6be9db 100644 --- a/port/linux/package/pikascript/test_module1.py +++ b/port/linux/package/pikascript/test_module1.py @@ -21,3 +21,6 @@ def while_loop(): while i < 4: i += 1 print(i) + +def newfun(): + print('test in new fun') diff --git a/port/linux/test/cJSON-test.cpp b/port/linux/test/cJSON-test.cpp index 738ef7f28..08b3ca7d7 100644 --- a/port/linux/test/cJSON-test.cpp +++ b/port/linux/test/cJSON-test.cpp @@ -229,7 +229,7 @@ TEST(cJSON, test1) { PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain); /* run */ __platform_printf("BEGIN\r\n"); - pikaVM_runFile(pikaMain, "../../examples/cJSON/test1.py"); + pikaVM_runSingleFile(pikaMain, "../../examples/cJSON/test1.py"); /* collect */ /* assert */ EXPECT_STREQ(log_buff[0], @@ -249,7 +249,7 @@ TEST(cJSON, test2) { PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain); /* run */ __platform_printf("BEGIN\r\n"); - pikaVM_runFile(pikaMain, "../../examples/cJSON/test2.py"); + pikaVM_runSingleFile(pikaMain, "../../examples/cJSON/test2.py"); /* collect */ /* assert */ EXPECT_STREQ(log_buff[0], @@ -269,7 +269,7 @@ TEST(cJSON, test3) { PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain); /* run */ __platform_printf("BEGIN\r\n"); - pikaVM_runFile(pikaMain, "../../examples/cJSON/test3.py"); + pikaVM_runSingleFile(pikaMain, "../../examples/cJSON/test3.py"); /* collect */ /* assert */ EXPECT_STREQ( @@ -291,7 +291,7 @@ TEST(cJSON, test4) { PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain); /* run */ __platform_printf("BEGIN\r\n"); - pikaVM_runFile(pikaMain, "../../examples/cJSON/test4.py"); + pikaVM_runSingleFile(pikaMain, "../../examples/cJSON/test4.py"); /* collect */ /* assert */ EXPECT_STREQ( @@ -317,7 +317,7 @@ TEST(cJSON, test5) { PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain); /* run */ __platform_printf("BEGIN\r\n"); - pikaVM_runFile(pikaMain, "../../examples/cJSON/test5.py"); + pikaVM_runSingleFile(pikaMain, "../../examples/cJSON/test5.py"); /* collect */ /* assert */ @@ -347,7 +347,7 @@ TEST(cJSON, test6) { PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain); /* run */ __platform_printf("BEGIN\r\n"); - pikaVM_runFile(pikaMain, "../../examples/cJSON/test6.py"); + pikaVM_runSingleFile(pikaMain, "../../examples/cJSON/test6.py"); /* collect */ /* assert */ @@ -421,7 +421,7 @@ TEST(cJSON, test7) { PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain); /* run */ __platform_printf("BEGIN\r\n"); - pikaVM_runFile(pikaMain, "../../examples/cJSON/test7.py"); + pikaVM_runSingleFile(pikaMain, "../../examples/cJSON/test7.py"); /* collect */ /* assert */ diff --git a/port/linux/test/chinese-test.cpp b/port/linux/test/chinese-test.cpp index a3a3fdc0d..a2ddb2b19 100644 --- a/port/linux/test/chinese-test.cpp +++ b/port/linux/test/chinese-test.cpp @@ -4,7 +4,7 @@ TEST(chinese, test1) { /* init */ pikaMemInfo.heapUsedMax = 0; PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain); - pikaVM_runFile(pikaMain, "../../examples/Chinese/test_operator.py"); + pikaVM_runSingleFile(pikaMain, "../../examples/Chinese/test_operator.py"); obj_run(pikaMain, "test()"); /* collect */ /* assert */ diff --git a/port/linux/test/event-test.cpp b/port/linux/test/event-test.cpp index c915251bf..b24cf51a5 100644 --- a/port/linux/test/event-test.cpp +++ b/port/linux/test/event-test.cpp @@ -5,7 +5,7 @@ TEST(event, gpio) { /* init */ PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain); /* run */ - pikaVM_runFile(pikaMain, "../../examples/TemplateDevice/gpio_cb.py"); + pikaVM_runSingleFile(pikaMain, "../../examples/TemplateDevice/gpio_cb.py"); #define EVENT_SIGAL_IO_RISING_EDGE 0x01 #define EVENT_SIGAL_IO_FALLING_EDGE 0x02 diff --git a/port/linux/test/pikaMain-test.cpp b/port/linux/test/pikaMain-test.cpp index 57e2149f4..82b5419b8 100644 --- a/port/linux/test/pikaMain-test.cpp +++ b/port/linux/test/pikaMain-test.cpp @@ -27,7 +27,7 @@ TEST(pikaMain, unitTest) { PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain); /* run */ __platform_printf("BEGIN\r\n"); - pikaVM_runFile(pikaMain, "../../examples/UnitTest/main.py"); + pikaVM_runSingleFile(pikaMain, "../../examples/UnitTest/main.py"); /* collect */ /* assert */ /* deinit */ diff --git a/port/linux/test/string-test.cpp b/port/linux/test/string-test.cpp index dea7b8160..997c3bb68 100644 --- a/port/linux/test/string-test.cpp +++ b/port/linux/test/string-test.cpp @@ -71,7 +71,7 @@ TEST(string, print_file) { PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain); /* run */ __platform_printf("BEGIN\r\n"); - pikaVM_runFile(pikaMain, "../../examples/BuiltIn/print.py"); + pikaVM_runSingleFile(pikaMain, "../../examples/BuiltIn/print.py"); /* collect */ /* assert */ EXPECT_STREQ(log_buff[0], "format: test,0123,15.50\r\n"); diff --git a/src/PikaCompiler.c b/src/PikaCompiler.c index e38157984..7cee930d8 100644 --- a/src/PikaCompiler.c +++ b/src/PikaCompiler.c @@ -587,20 +587,30 @@ int32_t __foreach_handler_linkCompiledModules(Arg* argEach, Args* context) { return 0; } -void pikaMaker_linkCompiledModules(PikaMaker* self, char* lib_name) { +void pikaMaker_linkCompiledModulesFullPath(PikaMaker* self, char* lib_path) { Args context = {0}; LibObj* lib = New_LibObj(NULL); Args buffs = {0}; - __platform_printf(" linking %s...\n", lib_name); + __platform_printf(" linking %s...\n", lib_path); args_setPtr(&context, "__lib", lib); args_setPtr(&context, "__maker", self); args_foreach(self->list, __foreach_handler_linkCompiledModules, &context); args_deinit_stack(&context); char* pwd = obj_getStr(self, "pwd"); - char* folder_path = strsAppend(&buffs, pwd, "pikascript-api/"); - char* lib_file_path = strsAppend(&buffs, folder_path, lib_name); + char* lib_path_folder = strsCopy(&buffs, lib_path); + strPopLastToken(lib_path_folder, '/'); + char* folder_path = strsAppend(&buffs, pwd, lib_path_folder); + folder_path = strsAppend(&buffs, folder_path, "/"); + char* lib_file_path = strsAppend(&buffs, pwd, lib_path); LibObj_saveLibraryFile(lib, lib_file_path); Lib_loadLibraryFileToArray(lib_file_path, folder_path); LibObj_deinit(lib); strsDeinit(&buffs); } + +void pikaMaker_linkCompiledModules(PikaMaker* self, char* lib_name) { + Args buffs = {0}; + char* lib_file_path = strsAppend(&buffs, "pikascript-api/", lib_name); + pikaMaker_linkCompiledModulesFullPath(self, lib_file_path); + strsDeinit(&buffs); +} diff --git a/src/PikaCompiler.h b/src/PikaCompiler.h index c51d99f21..09e8794a9 100644 --- a/src/PikaCompiler.h +++ b/src/PikaCompiler.h @@ -29,6 +29,7 @@ char* pikaMaker_getFirstNocompiled(PikaMaker* self); void pikaMaker_compileModuleWithDepends(PikaMaker* self, char* module_name); void pikaMaker_linkCompiledModules(PikaMaker* self, char* lib_name); int LibObj_loadLibrary(LibObj* self, uint8_t* library_bytes); +void pikaMaker_linkCompiledModulesFullPath(PikaMaker* self, char* lib_path); #define LIB_VERSION_NUMBER 1 #define LIB_INFO_BLOCK_SIZE 32 diff --git a/src/PikaObj.c b/src/PikaObj.c index 5b30b8693..4c0e6f471 100644 --- a/src/PikaObj.c +++ b/src/PikaObj.c @@ -996,6 +996,13 @@ PikaObj* obj_importModuleWithByteCodeFrame(PikaObj* self, return self; } +PikaObj* Obj_linkLibraryFile(PikaObj* self, char* input_file_name) { + obj_newMetaObj(self, "__lib", New_LibObj); + LibObj* lib = obj_getObj(self, "__lib"); + LibObj_loadLibraryFile(lib, input_file_name); + return self; +} + PikaObj* obj_linkLibrary(PikaObj* self, uint8_t* library_bytes) { obj_newMetaObj(self, "__lib", New_LibObj); LibObj* lib = obj_getObj(self, "__lib"); diff --git a/src/PikaObj.h b/src/PikaObj.h index f68bab993..b45a4994c 100644 --- a/src/PikaObj.h +++ b/src/PikaObj.h @@ -272,4 +272,5 @@ void pks_eventLicener_registEvent(PikaEventListener* self, void pks_eventLisener_init(PikaEventListener** p_self); void pks_eventLisener_deinit(PikaEventListener** p_self); PikaObj* methodArg_getDefContext(Arg* method_arg); +PikaObj* Obj_linkLibraryFile(PikaObj* self, char* input_file_name); #endif diff --git a/src/PikaVM.c b/src/PikaVM.c index cbaa69ddd..37d00a380 100644 --- a/src/PikaVM.c +++ b/src/PikaVM.c @@ -803,7 +803,7 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self, VMState* vs, char* data) { } if (strEqu("==", data) || strEqu("!=", data)) { int8_t is_equ = -1; - if(type_arg1 == ARG_TYPE_NONE && type_arg2 == ARG_TYPE_NONE){ + if (type_arg1 == ARG_TYPE_NONE && type_arg2 == ARG_TYPE_NONE) { is_equ = 1; goto EQU_exit; } @@ -1184,10 +1184,10 @@ exit: return globals; } -VMParameters* pikaVM_runFile(PikaObj* self, char* filename) { +VMParameters* pikaVM_runSingleFile(PikaObj* self, char* filename) { + Args buffs = {0}; Arg* file_arg = arg_loadFile(NULL, filename); char* lines = (char*)arg_getBytes(file_arg); - Args buffs = {0}; /* replace the "\r\n" to "\n" */ lines = strsReplace(&buffs, lines, "\r\n", "\n"); /* clear the void line */ @@ -1634,3 +1634,20 @@ void byteCodeFrame_printAsArray(ByteCodeFrame* self) { __platform_printf("};\n"); __platform_printf("pikaVM_runByteCode(self, (uint8_t*)bytes);\n"); } + +PikaObj* pikaVM_runFile(PikaObj* self, char* file_name) { + Args buffs = {0}; + char* module_name = strsCopy(&buffs, file_name); + strPopLastToken(module_name, '.'); + + PikaMaker* maker = New_PikaMaker(); + pikaMaker_compileModuleWithDepends(maker, module_name); + pikaMaker_linkCompiledModules(maker, "pikaModules_cache.py.a"); + obj_deinit(maker); + + pikaMemMaxReset(); + Obj_linkLibraryFile(self, "pikascript-api/pikaModules_cache.py.a"); + self = pikaVM_runSingleFile(self, file_name); + strsDeinit(&buffs); + return self; +} diff --git a/src/PikaVM.h b/src/PikaVM.h index 711033f86..505557587 100644 --- a/src/PikaVM.h +++ b/src/PikaVM.h @@ -131,7 +131,8 @@ void byteCodeFrame_init(ByteCodeFrame* self); VMParameters* pikaVM_runByteCode(PikaObj* self, uint8_t* bytecode); InstructUnit* instructArray_getNow(InstructArray* self); InstructUnit* instructArray_getNext(InstructArray* self); -VMParameters* pikaVM_runFile(PikaObj* self, char* filename); +VMParameters* pikaVM_runSingleFile(PikaObj* self, char* filename); Arg* obj_runMethodArg(PikaObj* self, PikaObj* method_args_obj, Arg* method_arg); +PikaObj* pikaVM_runFile(PikaObj* self, char* file_name); #endif