mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
support multy return in function
This commit is contained in:
parent
512424aef5
commit
8143c1376e
@ -1512,8 +1512,33 @@ TEST(vm, multi_return) {
|
||||
__platform_printf("BEGIN\r\n");
|
||||
obj_run(pikaMain, "a,b = (1,2)");
|
||||
/* collect */
|
||||
int a= obj_getInt(pikaMain, "a");
|
||||
int b= obj_getInt(pikaMain, "b");
|
||||
int a = obj_getInt(pikaMain, "a");
|
||||
int b = obj_getInt(pikaMain, "b");
|
||||
/* assert */
|
||||
EXPECT_EQ(a, 1);
|
||||
EXPECT_EQ(b, 2);
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !PIKA_NANO_ENABLE
|
||||
TEST(vm, multi_return_fn) {
|
||||
/* init */
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||
extern unsigned char pikaModules_py_a[];
|
||||
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
||||
/* run */
|
||||
__platform_printf("BEGIN\r\n");
|
||||
obj_run(pikaMain,
|
||||
"def test():\n"
|
||||
" return 1,2\n"
|
||||
"a,b = test()");
|
||||
/* collect */
|
||||
int a = obj_getInt(pikaMain, "a");
|
||||
int b = obj_getInt(pikaMain, "b");
|
||||
/* assert */
|
||||
EXPECT_EQ(a, 1);
|
||||
EXPECT_EQ(b, 2);
|
||||
|
@ -4182,3 +4182,23 @@ TEST(parser, multi_return_2) {
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !PIKA_NANO_ENABLE
|
||||
TEST(parser, multi_return_3) {
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
Args* buffs = New_strBuff();
|
||||
char* lines = "return a,b";
|
||||
__platform_printf("%s\n", lines);
|
||||
char* pikaAsm = Parser_linesToAsm(buffs, lines);
|
||||
__platform_printf("%s", pikaAsm);
|
||||
EXPECT_STREQ(pikaAsm,
|
||||
"B0\n"
|
||||
"1 REF a\n"
|
||||
"1 REF b\n"
|
||||
"0 RUN \n"
|
||||
"0 RET \n"
|
||||
"B0\n");
|
||||
args_deinit(buffs);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
#endif
|
||||
|
@ -1688,6 +1688,23 @@ char* _defGetDefault(Args* outBuffs, char** dec_out) {
|
||||
return default_out;
|
||||
}
|
||||
|
||||
static char* Suger_multiReturn(Args* out_buffs, char* line) {
|
||||
#if PIKA_NANO_ENABLE
|
||||
return line;
|
||||
#endif
|
||||
Cursor_forEachToken(cs, line) {
|
||||
Cursor_iterStart(&cs);
|
||||
if (cs.branket_deepth == 0 && strEqu(cs.token1.pyload, ",")) {
|
||||
line = strsFormat(out_buffs, strGetSize(line) + 3, "(%s)", line);
|
||||
Cursor_iterEnd(&cs);
|
||||
break;
|
||||
}
|
||||
Cursor_iterEnd(&cs);
|
||||
}
|
||||
Cursor_deinit(&cs);
|
||||
return line;
|
||||
}
|
||||
|
||||
/* match block start keywords */
|
||||
const char control_keywords[][9] = {"break", "continue"};
|
||||
|
||||
@ -1838,6 +1855,7 @@ AST* AST_parseLine_withBlockStack_withBlockDeepth(char* line,
|
||||
char* lineBuff = strsCopy(&buffs, line_start);
|
||||
strsPopToken(&buffs, lineBuff, ' ');
|
||||
stmt = lineBuff;
|
||||
stmt = Suger_multiReturn(&buffs, stmt);
|
||||
AST_setNodeAttr(ast, "return", "");
|
||||
goto block_matched;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user