Skip to content

Commit 294bc6c

Browse files
committed
fix if:import as parse issue
1 parent e7e237e commit 294bc6c

8 files changed

Lines changed: 60 additions & 4 deletions

File tree

examples/builtins/import.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if True:
2+
import PikaStdLib as std
3+
4+
5+
print("PASS")

port/linux/.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// "--gtest_filter=stddata.encode_decode"
2121
// "--gtest_filter=packtool.packfiles_txt"
2222
// "--gtest_filter=cmodule.class_attr_obj"
23-
// "--gtest_filter=event.event_thread"
23+
"--gtest_filter=parser.try_import_as"
2424
],
2525
"stopAtEntry": false,
2626
"cwd": "${workspaceFolder}",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if True:
2+
import PikaStdLib as std
3+
4+
5+
print("PASS")

src/PikaParser.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,8 +2024,8 @@ AST* AST_parseStmt(AST* ast, char* sStmt) {
20242024

20252025
static int32_t Parser_getPyLineBlockDeepth(char* sLine) {
20262026
int32_t iSpaceNum = strGetIndent(sLine);
2027-
if (0 == iSpaceNum % 4) {
2028-
return iSpaceNum / 4;
2027+
if (0 == iSpaceNum % PIKA_BLOCK_SPACE) {
2028+
return iSpaceNum / PIKA_BLOCK_SPACE;
20292029
}
20302030
/* space Num is not 4N, error*/
20312031
return -1;
@@ -2708,6 +2708,11 @@ static char* Suger_import(Args* outbuffs, char* sLine) {
27082708

27092709
static char* Parser_sugerProcess(Args* outbuffs, char* sLine) {
27102710
/* process import */
2711+
int32_t block_deepth = Parser_getPyLineBlockDeepth(sLine);
2712+
if (block_deepth < 0) {
2713+
return NULL;
2714+
}
2715+
sLine = sLine + block_deepth * PIKA_BLOCK_SPACE;
27112716
sLine = Suger_import(outbuffs, sLine);
27122717
/* process multi assign */
27132718
int iLineNum = strCountSign(sLine, '\n') + 1;
@@ -2721,6 +2726,8 @@ static char* Parser_sugerProcess(Args* outbuffs, char* sLine) {
27212726
aLine = arg_strAppend(aLine, sSingleLine);
27222727
}
27232728
sLine = strsCopy(outbuffs, arg_getStr(aLine));
2729+
sLine =
2730+
strsAddIndentation(outbuffs, sLine, block_deepth * PIKA_BLOCK_SPACE);
27242731
if (NULL != aLine) {
27252732
arg_deinit(aLine);
27262733
}

src/PikaParser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ typedef char* (*fn_parser_Ast2Target)(Parser* self, AST* ast);
9393
typedef char* (*fn_parser_Lines2Target)(Parser* self, char* sPyLines);
9494

9595
#define _VAL_NEED_INIT -1
96+
#define PIKA_BLOCK_SPACE 4
9697

9798
struct Parser {
9899
Args lineBuffs;

src/PikaVersion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
#define PIKA_VERSION_MINOR 13
33
#define PIKA_VERSION_MICRO 2
44

5-
#define PIKA_EDIT_TIME "2024/01/15 13:04:27"
5+
#define PIKA_EDIT_TIME "2024/01/28 12:20:58"

src/dataStrs.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,40 @@ char* strsRepeat(Args* buffs, char* str, int num) {
327327
buff[size] = '\0';
328328
return buff;
329329
}
330+
331+
static void _add_indentation(const char* input, int spaces, char* output) {
332+
if (input == NULL || output == NULL || spaces < 0) {
333+
return;
334+
}
335+
336+
const char* current = input;
337+
int output_index = 0;
338+
339+
// Iterate over the input string
340+
while (*current) {
341+
// Add spaces at the beginning of each new line
342+
if (current == input || *(current - 1) == '\n') {
343+
for (int i = 0; i < spaces; ++i) {
344+
output[output_index++] = ' ';
345+
}
346+
}
347+
// Copy the current character
348+
output[output_index++] = *current++;
349+
}
350+
351+
// Add the null terminator to the string
352+
output[output_index] = '\0';
353+
}
354+
355+
char* strsAddIndentation(Args* buffs, char* str, int spaces) {
356+
int lines = 1;
357+
for (const char* current = str; *current; current++) {
358+
if (*current == '\n') {
359+
lines++;
360+
}
361+
}
362+
int output_length = strGetSize(str) + spaces * lines;
363+
char* buff = args_getBuff(buffs, output_length + 1);
364+
_add_indentation(str, spaces, buff);
365+
return buff;
366+
}

src/dataStrs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ char* strsFilePreProcess(Args* outbuffs, char* lines);
5858
char* strsFilePreProcess_ex(Args* outbuffs, char* lines, char* endwith);
5959
char* strsSubStr(Args* buffs_p, char* name_start, char* name_end);
6060
char* strsRepeat(Args* buffs, char* str, int num);
61+
char* strsAddIndentation(Args* buffs, char* str, int spaces);
6162

6263
#endif
6364
#ifdef __cplusplus

0 commit comments

Comments
 (0)