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

544 lines
15 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) {
2021-10-16 18:39:09 +08:00
AST* ast = pikaParseLine((char*)"add(a,b)", NULL);
2021-10-13 18:07:08 +08:00
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) {
2021-10-16 18:39:09 +08:00
AST* ast = pikaParseLine((char*)"add( a , b)", NULL);
2021-10-13 18:07:08 +08:00
Args* buffs = New_strBuff();
2021-10-13 22:17:40 +08:00
char* pikaAsm = AST_toPikaAsm(ast, buffs);
printf("%s", pikaAsm);
2021-10-16 18:39:09 +08:00
EXPECT_STREQ(pikaAsm,
"B0\n"
"1 REF a\n"
"1 REF b\n"
"0 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) {
2021-10-16 18:39:09 +08:00
AST* ast = pikaParseLine((char*)"d = add(add(a,b) , c)", NULL);
2021-10-13 21:20:45 +08:00
Args* buffs = New_strBuff();
2021-10-13 22:17:40 +08:00
char* pikaAsm = AST_toPikaAsm(ast, buffs);
printf("%s", pikaAsm);
2021-10-16 18:39:09 +08:00
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");
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) {
2021-10-16 18:39:09 +08:00
AST* ast =
pikaParseLine((char*)"d.p = a.add(b.add(a,se.b) , pmw.c)", NULL);
2021-10-13 21:20:45 +08:00
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-16 18:39:09 +08:00
"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");
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-16 19:09:19 +08:00
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
2021-10-13 22:17:40 +08:00
printf("%s", pikaAsm);
2021-10-16 18:39:09 +08:00
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");
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-16 19:09:19 +08:00
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
2021-10-14 09:51:50 +08:00
printf("%s", pikaAsm);
2021-10-16 18:39:09 +08:00
EXPECT_STREQ(pikaAsm,
"B0\n"
"1 STR 2.322\n"
"0 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-16 19:09:19 +08:00
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
2021-10-14 09:53:37 +08:00
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
2021-10-16 18:39:09 +08:00
"B0\n"
"1 REF a\n"
"2 STR 1\n"
"1 RUN literal\n"
"0 RUN add\n"
"0 OUT b\n");
2021-10-14 09:53:37 +08:00
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();
2021-10-16 19:09:19 +08:00
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
2021-10-14 15:08:45 +08:00
printf("%s", pikaAsm);
2021-10-16 18:39:09 +08:00
EXPECT_STREQ(pikaAsm,
"B0\n"
"1 REF a\n"
"1 NUM 1\n"
"0 RUN add\n"
"0 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();
2021-10-16 19:09:19 +08:00
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
2021-10-14 15:08:45 +08:00
printf("%s", pikaAsm);
2021-10-16 18:39:09 +08:00
EXPECT_STREQ(pikaAsm,
"B0\n"
"1 REF a\n"
"1 STR 1\n"
"0 RUN add\n"
"0 OUT b\n");
2021-10-14 15:13:12 +08:00
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();
2021-10-16 19:09:19 +08:00
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
2021-10-14 15:13:12 +08:00
printf("%s", pikaAsm);
2021-10-16 18:39:09 +08:00
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");
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();
2021-10-16 19:09:19 +08:00
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
2021-10-14 15:50:42 +08:00
printf("%s", pikaAsm);
2021-10-16 18:39:09 +08:00
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();
2021-10-16 19:09:19 +08:00
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
2021-10-16 18:39:09 +08:00
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"0 REF true\n"
"0 JEZ 2\n");
2021-10-14 15:50:42 +08:00
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-16 19:52:19 +08:00
}
2021-10-17 16:14:01 +08:00
static char* parse(const char* line,
Args* outBuffs,
char* pikaAsm,
Stack* blockStack) {
2021-10-17 18:16:23 +08:00
printf("%s\n", line);
2021-10-17 16:14:01 +08:00
Args* runBuffs = New_strBuff();
pikaAsm = strsAppend(runBuffs, pikaAsm,
pikaParseLineToAsm(runBuffs, (char*)line, blockStack));
pikaAsm = strsCopy(outBuffs, pikaAsm);
args_deinit(runBuffs);
return pikaAsm;
}
2021-10-16 19:52:19 +08:00
TEST(parser, while_true_block) {
2021-10-17 16:14:01 +08:00
Args* bf = New_strBuff();
Stack* bs = New_Stack();
char* s = strsCopy(bf, (char*)"");
s = parse("while true:", bf, s, bs);
s = parse(" rgb.flow()", bf, s, bs);
s = parse("", bf, s, bs);
printf("%s", s);
EXPECT_STREQ(s,
2021-10-16 19:52:19 +08:00
"B0\n"
"0 REF true\n"
"0 JEZ 2\n"
"B1\n"
"0 RUN rgb.flow\n"
"B0\n"
"0 JMP -1\n"
"B0\n");
2021-10-17 16:14:01 +08:00
stack_deinit(bs);
args_deinit(bf);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, while_true_false) {
Args* bf = New_strBuff();
Stack* bs = New_Stack();
char* s = strsCopy(bf, (char*)"");
s = parse("while true:", bf, s, bs);
s = parse(" rgb.flow()", bf, s, bs);
s = parse(" while false:", bf, s, bs);
s = parse(" a=3", bf, s, bs);
s = parse(" test.on(add(2,3))", bf, s, bs);
s = parse(" print('flowing')", bf, s, bs);
s = parse("", bf, s, bs);
2021-10-17 17:57:02 +08:00
Arg* buffArg = arg_setStr(NULL, (char*)"", s);
stack_deinit(bs);
args_deinit(bf);
s = arg_getStr(buffArg);
2021-10-17 16:14:01 +08:00
printf("%s", s);
EXPECT_STREQ(s,
"B0\n"
"0 REF true\n"
"0 JEZ 2\n"
"B1\n"
"0 RUN rgb.flow\n"
"B1\n"
"0 REF false\n"
"0 JEZ 2\n"
"B2\n"
"0 NUM 3\n"
"0 OUT a\n"
"B2\n"
"2 NUM 2\n"
"2 NUM 3\n"
"1 RUN add\n"
"0 RUN test.on\n"
"B1\n"
"0 JMP -1\n"
"B1\n"
"1 STR flowing\n"
"0 RUN print\n"
"B0\n"
"0 JMP -1\n"
"B0\n");
2021-10-17 17:57:02 +08:00
arg_deinit(buffArg);
2021-10-17 16:14:01 +08:00
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, while_true_false_both_exit) {
Args* bf = New_strBuff();
Stack* bs = New_Stack();
char* s = strsCopy(bf, (char*)"");
s = parse("while true:", bf, s, bs);
s = parse(" rgb.flow()", bf, s, bs);
s = parse(" while false:", bf, s, bs);
s = parse(" a=3", bf, s, bs);
s = parse(" test.on(add(2,3))", bf, s, bs);
s = parse("", bf, s, bs);
printf("%s", s);
EXPECT_STREQ(s,
"B0\n"
"0 REF true\n"
"0 JEZ 2\n"
"B1\n"
"0 RUN rgb.flow\n"
"B1\n"
"0 REF false\n"
"0 JEZ 2\n"
"B2\n"
"0 NUM 3\n"
"0 OUT a\n"
"B2\n"
"2 NUM 2\n"
"2 NUM 3\n"
"1 RUN add\n"
"0 RUN test.on\n"
"B1\n"
"0 JMP -1\n"
"B0\n"
"0 JMP -1\n"
"B0\n");
stack_deinit(bs);
args_deinit(bf);
2021-10-16 19:52:19 +08:00
EXPECT_EQ(pikaMemNow(), 0);
2021-10-17 18:08:14 +08:00
}
TEST(parser, if_) {
Args* bf = New_strBuff();
Stack* bs = New_Stack();
char* s = strsCopy(bf, (char*)"");
s = parse("if true:", bf, s, bs);
s = parse(" rgb.flow()", bf, s, bs);
s = parse("", bf, s, bs);
printf("%s", s);
EXPECT_STREQ(s,
"B0\n"
"0 REF true\n"
"0 JEZ 1\n"
"B1\n"
"0 RUN rgb.flow\n"
"B0\n");
stack_deinit(bs);
args_deinit(bf);
EXPECT_EQ(pikaMemNow(), 0);
}
2021-10-17 20:05:47 +08:00
extern PikaMemInfo pikaMemInfo;
2021-10-17 18:08:14 +08:00
TEST(parser, while_true_if_false_both_exit) {
2021-10-17 20:05:47 +08:00
pikaMemInfo.heapUsedMax = 0;
2021-10-17 18:08:14 +08:00
Args* bf = New_strBuff();
Stack* bs = New_Stack();
char* s = strsCopy(bf, (char*)"");
s = parse("while true:", bf, s, bs);
s = parse(" rgb.flow()", bf, s, bs);
s = parse(" if false:", bf, s, bs);
s = parse(" a=3", bf, s, bs);
s = parse(" test.on(add(2,3))", bf, s, bs);
s = parse("", bf, s, bs);
printf("%s", s);
EXPECT_STREQ(s,
"B0\n"
"0 REF true\n"
"0 JEZ 2\n"
"B1\n"
"0 RUN rgb.flow\n"
"B1\n"
"0 REF false\n"
"0 JEZ 1\n"
"B2\n"
"0 NUM 3\n"
"0 OUT a\n"
"B2\n"
"2 NUM 2\n"
"2 NUM 3\n"
"1 RUN add\n"
"0 RUN test.on\n"
"B0\n"
"0 JMP -1\n"
"B0\n");
stack_deinit(bs);
args_deinit(bf);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-17 19:07:33 +08:00
}
TEST(parser, multiLine) {
2021-10-17 19:35:50 +08:00
pikaMemInfo.heapUsedMax = 0;
2021-10-17 19:07:33 +08:00
Args* buffs = New_strBuff();
char* lines =(char *)
"while true:\n"
" rgb.flow()\n"
" if false:\n"
" a=3\n"
" test.on(add(2,3))\n"
"\n";
printf("%s", lines);
char* pikaAsm = pikaParseMultiLineToAsm(buffs, (char*)lines);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"0 REF true\n"
"0 JEZ 2\n"
"B1\n"
"0 RUN rgb.flow\n"
"B1\n"
"0 REF false\n"
"0 JEZ 1\n"
"B2\n"
"0 NUM 3\n"
"0 OUT a\n"
"B2\n"
"2 NUM 2\n"
"2 NUM 3\n"
"1 RUN add\n"
"0 RUN test.on\n"
"B0\n"
"0 JMP -1\n"
"B0\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-19 08:27:09 +08:00
}
TEST(parser, pikaPi) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
const char lines[] =
"\n"
"time = STM32.Time()\n"
"uart = STM32.UART()\n"
"adc = STM32.ADC()\n"
"pin = STM32.GPIO()\n"
"pwm = STM32.PWM()\n"
"uart = STM32.UART()\n"
"rgb = PikaPiZero.RGB()\n"
"mem = PikaStdLib.MemChecker()\n"
"op = PikaMath.Operator()\n"
"\n"
"uart.init()\n"
"uart.setId(1)\n"
"uart.setBaudRate(115200)\n"
"uart.enable()\n"
"\n"
"rgb.init()\n"
"rgb.enable()\n"
"\n"
"print('hello 2')\n"
"print('mem used max:')\n"
"mem.max() \n"
"\n"
"while True:\n"
" time.sleep_ms(10)\n"
" rgb.flow()\n"
" print('flowing')\n"
"\n"
"\n"
"\n"
"\n";
printf("%s", lines);
char* pikaAsm = pikaParseMultiLineToAsm(buffs, (char*)lines);
printf("mem max in parse: %0.2f Kb\n", pikaMemMax() / 1024.0);
printf("%s", pikaAsm);
2021-10-20 20:34:23 +08:00
EXPECT_STREQ(pikaAsm,
"B0\n"
"B0\n"
"0 RUN STM32.Time\n"
"0 OUT time\n"
"B0\n"
"0 RUN STM32.UART\n"
"0 OUT uart\n"
"B0\n"
"0 RUN STM32.ADC\n"
"0 OUT adc\n"
"B0\n"
"0 RUN STM32.GPIO\n"
"0 OUT pin\n"
"B0\n"
"0 RUN STM32.PWM\n"
"0 OUT pwm\n"
"B0\n"
"0 RUN STM32.UART\n"
"0 OUT uart\n"
"B0\n"
"0 RUN PikaPiZero.RGB\n"
"0 OUT rgb\n"
"B0\n"
"0 RUN PikaStdLib.MemChecker\n"
"0 OUT mem\n"
"B0\n"
"0 RUN PikaMath.Operator\n"
"0 OUT op\n"
"B0\n"
"B0\n"
"0 RUN uart.init\n"
"B0\n"
"1 NUM 1\n"
"0 RUN uart.setId\n"
"B0\n"
"1 NUM 115200\n"
"0 RUN uart.setBaudRate\n"
"B0\n"
"0 RUN uart.enable\n"
"B0\n"
"B0\n"
"0 RUN rgb.init\n"
"B0\n"
"0 RUN rgb.enable\n"
"B0\n"
"B0\n"
"1 STR hello 2\n"
"0 RUN print\n"
"B0\n"
"1 STR mem used max:\n"
"0 RUN print\n"
"B0\n"
"0 RUN mem.max\n"
"B0\n"
"B0\n"
"0 REF True\n"
"0 JEZ 2\n"
"B1\n"
"1 NUM 10\n"
"0 RUN time.sleep_ms\n"
"B1\n"
"0 RUN rgb.flow\n"
"B1\n"
"1 STR flowing\n"
"0 RUN print\n"
"B0\n"
"0 JMP -1\n"
"B0\n"
"B0\n"
"B0\n"
"B0\n");
2021-10-19 08:27:09 +08:00
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
2021-10-20 21:42:32 +08:00
TEST(parser, add) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
char* lines = (char*)"a = 1 + 1\n";
printf("%s", lines);
char* pikaAsm = pikaParseMultiLineToAsm(buffs, (char*)lines);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"1 NUM 1\n"
"1 NUM 1\n"
"0 OPT +\n"
"0 OUT a\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}