mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
pikaVM_runFile change to interpreter mode
This commit is contained in:
parent
e96292debd
commit
ab795de30c
@ -1,7 +1,18 @@
|
||||
/* this demo shows the usage of method */
|
||||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
||||
}
|
||||
|
@ -1,2 +1,3 @@
|
||||
sh api-make.sh
|
||||
sh only_make.sh
|
||||
cp ./build/boot/demo06-pikamain/pikascript_demo06-pikamain package/pikascript/pika
|
||||
|
@ -21,3 +21,6 @@ def while_loop():
|
||||
while i < 4:
|
||||
i += 1
|
||||
print(i)
|
||||
|
||||
def newfun():
|
||||
print('test in new fun')
|
||||
|
@ -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 */
|
||||
|
||||
|
@ -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 */
|
||||
|
@ -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
|
||||
|
@ -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 */
|
||||
|
@ -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");
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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");
|
||||
|
@ -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
|
||||
|
23
src/PikaVM.c
23
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;
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user