support multiline comment

This commit is contained in:
lyon1998 2022-03-31 13:36:44 +08:00
parent 454034ac2a
commit 6e7961d6bb
2 changed files with 85 additions and 0 deletions

View File

@ -2559,3 +2559,50 @@ TEST(parser, multiLine_import) {
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, multiLine_comment) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
char* lines =(char *)
"'''\n"
"a = 1\n"
"'''\n"
"while true:\n"
" rgb.flow()\n"
" ''' \n"
" a = 1\n"
" ''' \n"
" if false:\n"
" \"\"\" \n"
" a = 1\n"
" a = 1\n"
" \"\"\"\n"
" a=3\n"
" test.on(add(2,3))\n"
"\n";
printf("%s", lines);
char* pikaAsm = Parser_multiLineToAsm(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);
}

View File

@ -1179,6 +1179,31 @@ static int Parser_isVoidLine(char* line) {
return 1;
}
static uint8_t Parser_checkIsMultiComment(char* line) {
for (uint32_t i = 0; i < strGetSize(line); i++) {
/* not match ' or " */
if ((line[i] != '\'') && (line[i] != '"')) {
continue;
}
/* not match ''' or """ */
if (!((line[i + 1] == line[i]) && (line[i + 2] == line[i]))) {
continue;
}
/* check char befor the ''' or """ */
if (!((0 == i) || (line[i - 1] == ' '))) {
continue;
}
/* check char after the ''' or """ */
if (!((line[i + 3] == ' ') || (line[i + 3] == 0))) {
continue;
}
/* mached */
return 1;
}
/* not mached */
return 0;
}
static char* Parser_parsePyLines(Args* outBuffs,
ByteCodeFrame* bytecode_frame,
char* py_lines) {
@ -1189,6 +1214,7 @@ static char* Parser_parsePyLines(Args* outBuffs,
uint32_t lines_size = strGetSize(py_lines);
uint16_t lines_num = strCountSign(py_lines, '\n');
uint16_t lines_index = 0;
uint8_t is_in_multi_comment = 0;
char* out_ASM = NULL;
uint32_t line_size = 0;
/* parse each line */
@ -1203,6 +1229,18 @@ static char* Parser_parsePyLines(Args* outBuffs,
goto next_line;
}
}
/* filter for multiline comment ''' or """ */
if (Parser_checkIsMultiComment(line)) {
is_in_multi_comment = ~is_in_multi_comment;
goto next_line;
}
/* skipe multiline comment */
if (is_in_multi_comment) {
goto next_line;
}
/* parse single Line to Asm */
char* single_ASM = Parser_LineToAsm(&buffs, line, &block_stack);
if (NULL == single_ASM) {