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/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain", // "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [ "args": [
// "--gtest_filter=module.__init__" // "--gtest_filter=string.format_parse1"
], ],
"stopAtEntry": false, "stopAtEntry": false,
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",

View File

@ -32,7 +32,6 @@ TEST(string, cformat1) {
} }
#endif #endif
#if PIKA_SYNTEX_ITEM_FORMAT_ENABLE #if PIKA_SYNTEX_ITEM_FORMAT_ENABLE
TEST(string, format1) { TEST(string, format1) {
/* init */ /* init */
@ -84,4 +83,23 @@ TEST(string, print_file) {
EXPECT_EQ(pikaMemNow(), 0); 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, "", ""); Arg* var_buf = arg_setStr(NULL, "", "");
PIKA_BOOL is_in_format = PIKA_FALSE; PIKA_BOOL is_in_format = PIKA_FALSE;
PIKA_BOOL is_tuple = 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}; Args buffs = {0};
char* fmt = NULL; char* fmt = NULL;
ParserState_forEachToken(ps, right) { ParserState_forEachToken(ps, right) {
@ -1029,33 +1029,33 @@ char* Parser_solveFormat(Args* outBuffs, char* right) {
/* is a tuple */ /* is a tuple */
if (strEqu(ps.token2.pyload, "(")) { if (strEqu(ps.token2.pyload, "(")) {
is_tuple = PIKA_TRUE; is_tuple = PIKA_TRUE;
} else {
var_buf = arg_strAppend(var_buf, ps.token2.pyload);
} }
goto iter_continue; goto iter_continue;
} }
if (!is_tuple) { /* found the end of tuple */
str_buf = arg_strAppend(str_buf, "cformat("); if (ps.iter_index == ps.length) {
str_buf = arg_strAppend(str_buf, fmt); is_out_vars = PIKA_TRUE;
str_buf = arg_strAppend(str_buf, ",");
str_buf = arg_strAppend(str_buf, ps.token1.pyload);
str_buf = arg_strAppend(str_buf, ")");
is_in_format = PIKA_FALSE; is_in_format = PIKA_FALSE;
} else {
/* push the vars inner the tuple */
var_buf = arg_strAppend(var_buf, ps.token2.pyload);
} }
if (is_tuple) { if (is_out_vars) {
/* found the end of tuple */ if (is_tuple) {
if (ps.branket_deepth == 0 && strEqu(ps.token1.pyload, ")")) { str_buf = arg_strAppend(str_buf, "cformat(");
is_out_tuple = 1; str_buf = arg_strAppend(str_buf, fmt);
is_in_format = PIKA_FALSE; str_buf = arg_strAppend(str_buf, ",");
str_buf = arg_strAppend(str_buf, arg_getStr(var_buf));
} else { } else {
/* push the vars inner the tuple */ str_buf = arg_strAppend(str_buf, "cformat(");
var_buf = arg_strAppend(var_buf, ps.token2.pyload); 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: iter_continue:
if (!is_in_format) { if (!is_in_format) {