OPT for < and while is ok

This commit is contained in:
lyon1998 2021-10-20 22:00:53 +08:00
parent 53ba6273a9
commit 347dd331bb
2 changed files with 44 additions and 0 deletions

View File

@ -559,3 +559,35 @@ TEST(parser, add_a_pp) {
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, while_a_pp) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
char* lines = (char*)
"while a < 10:\n"
" print(a)\n"
" a = a + 1\n"
"\n";
printf("%s", lines);
char* pikaAsm = pikaParseMultiLineToAsm(buffs, (char*)lines);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"1 REF a\n"
"1 NUM 10\n"
"0 OPT <\n"
"0 JEZ 2\n"
"B1\n"
"1 REF a\n"
"0 RUN print\n"
"B1\n"
"1 REF a\n"
"1 NUM 1\n"
"0 OPT +\n"
"0 OUT a\n"
"B0\n"
"0 JMP -1\n"
"B0\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}

View File

@ -80,6 +80,9 @@ static enum StmtType matchStmtType(char* right) {
if (strIsContain(rightWithoutSubStmt, '+') ||
strIsContain(rightWithoutSubStmt, '-') ||
strIsContain(rightWithoutSubStmt, '*') ||
strIsContain(rightWithoutSubStmt, '<') ||
strIsContain(rightWithoutSubStmt, '>') ||
strIsContain(rightWithoutSubStmt, '=') ||
strIsContain(rightWithoutSubStmt, '/')) {
stmtType = OPERATOR;
goto exit;
@ -148,6 +151,15 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
if (strIsContain(rightWithoutSubStmt, '/')) {
operator[0] = '/';
}
if (strIsContain(rightWithoutSubStmt, '<')) {
operator[0] = '<';
}
if (strIsContain(rightWithoutSubStmt, '>')) {
operator[0] = '>';
}
if (strIsContain(rightWithoutSubStmt, '=')) {
operator[0] = '==';
}
obj_setStr(ast, (char*)"operator", operator);
char* rightBuff = strsCopy(buffs, right);
char* subStmt1 = strsPopToken(buffs, rightBuff, operator[0]);