fix format syntax error when ''%method()

This commit is contained in:
pikastech 2022-06-15 15:45:44 +08:00
parent c02ffb51ab
commit 2c4bbfafad
3 changed files with 41 additions and 23 deletions

View File

@ -11,7 +11,7 @@
"program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
// "--gtest_filter=module.__init__"
// "--gtest_filter=string.format_parse1"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@ -32,7 +32,6 @@ TEST(string, cformat1) {
}
#endif
#if PIKA_SYNTEX_ITEM_FORMAT_ENABLE
TEST(string, format1) {
/* init */
@ -84,4 +83,23 @@ TEST(string, print_file) {
EXPECT_EQ(pikaMemNow(), 0);
}
#endif
#endif
#if PIKA_SYNTEX_ITEM_FORMAT_ENABLE
TEST(string, format_parse1) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
char* lines = "print('tes:%0.2f'% mem.getMax())";
printf("%s\n", lines);
char* pikaAsm = Parser_multiLineToAsm(buffs, lines);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"2 STR tes:%0.2f\n"
"2 RUN mem.getMax\n"
"1 RUN cformat\n"
"0 RUN print\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
#endif

View File

@ -999,7 +999,7 @@ char* Parser_solveFormat(Args* outBuffs, char* right) {
Arg* var_buf = arg_setStr(NULL, "", "");
PIKA_BOOL is_in_format = PIKA_FALSE;
PIKA_BOOL is_tuple = PIKA_FALSE;
PIKA_BOOL is_out_tuple = PIKA_FALSE;
PIKA_BOOL is_out_vars = PIKA_FALSE;
Args buffs = {0};
char* fmt = NULL;
ParserState_forEachToken(ps, right) {
@ -1029,33 +1029,33 @@ char* Parser_solveFormat(Args* outBuffs, char* right) {
/* is a tuple */
if (strEqu(ps.token2.pyload, "(")) {
is_tuple = PIKA_TRUE;
} else {
var_buf = arg_strAppend(var_buf, ps.token2.pyload);
}
goto iter_continue;
}
if (!is_tuple) {
str_buf = arg_strAppend(str_buf, "cformat(");
str_buf = arg_strAppend(str_buf, fmt);
str_buf = arg_strAppend(str_buf, ",");
str_buf = arg_strAppend(str_buf, ps.token1.pyload);
str_buf = arg_strAppend(str_buf, ")");
/* found the end of tuple */
if (ps.iter_index == ps.length) {
is_out_vars = PIKA_TRUE;
is_in_format = PIKA_FALSE;
} else {
/* push the vars inner the tuple */
var_buf = arg_strAppend(var_buf, ps.token2.pyload);
}
if (is_tuple) {
/* found the end of tuple */
if (ps.branket_deepth == 0 && strEqu(ps.token1.pyload, ")")) {
is_out_tuple = 1;
is_in_format = PIKA_FALSE;
if (is_out_vars) {
if (is_tuple) {
str_buf = arg_strAppend(str_buf, "cformat(");
str_buf = arg_strAppend(str_buf, fmt);
str_buf = arg_strAppend(str_buf, ",");
str_buf = arg_strAppend(str_buf, arg_getStr(var_buf));
} else {
/* push the vars inner the tuple */
var_buf = arg_strAppend(var_buf, ps.token2.pyload);
str_buf = arg_strAppend(str_buf, "cformat(");
str_buf = arg_strAppend(str_buf, fmt);
str_buf = arg_strAppend(str_buf, ",");
str_buf = arg_strAppend(str_buf, arg_getStr(var_buf));
str_buf = arg_strAppend(str_buf, ")");
}
}
if (is_out_tuple) {
str_buf = arg_strAppend(str_buf, "cformat(");
str_buf = arg_strAppend(str_buf, fmt);
str_buf = arg_strAppend(str_buf, ",");
str_buf = arg_strAppend(str_buf, arg_getStr(var_buf));
}
}
iter_continue:
if (!is_in_format) {