Selaa lähdekoodia

feat: 字面量匹配使用正则表达式

SongZihuan 3 vuotta sitten
vanhempi
sitoutus
8bd5cfbb65
3 muutettua tiedostoa jossa 25 lisäystä ja 5 poistoa
  1. 3 0
      include/env.h
  2. 17 5
      src/core/env.c
  3. 5 0
      src/main.c

+ 3 - 0
include/env.h

@@ -62,4 +62,7 @@ char *findEnvVar(char *name, af_Environment *env);
 /* 顶层消息处理器管理函数 */
 void addTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func, af_Environment *env);
 bool changeTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func, af_Environment *env);
+
+/* LiteralRegex操作函数 */
+bool pushLiteralRegex(char *pattern, char *func, bool in_protect, af_Environment *env);
 #endif //AFUN__ENV_H_PUBLIC

+ 17 - 5
src/core/env.c

@@ -950,15 +950,27 @@ static void freeAllLiteralRegex(af_LiteralRegex *lr) {
         lr = freeLiteralRegex(lr);
 }
 
+bool pushLiteralRegex(char *pattern, char *func, bool in_protect, af_Environment *env) {
+    af_LiteralRegex *lr = makeLiteralRegex(pattern, func, in_protect);
+    if (lr == NULL)
+        return false;
+    lr->next = env->core->lr;
+    env->core->lr = lr;
+    return true;
+}
+
 /*
  * 函数名: checkLiteralCode
  * 目标: 检查对象是否为字面量
+ * 注意: func被写入函数名, 但不是复制式写入
  */
-bool checkLiteralCode(char *literal, char **func, bool *in_protect, af_Environment *env) {  // 桩函数
-    if (strncmp(literal, "data", 4) == 0) {
-        *in_protect = true;
-        *func = "func";
-        return true;
+bool checkLiteralCode(char *literal, char **func, bool *in_protect, af_Environment *env) {
+    for (af_LiteralRegex *lr = env->core->lr; lr != NULL; lr = lr->next) {
+        if (matchRegex(literal, lr->rg) == 1) {
+            *func = lr->func;  // 不使用复制
+            *in_protect = lr->in_protect;
+            return true;
+        }
     }
     return false;
 }

+ 5 - 0
src/main.c

@@ -374,6 +374,11 @@ int main() {
     printf("Hello World\n");
 
     af_Environment *env = makeEnvironment(grt_always);
+    if(!pushLiteralRegex("data.*", "func", true, env)) {
+        fprintf(stderr, "pushLiteralRegex Error\n");
+        return 1;
+    }
+
     {
         af_ObjectAPI *api = makeObjectAPI();
         af_Object *obj;