Przeglądaj źródła

refactor: 暴露公开API

公开的API暴露在virtualmath.h文件中
修改CMakeLists.txt文件
把VirtualMathCore编译为静态库
SongZihuan 4 lat temu
rodzic
commit
96610ca350
11 zmienionych plików z 161 dodań i 120 usunięć
  1. 4 2
      CMakeLists.txt
  2. 1 11
      include/__macro.h
  3. 1 9
      include/__virtualmath.h
  4. 16 0
      include/arguement.h
  5. 4 1
      include/inter.h
  6. 15 0
      include/macro.h
  7. 1 0
      include/value.h
  8. 10 0
      include/virtualmath.h
  9. 4 97
      main.c
  10. 78 0
      src/argument.c
  11. 27 0
      src/inter.c

+ 4 - 2
CMakeLists.txt

@@ -10,5 +10,7 @@ AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/memory MEM_LIST)
 AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/parser PASER_LIST)
 AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/src SRC_LIST)
 
-message("project dir is ${PROJECT_SOURCE_DIR}")
-ADD_EXECUTABLE(VirtualMath main.c ${SRC_LIST} ${PASER_LIST} ${MEM_LIST})
+MESSAGE("project dir is ${PROJECT_SOURCE_DIR}")
+ADD_LIBRARY(VirtualMathCore STATIC ${SRC_LIST} ${PASER_LIST} ${MEM_LIST})
+ADD_EXECUTABLE(VirtualMath main.c)
+TARGET_LINK_LIBRARIES(VirtualMath VirtualMathCore)

+ 1 - 11
include/__macro.h

@@ -1,16 +1,6 @@
 #ifndef VIRTUALMATH___MACRO_H
 #define VIRTUALMATH___MACRO_H
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <getopt.h>
-
-// 布尔逻辑的定义
-#define bool int
-#define true 1
-#define false 0
+#include "macro.h"
 
 // PASS语句的定义
 #define PASS do{}while(0)

+ 1 - 9
include/__virtualmath.h

@@ -14,14 +14,6 @@
 #include "syntax.h"
 #include "grammar.h"
 #include "log.h"
-
-struct Args{
-    char *file;
-    char *log_file;
-    int level;
-    bool stdout_inter;
-} args;
-
-typedef struct Args Args;
+#include "arguement.h"
 
 #endif //VIRTUALMATH___VIRTUALMATH_H

+ 16 - 0
include/arguement.h

@@ -0,0 +1,16 @@
+#ifndef VIRTUALMATH_ARGUEMENT_H
+#define VIRTUALMATH_ARGUEMENT_H
+
+struct Args{
+    char *file;
+    char *log_file;
+    int level;
+    bool stdout_inter;
+} args;
+
+typedef struct Args Args;
+
+int getArgs(int argc, char *argv[]);
+void freeArgs();
+
+#endif //VIRTUALMATH_ARGUEMENT_H

+ 4 - 1
include/inter.h

@@ -1,6 +1,8 @@
 #ifndef VIRTUALMATH_INTER_H
 #define VIRTUALMATH_INTER_H
 
+struct Result;
+
 struct Inter{
     struct Value *base;
     struct LinkValue *link_base;
@@ -21,5 +23,6 @@ typedef struct Inter Inter;
 Inter *makeInter(char *debug);
 void freeInter(Inter *inter, bool self);
 void setBaseInterData(struct Inter *inter);
-
+Inter *newInter(char *code_file, char *debug_dir, struct Result *global_result);
+Inter *runBaseInter(char *code_file, char *debug_dir);
 #endif //VIRTUALMATH_INTER_H

+ 15 - 0
include/macro.h

@@ -0,0 +1,15 @@
+#ifndef VIRTUALMATH_MACRO_H
+#define VIRTUALMATH_MACRO_H
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <getopt.h>
+
+// 布尔逻辑的定义
+#define bool int
+#define true 1
+#define false 0
+
+#endif //VIRTUALMATH_MACRO_H

+ 1 - 0
include/value.h

@@ -1,5 +1,6 @@
 #ifndef VIRTUALMATH_VALUE_H
 #define VIRTUALMATH_VALUE_H
+#include "__macro.h"
 
 struct VarList;
 struct Argument;

+ 10 - 0
include/virtualmath.h

@@ -0,0 +1,10 @@
+#ifndef VIRTUALMATH_VIRTUALMATH_H
+#define VIRTUALMATH_VIRTUALMATH_H
+#include "macro.h"
+#include "arguement.h"
+
+typedef struct Inter Inter;
+typedef struct Result Result;
+struct Inter *runBaseInter(char *code_file, char *debug_dir);
+void freeInter(Inter *inter, int self);
+#endif //VIRTUALMATH_VIRTUALMATH_H

+ 4 - 97
main.c

@@ -1,52 +1,13 @@
-#include "__virtualmath.h"
-
-static const struct option long_option[]={
-        {"input",required_argument,NULL,'i'},
-        {"stdout",no_argument,NULL,'s'},
-
-#if OUT_LOG
-        {"log",required_argument,NULL,'l'},
-        {"debug",no_argument,&args.level,DEBUG},
-        {"ddebug",no_argument,&args.level,DEEP_DEBUG},
-        {"ldebug",no_argument,&args.level,LEXICAL_DEBUG},
-        {"lcdebug",no_argument,&args.level,LEXICAL_CHECKOUT_DEBUG},
-        {"info",no_argument,&args.level,INFO},
-#endif
-        // 最后一个元素应该全为0
-        {NULL,0,NULL,0}
-};
-
-#if OUT_LOG
-static const char *short_option = "si:l:";
-#else
-static const char *short_option = "si:";
-#endif
-int getArgs(int argc, char *argv[]);
-void freeArgs();
+#include "virtualmath.h"
 
 int main(int argc, char *argv[]) {
+    Inter *global_inter = NULL;
+
     if (getArgs(argc, argv))
         goto args_error_;
 
-    Inter *global_inter = makeInter(args.log_file);
-    ParserMessage *pm = makeParserMessage(args.file, args.log_file);
+    global_inter = runBaseInter(args.file, args.log_file);
 
-    parserCommandList(pm, global_inter, true, global_inter->statement);
-    if (pm->status != success){
-        writeLog(pm->paser_debug, ERROR, "Syntax Error: %s\n", pm->status_message);
-        writeLog(stderr, ERROR, "Syntax Error: %s\n", pm->status_message);
-        goto return_;
-    }
-
-    Result tmp;
-    tmp = globalIterStatement(global_inter);
-    if (tmp.type == error_return){
-        writeLog(global_inter->data.debug, ERROR, "Run Error\n", NULL);
-        writeLog(stderr, ERROR, "Run Error\n", NULL);
-    }
-
-    return_:
-    freeParserMessage(pm, true);
     freeInter(global_inter, true);
     freeArgs();
     return 0;
@@ -56,60 +17,6 @@ int main(int argc, char *argv[]) {
     return 1;
 }
 
-/**
- * 参数设置, args是全局结构体, 保存全局的参数设置
- * @param argc
- * @param argv
- * @return
- */
-int getArgs(int argc, char *argv[])
-{
-    args.file = NULL;
-    args.log_file = NULL;
-    args.level = LEXICAL_CHECKOUT_DEBUG;
-    args.stdout_inter = false;
-    opterr = false;
-    int opt;
-    while((opt=getopt_long(argc,argv,short_option ,long_option,NULL))!=-1)
-    {
-        switch(opt)
-        {
-            case 0:
-                break;
-            case 'i':
-                args.file = memStrcpy(optarg, 0, false, false);
-                break;
-            case 'l':
-                args.log_file = memStrcpy(optarg, 0, false, false);
-                break;
-            case 's':
-                args.stdout_inter = true;
-                break;
-            case '?':
-                printf("[Error]: get not success args : -%c\n", (char)optopt);
-                return -1;
-            default:
-                break;
-        }
-    }
-    if (args.file == NULL){
-        if (argc > optind){
-            args.file = memStrcpy(argv[optind], 0, false, false);
-        }
-        else{
-            return -1;
-        }
-    }
-    return 0;
-}
-
-/**
- * 释放args的成员而不包括其本身
- */
-void freeArgs(){
-    memFree(args.file);
-    memFree(args.file);
-}
 
 /**
  * TODO-szh 使用inter的Data成员

+ 78 - 0
src/argument.c

@@ -0,0 +1,78 @@
+#include "__virtualmath.h"
+
+static const struct option long_option[]={
+        {"input",required_argument,NULL,'i'},
+        {"stdout",no_argument,NULL,'s'},
+
+#if OUT_LOG
+        {"log",required_argument,NULL,'l'},
+        {"debug",no_argument,&args.level,DEBUG},
+        {"ddebug",no_argument,&args.level,DEEP_DEBUG},
+        {"ldebug",no_argument,&args.level,LEXICAL_DEBUG},
+        {"lcdebug",no_argument,&args.level,LEXICAL_CHECKOUT_DEBUG},
+        {"info",no_argument,&args.level,INFO},
+#endif
+        // 最后一个元素应该全为0
+        {NULL,0,NULL,0}
+};
+
+#if OUT_LOG
+static const char *short_option = "si:l:";
+#else
+const char *short_option = "si:";
+#endif
+
+/**
+ * 参数设置, args是全局结构体, 保存全局的参数设置
+ * @param argc
+ * @param argv
+ * @return
+ */
+int getArgs(int argc, char *argv[])
+{
+    args.file = NULL;
+    args.log_file = NULL;
+    args.level = LEXICAL_CHECKOUT_DEBUG;
+    args.stdout_inter = false;
+    opterr = false;
+    int opt;
+    while((opt=getopt_long(argc,argv,short_option ,long_option,NULL))!=-1)
+    {
+        switch(opt)
+        {
+            case 0:
+                break;
+            case 'i':
+                args.file = memStrcpy(optarg, 0, false, false);
+                break;
+            case 'l':
+                args.log_file = memStrcpy(optarg, 0, false, false);
+                break;
+            case 's':
+                args.stdout_inter = true;
+                break;
+            case '?':
+                printf("[Error]: get not success args : -%c\n", (char)optopt);
+                return -1;
+            default:
+                break;
+        }
+    }
+    if (args.file == NULL){
+        if (argc > optind){
+            args.file = memStrcpy(argv[optind], 0, false, false);
+        }
+        else{
+            return -1;
+        }
+    }
+    return 0;
+}
+
+/**
+ * 释放args的成员而不包括其本身
+ */
+void freeArgs(){
+    memFree(args.file);
+    memFree(args.file);
+}

+ 27 - 0
src/inter.c

@@ -1,5 +1,32 @@
 #include "__virtualmath.h"
 
+Inter *runBaseInter(char *code_file, char *debug_dir) {
+    Result global_result;
+    return newInter(code_file, debug_dir, &global_result);
+}
+
+Inter *newInter(char *code_file, char *debug_dir, Result *global_result) {
+    Inter *global_inter = makeInter(debug_dir);
+    ParserMessage *pm = makeParserMessage(code_file, debug_dir);
+
+    parserCommandList(pm, global_inter, true, global_inter->statement);
+    if (pm->status != success){
+        writeLog(pm->paser_debug, ERROR, "Syntax Error: %s\n", pm->status_message);
+        writeLog(stderr, ERROR, "Syntax Error: %s\n", pm->status_message);
+        goto return_;
+    }
+
+    *global_result = globalIterStatement(global_inter);
+    if (global_result->type == error_return){
+        writeLog(global_inter->data.debug, ERROR, "Run Error\n", NULL);
+        writeLog(stderr, ERROR, "Run Error\n", NULL);
+    }
+
+    return_:
+    freeParserMessage(pm, true);
+    return global_inter;
+}
+
 Inter *makeInter(char *debug){
     Inter *tmp = memCalloc(1, sizeof(Inter));
     setBaseInterData(tmp);