1
0
Эх сурвалжийг харах

feat: 添加前缀管理模块

SongZihuan 3 жил өмнө
parent
commit
7164ea0f22

+ 3 - 0
include/env.h

@@ -2,6 +2,7 @@
 #define AFUN__ENV_H_PUBLIC
 #include "macro.h"
 #include "tool.h"
+#include "prefix_macro.h"
 
 typedef struct af_Environment af_Environment;
 typedef struct af_Message af_Message;
@@ -12,6 +13,8 @@ DEFINE_DLC_SYMBOL(TopMsgProcessFunc, TopMsgProcessFunc);
 #include "object.h"
 #include "var.h"
 
+char getPrefix(size_t name, af_Environment *env);
+char setPrefix(size_t name, char prefix, af_Environment *env);
 af_Object *getBaseObject(char *name, af_Environment *env);
 
 af_Environment *makeEnvironment(void);

+ 23 - 0
include/prefix_macro.h

@@ -0,0 +1,23 @@
+/*
+ * 文件名: prefix_macro
+ * 目标: 前缀的宏定义
+ */
+#ifndef AFUN__PREFIX_MACRO_H
+#define AFUN__PREFIX_MACRO_H
+
+// 作为顶层代码,以及'()运行时
+#define L_NOT_REPEAT      (0)  /* 字面量前缀: 不重复构造 */
+#define V_QUOTE           (1)  /* 变量前缀: 引用 */
+
+#define B_EXEC            (2)  /* 括号前缀: 顺序执行 */
+#define B_EXEC_FIRST      (3)  /* 括号前缀: 顺序执行, 返回第一个 */
+#define B_MUST_COMMON_ARG (4)  /* 括号前缀: 强制普通参数调用 */
+#define B_NOT_STRICT      (5)  /* 括号前缀: 非严格参数匹配调用 */
+
+// 作为函数实参
+#define B_ARG_CUL         (6)  /* 括号前缀: 表示实际计算 */
+#define B_ARG_EXEC        (7)  /* 括号前缀: 表示顺序执行 */
+
+#define PREFIX_SIZE       (8)  /* 前缀总数 */
+
+#endif //AFUN__PREFIX_MACRO_H

+ 2 - 0
src/core/__env.h

@@ -35,6 +35,8 @@ struct af_Core {  // 解释器核心
     // 保护空间
     bool in_init;  // 是否在初始化模式
     struct af_VarSpace *protect;  // 顶级保护变量空间
+
+    char prefix[PREFIX_SIZE];  // 前缀
 };
 
 struct af_Message {

+ 24 - 0
src/core/env.c

@@ -28,6 +28,18 @@ static af_Core *makeCore(void) {
     core->protect = makeVarSpace();
     addVarSpaceGCByCore(core->protect, core);
     gc_addReference(core->protect);  // protect被外部引用, 由gc管理, 此处标记一个Reference
+
+    core->prefix[L_NOT_REPEAT] = '\'';
+    core->prefix[V_QUOTE] = '\'';
+
+    core->prefix[B_EXEC] = '\'';
+    core->prefix[B_EXEC_FIRST] = ',';
+    core->prefix[B_MUST_COMMON_ARG] = '<';
+    core->prefix[B_NOT_STRICT] = ',';
+
+    core->prefix[B_ARG_CUL] = '\'';
+    core->prefix[B_ARG_EXEC] = '\'';
+
     return core;
 }
 
@@ -41,6 +53,18 @@ static void freeCore(af_Core *core) {
     free(core);
 }
 
+char setPrefix(size_t name, char prefix, af_Environment *env) {
+    char old = env->core->prefix[name];
+    if (name >= PREFIX_SIZE || prefix == NUL)
+        return NUL;
+    env->core->prefix[name] = prefix;
+    return old;
+}
+
+char getPrefix(size_t name, af_Environment *env) {
+    return env->core->prefix[name];
+}
+
 /*
  * 函数名: getBaseObjectFromCore
  * 目标: 从VarSpace中获取一个量

+ 4 - 4
src/core/run.c

@@ -71,15 +71,15 @@ static void popLastActivity(af_Message *msg, af_Environment *env){
 }
 
 static void codeBlock(af_Code *bt, af_Environment *env) {
-    if (bt->prefix == '\'' && bt->block.type == parentheses)  // 顺序执行, 返回尾项
+    if (bt->prefix == env->core->prefix[B_EXEC] && bt->block.type == parentheses)  // 顺序执行, 返回尾项
         pushExecutionActivity(bt, false, env);
-    else if (bt->prefix == ',' && bt->block.type == brackets)  // 顺序执行, 返回首项
+    else if (bt->prefix == env->core->prefix[B_EXEC_FIRST] && bt->block.type == brackets)  // 顺序执行, 返回首项
         pushExecutionActivity(bt, true, env);
     else {
         pushFuncActivity(env->activity->bt_next, env);
-        if (bt->prefix == '<')
+        if (bt->prefix == env->core->prefix[B_MUST_COMMON_ARG])
             env->activity->must_common_arg = true;
-        else if (bt->prefix == ',')
+        else if (bt->prefix == env->core->prefix[B_NOT_STRICT])
             env->activity->not_strict = true;
     }
 }