Просмотр исходного кода

fix `if:import as` parse issue

lyon1998 2 лет назад
Родитель
Сommit
294bc6cd73

+ 5 - 0
examples/builtins/import.py

@@ -0,0 +1,5 @@
+if True:
+    import PikaStdLib as std
+
+
+print("PASS")

+ 1 - 1
port/linux/.vscode/launch.json

@@ -20,7 +20,7 @@
                 // "--gtest_filter=stddata.encode_decode"
                 // "--gtest_filter=packtool.packfiles_txt"
                 // "--gtest_filter=cmodule.class_attr_obj"
-                // "--gtest_filter=event.event_thread"
+                "--gtest_filter=parser.try_import_as"
             ],
             "stopAtEntry": false,
             "cwd": "${workspaceFolder}",

+ 5 - 0
port/linux/test/python/builtins/import.py

@@ -0,0 +1,5 @@
+if True:
+    import PikaStdLib as std
+
+
+print("PASS")

+ 9 - 2
src/PikaParser.c

@@ -2024,8 +2024,8 @@ __exit:
 
 static int32_t Parser_getPyLineBlockDeepth(char* sLine) {
     int32_t iSpaceNum = strGetIndent(sLine);
-    if (0 == iSpaceNum % 4) {
-        return iSpaceNum / 4;
+    if (0 == iSpaceNum % PIKA_BLOCK_SPACE) {
+        return iSpaceNum / PIKA_BLOCK_SPACE;
     }
     /* space Num is not 4N, error*/
     return -1;
@@ -2708,6 +2708,11 @@ static char* Suger_import(Args* outbuffs, char* sLine) {
 
 static char* Parser_sugerProcess(Args* outbuffs, char* sLine) {
     /* process import */
+    int32_t block_deepth = Parser_getPyLineBlockDeepth(sLine);
+    if (block_deepth < 0) {
+        return NULL;
+    }
+    sLine = sLine + block_deepth * PIKA_BLOCK_SPACE;
     sLine = Suger_import(outbuffs, sLine);
     /* process multi assign */
     int iLineNum = strCountSign(sLine, '\n') + 1;
@@ -2721,6 +2726,8 @@ static char* Parser_sugerProcess(Args* outbuffs, char* sLine) {
         aLine = arg_strAppend(aLine, sSingleLine);
     }
     sLine = strsCopy(outbuffs, arg_getStr(aLine));
+    sLine =
+        strsAddIndentation(outbuffs, sLine, block_deepth * PIKA_BLOCK_SPACE);
     if (NULL != aLine) {
         arg_deinit(aLine);
     }

+ 1 - 0
src/PikaParser.h

@@ -93,6 +93,7 @@ typedef char* (*fn_parser_Ast2Target)(Parser* self, AST* ast);
 typedef char* (*fn_parser_Lines2Target)(Parser* self, char* sPyLines);
 
 #define _VAL_NEED_INIT -1
+#define PIKA_BLOCK_SPACE 4
 
 struct Parser {
     Args lineBuffs;

+ 1 - 1
src/PikaVersion.h

@@ -2,4 +2,4 @@
 #define PIKA_VERSION_MINOR 13
 #define PIKA_VERSION_MICRO 2
 
-#define PIKA_EDIT_TIME "2024/01/15 13:04:27"
+#define PIKA_EDIT_TIME "2024/01/28 12:20:58"

+ 37 - 0
src/dataStrs.c

@@ -327,3 +327,40 @@ char* strsRepeat(Args* buffs, char* str, int num) {
     buff[size] = '\0';
     return buff;
 }
+
+static void _add_indentation(const char* input, int spaces, char* output) {
+    if (input == NULL || output == NULL || spaces < 0) {
+        return;
+    }
+
+    const char* current = input;
+    int output_index = 0;
+
+    // Iterate over the input string
+    while (*current) {
+        // Add spaces at the beginning of each new line
+        if (current == input || *(current - 1) == '\n') {
+            for (int i = 0; i < spaces; ++i) {
+                output[output_index++] = ' ';
+            }
+        }
+        // Copy the current character
+        output[output_index++] = *current++;
+    }
+
+    // Add the null terminator to the string
+    output[output_index] = '\0';
+}
+
+char* strsAddIndentation(Args* buffs, char* str, int spaces) {
+    int lines = 1;
+    for (const char* current = str; *current; current++) {
+        if (*current == '\n') {
+            lines++;
+        }
+    }
+    int output_length = strGetSize(str) + spaces * lines;
+    char* buff = args_getBuff(buffs, output_length + 1);
+    _add_indentation(str, spaces, buff);
+    return buff;
+}

+ 1 - 0
src/dataStrs.h

@@ -58,6 +58,7 @@ char* strsFilePreProcess(Args* outbuffs, char* lines);
 char* strsFilePreProcess_ex(Args* outbuffs, char* lines, char* endwith);
 char* strsSubStr(Args* buffs_p, char* name_start, char* name_end);
 char* strsRepeat(Args* buffs, char* str, int num);
+char* strsAddIndentation(Args* buffs, char* str, int spaces);
 
 #endif
 #ifdef __cplusplus