From 347dd331bb76f6ac9fef6a687508bc184a1b01c9 Mon Sep 17 00:00:00 2001 From: lyon1998 <645275593@qq.com> Date: Wed, 20 Oct 2021 22:00:53 +0800 Subject: [PATCH] OPT for < and while is ok --- port/linux/test/parse-test.cpp | 32 ++++++++++++++++++++++++++++++++ src/PikaParser.c | 12 ++++++++++++ 2 files changed, 44 insertions(+) diff --git a/port/linux/test/parse-test.cpp b/port/linux/test/parse-test.cpp index 0133cf68a..dc2b379e7 100644 --- a/port/linux/test/parse-test.cpp +++ b/port/linux/test/parse-test.cpp @@ -558,4 +558,36 @@ TEST(parser, add_a_pp) { "0 OUT a\n"); 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); } \ No newline at end of file diff --git a/src/PikaParser.c b/src/PikaParser.c index 21d5478e3..cb6586dc6 100644 --- a/src/PikaParser.c +++ b/src/PikaParser.c @@ -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]);