瀏覽代碼

feat: 添加LiteralRegex模块

SongZihuan 3 年之前
父節點
當前提交
204eab6c67
共有 2 個文件被更改,包括 50 次插入3 次删除
  1. 15 3
      src/core/__env.h
  2. 35 0
      src/core/env.c

+ 15 - 3
src/core/__env.h

@@ -9,6 +9,7 @@ typedef struct af_EnvVarSpace af_EnvVarSpace;
 typedef struct af_EnvVar af_EnvVar;
 typedef struct af_TopMsgProcess af_TopMsgProcess;
 typedef struct af_LiteralDataList af_LiteralDataList;
+typedef struct af_LiteralRegex af_LiteralRegex;
 
 #include "env.h"
 #include "__object.h"
@@ -16,6 +17,7 @@ typedef struct af_LiteralDataList af_LiteralDataList;
 #include "__code.h"
 #include "__gc.h"
 #include "__func.h"
+#include "regex.h"
 
 #define DEFAULT_GC_COUNT_MAX (10)
 #define ENV_VAR_HASH_SIZE (8)
@@ -28,7 +30,7 @@ struct af_Core {  // 解释器核心
         core_normal,  // 正常执行
     } status;
 
-    // GC基本信息
+    /* GC基本信息 */
     struct af_ObjectData *gc_ObjectData;
     struct af_Object *gc_Object;
     struct af_Var *gc_Var;
@@ -37,11 +39,14 @@ struct af_Core {  // 解释器核心
     size_t gc_count_max;  // gc计数最大值
     enum GcRunTime gc_run;
 
-    // 基本对象信息
+    /* 基本对象信息 */
     struct af_Object *global;  // 顶级属对象
 
-    // 保护空间
+    /* 保护空间 */
     struct af_VarSpace *protect;  // 顶级保护变量空间
+
+    /* 字面量基本信息 */
+    af_LiteralRegex *lr;
 };
 
 struct af_Message {
@@ -146,6 +151,13 @@ struct af_Environment {  // 运行环境
     struct af_TopMsgProcess *process;
 };
 
+struct af_LiteralRegex {
+    af_Regex *rg;
+    char *func;  // 调用的函数
+    bool in_protect;  // 是否在protect空间
+    struct af_LiteralRegex *next;
+};
+
 /* Core 管理函数 */
 af_Object *getBaseObjectFromCore(char *name, af_Core *core);
 

+ 35 - 0
src/core/env.c

@@ -38,6 +38,11 @@ static af_TopMsgProcess *findTopMsgProcessFunc(char *type, af_Environment *env);
 static af_LiteralDataList *makeLiteralDataList(char *data);
 static af_LiteralDataList *freeLiteralData_Pri(af_LiteralDataList *ld);
 
+/* LiteralRegex 创建与释放 */
+static af_LiteralRegex *makeLiteralRegex(char *pattern, char *func, bool in_protect);
+static af_LiteralRegex *freeLiteralRegex(af_LiteralRegex *lr);
+static void freeAllLiteralRegex(af_LiteralRegex *lr);
+
 static af_Core *makeCore(enum GcRunTime grt) {
     af_Core *core = calloc(sizeof(af_Core), 1);
     core->status = core_creat;
@@ -55,6 +60,7 @@ static af_Core *makeCore(enum GcRunTime grt) {
 static void freeCore(af_Environment *env) {
     printGCByCode(env->core);
     gc_freeAllValue(env);
+    freeAllLiteralRegex(env->core->lr);
     free(env->core);
 }
 
@@ -919,6 +925,35 @@ void popActivity(bool is_normal, af_Message *msg, af_Environment *env) {
     env->activity = freeActivity(env->activity);
 }
 
+static af_LiteralRegex *makeLiteralRegex(char *pattern, char *func, bool in_protect) {
+    af_Regex *rg = makeRegex(pattern);
+    if (rg == NULL)
+        return NULL;
+
+    af_LiteralRegex *lr = calloc(sizeof(af_LiteralRegex), 1);
+    lr->rg = rg;
+    lr->func = strCopy(func);
+    lr->in_protect = in_protect;
+    return lr;
+}
+
+static af_LiteralRegex *freeLiteralRegex(af_LiteralRegex *lr) {
+    af_LiteralRegex *next = lr->next;
+    freeRegex(lr->rg);
+    free(lr->func);
+    free(lr);
+    return next;
+}
+
+static void freeAllLiteralRegex(af_LiteralRegex *lr) {
+    while (lr != NULL)
+        lr = freeLiteralRegex(lr);
+}
+
+/*
+ * 函数名: checkLiteralCode
+ * 目标: 检查对象是否为字面量
+ */
 bool checkLiteralCode(char *literal, char **func, bool *in_protect, af_Environment *env) {  // 桩函数
     if (strncmp(literal, "data", 4) == 0) {
         *in_protect = true;