ソースを参照

feat: 添加arg模块

SongZihuan 3 年 前
コミット
69d35f7fd6
5 ファイル変更164 行追加1 行削除
  1. 28 0
      include/arg.h
  2. 4 0
      include/var.h
  3. 92 0
      src/core/__arg.c
  4. 26 0
      src/core/__arg.h
  5. 14 1
      src/core/var.c

+ 28 - 0
include/arg.h

@@ -0,0 +1,28 @@
+#ifndef AFUN__ARG_H_PUBLIC
+#define AFUN__ARG_H_PUBLIC
+#include "code.h"
+#include "object.h"
+
+typedef struct ArgCodeList ArgCodeList;
+typedef struct ArgList ArgList;
+
+/* ArgCodeList 创建与释放 */
+ArgCodeList *makeArgCodeList(af_Code *code, size_t size, bool free_code, bool run_in_func);
+ArgCodeList *freeArgCodeList(ArgCodeList *acl);
+void freeAllArgCodeList(ArgCodeList *acl);
+
+/* ArgCodeList 操作函数 */
+ArgCodeList **pushArgCodeList(ArgCodeList **base, ArgCodeList *new);
+ArgCodeList **pushNewArgCodeList(ArgCodeList **base, af_Code *code, size_t size, bool free_code, bool run_in_func);
+
+/* ArgList 创建与释放 */
+ArgList *makeArgList(char *name, af_Object *obj);
+ArgList *freeArgList(ArgList *al);
+void freeAllArgList(ArgList *al);
+
+/* ArgList 操作函数 */
+ArgList **pushArgList(ArgList **base, ArgList *new);
+ArgList **pushNewArgList(ArgList **base, char *name, af_Object *obj);
+bool runArgList(ArgList *al, af_VarSpaceListNode *vsl);
+
+#endif //AFUN__ARG_H_PUBLIC

+ 4 - 0
include/var.h

@@ -26,6 +26,10 @@ bool addVarToVarSpace(af_Var *var, af_VarSpace *vs);
 bool makeVarToVarSpace(char *name, char p_self, char p_posterity, char p_external, af_Object *obj,
                        af_VarSpace *vs);
 
+bool addVarToVarSpaceList(af_Var *var, af_VarSpaceListNode *vsl);
+bool makeVarToVarSpaceList(char *name, char p_self, char p_posterity, char p_external, af_Object *obj,
+                           af_VarSpaceListNode *vsl);
+
 af_Var *findVarFromVarSpace(char *name, af_VarSpace *vs);
 af_Var *findVarFromVarList(char *name, af_VarSpaceListNode *vsl);
 

+ 92 - 0
src/core/__arg.c

@@ -0,0 +1,92 @@
+#include "__arg.h"
+
+ArgCodeList *makeArgCodeList(af_Code *code, size_t size, bool free_code, bool run_in_func) {
+    ArgCodeList *acl = calloc(sizeof(ArgCodeList), 1);
+    acl->info = calloc(size, 1);
+    acl->size = size;
+    acl->code = code;
+    acl->free_code = free_code;
+    acl->run_in_func = run_in_func;
+    return acl;
+}
+
+ArgCodeList *freeArgCodeList(ArgCodeList *acl) {
+    ArgCodeList *next = acl->next;
+    free(acl->info);
+    if (acl->free_code)
+        freeAllCode(acl->code);
+    if (acl->result != NULL)
+        gc_delReference(acl->result);
+    free(acl);
+    return next;
+}
+
+void freeAllArgCodeList(ArgCodeList *acl) {
+    while (acl != NULL)
+        acl = freeArgCodeList(acl);
+}
+
+ArgCodeList **pushArgCodeList(ArgCodeList **base, ArgCodeList *new) {
+    while (*base != NULL)
+        base = &((*base)->next);
+    *base = new;
+
+    while (*base != NULL)
+        base = &((*base)->next);
+    return base;
+}
+
+ArgCodeList **pushNewArgCodeList(ArgCodeList **base, af_Code *code, size_t size, bool free_code, bool run_in_func) {
+    while (*base != NULL)
+        base = &((*base)->next);
+    *base = makeArgCodeList(code, size, free_code, run_in_func);
+    return &((*base)->next);
+}
+
+ArgList *makeArgList(char *name, af_Object *obj) {
+    ArgList *arg_list = calloc(sizeof(ArgList), 1);
+    arg_list->name = strCopy(name);
+    arg_list->obj = obj;
+    gc_addReference(obj);
+    return arg_list;
+}
+
+ArgList *freeArgList(ArgList *al) {
+    ArgList *next = al->next;
+    free(al->name);
+    if (al->obj != NULL)
+        gc_addReference(al->obj);
+    free(al);
+    return next;
+}
+
+void freeAllArgList(ArgList *al) {
+    while (al != NULL)
+        al = freeArgList(al);
+}
+
+ArgList **pushArgList(ArgList **base, ArgList *new) {
+    while (*base != NULL)
+        base = &((*base)->next);
+    *base = new;
+
+    while (*base != NULL)
+        base = &((*base)->next);
+    return base;
+}
+
+ArgList **pushNewArgList(ArgList **base, char *name, af_Object *obj) {
+    while (*base != NULL)
+        base = &((*base)->next);
+    *base = makeArgList(name, obj);
+    return &((*base)->next);
+}
+
+bool runArgList(ArgList *al, af_VarSpaceListNode *vsl) {
+    for (NULL; al != NULL; al = al->next) {
+        if (!makeVarToVarSpaceList(al->name, 3, 3, 3, al->obj, vsl))
+            return false;
+    }
+    return true;
+}
+

+ 26 - 0
src/core/__arg.h

@@ -0,0 +1,26 @@
+#ifndef AFUN__ARG_H
+#define AFUN__ARG_H
+#include "macro.h"
+#include "arg.h"
+#include "__object.h"
+#include "__code.h"
+
+struct ArgCodeList {
+    void *info;  // info信息
+    size_t size;
+
+    struct af_Code *code;
+    bool free_code;  // code 是否释放
+    bool run_in_func;  // 是否在函数的变量空间内运行
+
+    struct af_Object *result;  // 有gc引用计数
+    struct ArgCodeList *next;
+};
+
+struct ArgList {
+    char *name;
+    struct af_Object *obj;  // 有gc引用计数
+    struct ArgList *next;
+};
+
+#endif //AFUN__ARG_H

+ 14 - 1
src/core/var.c

@@ -151,7 +151,7 @@ bool addVarToVarSpace(af_Var *var, af_VarSpace *vs) {
     time33_t index = time33(var->name);
     af_VarCup **pCup = &vs->var[index];
 
-    if (vs->is_protect == true)
+    if (vs->is_protect)
         return false;
 
     for (NULL; *pCup != NULL; pCup = &((*pCup)->next)) {
@@ -174,6 +174,19 @@ bool makeVarToVarSpace(char *name, char p_self, char p_posterity, char p_externa
     return addVarToVarSpace(makeVar(name, p_self, p_posterity, p_external, obj), vs);
 }
 
+bool addVarToVarSpaceList(af_Var *var, af_VarSpaceListNode *vsl) {
+    for (NULL; vsl != NULL; vsl = vsl->next) {
+        if (!vsl->vs->is_protect)
+            return addVarToVarSpace(var, vsl->vs);
+    }
+    return false;
+}
+
+bool makeVarToVarSpaceList(char *name, char p_self, char p_posterity, char p_external, af_Object *obj,
+                           af_VarSpaceListNode *vsl) {
+    return addVarToVarSpaceList(makeVar(name, p_self, p_posterity, p_external, obj), vsl);
+}
+
 /*
  * 函数名: findVarFromVarSpaceByIndex
  * 目标: 根据指定的index, 在VarSpace中搜索var