Browse Source

feat: code.block使用is_empty字段代替elements参数

SongZihuan 3 years ago
parent
commit
9bcd4008d5
5 changed files with 19 additions and 16 deletions
  1. 1 1
      src/core/__code.h
  2. 16 13
      src/core/code.c
  3. 2 2
      src/core/env.c
  4. BIN
      test/af/test1.aub
  5. BIN
      test/af/test2.aub

+ 1 - 1
src/core/__code.h

@@ -18,7 +18,7 @@ struct af_Code {  // 一个 Code 的结构体
         } element;
 
         struct {
-            CodeUInt elements;  // 元素个数 (主要作用是检查是否为空)
+            bool is_empty;
             enum af_BlockType type;  // 括号类型
         } block;
     };

+ 16 - 13
src/core/code.c

@@ -29,8 +29,6 @@ static bool checkElementData(char *data);
 static char *codeToStr_(af_Code *code, LayerInt *layer, struct af_BlockEnd **bn);
 static char *codeEndToStr(CodeUInt code_end, LayerInt *layer, struct af_BlockEnd **bn);
 
-#define LAYER_ADD1(code) ((code)->type == code_block && (code)->block.elements != 0)
-
 static af_Code *makeCode(char prefix, FileLine line, FilePath path) {
     af_Code *bt = calloc(1, sizeof(af_Code));
     bt->line = line;
@@ -50,6 +48,10 @@ af_Code *makeElementCode(char *var, char prefix, FileLine line, FilePath path) {
     return bt;
 }
 
+/* 判断该code是否令layer需要加1 */
+/* 即判定是否有子元素 */
+#define LAYER_ADD1(code) ((code)->type == code_block && !(code)->block.is_empty)
+
 /*
  * 函数名: countElement
  * 目标: 统计元素个数(不包括元素的子元素
@@ -99,10 +101,11 @@ af_Code *makeBlockCode(enum af_BlockType type, af_Code *element, char prefix, Fi
     bt = makeCode(prefix, line, path);
     bt->type = code_block;
     bt->block.type = type;
-    bt->block.elements = elements;
     bt->next = element;
-    if (*next != NULL)
-        (*next)->code_end++;
+    if (elements == 0)
+        bt->block.is_empty = true;
+    else
+        (*next)->code_end++;  // 若 elements 不为 0, 则next指向最后一个元素
     return bt;
 }
 
@@ -130,7 +133,7 @@ af_Code *copyAllCode(af_Code *base, FilePath *path) {
                 (*pdest)->element.data = strCopy(base->element.data);
                 break;
             case code_block:
-                (*pdest)->block.elements = base->block.elements;
+                (*pdest)->block.is_empty = base->block.is_empty;
                 (*pdest)->block.type = base->block.type;
                 break;
             default:
@@ -163,7 +166,7 @@ af_Code *copyCode(af_Code *base, FilePath *path) {
                 (*pdest)->element.data = strCopy(base->element.data);
                 break;
             case code_block:
-                (*pdest)->block.elements = base->block.elements;
+                (*pdest)->block.is_empty = base->block.is_empty;
                 (*pdest)->block.type = base->block.type;
                 break;
             default:
@@ -249,7 +252,7 @@ static bool writeCode(af_Code *bt, FILE *file) {
             break;
         case code_block:
             Done(byteWriteUint_8(file, bt->block.type));
-            Done(byteWriteUint_32(file, bt->block.elements));
+            Done(byteWriteUint_8(file, bt->block.is_empty));
             break;
         default:
             break;
@@ -308,11 +311,11 @@ static bool readCode(af_Code **bt, FILE *file) {
             break;
         case code_block: {
             uint8_t block_type;
-            uint32_t elements;
+            uint8_t is_empty;
             Done(byteReadUint_8(file, &block_type));
-            Done(byteReadUint_32(file,&elements));
+            Done(byteReadUint_8(file,&is_empty));
             (*bt)->block.type = block_type;
-            (*bt)->block.elements = (CodeUInt)elements;
+            (*bt)->block.is_empty = (CodeUInt)is_empty;
             break;
         }
         default:
@@ -379,7 +382,7 @@ static char *codeToStr_(af_Code *code, LayerInt *layer, struct af_BlockEnd **bn)
             re = strJoin(re, "| ", true, false);
         } else
             re = strJoin(re, code->element.data, true, false);
-    } else if (code->block.elements == 0) {
+    } else if (code->block.is_empty) {
         switch(code->block.type) {
             case parentheses:
                 re = strJoin(re, "()", true, false);
@@ -481,7 +484,7 @@ void printCode(af_Code *bt) {
             case code_block:
                 if (LAYER_ADD1(bt))
                     layer++;
-                printf("code: [prefix (%c)] [end %u] [type %c] [elements %u]\n", bt->prefix, bt->code_end, bt->block.type, bt->block.elements);
+                printf("code: [prefix (%c)] [end %u] [type %c] [empty %d]\n", bt->prefix, bt->code_end, bt->block.type, bt->block.is_empty);
                 break;
             default:
                 printf("Unknown: [prefix (%c)] [end %u] [type %d]\n", bt->prefix, bt->code_end, bt->type);

+ 2 - 2
src/core/env.c

@@ -740,7 +740,7 @@ static bool isInfixFunc(af_Code *code, af_Environment *env) {
 bool pushExecutionActivity(af_Code *bt, bool return_first, af_Environment *env) {
     af_Code *next;
     next = getCodeNext(bt);
-    if (bt->type != code_block || bt->block.elements == 0) {
+    if (bt->type != code_block || bt->block.is_empty) {
         pushMessageDown(makeERRORMessage(SYNTAX_ERROR, NOT_CODE_INFO, env), env);
         return false;
     }
@@ -764,7 +764,7 @@ bool pushFuncActivity(af_Code *bt, af_Environment *env) {
     next = getCodeNext(bt);
     switch (bt->block.type) {
         case curly:
-            if (bt->block.elements == 0) {
+            if (bt->block.is_empty) {
                 pushMessageDown(makeERRORMessage(CALL_ERROR, CURLY_FUNC_BODY_INFO, env), env);
                 return false;
             }

BIN
test/af/test1.aub


BIN
test/af/test2.aub