Explorar el Código

feat: 添加FuncInfo模块

SongZihuan hace 3 años
padre
commit
eba5d8dc62
Se han modificado 6 ficheros con 136 adiciones y 3 borrados
  1. 1 1
      include/env.h
  2. 25 0
      include/func.h
  3. 1 1
      include/object.h
  4. 1 1
      include/tool.h
  5. 35 0
      src/core/__func.h
  6. 73 0
      src/core/func.c

+ 1 - 1
include/env.h

@@ -8,7 +8,7 @@ typedef struct af_Environment af_Environment;
 typedef struct af_Message af_Message;
 typedef struct af_Message af_Message;
 
 
 /* 顶层消息处理器的处理函数 DLC */
 /* 顶层消息处理器的处理函数 DLC */
-DEFINE_DLC_SYMBOL(TopMsgProcessFunc, TopMsgProcessFunc);
+DEFINE_DLC_SYMBOL(TopMsgProcessFunc);
 
 
 #include "code.h"
 #include "code.h"
 #include "object.h"
 #include "object.h"

+ 25 - 0
include/func.h

@@ -3,8 +3,23 @@
 #include "code.h"
 #include "code.h"
 #include "object.h"
 #include "object.h"
 
 
+enum af_FuncInfoScope {
+    normal_scope = 0,
+    inline_scope,
+    pure_scope,
+    super_pure_scope,
+};
+
+enum af_FuncInfoEmbedded {
+    not_embedded = 0,
+    protect_embedded,  // 内嵌函数
+    super_embedded,  // 超内嵌函数
+};
+
 typedef struct ArgCodeList ArgCodeList;
 typedef struct ArgCodeList ArgCodeList;
 typedef struct ArgList ArgList;
 typedef struct ArgList ArgList;
+typedef struct af_FuncInfo af_FuncInfo;
+DEFINE_DLC_SYMBOL(callFuncBody);
 
 
 /* ArgCodeList 创建与释放 */
 /* ArgCodeList 创建与释放 */
 ArgCodeList *makeArgCodeList(af_Code *code, size_t size, bool free_code, bool run_in_func);
 ArgCodeList *makeArgCodeList(af_Code *code, size_t size, bool free_code, bool run_in_func);
@@ -25,4 +40,14 @@ ArgList **pushArgList(ArgList **base, ArgList *new);
 ArgList **pushNewArgList(ArgList **base, char *name, af_Object *obj);
 ArgList **pushNewArgList(ArgList **base, char *name, af_Object *obj);
 bool runArgList(ArgList *al, af_VarSpaceListNode *vsl);
 bool runArgList(ArgList *al, af_VarSpaceListNode *vsl);
 
 
+/* FuncInfo 创建与释放 */
+af_FuncInfo *makeFuncInfo(enum af_FuncInfoScope scope, enum af_FuncInfoEmbedded embedded, bool is_macro,
+                          bool is_object, af_VarSpaceListNode *vsl);
+
+void freeFuncInfo(af_FuncInfo *fi);
+
+/* FuncInfo 操作函数 */
+void makeCFuncBodyToFuncInfo(DLC_SYMBOL(callFuncBody) c_func, af_FuncInfo *fi);
+void makeCodeFuncBodyToFuncInfo(af_Code *code, bool free_code, af_FuncInfo *fi);
+
 #endif //AFUN__FUNC_H_PUBLIC
 #endif //AFUN__FUNC_H_PUBLIC

+ 1 - 1
include/object.h

@@ -7,7 +7,7 @@ typedef struct af_Inherit af_Inherit;
 typedef struct af_ObjectAPI af_ObjectAPI;
 typedef struct af_ObjectAPI af_ObjectAPI;
 
 
 /* 对象API函数 DLC */
 /* 对象API函数 DLC */
-DEFINE_DLC_SYMBOL(objectAPIFunc, objectAPIFunc);
+DEFINE_DLC_SYMBOL(objectAPIFunc);
 
 
 #include "env.h"
 #include "env.h"
 
 

+ 1 - 1
include/tool.h

@@ -104,7 +104,7 @@ void safeSleep(double ms);
  * dlcExit: 释放所有动态库
  * dlcExit: 释放所有动态库
  */
  */
 
 
-#define DEFINE_DLC_SYMBOL(TYPE, NAME) typedef struct DLC##NAME##SYMBOL *pDLC##NAME##SYMBOL
+#define DEFINE_DLC_SYMBOL(NAME) typedef struct DLC##NAME##SYMBOL *pDLC##NAME##SYMBOL
 #define NEW_DLC_SYMBOL(TYPE, NAME) typedef struct DLC##NAME##SYMBOL { \
 #define NEW_DLC_SYMBOL(TYPE, NAME) typedef struct DLC##NAME##SYMBOL { \
 TYPE *symbol; \
 TYPE *symbol; \
 struct DlcHandle *dlc; \
 struct DlcHandle *dlc; \

+ 35 - 0
src/core/__func.h

@@ -5,6 +5,8 @@
 #include "__object.h"
 #include "__object.h"
 #include "__code.h"
 #include "__code.h"
 
 
+typedef struct af_FuncBody af_FuncBody;
+
 struct ArgCodeList {
 struct ArgCodeList {
     void *info;  // info信息
     void *info;  // info信息
     size_t size;
     size_t size;
@@ -23,4 +25,37 @@ struct ArgList {
     struct ArgList *next;
     struct ArgList *next;
 };
 };
 
 
+typedef void callFuncBody(af_Environment *env);
+NEW_DLC_SYMBOL(callFuncBody, callFuncBody);
+
+struct af_FuncBody {
+    enum af_FuncBodyType {
+        func_body_c,  // 回调C函数
+        func_body_code,  // 执行af_Code
+    } type;
+
+    union {
+        DLC_SYMBOL(callFuncBody) c_func;
+        struct {
+            af_Code *code;
+            bool free_code;
+        };
+    };
+
+    struct af_FuncBody *next;
+};
+
+struct af_FuncInfo {
+    // 函数属性
+    enum af_FuncInfoScope scope;  // 定义在 func.h
+    enum af_FuncInfoEmbedded embedded;  // 定义在 func.h
+
+    bool is_macro;  // 宏函数
+    bool is_object;  // 对象函数
+
+    // 函数信息
+    struct af_VarSpaceListNode *vsl;
+    struct af_FuncBody *body;
+};
+
 #endif //AFUN__FUNC_H
 #endif //AFUN__FUNC_H

+ 73 - 0
src/core/func.c

@@ -1,5 +1,15 @@
 #include "__func.h"
 #include "__func.h"
 
 
+/* FuncBody 创建与释放 */
+static af_FuncBody *makeFuncBody(enum af_FuncBodyType type);
+static af_FuncBody *makeCodeFuncBody(af_Code *code, bool free_code);
+static af_FuncBody *makeCFuncBody(DLC_SYMBOL(callFuncBody) c_func);
+static af_FuncBody *freeFuncBody(af_FuncBody *fb);
+static void freeAllFuncBody(af_FuncBody *fb);
+
+/* FuncBody 操作函数 */
+static void pushFuncBody(af_FuncBody **base, af_FuncBody *body);
+
 ArgCodeList *makeArgCodeList(af_Code *code, size_t size, bool free_code, bool run_in_func) {
 ArgCodeList *makeArgCodeList(af_Code *code, size_t size, bool free_code, bool run_in_func) {
     ArgCodeList *acl = calloc(sizeof(ArgCodeList), 1);
     ArgCodeList *acl = calloc(sizeof(ArgCodeList), 1);
     acl->info = calloc(size, 1);
     acl->info = calloc(size, 1);
@@ -90,3 +100,66 @@ bool runArgList(ArgList *al, af_VarSpaceListNode *vsl) {
     return true;
     return true;
 }
 }
 
 
+static af_FuncBody *makeFuncBody(enum af_FuncBodyType type) {
+    af_FuncBody *fb = calloc(sizeof(af_FuncBody), 1);
+    fb->type = type;
+    return fb;
+}
+
+static af_FuncBody *makeCodeFuncBody(af_Code *code, bool free_code) {
+    af_FuncBody *fb = makeFuncBody(func_body_code);
+    fb->code = code;
+    fb->free_code = free_code;
+    return fb;
+}
+
+static af_FuncBody *makeCFuncBody(DLC_SYMBOL(callFuncBody) c_func) {
+    af_FuncBody *fb = makeFuncBody(func_body_c);
+    fb->c_func = COPY_SYMBOL(c_func, callFuncBody);
+    return fb;
+}
+
+static af_FuncBody *freeFuncBody(af_FuncBody *fb) {
+    af_FuncBody *next = fb->next;
+    if (fb->type == func_body_code && fb->free_code)
+        freeAllCode(fb->code);
+    else if (fb->type == func_body_c)
+        FREE_SYMBOL(fb->c_func);
+    free(fb);
+    return next;
+}
+
+static void freeAllFuncBody(af_FuncBody *fb) {
+    while (fb != NULL)
+        fb = freeFuncBody(fb);
+}
+
+static void pushFuncBody(af_FuncBody **base, af_FuncBody *body) {
+    while (*base != NULL)
+        base = &((*base)->next);
+    *base = body;
+}
+
+af_FuncInfo *makeFuncInfo(enum af_FuncInfoScope scope, enum af_FuncInfoEmbedded embedded, bool is_macro,
+                          bool is_object, af_VarSpaceListNode *vsl) {
+    af_FuncInfo *fi = calloc(sizeof(af_FuncInfo), 1);
+    fi->scope = scope;
+    fi->embedded = embedded;
+    fi->is_macro = is_macro;
+    fi->is_object = is_object;
+    fi->vsl = vsl;
+    return fi;
+}
+
+void freeFuncInfo(af_FuncInfo *fi) {  // vsl是不释放的
+    freeAllFuncBody(fi->body);
+    free(fi);
+}
+
+void makeCFuncBodyToFuncInfo(DLC_SYMBOL(callFuncBody) c_func, af_FuncInfo *fi) {
+    pushFuncBody(&fi->body, makeCFuncBody(c_func));
+}
+
+void makeCodeFuncBodyToFuncInfo(af_Code *code, bool free_code, af_FuncInfo *fi) {
+    pushFuncBody(&fi->body, makeCodeFuncBody(code, free_code));
+}