pikapython/port/linux/test/parse-test.cpp
2021-10-16 19:09:19 +08:00

195 lines
5.3 KiB
C

#include "gtest/gtest.h"
extern "C" {
#include "BaseObj.h"
#include "PikaParser.h"
#include "dataMemory.h"
#include "dataStrs.h"
}
TEST(parser, NEW) {
AST* ast = pikaParseLine((char*)"add(a,b)", NULL);
Args* buffs = New_strBuff();
char* pikaAsm = AST_toPikaAsm(ast, buffs);
printf("%s", pikaAsm);
args_deinit(buffs);
AST_deinit(ast);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, add_a_b) {
AST* ast = pikaParseLine((char*)"add( a , b)", NULL);
Args* buffs = New_strBuff();
char* pikaAsm = AST_toPikaAsm(ast, buffs);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"1 REF a\n"
"1 REF b\n"
"0 RUN add\n");
args_deinit(buffs);
AST_deinit(ast);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, add_a_b_c) {
AST* ast = pikaParseLine((char*)"d = add(add(a,b) , c)", NULL);
Args* buffs = New_strBuff();
char* pikaAsm = AST_toPikaAsm(ast, buffs);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"2 REF a\n"
"2 REF b\n"
"1 RUN add\n"
"1 REF c\n"
"0 RUN add\n"
"0 OUT d\n");
args_deinit(buffs);
AST_deinit(ast);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, method1) {
AST* ast =
pikaParseLine((char*)"d.p = a.add(b.add(a,se.b) , pmw.c)", NULL);
Args* buffs = New_strBuff();
char* pikaAsm = AST_toPikaAsm(ast, buffs);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"2 REF a\n"
"2 REF se.b\n"
"1 RUN b.add\n"
"1 REF pmw.c\n"
"0 RUN "
"a.add\n"
"0 OUT d.p\n");
args_deinit(buffs);
AST_deinit(ast);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, method2) {
char* line = (char*)"d.p = a.add(b.add(a,se.b,diek(pp)) , pmw.c())";
Args* buffs = New_strBuff();
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"2 REF a\n"
"2 REF se.b\n"
"3 REF pp\n"
"2 RUN diek\n"
"1 RUN b.add\n"
"1 RUN pmw.c\n"
"0 RUN a.add\n"
"0 OUT d.p\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, str1) {
char* line = (char*)"literal('2.322')";
Args* buffs = New_strBuff();
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"1 STR 2.322\n"
"0 RUN literal\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, str2) {
char* line = (char*)"b=add(a,literal('1'))";
Args* buffs = New_strBuff();
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"1 REF a\n"
"2 STR 1\n"
"1 RUN literal\n"
"0 RUN add\n"
"0 OUT b\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, num1) {
char* line = (char*)"b=add(a,1)";
Args* buffs = New_strBuff();
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"1 REF a\n"
"1 NUM 1\n"
"0 RUN add\n"
"0 OUT b\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, add_str) {
char* line = (char*)"b=add(a,'1')";
Args* buffs = New_strBuff();
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"1 REF a\n"
"1 STR 1\n"
"0 RUN add\n"
"0 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 = pikaParseLineToAsm(buffs, line, NULL);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"4 NUM 1\n"
"4 NUM 2\n"
"3 RUN add\n"
"3 NUM 3\n"
"2 RUN add\n"
"2 NUM 4\n"
"1 RUN add\n"
"1 NUM 5\n"
"0 RUN add\n"
"0 OUT b\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, a_1) {
char* line = (char*)"a = 1";
Args* buffs = New_strBuff();
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"0 NUM 1\n"
"0 OUT a\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, while_true) {
char* line = (char*)"while true:";
Args* buffs = New_strBuff();
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"0 REF true\n"
"0 JEZ 2\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}