pikapython/port/linux/test/parse-test.cpp

126 lines
3.7 KiB
C
Raw Normal View History

2021-10-13 18:07:08 +08:00
#include "gtest/gtest.h"
extern "C" {
#include "BaseObj.h"
#include "PikaParser.h"
#include "dataMemory.h"
#include "dataStrs.h"
}
TEST(parser, NEW) {
AST* ast = pikaParse((char*)"add(a,b)");
Args* buffs = New_strBuff();
2021-10-13 22:17:40 +08:00
char* pikaAsm = AST_toPikaAsm(ast, buffs);
printf("%s", pikaAsm);
2021-10-13 18:07:08 +08:00
args_deinit(buffs);
2021-10-14 10:44:59 +08:00
AST_deinit(ast);
2021-10-13 18:07:08 +08:00
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, add_a_b) {
AST* ast = pikaParse((char*)"add( a , b)");
Args* buffs = New_strBuff();
2021-10-13 22:17:40 +08:00
char* pikaAsm = AST_toPikaAsm(ast, buffs);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm, "1 REF a\n1 REF b\n0 RUN add\n");
2021-10-13 21:20:45 +08:00
args_deinit(buffs);
2021-10-14 10:44:59 +08:00
AST_deinit(ast);
2021-10-13 21:20:45 +08:00
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, add_a_b_c) {
AST* ast = pikaParse((char*)"d = add(add(a,b) , c)");
Args* buffs = New_strBuff();
2021-10-13 22:17:40 +08:00
char* pikaAsm = AST_toPikaAsm(ast, buffs);
printf("%s", pikaAsm);
2021-10-14 10:44:59 +08:00
EXPECT_STREQ(pikaAsm,
"2 REF a\n2 REF b\n1 RUN add\n1 REF c\n0 RUN add\n0 OUT d\n");
2021-10-13 21:20:45 +08:00
args_deinit(buffs);
2021-10-14 10:44:59 +08:00
AST_deinit(ast);
2021-10-13 21:20:45 +08:00
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, method1) {
AST* ast = pikaParse((char*)"d.p = a.add(b.add(a,se.b) , pmw.c)");
Args* buffs = New_strBuff();
2021-10-13 22:17:40 +08:00
char* pikaAsm = AST_toPikaAsm(ast, buffs);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
2021-10-14 10:44:59 +08:00
"2 REF a\n2 REF se.b\n1 RUN b.add\n1 REF pmw.c\n0 RUN "
"a.add\n0 OUT d.p\n");
2021-10-13 21:53:01 +08:00
args_deinit(buffs);
2021-10-14 10:44:59 +08:00
AST_deinit(ast);
2021-10-13 21:53:01 +08:00
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, method2) {
2021-10-14 10:44:59 +08:00
char* line = (char*)"d.p = a.add(b.add(a,se.b,diek(pp)) , pmw.c())";
2021-10-13 21:53:01 +08:00
Args* buffs = New_strBuff();
2021-10-14 10:44:59 +08:00
char* pikaAsm = pikaParseToAsm(buffs, line);
2021-10-13 22:17:40 +08:00
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
2021-10-14 10:44:59 +08:00
"2 REF a\n2 REF se.b\n3 REF pp\n2 RUN diek\n1 RUN b.add\n1 "
"RUN pmw.c\n0 RUN a.add\n0 OUT d.p\n");
2021-10-13 18:07:08 +08:00
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-14 09:51:50 +08:00
}
TEST(parser, str1) {
2021-10-14 10:44:59 +08:00
char* line = (char*)"literal('2.322')";
2021-10-14 09:51:50 +08:00
Args* buffs = New_strBuff();
2021-10-14 10:44:59 +08:00
char* pikaAsm = pikaParseToAsm(buffs, line);
2021-10-14 09:51:50 +08:00
printf("%s", pikaAsm);
2021-10-14 10:44:59 +08:00
EXPECT_STREQ(pikaAsm, "1 STR 2.322\n0 RUN literal\n");
2021-10-14 09:51:50 +08:00
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-14 09:53:37 +08:00
}
TEST(parser, str2) {
2021-10-14 10:44:59 +08:00
char* line = (char*)"b=add(a,literal('1'))";
2021-10-14 09:53:37 +08:00
Args* buffs = New_strBuff();
2021-10-14 10:44:59 +08:00
char* pikaAsm = pikaParseToAsm(buffs, line);
2021-10-14 09:53:37 +08:00
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"1 REF a\n2 STR 1\n1 RUN literal\n0 RUN add\n0 OUT b\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-14 15:08:45 +08:00
}
TEST(parser, num1) {
char* line = (char*)"b=add(a,1)";
Args* buffs = New_strBuff();
char* pikaAsm = pikaParseToAsm(buffs, line);
printf("%s", pikaAsm);
2021-10-14 15:13:12 +08:00
EXPECT_STREQ(pikaAsm, "1 REF a\n1 NUM 1\n0 RUN add\n0 OUT b\n");
2021-10-14 15:08:45 +08:00
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, add_str) {
char* line = (char*)"b=add(a,'1')";
Args* buffs = New_strBuff();
char* pikaAsm = pikaParseToAsm(buffs, line);
printf("%s", pikaAsm);
2021-10-14 15:13:12 +08:00
EXPECT_STREQ(pikaAsm, "1 REF a\n1 STR 1\n0 RUN add\n0 OUT b\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, deep4) {
char* line = (char*)"b = add(add(add(add(1, 2), 3), 4), 5)";
Args* buffs = New_strBuff();
char* pikaAsm = pikaParseToAsm(buffs, line);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm, "4 NUM 1\n4 NUM 2\n3 RUN add\n3 NUM 3\n2 RUN add\n2 NUM 4\n1 RUN add\n1 NUM 5\n0 RUN add\n0 OUT b\n");
2021-10-14 15:08:45 +08:00
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-14 15:50:42 +08:00
}
TEST(parser, a_1) {
char* line = (char*)"a = 1";
Args* buffs = New_strBuff();
char* pikaAsm = pikaParseToAsm(buffs, line);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm, "0 NUM 1\n0 OUT a\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-13 18:07:08 +08:00
}