Quellcode durchsuchen

feat: 允许import文件夹和执行文件夹

import文件夹时,自动访问__init__.vm文件
执行文件夹时,自动访问__main__.vm文件
可以对文件路径末尾的路径分隔符做处理

link #5
SongZihuan vor 4 Jahren
Ursprung
Commit
d2595b41b7
4 geänderte Dateien mit 38 neuen und 14 gelöschten Zeilen
  1. 3 3
      VirtulMathCore/file/file.c
  2. 1 1
      VirtulMathCore/include/file.h
  3. 23 9
      VirtulMathCore/src/runfile.c
  4. 11 1
      src/virtualmath.c

+ 3 - 3
VirtulMathCore/file/file.c

@@ -5,15 +5,15 @@
  * @param dir 文件地址
  * @return 0-错误, 1-普通文件, 2-目录
  */
-int checkFile(char *dir){
+int checkFileReadble(char *dir){
     struct stat my_stat;
     int status;
     if (dir == NULL)
         return 3;
     status = stat(dir, &my_stat);
     if (status != 0)
-        return 0;
-    else if (S_ISREG(my_stat.st_mode))
+        return 3;
+    else if (S_ISREG(my_stat.st_mode))  // 普通文件
         return 1;
     else if (S_ISDIR(my_stat.st_mode))
         return 2;

+ 1 - 1
VirtulMathCore/include/file.h

@@ -1,7 +1,7 @@
 #ifndef VIRTUALMATH_FILE_H
 #define VIRTUALMATH_FILE_H
 
-int checkFile(char *dir);
+int checkFileReadble(char *dir);
 char *splitDir(char * dir);
 
 #endif //VIRTUALMATH_FILE_H

+ 23 - 9
VirtulMathCore/src/runfile.c

@@ -23,12 +23,27 @@ int isAbsolutePath(const char *path) {
     }
 }
 
-bool isExist(char **path, bool is_ab) {
+static bool isExist(char **path, bool is_ab, char *file) {
     char *backup = is_ab ? memStrcpy((*path) + 1) : memStrcpy(*path);
-    if (checkFile(backup) == 1 || checkFile(backup = memStrcat(backup, ".vm", true, false)) == 1) {
+    int status;
+    if ((status = checkFileReadble(backup)) != 3 || (status = checkFileReadble(backup = memStrcat(backup, ".vm", true, false))) != 3) {
         memFree(*path);
         *path = backup;
-        return true;
+        if (status == 2) {
+            if (file == NULL)
+                return false;
+#if __linux__
+            if ((*path)[memStrlen(*path) - 1] != '/')
+                *path = memStrcat(*path, "/", true, false);
+            *path = memStrcat(*path, file, true, false);
+#else
+            if ((*path)[memStrlen(*path) - 1] != '\\')
+                *path = memStrcat(*path, "\\", true, false);
+            *path = memStrcat(*path, file, true, false);
+#endif
+            return isExist(path, false, NULL);
+        } else
+            return true;
     }
     memFree(backup);
     return false;
@@ -37,7 +52,7 @@ bool isExist(char **path, bool is_ab) {
 int checkFileDir(char **file_dir, INTER_FUNCTIONSIG) {
     switch (isAbsolutePath(*file_dir)) {
         case 1:
-            if (isExist(file_dir, true))
+            if (isExist(file_dir, true, "__init__.vm"))
                 return 1;
             goto error_;
         case 2:
@@ -48,20 +63,19 @@ int checkFileDir(char **file_dir, INTER_FUNCTIONSIG) {
             break;
     }
 
-    if (isExist(file_dir, false))
+    if (isExist(file_dir, false, "__init__.vm"))
         return 1;
 
     {
         char arr_cwd[200] = {};
         char *p_cwd = NULL;
-#ifdef __linux__
         getcwd(arr_cwd, 200);
+#ifdef __linux__
         p_cwd = memStrcatIter(arr_cwd, false, "/", *file_dir, NULL);
 #else
-        _getcwd(arr_cwd, 200);
         p_cwd = memStrcatIter(arr_cwd, false, "\\", *file_dir);
 #endif
-        if (isExist(&p_cwd, false)) {
+        if (isExist(&p_cwd, false, "__init__.vm")) {
             memFree(*file_dir);
             *file_dir = p_cwd;
             return 1;
@@ -82,7 +96,7 @@ int checkFileDir(char **file_dir, INTER_FUNCTIONSIG) {
             else
                 new_dir = memStrcat(tmp, *file_dir, false, false);
 
-            if (isExist(&new_dir, false)) {
+            if (isExist(&new_dir, false, "__init__.vm")) {
                 memFree(*file_dir);
                 *file_dir = new_dir;
                 return 1;

+ 11 - 1
src/virtualmath.c

@@ -6,8 +6,18 @@ void runCodeFile(Inter *inter, char *file[]) {
     bool should_break = false;
     setResultCore(&result);
     for (PASS; !should_break && *file != NULL; file++) {
-        if (checkFile((*file)) != 1)
+        int status;
+        if ((status = checkFileReadble((*file))) == 3)
             continue;
+        else if (status == 2) {
+#if __linux__
+            *file = memStrcat(*file, (*file)[memStrlen(*file) - 1] != '/' ? "/__main__.vm" : "__main__.vm", false, false);
+#else
+            *file = memStrcat(*file, (*file)[memStrlen(*file) - 1] != '\\' ? "\\__main__.vm" : "__main__.vm", false, false);
+#endif
+            if (checkFileReadble(*file) != 1)
+                continue;
+        }
         if (runParser(*file, inter, false, &pst)) {
             globalIterStatement(&result, inter, pst);
             if (result.type == error_return) {