Parcourir la source

feat: 添加bool类型的__init__

SongZihuan il y a 4 ans
Parent
commit
7d4d9355ed
4 fichiers modifiés avec 42 ajouts et 22 suppressions
  1. 21 2
      ofunc/src/bool.c
  2. 20 0
      src/__run.c
  3. 1 0
      src/include/__run.h
  4. 0 20
      src/runbranch.c

+ 21 - 2
ofunc/src/bool.c

@@ -1,11 +1,30 @@
 #include "__ofunc.h"
 
+ResultType bool_init(OFFICAL_FUNCTIONSIG){
+    ArgumentParser ap[] = {{.type=only_value, .must=1, .long_arg=false},
+                           {.type=only_value, .must=1, .long_arg=false},
+                           {.must=-1}};
+    LinkValue *base;
+    setResultCore(result);
+    parserArgumentUnion(ap, arg, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
+    if (!CHECK_RESULT(result))
+        return result->type;
+    freeResult(result);
+
+    base = ap[0].value;
+    base->value->type = bool_;
+    base->value->data.bool_.bool_ = checkBool(ap[1].value->value);
+    setResultBase(result, inter, belong);
+    return result->type;
+}
+
 void registeredBool(REGISTERED_FUNCTIONSIG){
     LinkValue *object = makeLinkValue(inter->data.bool_, inter->base_father, inter);
-//    NameFunc tmp[] = {{NULL, NULL}};
+    NameFunc tmp[] = {{"__init__", bool_init, object_free_},
+                      {NULL, NULL}};
     gc_addTmpLink(&object->gc_status);
     addStrVar("bool", false, true, object, belong, CALL_INTER_FUNCTIONSIG_CORE(inter->var_list));
-//    iterClassFunc(tmp, object, CALL_INTER_FUNCTIONSIG_CORE(inter->var_list));
+    iterClassFunc(tmp, object, CALL_INTER_FUNCTIONSIG_CORE(inter->var_list));
     gc_freeTmpLink(&object->gc_status);
 }
 

+ 20 - 0
src/__run.c

@@ -280,3 +280,23 @@ ResultType getIter(LinkValue *value, int status, fline line, char *file, INTER_F
 
     return result->type;
 }
+
+bool checkBool(Value *value){
+    switch (value->type) {
+        case number:
+            return value->data.num.num != 0;
+        case string:
+            return memStrlen(value->data.str.str) > 0;
+        case bool_:
+            return value->data.bool_.bool_;
+        case pass_:
+        case none:
+            return false;
+        case list:
+            return value->data.list.size > 0;
+        case dict:
+            return value->data.dict.size > 0;
+        default:
+            return true;
+    }
+}

+ 1 - 0
src/include/__run.h

@@ -37,4 +37,5 @@ void addAttributes(char *name, bool free_old, LinkValue *value, LinkValue *belon
 void newObjectSetting(LinkValue *name, LinkValue *belong, Inter *inter);
 ResultType elementDownOne(LinkValue *element, LinkValue *index, fline line, char *file, INTER_FUNCTIONSIG_NOT_ST);
 ResultType getIter(LinkValue *value, int status, fline line, char *file, INTER_FUNCTIONSIG_NOT_ST);
+bool checkBool(Value *value);
 #endif //VIRTUALMATH___RUN_H

+ 0 - 20
src/runbranch.c

@@ -16,26 +16,6 @@ static bool checkString(INTER_FUNCTIONSIG){
     return true;
 }
 
-static bool checkBool(Value *value){
-    switch (value->type) {
-        case number:
-            return value->data.num.num != 0;
-        case string:
-            return memStrlen(value->data.str.str) > 0;
-        case bool_:
-            return value->data.bool_.bool_;
-        case pass_:
-        case none:
-            return false;
-        case list:
-            return value->data.list.size > 0;
-        case dict:
-            return value->data.dict.size > 0;
-        default:
-            return true;
-    }
-}
-
 void newBranchYield(Statement *branch_st, Statement *node, StatementList *sl_node, VarList *new_var, enum StatementInfoStatus status, Inter *inter){
     if (new_var != NULL)
         new_var->next = NULL;