瀏覽代碼

refactor: 对于af_Code将变量和字面量整合

SongZihuan 3 年之前
父節點
當前提交
9bc7899df8
共有 12 個文件被更改,包括 154 次插入194 次删除
  1. 2 2
      include/cJSON/cJSON.h
  2. 1 2
      include/code.h
  3. 1 1
      include/dlfcn/dlfcn.h
  4. 4 4
      src/cjson/cJSON.c
  5. 4 11
      src/core/__code.h
  6. 2 1
      src/core/__env.h
  7. 20 61
      src/core/code.c
  8. 13 4
      src/core/env.c
  9. 40 41
      src/core/run.c
  10. 53 53
      src/main.c
  11. 10 10
      test/CMakeLists.txt
  12. 4 4
      test/test_byte_code.c

+ 2 - 2
include/cJSON/cJSON.h

@@ -118,7 +118,7 @@ typedef struct cJSON
     /* The item's number, if type==cJSON_Number */
     double valuedouble;
 
-    /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
+    /* The item's data string, if this item is the child of, or is in the list of subitems of an object. */
     char *string;
 } cJSON;
 
@@ -144,7 +144,7 @@ CJSON_PUBLIC(const char*) cJSON_Version(void);
 CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
 
 /* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */
-/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
+/* Supply a code_block of JSON, and this returns a cJSON object you can interrogate. */
 CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
 CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length);
 /* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */

+ 1 - 2
include/code.h

@@ -12,8 +12,7 @@ enum af_BlockType {
 };
 
 /* 代码块创建函数 */
-af_Code *makeLiteralCode(char *literal_data, char *func, bool in_protect, char prefix, FileLine line, FilePath path);
-af_Code *makeVariableCode(char *var, char prefix, FileLine line, FilePath path);
+af_Code *makeElementCode(char *var, char prefix, FileLine line, FilePath path);
 af_Code *makeBlockCode(enum af_BlockType type, af_Code *element, char prefix, FileLine line, FilePath path, af_Code **next);
 
 /* 代码块释放函数 */

+ 1 - 1
include/dlfcn/dlfcn.h

@@ -60,7 +60,7 @@ extern "C" {
 /* The symbol lookup happens in the normal global scope. */
 #define RTLD_DEFAULT    ((void *)0)
 
-/* Specifies the next object after this one that defines name. */
+/* Specifies the next object after this one that defines data. */
 #define RTLD_NEXT       ((void *)-1)
 
 /* Structure filled in by dladdr() */

+ 4 - 4
src/cjson/cJSON.c

@@ -1648,16 +1648,16 @@ static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_bu
             current_item = new_item;
         }
 
-        /* parse the name of the child */
+        /* parse the data of the child */
         input_buffer->offset++;
         buffer_skip_whitespace(input_buffer);
         if (!parse_string(current_item, input_buffer))
         {
-            goto fail; /* failed to parse name */
+            goto fail; /* failed to parse data */
         }
         buffer_skip_whitespace(input_buffer);
 
-        /* swap valuestring and string, because we parsed the name */
+        /* swap valuestring and string, because we parsed the data */
         current_item->string = current_item->valuestring;
         current_item->valuestring = NULL;
 
@@ -2349,7 +2349,7 @@ static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSO
         return false;
     }
 
-    /* replace the name in the replacement */
+    /* replace the data in the replacement */
     if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL))
     {
         cJSON_free(replacement->string);

+ 4 - 11
src/core/__code.h

@@ -11,9 +11,8 @@
 typedef unsigned int CodeUint;  // Code uint
 
 enum af_CodeType {
-    literal = 0,
-    variable,
-    block,  // 括号
+    code_element = 0,
+    code_block,  // 括号
 };
 
 
@@ -22,14 +21,8 @@ struct af_Code {  // 一个 Code 的结构体
     char prefix;  // 前缀
     union {
         struct {
-            char *literal_data;
-            char *func;  // 函数名称
-            bool in_protect;  // 是否在保护区域
-        } literal;
-
-        struct {
-            char *name;
-        } variable;
+            char *data;
+        } element;
 
         struct {
             CodeUint elements;  // 元素个数

+ 2 - 1
src/core/__env.h

@@ -161,7 +161,7 @@ void popActivity(bool is_normal, af_Message *msg, af_Environment *env);
 bool pushDestructActivity(gc_DestructList *dl, af_Environment *env);
 void pushGCActivity(gc_DestructList *dl, gc_DestructList **pdl, af_Environment *env);
 bool pushVariableActivity(af_Code *bt, af_Object *func, af_Environment *env);
-bool pushLiteralActivity(af_Code *bt, af_Object *func, af_Environment *env);
+bool pushLiteralActivity(af_Code *bt, char *data, af_Object *func, af_Environment *env);
 bool pushMacroFuncActivity(af_Object *func, af_Environment *env);
 bool setFuncActivityToArg(af_Object *func, af_Environment *env);
 bool setFuncActivityAddVar(af_Environment *env);
@@ -173,6 +173,7 @@ void freeAllLiteralData(af_LiteralDataList *ld);
 
 /* LiteralData 操作函数 */
 void pushLiteralData(char *data, af_Environment *env);
+bool checkLiteralCode(char *literal, char **func, bool *in_protect, af_Environment *env);
 
 /* 顶层消息处理器 处理函数 */
 void runTopMessageProcess(bool is_gc, af_Environment *env);

+ 20 - 61
src/core/code.c

@@ -28,26 +28,13 @@ static af_Code *makeCode(char prefix, FileLine line, FilePath path) {
     return bt;
 }
 
-af_Code *makeLiteralCode(char *literal_data, char *func, bool in_protect, char prefix, FileLine line, FilePath path) {
+af_Code *makeElementCode(char *var, char prefix, FileLine line, FilePath path) {
     if (prefix != NUL && strchr(LV_PREFIX, prefix) == NULL)
         prefix = NUL;
 
     af_Code *bt = makeCode(prefix, line, path);
-    bt->type = literal;
-    bt->literal.literal_data = strCopy(literal_data);
-    bt->literal.func = strCopy(func);
-    bt->literal.in_protect = in_protect;
-    return bt;
-}
-
-
-af_Code *makeVariableCode(char *var, char prefix, FileLine line, FilePath path) {
-    if (prefix != NUL && strchr(LV_PREFIX, prefix) == NULL)
-        prefix = NUL;
-
-    af_Code *bt = makeCode(prefix, line, path);
-    bt->type = variable;
-    bt->variable.name = strCopy(var);
+    bt->type = code_element;
+    bt->element.data = strCopy(var);
     return bt;
 }
 
@@ -65,7 +52,7 @@ static void countElement(af_Code *element, CodeUint *elements, CodeUint *count,
         else
             to_next--;
 
-        if (element->type == block)
+        if (element->type == code_block)
             to_next += element->block.elements;
     }
 }
@@ -84,7 +71,7 @@ af_Code *makeBlockCode(enum af_BlockType type, af_Code *element, char prefix, Fi
 
     countElement(element, &elements, &count, next);
     bt = makeCode(prefix, line, path);
-    bt->type = block;
+    bt->type = code_block;
     bt->block.type = type;
     bt->block.elements = elements;
     bt->block.count = count;
@@ -111,22 +98,14 @@ af_Code *copyCode(af_Code *base, FilePath *path) {
         *pdest = makeCode(base->prefix, base->line, base->path);
         (*pdest)->type = base->type;
         switch (base->type) {
-            case literal:
-                (*pdest)->literal.literal_data = strCopy(base->literal.literal_data);
-                (*pdest)->literal.func = strCopy(base->literal.func);
-                (*pdest)->literal.in_protect = base->literal.in_protect;
+            case code_element:
+                (*pdest)->element.data = strCopy(base->element.data);
                 break;
-
-            case variable:
-                (*pdest)->variable.name = strCopy(base->variable.name);
-                break;
-
-            case block:
+            case code_block:
                 (*pdest)->block.count = base->block.count;
                 (*pdest)->block.elements = base->block.elements;
                 (*pdest)->block.type = base->block.type;
                 break;
-
             default:
                 break;
         }
@@ -144,12 +123,8 @@ static af_Code *freeCode(af_Code *bt) {
     af_Code *next = bt->next;
     free(bt->path);
     switch (bt->type) {
-        case literal:
-            free(bt->literal.literal_data);
-            free(bt->literal.func);
-            break;
-        case variable:
-            free(bt->variable.name);
+        case code_element:
+            free(bt->element.data);
             break;
         default:
             break;
@@ -200,15 +175,10 @@ static bool writeCode(af_Code *bt, FILE *file) {
     }
 
     switch (bt->type) {
-        case literal:
-            Done(byteWriteStr(file, bt->literal.literal_data));
-            Done(byteWriteStr(file, bt->literal.func));
-            Done(byteWriteUint_8(file, bt->literal.in_protect));
+        case code_element:
+            Done(byteWriteStr(file, bt->element.data));
             break;
-        case variable:
-            Done(byteWriteStr(file, bt->variable.name));
-            break;
-        case block:
+        case code_block:
             Done(byteWriteUint_8(file, bt->block.type));
             Done(byteWriteUint_32(file, bt->block.elements));
             Done(byteWriteUint_32(file, bt->block.count));
@@ -262,18 +232,10 @@ static bool readCode(af_Code **bt, FILE *file) {
     (*bt)->type = type;
 
     switch (type) {
-        case literal: {
-            uint8_t in_protect;
-            Done(byteReadStr(file, &((*bt)->literal.literal_data)));
-            Done(byteReadStr(file, &((*bt)->literal.func)));
-            Done(byteReadUint_8(file, &in_protect));
-            (*bt)->literal.in_protect = in_protect;
-            break;
-        }
-        case variable:
-            Done(byteReadStr(file, &((*bt)->variable.name)));
+        case code_element:
+            Done(byteReadStr(file, &((*bt)->element.data)));
             break;
-        case block: {
+        case code_block: {
             uint8_t block_type;
             uint32_t elements;
             uint32_t count;
@@ -305,14 +267,11 @@ bool readAllCode(af_Code **bt, FILE *file) {
 void printCode(af_Code *bt) {
     for (NULL; bt != NULL; bt = bt->next) {
         switch (bt->type) {
-            case literal:
-                printf("literal: %s %s prefix: %d\n", bt->literal.literal_data, bt->literal.func, bt->prefix);
-                break;
-            case variable:
-                printf("variable: %s prefix: %d\n", bt->variable.name, bt->prefix);
+            case code_element:
+                printf("code_element: %s prefix: %d\n", bt->element.data, bt->prefix);
                 break;
-            case block:
-                printf("variable: %d %d prefix: %d\n", bt->block.elements, bt->block.type, bt->prefix);
+            case code_block:
+                printf("code_block: %d %d prefix: %d\n", bt->block.elements, bt->block.type, bt->prefix);
                 break;
             default:
                 printf("Unknow: %d prefix: %d\n", bt->type, bt->prefix);

+ 13 - 4
src/core/env.c

@@ -534,10 +534,10 @@ bool pushExecutionActivity(af_Code *bt, bool return_first, af_Environment *env)
 }
 
 static bool isInfixFunc(af_Code *code, af_Environment *env) {
-    if (code == NULL || code->type != variable || code->prefix == getPrefix(V_QUOTE, env))
+    if (code == NULL || code->type != code_element || code->prefix == getPrefix(V_QUOTE, env))
         return false;
 
-    af_Var *var = findVarFromVarList(code->variable.name, env->activity->belong, env->activity->var_list);
+    af_Var *var = findVarFromVarList(code->element.data, env->activity->belong, env->activity->var_list);
     if (var == NULL)
         return false;
 
@@ -608,12 +608,12 @@ bool pushFuncActivity(af_Code *bt, af_Environment *env) {
     return true;
 }
 
-bool pushLiteralActivity(af_Code *bt, af_Object *func, af_Environment *env) {
+bool pushLiteralActivity(af_Code *bt, char *data, af_Object *func, af_Environment *env) {
     env->activity->bt_next = bt->next;
 
     newActivity(bt, bt->next, false, env);
     env->activity->is_literal = true;
-    pushLiteralData(strCopy(bt->literal.literal_data), env);  // FuncBody的释放导致code和literal_data释放, 所以要复制
+    pushLiteralData(strCopy(data), env);  // FuncBody的释放导致code和literal_data释放, 所以要复制
     return setFuncActivityToArg(func, env);
 }
 
@@ -918,3 +918,12 @@ void popActivity(bool is_normal, af_Message *msg, af_Environment *env) {
 
     env->activity = freeActivity(env->activity);
 }
+
+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;
+    }
+    return false;
+}

+ 40 - 41
src/core/run.c

@@ -25,7 +25,7 @@ bool checkNormalEnd(af_Message *msg, af_Environment *env);
 static bool checkGetArgEnd(af_Message *msg, af_Environment *env);
 
 /* Code 执行函数 */
-static bool codeVariable(af_Code *code, af_Environment *env);
+static bool codeElement(af_Code *code, af_Environment *env);
 static bool codeLiteral(af_Code *code, af_Environment *env);
 static bool codeBlock(af_Code *code, af_Environment *env);
 
@@ -115,17 +115,42 @@ static bool iterCodeInit(af_Code *code, af_Environment *env) {
 }
 
 /*
- * 函数名: codeVariable
- * 目标: 执行变量访问代码 (设置bt_next)
- * 返回-false 表示执行失败, 或执行成功得到一个变量值      (msg_down中写入消息)
- * 返回-true  表示执行成功, 得到一个对象函数, 并且隐式调用 (msg_down中无消息写入, 函数仅设置activity无实质性代码执行)
+ * 函数名: codeElement
+ * 目标: 执行变量访问或字面量生成 (设置bt_next)
+ * (1) 执行字面量生成代码 (设置bt_next)
+ *     返回-false 表示执行错误 (msg_down中写入消息)
+ *     返回-true  表示执行成功 (msg_down中无消息写入, 函数仅设置activity无实质性代码执行)
+ * (2) 执行变量访问代码:
+ *     返回-false 表示执行失败, 或执行成功得到一个变量值      (msg_down中写入消息)
+ *     返回-true  表示执行成功, 得到一个对象函数, 并且隐式调用 (msg_down中无消息写入, 函数仅设置activity无实质性代码执行)
  */
-static bool codeVariable(af_Code *code, af_Environment *env) {
-    af_Var *var = findVarFromVarList(code->variable.name, env->activity->belong, env->activity->vsl);
+static bool codeElement(af_Code *code, af_Environment *env) {
+    bool in_protect;
+    char *func;
+    af_Var *var;
+
+    if (checkLiteralCode(code->element.data, &func, &in_protect, env)) {
+        /* 字面量执行 */
+        if (in_protect)
+            var = findVarFromVarSpace(func, env->activity->belong, env->core->protect);
+        else
+            var = findVarFromVarList(func, env->activity->belong, env->activity->vsl);
+
+        if (var == NULL) {
+            pushMessageDown(makeMessage("ERROR-STR", 0), env);
+            printf("Literal not found: %s\n", code->element.data);
+            return false;
+        }
+
+        return pushLiteralActivity(code, code->element.data, var->vn->obj, env);
+    }
+
+    /* 变量执行 */
+    var = findVarFromVarList(code->element.data, env->activity->belong, env->activity->vsl);
 
     if (var == NULL) {
         pushMessageDown(makeMessage("ERROR-STR", 0), env);
-        printf("Variable not found: %s\n", code->variable.name);
+        printf("Variable not found: %s\n", code->element.data);
         return false;
     }
 
@@ -139,39 +164,17 @@ static bool codeVariable(af_Code *code, af_Environment *env) {
         else if (env->activity->status != act_func_get && // 在act_func模式时关闭保护
                  (is_infix = findAPI("obj_isInfixFunc", obj->data->api)) != NULL && is_infix(obj)) {
             pushMessageDown(makeMessage("ERROR-STR", 0), env);
-            printf("Infix protect : %s\n", code->variable.name);
+            printf("Infix protect : %s\n", code->element.data);
             return false;
         }
     }
 
     pushMessageDown(makeNORMALMessage(obj), env);
     env->activity->bt_next = env->activity->bt_next->next;
-    printf("Get Variable %s : %p\n", code->variable.name, obj);
+    printf("Get Variable %s : %p\n", code->element.data, obj);
     return false;
 }
 
-/*
- * 函数名: codeLiteral
- * 目标: 执行字面量生成代码 (设置bt_next)
- * 返回-false 表示执行错误 (msg_down中写入消息)
- * 返回-true  表示执行成功 (msg_down中无消息写入, 函数仅设置activity无实质性代码执行)
- */
-static bool codeLiteral(af_Code *code, af_Environment *env) {
-    af_Var *var;
-    if (code->literal.in_protect)
-        var = findVarFromVarSpace(code->literal.func, env->activity->belong, env->core->protect);
-    else
-        var = findVarFromVarList(code->literal.func, env->activity->belong, env->activity->vsl);
-
-    if (var == NULL) {
-        pushMessageDown(makeMessage("ERROR-STR", 0), env);
-        printf("Literal not found: %s\n", code->variable.name);
-        return false;
-    }
-
-    return pushLiteralActivity(code, var->vn->obj, env);
-}
-
 /*
  * 函数名: codeBlock
  * 目标: 执行括号语法 (顺序执行, 函数调用)
@@ -201,21 +204,17 @@ static void setRunVarSpaceList(af_Environment *env) {
 
 /*
  * 函数名: runCodeBase
- * 目标: 获取代码类型, 调用(codeLiteral, codeVariable, codeBlock)运行代码
+ * 目标: 获取代码类型, 调用(codeLiteral, codeElement, codeBlock)运行代码
  * 返回true- 表示执行无消息写入 msg_down (通常是无实质性代码运算)
  * 返回false-表示有消息写入 msg_down
  */
 static bool runCodeBase(af_Environment *env) {
     switch (env->activity->bt_next->type) {
-        case literal:
-            if (codeLiteral(env->activity->bt_next, env))
-                return true;  // 若运行成功则跳转到下一次运行, 该步骤仅为设置Activity
-            break;
-        case variable:
-            if (codeVariable(env->activity->bt_next, env))
+        case code_element:
+            if (codeElement(env->activity->bt_next, env))
                 return true;
             break;
-        case block:
+        case code_block:
             if (codeBlock(env->activity->bt_next, env))
                 return true;  // 若运行成功则跳转到下一次运行, 该步骤仅为设置Activity
         default:
@@ -321,7 +320,7 @@ bool checkNormalEnd(af_Message *msg, af_Environment *env) {
             freeMessage(msg);
         }
     } else {
-        if (env->activity->bt_next->type == block && env->activity->bt_next->block.type == parentheses &&
+        if (env->activity->bt_next->type == code_block && env->activity->bt_next->block.type == parentheses &&
             env->activity->bt_next->prefix != getPrefix(B_EXEC, env)) {
             env->activity->parentheses_call = *(af_Object **) (msg->msg);  // 类前缀调用
         }

+ 53 - 53
src/main.c

@@ -63,7 +63,7 @@ af_VarSpace *getShareVS(af_Object *obj) {
 }
 
 bool getAcl(af_ArgCodeList **acl, af_Object *obj, af_Code *code, int **mark, af_Environment *env) {
-    *acl = makeArgCodeList(makeVariableCode("object", NUL, 0, "Unknown"), 0, true, false);
+    *acl = makeArgCodeList(makeElementCode("object", NUL, 0, "Unknown"), 0, true, false);
     *mark = calloc(sizeof(int), 1);
     **mark = 100;
     return true;
@@ -107,7 +107,7 @@ af_FuncBody *testFunc(int *mark, af_Environment *env) {  // 测试用函数
 
 bool getInfo(af_FuncInfo **fi, af_Object *obj, af_Code *code, void *mark, af_Environment *env) {
     *fi = makeFuncInfo(normal_scope, not_embedded, false, true, true);
-    makeCodeFuncBodyToFuncInfo(makeVariableCode("test", NUL, 0, "Unknow"), true, NULL, *fi);
+    makeCodeFuncBodyToFuncInfo(makeElementCode("test", NUL, 0, "Unknow"), true, NULL, *fi);
 
     DLC_SYMBOL(callFuncBody) func = MAKE_SYMBOL(testFunc, callFuncBody);
     makeCFuncBodyToFuncInfo(func, NULL, *fi);
@@ -172,7 +172,7 @@ af_FuncBody *testFunc2(int *mark, af_Environment *env) {  // 测试用函数
 
 bool getInfo2(af_FuncInfo **fi, af_Object *obj, af_Code *code, void *mark, af_Environment *env) {
     *fi = makeFuncInfo(normal_scope, not_embedded, true, true, true);
-    makeCodeFuncBodyToFuncInfo(makeVariableCode("test", NUL, 0, "Unknow"), true, NULL, *fi);
+    makeCodeFuncBodyToFuncInfo(makeElementCode("test", NUL, 0, "Unknow"), true, NULL, *fi);
     DLC_SYMBOL(callFuncBody) func = MAKE_SYMBOL(testFunc2, callFuncBody);
     makeCFuncBodyToFuncInfo(func, NULL, *fi);
     FREE_SYMBOL(func);
@@ -181,7 +181,7 @@ bool getInfo2(af_FuncInfo **fi, af_Object *obj, af_Code *code, void *mark, af_En
 
 bool getInfo3(af_FuncInfo **fi, af_Object *obj, af_Code *code, void *mark, af_Environment *env) {
     *fi = makeFuncInfo(normal_scope, not_embedded, false, true, true);
-    makeCodeFuncBodyToFuncInfo(makeLiteralCode("data3", "func", true, NUL, 0, "Unknow"), true, NULL, *fi);
+    makeCodeFuncBodyToFuncInfo(makeElementCode("data3", NUL, 0, "Unknow"), true, NULL, *fi);
     return true;
 }
 
@@ -357,7 +357,7 @@ bool getInfo5(af_FuncInfo **fi, af_Object *obj, af_Code *code, void *mark, af_En
     DLC_SYMBOL(callFuncBody) func = MAKE_SYMBOL(testFunc5, callFuncBody);
     makeCFuncBodyToFuncInfo(func, NULL, *fi);
     FREE_SYMBOL(func);
-    makeCodeFuncBodyToFuncInfo(makeVariableCode("test", NUL, 0, "Unknow"), true, NULL, *fi);
+    makeCodeFuncBodyToFuncInfo(makeElementCode("test", NUL, 0, "Unknow"), true, NULL, *fi);
     return true;
 }
 
@@ -785,15 +785,15 @@ int main() {
 
     {  // 正常程序
         printf("TAG A:\n");
-        af_Code *bt1 = makeVariableCode("object", 0, 1, NULL);
-        af_Code *bt2 = makeLiteralCode("data", "func", false, ',', 0, "Unknow");
+        af_Code *bt1 = makeElementCode("object", 0, 1, NULL);
+        af_Code *bt2 = makeElementCode("data", ',', 0, "Unknow");
         connectCode(&bt1, bt2);
 
-        af_Code *bt3 = makeVariableCode("func", 0, 1, NULL);
+        af_Code *bt3 = makeElementCode("func", 0, 1, NULL);
         af_Code *bt5 = makeBlockCode(curly, bt3, 0, 1, NULL, NULL);
         connectCode(&bt2, bt5);
 
-        af_Code *bt6 = makeVariableCode("global", 0, 1, NULL);
+        af_Code *bt6 = makeElementCode("global", 0, 1, NULL);
         connectCode(&bt5, bt6);
 
         iterCode(bt1, env);
@@ -803,13 +803,13 @@ int main() {
 
     {  // 宏函数
         printf("TAG L:\n");
-        af_Code *bt1 = makeVariableCode("object", 0, 1, NULL);
+        af_Code *bt1 = makeElementCode("object", 0, 1, NULL);
 
-        af_Code *bt3 = makeVariableCode("func2", 0, 1, NULL);
+        af_Code *bt3 = makeElementCode("func2", 0, 1, NULL);
         af_Code *bt5 = makeBlockCode(curly, bt3, 0, 1, NULL, NULL);
         connectCode(&bt1, bt5);
 
-        af_Code *bt6 = makeVariableCode("global", 0, 1, NULL);
+        af_Code *bt6 = makeElementCode("global", 0, 1, NULL);
         connectCode(&bt5, bt6);
 
         iterCode(bt1, env);
@@ -820,11 +820,11 @@ int main() {
 
     {  // 尾调递归优化
         printf("TAG B:\n");
-        af_Code *bt1 = makeLiteralCode("data", "func", false, ',', 0, "Unknow");
-        af_Code *bt2 = makeVariableCode("object", 0, 1, NULL);
+        af_Code *bt1 = makeElementCode("data", ',', 0, "Unknow");
+        af_Code *bt2 = makeElementCode("object", 0, 1, NULL);
         connectCode(&bt1, bt2);
 
-        af_Code *bt3 = makeVariableCode("func", 0, 1, NULL);
+        af_Code *bt3 = makeElementCode("func", 0, 1, NULL);
         af_Code *bt5 = makeBlockCode(curly, bt3, 0, 1, NULL, NULL);
         connectCode(&bt2, bt5);
 
@@ -835,7 +835,7 @@ int main() {
 
     {  // 尾调递归优化2
         printf("TAG C:\n");
-        af_Code *bt1 = makeLiteralCode("data", "func", false, ',', 0, "Unknow");
+        af_Code *bt1 = makeElementCode("data", ',', 0, "Unknow");
 
         iterCode(bt1, env);
         freeAllCode(bt1);
@@ -844,15 +844,15 @@ int main() {
 
     {  // 测试类前缀调用
         printf("TAG D:\n");
-        af_Code *bt1 = makeLiteralCode("data", "func", false, ',', 0, "Unknow");
-        af_Code *bt2 = makeVariableCode("func", 0, 1, NULL);
+        af_Code *bt1 = makeElementCode("data", ',', 0, "Unknow");
+        af_Code *bt2 = makeElementCode("func", 0, 1, NULL);
         connectCode(&bt1, bt2);
 
         af_Code *bt5 = makeBlockCode(parentheses, NULL, 0, 1, NULL, NULL);
         connectCode(&bt2, bt5);
 
 
-        af_Code *bt6 = makeVariableCode("global", 0, 1, NULL);
+        af_Code *bt6 = makeElementCode("global", 0, 1, NULL);
         connectCode(&bt5, bt6);
 
         iterCode(bt1, env);
@@ -862,12 +862,12 @@ int main() {
 
     {  // 测试错误 (无函数指定)
         printf("TAG F: ERROR\n");
-        af_Code *bt1 = makeLiteralCode("data", "func", false, ',', 0, "Unknow");
+        af_Code *bt1 = makeElementCode("data", ',', 0, "Unknow");
 
         af_Code *bt5 = makeBlockCode(curly, NULL, 0, 1, NULL, NULL);
         connectCode(&bt1, bt5);
 
-        af_Code *bt6 = makeVariableCode("global", 0, 1, NULL);
+        af_Code *bt6 = makeElementCode("global", 0, 1, NULL);
         connectCode(&bt5, bt6);
 
         iterCode(bt1, env);
@@ -877,8 +877,8 @@ int main() {
 
     {  // 测试错误 (object2 Var not found)
         printf("TAG G: ERROR\n");
-        af_Code *bt1 = makeLiteralCode("data", "func", false, ',', 0, "Unknow");
-        af_Code *bt2 = makeVariableCode("object2", 0, 1, NULL);
+        af_Code *bt1 = makeElementCode("data", ',', 0, "Unknow");
+        af_Code *bt2 = makeElementCode("object2", 0, 1, NULL);
 
         connectCode(&bt1, bt2);
 
@@ -889,14 +889,14 @@ int main() {
 
     {  // 测试顺序执行 '(xxx)
         printf("TAG H:\n");
-        af_Code *bt3 = makeLiteralCode("data2", "func", false, 0, 0, NULL);
-        af_Code *bt4 = makeVariableCode("global", 0, 1, NULL);
+        af_Code *bt3 = makeElementCode("data2", 0, 0, NULL);
+        af_Code *bt4 = makeElementCode("global", 0, 1, NULL);
 
         connectCode(&bt3, bt4);
 
         af_Code *bt5 = makeBlockCode(parentheses, bt3, '\'', 1, NULL, NULL);
 
-        af_Code *bt6 = makeVariableCode("global", 0, 1, NULL);
+        af_Code *bt6 = makeElementCode("global", 0, 1, NULL);
         connectCode(&bt5, bt6);
 
         iterCode(bt5, env);
@@ -906,14 +906,14 @@ int main() {
 
     {  // 测试顺序执行 ,[xxx]
         printf("TAG I:\n");
-        af_Code *bt3 = makeLiteralCode("data2", "func", false, 0, 0, NULL);
-        af_Code *bt4 = makeVariableCode("global", 0, 1, NULL);
+        af_Code *bt3 = makeElementCode("data2", 0, 0, NULL);
+        af_Code *bt4 = makeElementCode("global", 0, 1, NULL);
 
         connectCode(&bt3, bt4);
 
         af_Code *bt5 = makeBlockCode(brackets, bt3, ',', 1, NULL, NULL);
 
-        af_Code *bt6 = makeVariableCode("global", 0, 1, NULL);
+        af_Code *bt6 = makeElementCode("global", 0, 1, NULL);
         connectCode(&bt5, bt6);
 
         iterCode(bt5, env);
@@ -923,8 +923,8 @@ int main() {
 
     {  // 测试顺序执行 '(xxx) 【尾调递归优化】
         printf("TAG J:\n");
-        af_Code *bt3 = makeLiteralCode("data2", "func", false, 0, 0, NULL);
-        af_Code *bt4 = makeVariableCode("global", 0, 1, NULL);
+        af_Code *bt3 = makeElementCode("data2", 0, 0, NULL);
+        af_Code *bt4 = makeElementCode("global", 0, 1, NULL);
 
         connectCode(&bt3, bt4);
 
@@ -937,8 +937,8 @@ int main() {
 
     {  // 测试顺序执行 ,[xxx] 【尾调递归优化】
         printf("TAG K:\n");
-        af_Code *bt3 = makeLiteralCode("data2", "func", false, 0, 0, NULL);
-        af_Code *bt4 = makeVariableCode("global", 0, 1, NULL);
+        af_Code *bt3 = makeElementCode("data2", 0, 0, NULL);
+        af_Code *bt4 = makeElementCode("global", 0, 1, NULL);
 
         connectCode(&bt3, bt4);
 
@@ -951,14 +951,14 @@ int main() {
 
     {  // 双层尾调递归优化 (函数内调用函数)
         printf("TAG M:\n");
-        af_Code *bt2 = makeVariableCode("func3", 0, 1, NULL);
+        af_Code *bt2 = makeElementCode("func3", 0, 1, NULL);
         af_Code *bt3 = makeBlockCode(curly, bt2, 0, 1, NULL, NULL);
 
-        af_Code *bt4 = makeVariableCode("func3", 0, 1, NULL);
+        af_Code *bt4 = makeElementCode("func3", 0, 1, NULL);
         af_Code *bt5 = makeBlockCode(curly, bt4, 0, 1, NULL, NULL);
         connectCode(&bt3, bt5);
 
-        af_Code *bt6 = makeVariableCode("global", 0, 1, NULL);
+        af_Code *bt6 = makeElementCode("global", 0, 1, NULL);
         connectCode(&bt5, bt6);
 
         iterCode(bt3, env);
@@ -968,8 +968,8 @@ int main() {
 
     {  // 对象函数的调用
         printf("TAG N:\n");
-        af_Code *bt1 = makeVariableCode("func4", 0, 1, NULL);
-        af_Code *bt2 = makeVariableCode("global", 0, 1, NULL);
+        af_Code *bt1 = makeElementCode("func4", 0, 1, NULL);
+        af_Code *bt2 = makeElementCode("global", 0, 1, NULL);
         connectCode(&bt1, bt2);
 
         iterCode(bt1, env);
@@ -979,8 +979,8 @@ int main() {
 
     {  // 变量引用调用
         printf("TAG O:\n");
-        af_Code *bt1 = makeVariableCode("func4", '\'', 1, NULL);
-        af_Code *bt2 = makeVariableCode("global", 0, 1, NULL);
+        af_Code *bt1 = makeElementCode("func4", '\'', 1, NULL);
+        af_Code *bt2 = makeElementCode("global", 0, 1, NULL);
         connectCode(&bt1, bt2);
 
         iterCode(bt1, env);
@@ -990,7 +990,7 @@ int main() {
 
     {  // 对象函数的调用 (尾调递归优化)
         printf("TAG P:\n");
-        af_Code *bt1 = makeVariableCode("func4", 0, 1, NULL);
+        af_Code *bt1 = makeElementCode("func4", 0, 1, NULL);
 
         iterCode(bt1, env);
         freeAllCode(bt1);
@@ -1000,7 +1000,7 @@ int main() {
     {  // 函数调用
         printf("TAG P:\n");
 
-        af_Code *bt2 = makeVariableCode("func", 0, 1, NULL);
+        af_Code *bt2 = makeElementCode("func", 0, 1, NULL);
         af_Code *bt1 = makeBlockCode(curly, bt2, 0, 1, NULL, NULL);
 
         iterCode(bt1, env);
@@ -1011,10 +1011,10 @@ int main() {
     {  // gc测试
         printf("TAG Q:\n");
 
-        af_Code *bt2 = makeVariableCode("func5", 0, 1, NULL);
+        af_Code *bt2 = makeElementCode("func5", 0, 1, NULL);
         af_Code *bt1 = makeBlockCode(curly, bt2, 0, 1, NULL, NULL);
-        af_Code *bt3 = makeVariableCode("global", 0, 1, NULL);
-        af_Code *bt4 = makeVariableCode("global", 0, 1, NULL);
+        af_Code *bt3 = makeElementCode("global", 0, 1, NULL);
+        af_Code *bt4 = makeElementCode("global", 0, 1, NULL);
 
         connectCode(&bt1, bt3);
         connectCode(&bt3, bt4);
@@ -1027,9 +1027,9 @@ int main() {
     {  // func_body_dynamic 测试
         printf("TAG R:\n");
 
-        af_Code *bt2 = makeVariableCode("func7", 0, 1, NULL);
+        af_Code *bt2 = makeElementCode("func7", 0, 1, NULL);
         af_Code *bt1 = makeBlockCode(curly, bt2, 0, 1, NULL, NULL);
-        af_Code *bt3 = makeVariableCode("global", 0, 1, NULL);
+        af_Code *bt3 = makeElementCode("global", 0, 1, NULL);
 
         connectCode(&bt1, bt3);
 
@@ -1041,9 +1041,9 @@ int main() {
     {  // 中缀调用测试
         printf("TAG S:\n");
 
-        af_Code *bt2 = makeVariableCode("func8", 0, 1, NULL);
+        af_Code *bt2 = makeElementCode("func8", 0, 1, NULL);
         af_Code *bt1 = makeBlockCode(brackets, bt2, 0, 1, NULL, NULL);
-        af_Code *bt3 = makeVariableCode("global", 0, 1, NULL);
+        af_Code *bt3 = makeElementCode("global", 0, 1, NULL);
 
         connectCode(&bt1, bt3);
 
@@ -1055,9 +1055,9 @@ int main() {
     {  // 中缀调用测试
         printf("TAG T: ERROR\n");
 
-        af_Code *bt2 = makeVariableCode("func", 0, 1, NULL);
+        af_Code *bt2 = makeElementCode("func", 0, 1, NULL);
         af_Code *bt1 = makeBlockCode(brackets, bt2, 0, 1, NULL, NULL);
-        af_Code *bt3 = makeVariableCode("global", 0, 1, NULL);
+        af_Code *bt3 = makeElementCode("global", 0, 1, NULL);
 
         connectCode(&bt1, bt3);
 
@@ -1069,8 +1069,8 @@ int main() {
     {  // 中缀保护测试
         printf("TAG U: ERROR\n");
 
-        af_Code *bt2 = makeVariableCode("func8", 0, 1, NULL);
-        af_Code *bt1 = makeVariableCode("global", 0, 1, NULL);
+        af_Code *bt2 = makeElementCode("func8", 0, 1, NULL);
+        af_Code *bt1 = makeElementCode("global", 0, 1, NULL);
 
         connectCode(&bt1, bt2);
 

+ 10 - 10
test/CMakeLists.txt

@@ -35,15 +35,15 @@ SET_PASS(lib "num = 100 test = 110")
 SET_PASS(dlc "a = 100, test = 110")
 SET_PASS(byte_code
 "out:
-literal: data func prefix: 44
-variable: var1 prefix: 0
-variable: 2 0 prefix: 0
-literal: data2 func prefix: 0
-variable: var2 prefix: 0
+code_element: data prefix: 44
+code_element: var1 prefix: 0
+code_block: 2 0 prefix: 0
+code_element: data2 prefix: 0
+code_element: var2 prefix: 0
 in:
-literal: data func prefix: 44
-variable: var1 prefix: 0
-variable: 2 0 prefix: 0
-literal: data2 func prefix: 0
-variable: var2 prefix: 0"
+code_element: data prefix: 44
+code_element: var1 prefix: 0
+code_block: 2 0 prefix: 0
+code_element: data2 prefix: 0
+code_element: var2 prefix: 0"
         )

+ 4 - 4
test/test_byte_code.c

@@ -3,11 +3,11 @@
 #include "aFun.h"
 
 int main() {
-    af_Code *bt1 = makeLiteralCode("data", "func", false, ',', 0, "Unknown");
-    af_Code *bt2 = makeVariableCode("var1", 0, 1, NULL);
+    af_Code *bt1 = makeElementCode("data", ',', 0, "Unknown");
+    af_Code *bt2 = makeElementCode("var1", 0, 1, NULL);
 
-    af_Code *bt3 = makeLiteralCode("data2", "func", false, 0, 0, NULL);
-    af_Code *bt4 = makeVariableCode("var2", 0, 1, NULL);
+    af_Code *bt3 = makeElementCode("data2", 0, 0, NULL);
+    af_Code *bt4 = makeElementCode("var2", 0, 1, NULL);
 
     connectCode(&bt1, bt2);
     connectCode(&bt3, bt4);