diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index 3b811c29f..ef84022ba 100644 --- a/port/linux/.vscode/launch.json +++ b/port/linux/.vscode/launch.json @@ -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}", diff --git a/port/linux/test/string-test.cpp b/port/linux/test/string-test.cpp index 411dd18ec..25d837103 100644 --- a/port/linux/test/string-test.cpp +++ b/port/linux/test/string-test.cpp @@ -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 \ No newline at end of file +#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 diff --git a/src/PikaParser.c b/src/PikaParser.c index 7002dd402..9eabe530a 100644 --- a/src/PikaParser.c +++ b/src/PikaParser.c @@ -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) {