ソースを参照

feat: 添加__str__函数用于str()

SongZihuan 4 年 前
コミット
b6994f3758
10 ファイル変更59 行追加27 行削除
  1. 1 0
      include/inter.h
  2. 16 7
      ofunc/src/dict.c
  3. 1 1
      ofunc/src/io.c
  4. 15 6
      ofunc/src/list.c
  5. 15 5
      ofunc/src/object.c
  6. 3 3
      ofunc/src/str.c
  7. 1 0
      ofunc/src/vobject.c
  8. 4 4
      src/__run.c
  9. 1 1
      src/include/__run.h
  10. 2 0
      src/inter.c

+ 1 - 0
include/inter.h

@@ -79,6 +79,7 @@ struct Inter{
         char *object_father;
         char *object_message;
         char *object_down_assignment;
+        char *object_str;
         int default_pt_type;
     } data;
 };

+ 16 - 7
ofunc/src/dict.c

@@ -100,7 +100,7 @@ ResultType dict_iter(OFFICAL_FUNCTIONSIG){
     return result->type;
 }
 
-ResultType dict_repo(OFFICAL_FUNCTIONSIG){
+ResultType dictRepoStrCore(OFFICAL_FUNCTIONSIG, bool is_repo){
     ArgumentParser ap[] = {{.type=only_value, .must=1, .long_arg=false},
                            {.must=-1}};
     char *repo = NULL;
@@ -114,10 +114,10 @@ ResultType dict_repo(OFFICAL_FUNCTIONSIG){
     value = ap[0].value->value;
 
     if (value->type != dict){
-        setResultError(E_TypeException, "dict.__repo__ gets unsupported data", 0, "sys", true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
+        setResultError(E_TypeException, "dict.__repo__/__str__ gets unsupported data", 0, "sys", true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
         return error_return;
     }
-    again = findAttributes("repo_again", false, ap[0].value, inter);
+    again = findAttributes(is_repo ? "repo_again" : "str_again", false, ap[0].value, inter);
     if (again != NULL){
         bool again_ = checkBool(again, 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
         if (!CHECK_RESULT(result))
@@ -128,7 +128,7 @@ ResultType dict_repo(OFFICAL_FUNCTIONSIG){
         }
     }
 
-    addAttributes("repo_again", false, makeLinkValue(makeBoolValue(true, inter), belong, inter), ap[0].value, inter);
+    addAttributes(is_repo ? "repo_again" : "str_again", false, makeLinkValue(makeBoolValue(true, inter), belong, inter), ap[0].value, inter);
     repo = memStrcpy("{");
     for (int i = 0, count = 0; i < MAX_SIZE; i++) {
         for (Var *var = value->data.dict.dict->hashtable[i]; var != NULL; var = var->next, count++) {
@@ -138,14 +138,14 @@ ResultType dict_repo(OFFICAL_FUNCTIONSIG){
                 repo = memStrcat(repo, ", ", true, false);
 
             freeResult(result);
-            name_tmp = getRepo(var->name_, 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
+            name_tmp = getRepoStr(var->name_, is_repo, 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
             if (!CHECK_RESULT(result))
                 goto return_;
             repo = memStrcat(repo, name_tmp, true, false);
             repo = memStrcat(repo, ": ", true, false);
 
             freeResult(result);
-            value_tmp = getRepo(var->value, 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
+            value_tmp = getRepoStr(var->value, is_repo, 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
             if (!CHECK_RESULT(result))
                 goto return_;
             repo = memStrcat(repo, value_tmp, true, false);
@@ -156,17 +156,26 @@ ResultType dict_repo(OFFICAL_FUNCTIONSIG){
     setResultOperation(result, makeLinkValue(makeStringValue(repo, inter), belong, inter));
 
     return_:
-    addAttributes("repo_again", false, makeLinkValue(makeBoolValue(false, inter), belong, inter), ap[0].value, inter);
+    addAttributes(is_repo ? "repo_again" : "str_again", false, makeLinkValue(makeBoolValue(false, inter), belong, inter), ap[0].value, inter);
     memFree(repo);
     return result->type;
 }
 
+ResultType dict_repo(OFFICAL_FUNCTIONSIG){
+    return dictRepoStrCore(CALL_OFFICAL_FUNCTION(arg, var_list, result, belong), true);
+}
+
+ResultType dict_str(OFFICAL_FUNCTIONSIG){
+    return dictRepoStrCore(CALL_OFFICAL_FUNCTION(arg, var_list, result, belong), false);
+}
+
 void registeredDict(REGISTERED_FUNCTIONSIG){
     LinkValue *object = makeLinkValue(inter->data.dict, inter->base_father, inter);
     NameFunc tmp[] = {{"keys", dict_keys, object_free_},
                       {inter->data.object_down, dict_down, object_free_},
                       {inter->data.object_iter, dict_iter, object_free_},
                       {inter->data.object_repo, dict_repo, object_free_},
+                      {inter->data.object_str, dict_str, object_free_},
                       {inter->data.object_down_assignment, dict_down_assignment, object_free_},
                       {NULL, NULL}};
     gc_addTmpLink(&object->gc_status);

+ 1 - 1
ofunc/src/io.c

@@ -13,7 +13,7 @@ ResultType vm_print(OFFICAL_FUNCTIONSIG){
     arg = ap[0].arg;
     for (int i=0; i < ap[0].c_count; arg = arg->next,i++){
         freeResult(result);
-        char *tmp = getRepo(arg->data.value, 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
+        char *tmp = getRepoStr(arg->data.value, true, 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
         if (!CHECK_RESULT(result))
             return result->type;
         if (i != 0)

+ 15 - 6
ofunc/src/list.c

@@ -171,7 +171,7 @@ ResultType list_iter(OFFICAL_FUNCTIONSIG){
     return result->type;
 }
 
-ResultType list_repo(OFFICAL_FUNCTIONSIG){
+ResultType listRepoStrCore(OFFICAL_FUNCTIONSIG, bool is_repo){
     ArgumentParser ap[] = {{.type=only_value, .must=1, .long_arg=false},
                            {.must=-1}};
     char *repo = NULL;
@@ -186,11 +186,11 @@ ResultType list_repo(OFFICAL_FUNCTIONSIG){
     value = ap[0].value->value;
 
     if (value->type != list){
-        setResultError(E_TypeException, "list.__repo__ gets unsupported data", 0, "sys", true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
+        setResultError(E_TypeException, "list.__repo__/__list__ gets unsupported data", 0, "sys", true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
         return error_return;
     }
     lt = value->data.list.type;
-    again = findAttributes("repo_again", false, ap[0].value, inter);
+    again = findAttributes(is_repo ? "repo_again" : "str_again", false, ap[0].value, inter);
     if (again != NULL){
         bool again_ = checkBool(again, 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
         if (!CHECK_RESULT(result))
@@ -201,7 +201,7 @@ ResultType list_repo(OFFICAL_FUNCTIONSIG){
         }
     }
 
-    addAttributes("repo_again", false, makeLinkValue(makeBoolValue(true, inter), belong, inter), ap[0].value, inter);
+    addAttributes(is_repo ? "repo_again" : "str_again", false, makeLinkValue(makeBoolValue(true, inter), belong, inter), ap[0].value, inter);
     if (lt == value_list)
         repo = memStrcpy("[");
     else
@@ -211,7 +211,7 @@ ResultType list_repo(OFFICAL_FUNCTIONSIG){
         freeResult(result);
         if (i > 0)
             repo = memStrcat(repo, ", ", true, false);
-        tmp = getRepo(value->data.list.list[i], 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
+        tmp = getRepoStr(value->data.list.list[i], is_repo, 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
         if (!CHECK_RESULT(result))
             goto return_;
         repo = memStrcat(repo, tmp, true, false);
@@ -223,11 +223,19 @@ ResultType list_repo(OFFICAL_FUNCTIONSIG){
     setResultOperation(result, makeLinkValue(makeStringValue(repo, inter), belong, inter));
 
     return_:
-    addAttributes("repo_again", false, makeLinkValue(makeBoolValue(false, inter), belong, inter), ap[0].value, inter);
+    addAttributes(is_repo ? "repo_again" : "str_again", false, makeLinkValue(makeBoolValue(false, inter), belong, inter), ap[0].value, inter);
     memFree(repo);
     return result->type;
 }
 
+ResultType list_repo(OFFICAL_FUNCTIONSIG){
+    return listRepoStrCore(CALL_OFFICAL_FUNCTION(arg, var_list, result, belong), true);
+}
+
+ResultType list_str(OFFICAL_FUNCTIONSIG){
+    return listRepoStrCore(CALL_OFFICAL_FUNCTION(arg, var_list, result, belong), false);
+}
+
 void registeredList(REGISTERED_FUNCTIONSIG){
     {
         LinkValue *object = makeLinkValue(inter->data.tuple, inter->base_father, inter);
@@ -235,6 +243,7 @@ void registeredList(REGISTERED_FUNCTIONSIG){
                           {inter->data.object_slice, list_slice, object_free_},
                           {inter->data.object_iter, list_iter, object_free_},
                           {inter->data.object_repo, list_repo, object_free_},
+                          {inter->data.object_str, list_str, object_free_},
                           {NULL, NULL}};
         gc_addTmpLink(&object->gc_status);
         addStrVar("tuple", false, true, object, belong, CALL_INTER_FUNCTIONSIG_CORE(inter->var_list));

+ 15 - 5
ofunc/src/object.c

@@ -1,6 +1,6 @@
 #include "__ofunc.h"
 
-ResultType object_new_(OFFICAL_FUNCTIONSIG){
+ResultType object_new(OFFICAL_FUNCTIONSIG){
     LinkValue *value = NULL;
     LinkValue *_init_ = NULL;
     setResultCore(result);
@@ -44,7 +44,7 @@ ResultType object_new_(OFFICAL_FUNCTIONSIG){
     return result->type;
 }
 
-ResultType object_repo_(OFFICAL_FUNCTIONSIG){
+ResultType objectRepoStrCore(OFFICAL_FUNCTIONSIG, bool is_repo){
     ArgumentParser ap[] = {{.type=only_value, .must=1, .long_arg=false},
                            {.must=-1}};
     char *repo;
@@ -61,7 +61,7 @@ ResultType object_repo_(OFFICAL_FUNCTIONSIG){
     name_value = findAttributes(inter->data.object_name, false, ap[0].value, inter);
     if (name_value != NULL){
         gc_addTmpLink(&name_value->gc_status);
-        name = getRepo(name_value, 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
+        name = getRepoStr(name_value, is_repo, 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
         gc_freeTmpLink(&name_value->gc_status);
         if (!CHECK_RESULT(result))
             return result->type;
@@ -79,13 +79,23 @@ ResultType object_repo_(OFFICAL_FUNCTIONSIG){
     snprintf(repo, len, "(%s: %s on %p)", type, name, ap[0].value->value);
     setResultOperationBase(result, makeLinkValue(makeStringValue(repo, inter), belong, inter));
     memFree(repo);
+
     return result->type;
 }
 
+ResultType object_repo(OFFICAL_FUNCTIONSIG){
+    return objectRepoStrCore(CALL_OFFICAL_FUNCTION(arg, var_list, result, belong), true);
+}
+
+ResultType object_str(OFFICAL_FUNCTIONSIG){
+    return objectRepoStrCore(CALL_OFFICAL_FUNCTION(arg, var_list, result, belong), false);
+}
+
 void registeredObject(REGISTERED_FUNCTIONSIG){
     LinkValue *object = makeLinkValue(inter->data.object, inter->base_father, inter);
-    NameFunc tmp[] = {{inter->data.object_new, object_new_, class_free_},
-                      {inter->data.object_repo, object_repo_, all_free_},
+    NameFunc tmp[] = {{inter->data.object_new,  object_new,  class_free_},
+                      {inter->data.object_repo, object_repo, all_free_},
+                      {inter->data.object_str,  object_str,  all_free_},
                       {NULL, NULL}};
     gc_addTmpLink(&object->gc_status);
     addStrVar("object", false, true, object, belong, CALL_INTER_FUNCTIONSIG_CORE(inter->var_list));

+ 3 - 3
ofunc/src/str.c

@@ -5,7 +5,7 @@ ResultType str_init(OFFICAL_FUNCTIONSIG){
                            {.type=only_value, .must=1, .long_arg=false},
                            {.must=-1}};
     LinkValue *base;
-    char *repo = NULL;
+    char *str = NULL;
     setResultCore(result);
     parserArgumentUnion(ap, arg, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
     if (!CHECK_RESULT(result))
@@ -13,11 +13,11 @@ ResultType str_init(OFFICAL_FUNCTIONSIG){
     freeResult(result);
 
     base = ap[0].value;
-    repo = getRepo(ap[1].value, 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
+    str = getRepoStr(ap[1].value, false, 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
     if (!CHECK_RESULT(result))
         return result->type;
     base->value->type = string;
-    base->value->data.str.str = memStrcpy(repo);
+    base->value->data.str.str = memStrcpy(str);
     setResult(result, inter, belong);
     return result->type;
 }

+ 1 - 0
ofunc/src/vobject.c

@@ -197,6 +197,7 @@ void registeredVObject(REGISTERED_FUNCTIONSIG){
                       {inter->data.object_div, vobject_div, object_free_},
                       {inter->data.object_bool, vobject_bool, object_free_},
                       {inter->data.object_repo, vobject_repo, object_free_},
+                      {inter->data.object_str, vobject_repo, object_free_},
                       {NULL, NULL}};
     gc_addTmpLink(&object->gc_status);
     addStrVar("vobject", false, true, object, belong, CALL_INTER_FUNCTIONSIG_CORE(inter->var_list));

+ 4 - 4
src/__run.c

@@ -323,8 +323,8 @@ bool checkBool(LinkValue *value, fline line, char *file, INTER_FUNCTIONSIG_NOT_S
     return false;
 }
 
-char *getRepo(LinkValue *value, fline line, char *file, INTER_FUNCTIONSIG_NOT_ST){
-    LinkValue *_repo_ = findAttributes(inter->data.object_repo, false, value, inter);
+char *getRepoStr(LinkValue *value, bool is_repot, fline line, char *file, INTER_FUNCTIONSIG_NOT_ST){
+    LinkValue *_repo_ = findAttributes(is_repot ? inter->data.object_repo : inter->data.object_str, false, value, inter);
     setResultCore(result);
     if (_repo_ != NULL){
         gc_addTmpLink(&value->gc_status);
@@ -336,12 +336,12 @@ char *getRepo(LinkValue *value, fline line, char *file, INTER_FUNCTIONSIG_NOT_ST
         if (!CHECK_RESULT(result))
             return NULL;
         else if (result->value->value->type != string){
-            setResultError(E_TypeException, "__repo__ function should return str type data", line, file, true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
+            setResultError(E_TypeException, "__repo__/__str__ function should return str type data", line, file, true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
             return NULL;
         }
         return result->value->value->data.str.str;
     }
     else
-        setResultError(E_TypeException, "list.__repo__ gets unsupported data", line, file, true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
+        setResultError(E_TypeException, "__repo__/__str__ gets unsupported data", line, file, true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
     return NULL;
 }

+ 1 - 1
src/include/__run.h

@@ -38,5 +38,5 @@ 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(LinkValue *value, fline line, char *file, INTER_FUNCTIONSIG_NOT_ST);
-char *getRepo(LinkValue *value, fline line, char *file, INTER_FUNCTIONSIG_NOT_ST);
+char *getRepoStr(LinkValue *value, bool is_repot, fline line, char *file, INTER_FUNCTIONSIG_NOT_ST);
 #endif //VIRTUALMATH___RUN_H

+ 2 - 0
src/inter.c

@@ -98,6 +98,7 @@ void setBaseInterData(struct Inter *inter){
     inter->data.object_father = memStrcpy("__father__");
     inter->data.object_message = memStrcpy("__message__");
     inter->data.object_down_assignment = memStrcpy("__down_assignment__");
+    inter->data.object_str = memStrcpy("__str__");
     inter->data.default_pt_type = free_;
 }
 
@@ -164,6 +165,7 @@ void freeBaseInterData(struct Inter *inter){
     memFree(inter->data.object_father);
     memFree(inter->data.object_message);
     memFree(inter->data.object_down_assignment);
+    memFree(inter->data.object_str);
 
     if (!inter->data.is_stdout)
         fclose(inter->data.inter_stdout);