浏览代码

fix: 修复了import的bug

SongZihuan 4 年之前
父节点
当前提交
7965029a97
共有 5 个文件被更改,包括 61 次插入28 次删除
  1. 1 0
      CMakeLists.txt
  2. 1 0
      include/macro.h
  3. 3 4
      main.c
  4. 3 5
      src/inter.c
  5. 53 19
      src/runfile.c

+ 1 - 0
CMakeLists.txt

@@ -18,3 +18,4 @@ AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/signalhandler HANDLER_LIST)
 
 
 MESSAGE("project dir is ${PROJECT_SOURCE_DIR}")
 MESSAGE("project dir is ${PROJECT_SOURCE_DIR}")
 ADD_EXECUTABLE(VirtualMath main.c ${SRC_LIST} ${GC_LIST} ${PASER_LIST} ${MEM_LIST} ${FILE_LIST} ${ARGUMENT_LIST} ${OFUNC_LIST} ${HANDLER_LIST})
 ADD_EXECUTABLE(VirtualMath main.c ${SRC_LIST} ${GC_LIST} ${PASER_LIST} ${MEM_LIST} ${FILE_LIST} ${ARGUMENT_LIST} ${OFUNC_LIST} ${HANDLER_LIST})
+TARGET_LINK_LIBRARIES(VirtualMath dlfcn)

+ 1 - 0
include/macro.h

@@ -12,6 +12,7 @@
 #include <setjmp.h>
 #include <setjmp.h>
 #include <signal.h>
 #include <signal.h>
 #include <unistd.h>
 #include <unistd.h>
+#include <dlfcn.h>
 
 
 // 布尔逻辑的定义
 // 布尔逻辑的定义
 #define bool int
 #define bool int

+ 3 - 4
main.c

@@ -1,8 +1,9 @@
 #include "__virtualmath.h"
 #include "__virtualmath.h"
 int main(int argc, char *argv[]) {
 int main(int argc, char *argv[]) {
     Inter *inter = NULL;
     Inter *inter = NULL;
-    signal(SIGINT, SIG_IGN);
 
 
+    atexit(freeArgs);
+    signal(SIGINT, SIG_IGN);
     if (setjmp(memVirtualMath_Env) == -1){
     if (setjmp(memVirtualMath_Env) == -1){
         fprintf(stderr, "ERROR: Fatal memory error encountered, May be caused by insufficient memory!\n");
         fprintf(stderr, "ERROR: Fatal memory error encountered, May be caused by insufficient memory!\n");
         return 1;
         return 1;
@@ -10,14 +11,12 @@ int main(int argc, char *argv[]) {
     memVirtualMathUseJmp = true;
     memVirtualMathUseJmp = true;
 
 
     if (getArgs(argc, argv))
     if (getArgs(argc, argv))
-        goto args_error;
+        return 2;
 
 
     inter = makeInter(args.out_file, args.error_file, args.in_file, NULL);
     inter = makeInter(args.out_file, args.error_file, args.in_file, NULL);
     runCodeFile(inter, argv + optind);
     runCodeFile(inter, argv + optind);
     if (args.run_commandLine)
     if (args.run_commandLine)
         runCodeStdin(inter);
         runCodeStdin(inter);
     freeInter(inter, true);
     freeInter(inter, true);
-
-    args_error: freeArgs();
     return 0;
     return 0;
 }
 }

+ 3 - 5
src/inter.c

@@ -3,12 +3,12 @@
 Inter *makeInter(char *out, char *error_, char *in, LinkValue *belong) {
 Inter *makeInter(char *out, char *error_, char *in, LinkValue *belong) {
     Inter *tmp = memCalloc(1, sizeof(Inter));
     Inter *tmp = memCalloc(1, sizeof(Inter));
     LinkValue *base_father = NULL;
     LinkValue *base_father = NULL;
-    setBaseInterData(tmp);
     tmp->base = NULL;
     tmp->base = NULL;
     tmp->link_base = NULL;
     tmp->link_base = NULL;
     tmp->hash_base = NULL;
     tmp->hash_base = NULL;
     tmp->base_var = NULL;
     tmp->base_var = NULL;
 
 
+    setBaseInterData(tmp);
     tmp->var_list = makeVarList(tmp, true);
     tmp->var_list = makeVarList(tmp, true);
 
 
     if (out != NULL) {
     if (out != NULL) {
@@ -212,11 +212,9 @@ void mergeInter(Inter *new, Inter *base){
     HashTable **base_hash = NULL;
     HashTable **base_hash = NULL;
     Var **base_var = NULL;
     Var **base_var = NULL;
 
 
-    gc_freeStatementLink(&new->base_father->gc_status);
-    new->base_father = NULL;
-
-    freeVarList(new->var_list);
+    gc_runDelAll(new);
     freeBaseInterData(new);
     freeBaseInterData(new);
+    freeVarList(new->var_list);
 
 
     for (base_value = &base->base; *base_value != NULL; base_value = &(*base_value)->gc_next)
     for (base_value = &base->base; *base_value != NULL; base_value = &(*base_value)->gc_next)
         PASS;
         PASS;

+ 53 - 19
src/runfile.c

@@ -46,26 +46,11 @@ ResultType includeFile(INTER_FUNCTIONSIG) {
     return result->type;
     return result->type;
 }
 }
 
 
-ResultType importFileCore(VarList **new_object, char **file_dir, INTER_FUNCTIONSIG) {
+ResultType importVMFileCore(VarList **new_object, char *file_dir, INTER_FUNCTIONSIG) {
     Inter *import_inter = NULL;
     Inter *import_inter = NULL;
     ParserMessage *pm = NULL;
     ParserMessage *pm = NULL;
     Statement *run_st = NULL;
     Statement *run_st = NULL;
     setResultCore(result);
     setResultCore(result);
-    if (operationSafeInterStatement(CALL_INTER_FUNCTIONSIG(st, var_list, result, belong)))
-        goto return_;
-
-    if (!isType(result->value->value, string)) {
-        setResultErrorSt(E_ImportException, ONLY_ACC(include file dir, string), true, st, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
-        goto return_;
-    }
-
-    *file_dir = result->value->value->data.str.str;
-    freeResult(result);
-    if (checkFile(*file_dir) != 1) {
-        setResultErrorSt(E_ImportException, "include file is not readable", true, st, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
-        goto return_;
-    }
-
 
 
     import_inter = makeInter(NULL, NULL, NULL, belong);
     import_inter = makeInter(NULL, NULL, NULL, belong);
     import_inter->data.inter_stdout = inter->data.inter_stdout;
     import_inter->data.inter_stdout = inter->data.inter_stdout;
@@ -75,8 +60,8 @@ ResultType importFileCore(VarList **new_object, char **file_dir, INTER_FUNCTIONS
     import_inter->data.is_stderr = true;
     import_inter->data.is_stderr = true;
     import_inter->data.is_stdin = true;
     import_inter->data.is_stdin = true;
 
 
-    pm = makeParserMessage(*file_dir);
-    run_st = makeStatement(0, *file_dir);
+    pm = makeParserMessage(file_dir);
+    run_st = makeStatement(0, file_dir);
     parserCommandList(pm, import_inter, true, false, run_st);
     parserCommandList(pm, import_inter, true, false, run_st);
     if (pm->status == int_error) {
     if (pm->status == int_error) {
         setResultErrorSt(E_KeyInterrupt, KEY_INTERRUPT, true, st, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
         setResultErrorSt(E_KeyInterrupt, KEY_INTERRUPT, true, st, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
@@ -92,7 +77,7 @@ ResultType importFileCore(VarList **new_object, char **file_dir, INTER_FUNCTIONS
     globalIterStatement(result, import_inter, run_st);
     globalIterStatement(result, import_inter, run_st);
     if (!CHECK_RESULT(result)) {
     if (!CHECK_RESULT(result)) {
         freeInter(import_inter, false);
         freeInter(import_inter, false);
-        result->value = makeLinkValue(inter->base, belong, inter);  // 重新设定none值
+        result->value = makeLinkValue(makeNoneValue(inter), belong, inter);
         setResultErrorSt(E_BaseException, NULL, false, st, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
         setResultErrorSt(E_BaseException, NULL, false, st, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
         goto return_;
         goto return_;
     }
     }
@@ -108,6 +93,55 @@ ResultType importFileCore(VarList **new_object, char **file_dir, INTER_FUNCTIONS
     return result->type;
     return result->type;
 }
 }
 
 
+ResultType importShareFileCore(VarList **new_object, char *file_dir, INTER_FUNCTIONSIG) {
+    Inter *import_inter;
+    setResultCore(result);
+    void* handle = dlopen(file_dir, RTLD_LAZY);
+    Registered reg;
+    reg = dlsym(handle, "registered");
+
+    import_inter = makeInter(NULL, NULL, NULL, belong);
+    import_inter->data.inter_stdout = inter->data.inter_stdout;
+    import_inter->data.inter_stderr = inter->data.inter_stderr;
+    import_inter->data.inter_stdin = inter->data.inter_stdin;
+    import_inter->data.is_stdout = true;
+    import_inter->data.is_stderr = true;
+    import_inter->data.is_stdin = true;
+
+    reg(belong, import_inter, import_inter->var_list);
+    *new_object = import_inter->var_list;
+    import_inter->var_list = NULL;
+    mergeInter(import_inter, inter);
+    setResult(result, inter, belong);
+    return result->type;
+}
+
+ResultType importFileCore(VarList **new_object, char **file_dir, INTER_FUNCTIONSIG) {
+    setResultCore(result);
+    if (operationSafeInterStatement(CALL_INTER_FUNCTIONSIG(st, var_list, result, belong)))
+        return result->type;
+
+    if (!isType(result->value->value, string)) {
+        setResultErrorSt(E_ImportException, ONLY_ACC(include file dir, string), true, st, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
+        return error_return;
+    }
+
+    *file_dir = result->value->value->data.str.str;
+    freeResult(result);
+    if (checkFile(*file_dir) != 1) {
+        setResultErrorSt(E_ImportException, "import file is not readable", true, st, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
+        return error_return;
+    }
+    {
+        char *file = strrchr(*file_dir, '.');
+        if (eqString(file, ".vm"))
+            importVMFileCore(new_object, *file_dir, CALL_INTER_FUNCTIONSIG(st, var_list, result, belong));
+        else
+            importShareFileCore(new_object, *file_dir, CALL_INTER_FUNCTIONSIG(st, var_list, result, belong));
+    }
+    return result->type;
+}
+
 ResultType importFile(INTER_FUNCTIONSIG) {
 ResultType importFile(INTER_FUNCTIONSIG) {
     char *file_dir = NULL;
     char *file_dir = NULL;
     VarList *new_object = NULL;
     VarList *new_object = NULL;