2022-04-27 19:19:14 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
extern "C" {
|
2022-04-28 16:55:48 +08:00
|
|
|
#include "PikaMain.h"
|
2022-04-27 19:19:14 +08:00
|
|
|
#include "PikaParser.h"
|
2022-04-28 16:55:48 +08:00
|
|
|
#include "PikaStdLib_MemChecker.h"
|
|
|
|
#include "PikaVM.h"
|
|
|
|
#include "dataArgs.h"
|
2022-04-27 19:19:14 +08:00
|
|
|
#include "dataMemory.h"
|
|
|
|
#include "dataStrs.h"
|
2022-04-30 21:13:42 +08:00
|
|
|
#include "pikaCompiler.h"
|
2022-04-28 16:55:48 +08:00
|
|
|
#include "pikaScript.h"
|
|
|
|
#include "pika_config_gtest.h"
|
2022-04-27 19:19:14 +08:00
|
|
|
}
|
|
|
|
|
2022-04-28 16:55:48 +08:00
|
|
|
extern char log_buff[LOG_BUFF_MAX][LOG_SIZE];
|
2022-04-29 01:13:41 +08:00
|
|
|
|
2022-04-27 19:19:14 +08:00
|
|
|
TEST(compiler, file) {
|
2022-04-30 21:17:19 +08:00
|
|
|
char* lines =
|
2022-04-27 19:19:14 +08:00
|
|
|
"len = __calls.len()\n"
|
|
|
|
"mode = 'none'\n"
|
|
|
|
"info_index = 0\n"
|
|
|
|
"for i in range(0, len):\n"
|
|
|
|
" if len == 0:\n"
|
|
|
|
" break\n"
|
|
|
|
" if info_index == 0:\n"
|
|
|
|
" mode = __calls[i]\n"
|
|
|
|
" info_index = 1\n"
|
|
|
|
" elif info_index == 1:\n"
|
|
|
|
" if mode == 'always':\n"
|
|
|
|
" todo = __calls[i]\n"
|
|
|
|
" todo()\n"
|
|
|
|
" info_index = 0\n"
|
|
|
|
" elif mode == 'when':\n"
|
|
|
|
" when = __calls[i]\n"
|
|
|
|
" info_index = 2\n"
|
|
|
|
" elif mode == 'period_ms':\n"
|
|
|
|
" period_ms = __calls[i]\n"
|
|
|
|
" info_index = 2\n"
|
|
|
|
" elif info_index == 2:\n"
|
|
|
|
" if mode == 'when':\n"
|
|
|
|
" if when():\n"
|
|
|
|
" todo = __calls[i]\n"
|
|
|
|
" todo()\n"
|
|
|
|
" info_index = 0\n"
|
|
|
|
" elif mode == 'period_ms':\n"
|
|
|
|
" todo = __calls[i]\n"
|
|
|
|
" info_index = 3\n"
|
|
|
|
" elif info_index == 3:\n"
|
|
|
|
" if mode == 'period_ms':\n"
|
|
|
|
" if __tick > __calls[i]:\n"
|
|
|
|
" todo()\n"
|
|
|
|
" __calls[i] = __tick + period_ms\n"
|
|
|
|
" info_index = 0\n"
|
|
|
|
"\n";
|
2022-04-30 21:17:19 +08:00
|
|
|
pikaCompile("task.bin", lines);
|
2022-04-27 19:19:14 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(compiler, task) {
|
2022-04-30 21:17:19 +08:00
|
|
|
char* lines =
|
2022-04-27 19:19:14 +08:00
|
|
|
"len = __calls.len()\n"
|
|
|
|
"mode = 'none'\n"
|
|
|
|
"info_index = 0\n"
|
|
|
|
"for i in range(0, len):\n"
|
|
|
|
" if len == 0:\n"
|
|
|
|
" break\n"
|
|
|
|
" if info_index == 0:\n"
|
|
|
|
" mode = __calls[i]\n"
|
|
|
|
" info_index = 1\n"
|
|
|
|
" elif info_index == 1:\n"
|
|
|
|
" if mode == 'always':\n"
|
|
|
|
" todo = __calls[i]\n"
|
|
|
|
" todo()\n"
|
|
|
|
" info_index = 0\n"
|
|
|
|
" elif mode == 'when':\n"
|
|
|
|
" when = __calls[i]\n"
|
|
|
|
" info_index = 2\n"
|
|
|
|
" elif mode == 'period_ms':\n"
|
|
|
|
" period_ms = __calls[i]\n"
|
|
|
|
" info_index = 2\n"
|
|
|
|
" elif info_index == 2:\n"
|
|
|
|
" if mode == 'when':\n"
|
|
|
|
" if when():\n"
|
|
|
|
" todo = __calls[i]\n"
|
|
|
|
" todo()\n"
|
|
|
|
" info_index = 0\n"
|
|
|
|
" elif mode == 'period_ms':\n"
|
|
|
|
" todo = __calls[i]\n"
|
|
|
|
" info_index = 3\n"
|
|
|
|
" elif info_index == 3:\n"
|
|
|
|
" if mode == 'period_ms':\n"
|
|
|
|
" if __tick > __calls[i]:\n"
|
|
|
|
" todo()\n"
|
|
|
|
" __calls[i] = __tick + period_ms\n"
|
|
|
|
" info_index = 0\n"
|
|
|
|
"\n";
|
|
|
|
|
|
|
|
Args buffs = {0};
|
2022-04-30 21:17:19 +08:00
|
|
|
char* pikaAsm = Parser_multiLineToAsm(&buffs, lines);
|
2022-04-27 19:19:14 +08:00
|
|
|
|
|
|
|
ByteCodeFrame bytecode_frame;
|
|
|
|
byteCodeFrame_init(&bytecode_frame);
|
|
|
|
byteCodeFrame_appendFromAsm(&bytecode_frame, pikaAsm);
|
|
|
|
/* do something */
|
|
|
|
byteCodeFrame_print(&bytecode_frame);
|
|
|
|
printf("Asm size: %d\r\n", strGetSize(pikaAsm));
|
|
|
|
|
|
|
|
byteCodeFrame_printAsArray(&bytecode_frame);
|
|
|
|
|
|
|
|
/* deinit */
|
|
|
|
byteCodeFrame_deinit(&bytecode_frame);
|
|
|
|
strsDeinit(&buffs);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(compiler, demo1) {
|
2022-04-30 21:17:19 +08:00
|
|
|
char* lines = "append(__val)";
|
2022-04-27 19:19:14 +08:00
|
|
|
Parser_compilePyToBytecodeArray(lines);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(compiler, snake_file) {
|
2022-04-30 21:17:19 +08:00
|
|
|
char* lines =
|
2022-04-27 19:19:14 +08:00
|
|
|
"from PikaObj import *\n"
|
|
|
|
"import PikaStdLib\n"
|
|
|
|
"import machine \n"
|
|
|
|
"\n"
|
|
|
|
"# hardware init\n"
|
|
|
|
"lcd = machine.LCD()\n"
|
|
|
|
"lcd.init()\n"
|
|
|
|
"lcd.clear('white')\n"
|
|
|
|
"key = machine.KEY()\n"
|
|
|
|
"key.init()\n"
|
|
|
|
"time = machine.Time()\n"
|
|
|
|
"x_max = 120\n"
|
|
|
|
"y_max = 150\n"
|
|
|
|
"\n"
|
|
|
|
"# snake init\n"
|
|
|
|
"s = machine.Point()\n"
|
|
|
|
"w = 9\n"
|
|
|
|
"h = 9\n"
|
|
|
|
"s.x = 50\n"
|
|
|
|
"s.y = 10\n"
|
|
|
|
"len = 0\n"
|
|
|
|
"while len < 3:\n"
|
|
|
|
" b = s\n"
|
|
|
|
" i = 0\n"
|
|
|
|
" while i < len:\n"
|
|
|
|
" b = b.next\n"
|
|
|
|
" i = i + 1\n"
|
|
|
|
" b.next = machine.Point()\n"
|
|
|
|
" b.next.x = b.x - 10\n"
|
|
|
|
" b.next.y = b.y\n"
|
|
|
|
" b.next.prev = b\n"
|
|
|
|
" len = len + 1\n"
|
|
|
|
"# ring link\n"
|
|
|
|
"b.next = s\n"
|
|
|
|
"s.prev = b\n"
|
|
|
|
"\n"
|
|
|
|
"i = 0\n"
|
|
|
|
"b = s\n"
|
|
|
|
"while i < len:\n"
|
|
|
|
" lcd.fill(b.x, b.y, w, h, 'blue')\n"
|
|
|
|
" b = b.next\n"
|
|
|
|
" i = i + 1\n"
|
|
|
|
"\n"
|
|
|
|
"print('snake lengh')\n"
|
|
|
|
"print(len)\n"
|
|
|
|
"\n"
|
|
|
|
"# fruit init\n"
|
|
|
|
"f = machine.Point()\n"
|
|
|
|
"f.x = 30\n"
|
|
|
|
"f.y = 20\n"
|
|
|
|
"lcd.fill(f.x, f.y, w, h, 'green')\n"
|
|
|
|
"\n"
|
|
|
|
"# memory check\n"
|
|
|
|
"mem = PikaStdLib.MemChecker()\n"
|
|
|
|
"print('mem used max:')\n"
|
|
|
|
"mem.max()\n"
|
|
|
|
"\n"
|
|
|
|
"# main loop\n"
|
|
|
|
"d = 0\n"
|
|
|
|
"isUpdate = 1\n"
|
|
|
|
"isEat = 0\n"
|
|
|
|
"while True:\n"
|
|
|
|
" if isUpdate:\n"
|
|
|
|
" # isUpdate = 0\n"
|
|
|
|
" # check eat fruit\n"
|
|
|
|
" if f.x == s.x and f.y == s.y:\n"
|
|
|
|
" # have eat fruit\n"
|
|
|
|
" isEat = 1\n"
|
|
|
|
" f.x = f.x + 30\n"
|
|
|
|
" if f.x > x_max:\n"
|
|
|
|
" f.x = f.x - x_max\n"
|
|
|
|
" f.y = f.y + 30\n"
|
|
|
|
" if f.y > y_max:\n"
|
|
|
|
" f.y = f.y - y_max\n"
|
|
|
|
" lcd.fill(f.x, f.y, w, h, 'green')\n"
|
|
|
|
" # move snake by the direction\n"
|
|
|
|
" if d == 0:\n"
|
|
|
|
" x_new = s.x + 10\n"
|
|
|
|
" y_new = s.y\n"
|
|
|
|
" if x_new > x_max:\n"
|
|
|
|
" x_new = 0\n"
|
|
|
|
" elif d == 1:\n"
|
|
|
|
" x_new = s.x\n"
|
|
|
|
" y_new = s.y - 10\n"
|
|
|
|
" if y_new < 0:\n"
|
|
|
|
" y_new = y_max\n"
|
|
|
|
" elif d == 2:\n"
|
|
|
|
" x_new = s.x\n"
|
|
|
|
" y_new = s.y + 10\n"
|
|
|
|
" if y_new > y_max:\n"
|
|
|
|
" y_new = 0\n"
|
|
|
|
" elif d == 3:\n"
|
|
|
|
" x_new = s.x - 10\n"
|
|
|
|
" y_new = s.y\n"
|
|
|
|
" if x_new < 0:\n"
|
|
|
|
" x_new = x_max\n"
|
|
|
|
" if isEat:\n"
|
|
|
|
" isEat = 0\n"
|
|
|
|
" b_new = machine.Point()\n"
|
|
|
|
" b_new.x = x_new\n"
|
|
|
|
" b_new.y = y_new\n"
|
|
|
|
" b_new.prev = s.prev\n"
|
|
|
|
" b_new.next = s\n"
|
|
|
|
" s.prev.next = b_new\n"
|
|
|
|
" s.prev = b_new\n"
|
|
|
|
" s = b_new\n"
|
|
|
|
" len = len + 1\n"
|
|
|
|
" print('snake lengh')\n"
|
|
|
|
" print(len)\n"
|
|
|
|
" print('mem used max:')\n"
|
|
|
|
" mem.max()\n"
|
|
|
|
" # drow the snake and fruit\n"
|
|
|
|
" # clear last body\n"
|
|
|
|
" lcd.fill(s.prev.x, s.prev.y, w, h, 'white')\n"
|
|
|
|
" # new body\n"
|
|
|
|
" s.prev.x = x_new\n"
|
|
|
|
" s.prev.y = y_new\n"
|
|
|
|
" # head is last body\n"
|
|
|
|
" s = s.prev\n"
|
|
|
|
" lcd.fill(s.x, s.y, w, h, 'blue')\n"
|
|
|
|
" b = s\n"
|
|
|
|
" i = 0\n"
|
|
|
|
" # scan key\n"
|
|
|
|
" key_val = key.get()\n"
|
|
|
|
" if key_val == 0:\n"
|
|
|
|
" d = 0\n"
|
|
|
|
" isUpdate = 1\n"
|
|
|
|
" elif key_val == 1:\n"
|
|
|
|
" d = 1\n"
|
|
|
|
" isUpdate = 1\n"
|
|
|
|
" elif key_val == 2:\n"
|
|
|
|
" d = 2\n"
|
|
|
|
" isUpdate = 1\n"
|
|
|
|
" elif key_val == 3:\n"
|
|
|
|
" d = 3\n"
|
|
|
|
" isUpdate = 1\n";
|
2022-04-30 21:17:19 +08:00
|
|
|
pikaCompile("snake.bin", lines);
|
2022-04-27 19:19:14 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2022-04-28 16:55:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(compiler, import_bf_mem) {
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
char* lines =
|
2022-04-28 16:55:48 +08:00
|
|
|
"def mytest():\n"
|
|
|
|
" print('test')\n"
|
|
|
|
"\n";
|
|
|
|
ByteCodeFrame bf;
|
|
|
|
byteCodeFrame_init(&bf);
|
|
|
|
bytecodeFrame_fromMultiLine(&bf, lines);
|
2022-04-30 21:17:19 +08:00
|
|
|
obj_importModuleWithByteCodeFrame(pikaMain, "mtest", &bf);
|
2022-04-28 16:55:48 +08:00
|
|
|
byteCodeFrame_deinit(&bf);
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(compiler, import_bf1) {
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
char* lines =
|
2022-04-28 16:55:48 +08:00
|
|
|
"def mytest():\n"
|
|
|
|
" print('test_import_bf1')\n"
|
|
|
|
"\n";
|
|
|
|
ByteCodeFrame bf;
|
|
|
|
byteCodeFrame_init(&bf);
|
|
|
|
bytecodeFrame_fromMultiLine(&bf, lines);
|
2022-04-30 21:17:19 +08:00
|
|
|
obj_importModuleWithByteCodeFrame(pikaMain, "mtest", &bf);
|
2022-04-30 22:40:52 +08:00
|
|
|
obj_run(pikaMain,
|
2022-04-28 16:55:48 +08:00
|
|
|
"mtest.mytest()\n"
|
2022-04-30 22:40:52 +08:00
|
|
|
"\n");
|
2022-04-28 16:55:48 +08:00
|
|
|
EXPECT_STREQ(log_buff[0], "test_import_bf1\r\n");
|
|
|
|
byteCodeFrame_deinit(&bf);
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(compiler, import_bf2) {
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
char* lines =
|
2022-04-28 16:55:48 +08:00
|
|
|
"class Test:\n"
|
|
|
|
" def mytest(self):\n"
|
|
|
|
" print('test_import_bf2')\n"
|
|
|
|
"\n";
|
|
|
|
ByteCodeFrame bf;
|
|
|
|
byteCodeFrame_init(&bf);
|
|
|
|
bytecodeFrame_fromMultiLine(&bf, lines);
|
2022-04-30 21:17:19 +08:00
|
|
|
obj_importModuleWithByteCodeFrame(pikaMain, "mtest", &bf);
|
2022-04-30 22:40:52 +08:00
|
|
|
obj_run(pikaMain,
|
2022-04-28 16:55:48 +08:00
|
|
|
"m = mtest.Test()\n"
|
|
|
|
"m.mytest()\n"
|
2022-04-30 22:40:52 +08:00
|
|
|
"\n");
|
2022-04-28 16:55:48 +08:00
|
|
|
byteCodeFrame_deinit(&bf);
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_STREQ(log_buff[0], "test_import_bf2\r\n");
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-04-29 01:24:28 +08:00
|
|
|
|
|
|
|
TEST(compiler, file1) {
|
2022-04-30 21:17:19 +08:00
|
|
|
pikaCompileFile("test/python/main.py");
|
2022-04-29 01:24:28 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(compiler, file2) {
|
2022-04-30 21:17:19 +08:00
|
|
|
pikaCompileFile("test/python/main_snake_LCD.py");
|
2022-04-29 01:24:28 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-04-29 23:56:25 +08:00
|
|
|
|
|
|
|
TEST(lib, init) {
|
2022-04-30 22:40:52 +08:00
|
|
|
LibObj* lib = New_LibObj();
|
|
|
|
LibObj_deinit(lib);
|
2022-04-29 23:56:25 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-04-30 21:13:42 +08:00
|
|
|
|
|
|
|
TEST(lib, lib_link_bytecode) {
|
2022-04-30 22:40:52 +08:00
|
|
|
LibObj* lib = New_LibObj();
|
2022-04-30 23:13:05 +08:00
|
|
|
LibObj_dynamicLink(lib, "module1", (uint8_t*)0x3344);
|
2022-05-01 00:02:19 +08:00
|
|
|
LibObj_dynamicLink(lib, "module2", (uint8_t*)0x33433);
|
|
|
|
LibObj_dynamicLink(lib, "module3", (uint8_t*)0x33433);
|
|
|
|
LibObj_dynamicLink(lib, "module4", (uint8_t*)0x33433);
|
|
|
|
LibObj_dynamicLink(lib, "module5", (uint8_t*)0x33433);
|
2022-04-30 21:13:42 +08:00
|
|
|
EXPECT_STREQ(obj_getStr(lib, "index.module1.name"), "module1");
|
|
|
|
EXPECT_EQ((uintptr_t)obj_getPtr(lib, "index.module1.bytecode"), 0x3344);
|
|
|
|
/* deinit */
|
2022-04-30 22:40:52 +08:00
|
|
|
LibObj_deinit(lib);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(lib, lib_push_file) {
|
|
|
|
LibObj* lib = New_LibObj();
|
2022-04-30 23:13:05 +08:00
|
|
|
LibObj_staticLinkFile(lib, "test/python/main.py.o");
|
2022-04-30 22:40:52 +08:00
|
|
|
/* deinit */
|
|
|
|
LibObj_deinit(lib);
|
2022-04-30 21:13:42 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-04-30 23:07:30 +08:00
|
|
|
|
|
|
|
TEST(lib, lib_push_files) {
|
|
|
|
LibObj* lib = New_LibObj();
|
2022-04-30 23:13:05 +08:00
|
|
|
LibObj_staticLinkFile(lib, "test/python/main.py.o");
|
|
|
|
LibObj_staticLinkFile(lib, "test/python/main_snake_LCD.py.o");
|
2022-04-30 23:07:30 +08:00
|
|
|
LibObj_listModules(lib);
|
|
|
|
/* asset */
|
|
|
|
EXPECT_STREQ(log_buff[0], "main\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[1], "main_snake_LCD\r\n");
|
|
|
|
/* deinit */
|
|
|
|
LibObj_deinit(lib);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-04-30 23:51:25 +08:00
|
|
|
|
|
|
|
TEST(lib, lib_compile_link) {
|
2022-04-30 23:53:28 +08:00
|
|
|
LibObj* lib = New_LibObj();
|
|
|
|
|
2022-04-30 23:51:25 +08:00
|
|
|
pikaCompileFile("test/python/UnitTest.py");
|
|
|
|
pikaCompileFile("test/python/main.py");
|
|
|
|
pikaCompileFile("test/python/main_snake_LCD.py");
|
2022-04-30 23:53:28 +08:00
|
|
|
|
2022-04-30 23:51:25 +08:00
|
|
|
LibObj_staticLinkFile(lib, "test/python/UnitTest.py.o");
|
|
|
|
LibObj_staticLinkFile(lib, "test/python/main.py.o");
|
|
|
|
LibObj_staticLinkFile(lib, "test/python/main_snake_LCD.py.o");
|
2022-04-30 23:53:28 +08:00
|
|
|
|
2022-04-30 23:51:25 +08:00
|
|
|
LibObj_listModules(lib);
|
|
|
|
/* asset */
|
|
|
|
EXPECT_STREQ(log_buff[0], "UnitTest\r\n");
|
2022-04-30 23:52:41 +08:00
|
|
|
EXPECT_STREQ(log_buff[1], "main\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[2], "main_snake_LCD\r\n");
|
2022-04-30 23:51:25 +08:00
|
|
|
/* deinit */
|
|
|
|
LibObj_deinit(lib);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|