parse for brk is ok

This commit is contained in:
lyon 2021-12-29 21:06:56 +08:00
parent f4a3b1079a
commit 45ee8621cc
2 changed files with 62 additions and 0 deletions

View File

@ -1562,3 +1562,51 @@ TEST(parser, for_for_range) {
args_deinit(buffs); args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0); EXPECT_EQ(pikaMemNow(), 0);
} }
TEST(parser, break_) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
char* lines = (char*)
"for i in range(0, 3):\n"
" if i = 1:\n"
" break\n"
" break \n"
"\n";
printf("%s", lines);
char* pikaAsm = Parser_multiLineToAsm(buffs, (char*)lines);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"2 NUM 0\n"
"2 NUM 3\n"
"1 RUN range\n"
"0 RUN iter\n"
"0 OUT _l0\n"
"0 REF _r1\n"
"0 REF _r2\n"
"0 REF _r3\n"
"0 OUT _l0.a1\n"
"0 OUT _l0.a2\n"
"0 OUT _l0.a3\n"
"B0\n"
"0 RUN _l0.__next__\n"
"0 OUT i\n"
"0 EST i\n"
"0 JEZ 2\n"
"B1\n"
"0 NUM 1\n"
"0 OUT i\n"
"0 JEZ 1\n"
"B2\n"
"0 BRK\n"
"B2\n"
"0 BRK\n"
"B0\n"
"0 JMP -1\n"
"B0\n"
"0 DEL _l0\n"
"B0\n"
);
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}

View File

@ -796,6 +796,13 @@ AST* AST_parseLine(char* line, Stack* blockStack) {
} }
goto block_matched; goto block_matched;
} }
if (0 == strncmp(lineStart, (char*)"break", 5)) {
if ((lineStart[5] == ' ') || (lineStart[5] == 0)) {
obj_setStr(ast, "break", "");
stmt = "";
goto block_matched;
}
}
if (strEqu(lineStart, (char*)"return")) { if (strEqu(lineStart, (char*)"return")) {
obj_setStr(ast, "return", ""); obj_setStr(ast, "return", "");
stmt = ""; stmt = "";
@ -1119,6 +1126,13 @@ char* AST_toPikaAsm(AST* ast, Args* buffs) {
is_block_matched = 1; is_block_matched = 1;
goto exit; goto exit;
} }
if (obj_isArgExist(ast, "break")) {
/* parse stmt ast */
pikaAsm = AST_appandPikaAsm(ast, ast, runBuffs, pikaAsm);
pikaAsm = strsAppend(runBuffs, pikaAsm, "0 BRK\n");
is_block_matched = 1;
goto exit;
}
exit: exit:
if (!is_block_matched) { if (!is_block_matched) {
/* parse stmt ast */ /* parse stmt ast */