Bladeren bron

fix: 调整了import判断文件类型的策略

SongZihuan 4 jaren geleden
bovenliggende
commit
c133994a1f
4 gewijzigde bestanden met toevoegingen van 72 en 29 verwijderingen
  1. 40 3
      README.md
  2. 2 2
      argument/argument.c
  3. 3 3
      vmcore/parser/syntax.c
  4. 27 21
      vmcore/src/runfile.c

+ 40 - 3
README.md

@@ -16,23 +16,27 @@ VirtualMath是基于``C``语言实现的动态强类型解释形编程语言,
 ``cmake``支持参数包括:
 
 1. ``GC``,默认为``ON``状态表示编译GC模块,否则不编译``GC``模块
-2. ``CMAKE_BUILD_TYPE``,指定编译类型,设置为``Debug``的时候可以编译``Debug``程序。
+2. ``SET_DEBUG``,默认为``ON``状态,表示以Debug的形式编译代码。
+3. ``PG``,默认为``OFF``状态,``ON``表示编译时为编译器和连接器添加``-pg``参数,并且将代码编译为静态库。
 
 ## 使用VirtualMath
 
 ``VirtualMath``支持如下命令行参数:
 
 ```
--n 不进入command line模式
+-n --not-run-cl 不进入command line模式
 --stdout xxx 指定标准输出的位置
 --stderr xxx 指定标准错误的位置
 --stdin xxx 指定标准输入的位置
+-l --locale 设置本地化选项
 ```
 
 注意以上设定的标准输入\输出\错误的位置只适用于解释器所运行的``VM``代码。
 
 命令行参数末尾的单独参数被解释为``vm``文件名,将交由``VirtualMathCore``逐一解释执行。
 
+若未使用`-n`选项,则运行完文件代码后会自动进入`command line`模式。
+
 ## VirtualMath语法
 
 ### 字面量
@@ -54,7 +58,7 @@ VirtualMath是基于``C``语言实现的动态强类型解释形编程语言,
 
 ``VirtualMath``支持使用字面量后缀。
 
-如,``123i``等价于``i(123)``。关于回调将会在后面详细说明。
+如,``123i``等价于``i(123)``。关于函数调用将会在后面详细说明。
 
 ### 变量
 
@@ -96,10 +100,41 @@ $(20 + 30)
 $20+30 等价于 ($20) + 30
 ```
 
+#### 关于超级变量的返回值
+
+通过`$(xxx)`获取超级变量的值的时候,分为二种情况。
+
+1. `$(xxx)`的值存在,则直接返回该变量值。如:
+
+   ```
+   $(10) = 20
+   print($(10))  # 20
+   ```
+
+2. `$(xxx)`的值不存在,则直接返回`xxx`。如:
+
+   ```
+   print($(10))  # 10
+   ```
+
+可以通过设定解释器参数的方式来改变这种行为,具体将在后面介绍。
+
 ### 运算
 
 ``VirtualMath``支持如下运算:
 
+索引,切片,函数回调,成员运算(``.``和``->``)
+
+加法、减法
+
+乘法,除法,整除,幂
+
+大于,大于或等于,小于,小于或等于,等于,不等于
+
+位与,位或,按位异或,按位取反,左移,右移
+
+布尔和,布尔或,布尔非
+
 #### 加法和减法
 
 ```
@@ -152,6 +187,8 @@ def f(x, y){
 }
 ```
 
+关于函数定义将在后面介绍。
+
 ### 分支语句
 
 #### 条件分支

+ 2 - 2
argument/argument.c

@@ -46,13 +46,13 @@ int getArgs(const int argc, char **argv)
             case 'n':
                 args.run_commandLine = false;
                 break;
-            case 'p':
+            case 'l':
                 if (optarg != NULL)
                     args.locale = optarg;  // 不需要复制
                 else
                     args.locale = "";
                 break;
-            case 'l':
+            case 'p':
                 args.p_clock = true;
                 break;
             case '?':

+ 3 - 3
vmcore/parser/syntax.c

@@ -394,9 +394,9 @@ int getMatherStatus(LexFile *file, LexMathers *mathers) {
         strMatherMacro(MATHER_BITLEFT, "<<");
         strMatherMacro(MATHER_BITRIGHT, ">>");
 
-        strMatherMacro(MATHER_BOOLAND, "&&");
-        strMatherMacro(MATHER_BOOLOR, "||");
-        charMatherMacro(MATHER_BOOLNOT, '!');
+        strMatherMacro(MATHER_BOOLAND, "and");
+        strMatherMacro(MATHER_BOOLOR, "or");
+        strMatherMacro(MATHER_BOOLNOT, "not");
 
         charMatherMacro(MATHER_ASSIGNMENT, '=');
         charMatherMacro(MATHER_POINT, '.');

+ 27 - 21
vmcore/src/runfile.c

@@ -52,21 +52,27 @@ static bool isExist(char **path, bool is_ab, char *file) {  // is_ab 参数参
 }
 
 #define GOTO_RETURN(num) do{return_num = num; goto return_;}while(0)
-#define CHECK_TYPE(file) do { \
+#define CHECK_TYPE(file) do { /* 判断文件类型 */ \
     void *dl; \
-    if (CHECK_CLIB(file, dl)) { \
-        GOTO_RETURN(2);  /* return 2 表示clib模式 */ \
-    } else \
-        GOTO_RETURN(1);  /* return 1 表示.vm模式 */  \
+    if (eqWide((file) + memStrlen(file) - 3, ".vm")) { \
+        GOTO_RETURN(1);  /* return 2 表示clib模式 */ \
+    } else if (CHECK_CLIB(file, dl)) { \
+        GOTO_RETURN(2);  /* return 1 表示.vm模式 */ \
+    } else { \
+        goto error_; \
+    } \
     goto return_; \
 }while(0)
 
 int checkFileDir(char **file_dir, FUNC) {
     int return_num;
     char *arr_cwd = inter->data.env;
-    char *lib_file = strncmp(*file_dir, "lib", 3) == 0 ? memStrcpy(*file_dir) : memStrcat("libvm", *file_dir, false, false);  // 自动增加libvm前缀
-    if (strstr(lib_file, SHARED_MARK) == NULL)
+    bool diff = false;
+    char *lib_file = strncmp(*file_dir, "lib", 3) == 0 ? memStrcpy(*file_dir) : (diff = true, memStrcat("libvm", *file_dir, false, false));  // 自动增加libvm前缀
+    if (strstr(lib_file, SHARED_MARK) == NULL) {
         lib_file = memStrcat(lib_file, SHARED_MARK, true, false);
+        diff = true;
+    }
 
     switch (isAbsolutePath(*file_dir)) {
         case 1:  // 表示输入的一定是绝对路径
@@ -74,6 +80,7 @@ int checkFileDir(char **file_dir, FUNC) {
                 CHECK_TYPE(*file_dir);
             goto error_;
         case 2:  // 表示仅为clib
+            diff = true;
             goto clib;
         case 3:  // 表示一定是全局包
             goto path;
@@ -89,18 +96,16 @@ int checkFileDir(char **file_dir, FUNC) {
         if (isExist(&p_cwd, false, "__init__.vm")) {
             memFree(*file_dir);
             *file_dir = p_cwd;  // p_cwd 不需要释放
-            GOTO_RETURN(1);
-        }
-        memFree(p_cwd);
-    }
-
-    {
-        void *tmp_dl;
-        char *p_cwd = memStrcatIter(arr_cwd, false, SEP, lib_file, NULL);  // 以NULL结尾表示结束
-        if (CHECK_CLIB(p_cwd, tmp_dl)) {
-            memFree(*file_dir);
-            *file_dir = p_cwd;  // p_cwd 不需要释放
-            GOTO_RETURN(2);
+            CHECK_TYPE(*file_dir);
+        } else if (diff) {  // 检查是否为动态库, 若 lib_file 和 file_dir 一致则不检查
+            void *tmp_dl;
+            memFree(p_cwd);
+            p_cwd = memStrcatIter(arr_cwd, false, SEP, lib_file, NULL);  // 以NULL结尾表示结束
+            if (CHECK_CLIB(p_cwd, tmp_dl)) {
+                memFree(*file_dir);
+                *file_dir = p_cwd;  // p_cwd 不需要释放
+                GOTO_RETURN(2);
+            }
         }
         memFree(p_cwd);
     }
@@ -116,14 +121,15 @@ int checkFileDir(char **file_dir, FUNC) {
                 memFree(*file_dir);
                 *file_dir = new_dir;
                 memFree(path);  // 释放path
-                GOTO_RETURN(1);
+                CHECK_TYPE(*file_dir);
             }
             memFree(new_dir);
         }
         memFree(path);
     }
 
-    clib: {
+    clib:
+    if (diff) {  // 检查是否为动态库, 若 lib_file 和 file_dir 一致则不检查
         void *tmp_dl;
         char *path = memStrcpy(getenv("VIRTUALMATHPATH"));  // 因为 strtok 需要修改path, 所以path不能重复使用
         for (char *tmp = strtok(path, ";"), *new_dir; tmp != NULL; tmp = strtok(NULL, ";")) {