Browse Source

feat: struct允许通过下标获取元素

link #7
SongZihuan 4 years ago
parent
commit
e94424a8fd
4 changed files with 32 additions and 2 deletions
  1. 1 0
      vmcore/include/macro.h
  2. 1 1
      vmcore/ofunc/src/str.c
  3. 30 0
      vmcore/ofunc/src/struct_.c
  4. 0 1
      vmcore/src/runfile.c

+ 1 - 0
vmcore/include/macro.h

@@ -21,6 +21,7 @@
 #include <locale.h>
 #include <getopt.h>
 #include <unistd.h>
+#include "direct.h"
 #include <dlfcn.h>
 #include <ffi.h>
 

+ 1 - 1
vmcore/ofunc/src/str.c

@@ -117,7 +117,7 @@ static ResultType str_down(O_FUNC){
         return R_error;
     }
     if (ap[1].value->value->type != V_int){
-        setResultError(E_TypeException, ONLY_ACC(str index, V_int), LINEFILE, true, CNEXT_NT);
+        setResultError(E_TypeException, ONLY_ACC(str index, int), LINEFILE, true, CNEXT_NT);
         return R_error;
     }
 

+ 30 - 0
vmcore/ofunc/src/struct_.c

@@ -71,10 +71,40 @@ static ResultType struct_init(O_FUNC){
     return result->type;
 }
 
+static ResultType struct_down(O_FUNC){
+    ArgumentParser ap[] = {{.type=only_value, .must=1, .long_arg=false},
+                           {.type=only_value, .must=1, .long_arg=false},
+                           {.must=-1}};
+    vint size;
+    vint index;
+    setResultCore(result);
+    parserArgumentUnion(ap, arg, CNEXT_NT);
+    if (!CHECK_RESULT(result))
+        return result->type;
+    freeResult(result);
+
+    if (ap[0].value->value->type != V_struct){
+        setResultError(E_TypeException, INSTANCE_ERROR(struct), LINEFILE, true, CNEXT_NT);
+        return R_error;
+    }
+    if (ap[1].value->value->type != V_int){
+        setResultError(E_TypeException, ONLY_ACC(struct index, int), LINEFILE, true, CNEXT_NT);
+        return R_error;
+    }
+
+    size = ap[0].value->value->data.struct_.len;
+    index = ap[1].value->value->data.int_.num;
+    if (!checkIndex(&index, &size, CNEXT_NT))
+        return result->type;
+    makeStructValue(ap[0].value->value->data.struct_.data + index, 1, LINEFILE, CNEXT_NT);
+    return result->type;
+}
+
 void registeredStruct(R_FUNC){
     LinkValue *object = inter->data.base_obj[B_BOOL];
     NameFunc tmp[] = {{inter->data.mag_func[M_NEW], struct_new, fp_class, .var=nfv_notpush},
                       {inter->data.mag_func[M_INIT], struct_init, fp_obj, .var=nfv_notpush},
+                      {inter->data.mag_func[M_DOWN], struct_down, fp_obj, .var=nfv_notpush},
                       {NULL, NULL}};
     gc_addTmpLink(&object->gc_status);
     addBaseClassVar(L"struct", object, belong, inter);

+ 0 - 1
vmcore/src/runfile.c

@@ -62,7 +62,6 @@ int checkFileDir(char **file_dir, FUNC) {
 
     if (isExist(file_dir, false, "__init__.vm"))
         return 1;
-
     {
         char arr_cwd[200];
         char *p_cwd = NULL;