浏览代码

refactor & feat: 添加Runtime初始化程序

SongZihuan 3 年之前
父节点
当前提交
b41247b9e3

+ 1 - 1
include/core/aFuncore.h

@@ -1,7 +1,7 @@
 #ifndef AFUN_AFUNCORE_H
 #define AFUN_AFUNCORE_H
 
-#include "init.h"
+#include "core-init.h"
 #include "code.h"
 #include "msg.h"
 #include "env-var.h"

+ 11 - 3
include/core/init.h → include/core/core-init.h

@@ -1,5 +1,5 @@
-#ifndef AFUN_INIT_H
-#define AFUN_INIT_H
+#ifndef AFUN_CORE_INIT_H
+#define AFUN_CORE_INIT_H
 #include "aFuntool.h"
 #include "aFunCoreExport.h"
 
@@ -9,6 +9,12 @@ namespace aFuncore {
         aFuntool::LogFactory &factor;
         bool log_asyn;
         aFuntool::LogLevel level;
+
+        inline InitInfo(const std::string &base_dir_,
+                        aFuntool::LogFactory &factor_,
+                        bool log_asyn_,
+                        aFuntool::LogLevel level_);
+
     };
 
     AFUN_CORE_EXPORT extern std::string log_path;
@@ -19,4 +25,6 @@ namespace aFuncore {
     AFUN_CORE_EXPORT bool aFunCoreInit(InitInfo *info);
 }
 
-#endif //AFUN_INIT_H
+#include "core-init.inline.h"
+
+#endif //AFUN_CORE_INIT_H

+ 16 - 0
include/core/core-init.inline.h

@@ -0,0 +1,16 @@
+#ifndef AFUN_CORE_INIT_INLINE_H
+#define AFUN_CORE_INIT_INLINE_H
+
+#include "core-init.h"
+
+namespace aFuncore {
+    inline InitInfo::InitInfo(const std::string &base_dir_,
+                              aFuntool::LogFactory &factor_,
+                              bool log_asyn_,
+                              aFuntool::LogLevel level_) :
+        base_dir{base_dir_}, factor{factor_}, log_asyn{log_asyn_}, level{level_} {
+
+    }
+}
+
+#endif //AFUN_CORE_INIT_INLINE_H

+ 23 - 0
include/runtime/rt-init.h

@@ -0,0 +1,23 @@
+#ifndef AFUN_RT_INIT_H
+#define AFUN_RT_INIT_H
+#include "aFunlangExport.h"
+#include "aFuncore.h"
+
+namespace aFunrt {
+    struct aFunInitInfo : public aFuncore::InitInfo {
+        aFuntool::LogLevel lang_level;
+
+        inline aFunInitInfo(const std::string &base_dir_,
+                           aFuntool::LogFactory &factor_,
+                           bool log_asyn_,
+                           aFuntool::LogLevel core_level_,
+                           aFuntool::LogLevel lang_level_);
+    };
+
+    AFUN_LANG_EXPORT extern aFuntool::Logger *aFunLogger;
+    AFUN_LANG_EXPORT bool aFunInit(aFunInitInfo *info);
+}
+
+#include "rt-init.inline.h"
+
+#endif //AFUN_RT_INIT_H

+ 18 - 0
include/runtime/rt-init.inline.h

@@ -0,0 +1,18 @@
+#ifndef AFUN_RT_INIT_INLINE_H
+#define AFUN_RT_INIT_INLINE_H
+
+#include "rt-init.h"
+
+namespace aFunrt {
+    inline aFunInitInfo::aFunInitInfo(const std::string &base_dir_,
+                                      aFuntool::LogFactory &factor_,
+                                      bool log_asyn_,
+                                      aFuntool::LogLevel core_level_,
+                                      aFuntool::LogLevel lang_level_) :
+        InitInfo(base_dir_, factor_, log_asyn_, core_level_), lang_level{lang_level_} {
+
+    }
+}
+
+
+#endif //AFUN_RT_INIT_INLINE_H

+ 4 - 2
include/runtime/rt-reader.h

@@ -1,10 +1,12 @@
 #ifndef AFUN_RT_READER_H
 #define AFUN_RT_READER_H
 #include <functional>
+#include "aFunlangExport.h"
 #include "aFuncore.h"
+#include "rt-init.h"
 
 namespace aFunrt {
-    class StringReader : public aFuncore::Reader {
+    class AFUN_LANG_EXPORT StringReader : public aFuncore::Reader {
     public:
         inline StringReader(std::string str_, const aFuntool::FilePath &path_);
         size_t readText(char *dest, size_t read_len, ReadMode &mode) override;
@@ -14,7 +16,7 @@ namespace aFunrt {
         size_t len;
     };
 
-    class FileReader : public aFuncore::Reader {
+    class AFUN_LANG_EXPORT FileReader : public aFuncore::Reader {
     public:
         inline explicit FileReader(const aFuntool::FilePath &path_) noexcept(false);
         size_t readText(char *dest, size_t read_len, ReadMode &mode) override;

+ 7 - 7
include/tool/log.h

@@ -105,13 +105,13 @@ namespace aFuntool {
 #include "log-macro.h"
 
 #if aFunWriteTrack
-#define trackLog(logger, ...) (logger)->writeTrackLog(__FILENAME__ , (int)__LINE__, __FUNCTION__, __VA_ARGS__)
+#define trackLog(logger, ...) ((logger) ? (logger)->writeTrackLog(__FILENAME__ , (int)__LINE__, __FUNCTION__, __VA_ARGS__) : 0)
 #else
 #define trackLog(logger, ...) (nullptr)
 #endif
 
 #if aFunWriteDebug
-#define debugLog(logger, ...) (logger)->writeDebugLog(__FILENAME__ , (int)__LINE__, __FUNCTION__, __VA_ARGS__)
+#define debugLog(logger, ...) ((logger) ? (logger)->writeDebugLog(__FILENAME__ , (int)__LINE__, __FUNCTION__, __VA_ARGS__) : 0)
 #define assertDebugLog(c, logger, ...) ((c) || debugLog(logger, "Assert " #c " error : " __VA_ARGS__))
 #else
 #define debugLog(logger, ...) (nullptr)
@@ -119,7 +119,7 @@ namespace aFuntool {
 #endif
 
 #if aFunWriteInfo
-#define infoLog(logger, ...) (logger)->writeInfoLog(__FILENAME__ , (int)__LINE__, __FUNCTION__, __VA_ARGS__)
+#define infoLog(logger, ...) ((logger) ? (logger)->writeInfoLog(__FILENAME__ , (int)__LINE__, __FUNCTION__, __VA_ARGS__) : 0)
 #define assertInfoLog(c, logger, ...) ((c) || infoLog(logger, "Assert " #c " error : " __VA_ARGS__))
 #else
 #define infoLog(logger, ...) (nullptr)
@@ -127,7 +127,7 @@ namespace aFuntool {
 #endif
 
 #if !aFunIgnoreWarning
-#define warningLog(logger, ...) (logger)->writeWarningLog(__FILENAME__ , (int)__LINE__, __FUNCTION__, __VA_ARGS__)
+#define warningLog(logger, ...) ((logger) ? (logger)->writeWarningLog(__FILENAME__ , (int)__LINE__, __FUNCTION__, __VA_ARGS__) : 0)
 #define assertWarningLog(c, logger, ...) ((c) || warningLog(logger, "Assert " #c " error : " __VA_ARGS__))
 #else
 #define warningLog(logger, ...) (nullptr)
@@ -135,7 +135,7 @@ namespace aFuntool {
 #endif
 
 #if !aFunIgnoreError
-#define errorLog(logger, ...) (logger)->writeErrorLog(__FILENAME__ , (int)__LINE__, __FUNCTION__, __VA_ARGS__)
+#define errorLog(logger, ...) ((logger) ? (logger)->writeErrorLog(__FILENAME__ , (int)__LINE__, __FUNCTION__, __VA_ARGS__) : 0)
 #define assertErrorLog(c, logger, ...) ((c) || errorLog(logger, "Assert " #c " error : " __VA_ARGS__))
 #else
 #define errorLog(logger, ...) (nullptr)
@@ -143,8 +143,8 @@ namespace aFuntool {
 #endif
 
 #if !aFunOFFAllLog
-#define sendErrorLog(logger, ...) (logger)->writeSendErrorLog(__FILENAME__ , (int)__LINE__, __FUNCTION__, __VA_ARGS__)
-#define fatalErrorLog(logger, exit_code, ...) (logger)->writeFatalErrorLog(__FILENAME__ , (int)__LINE__, __FUNCTION__, exit_code, __VA_ARGS__)
+#define sendErrorLog(logger, ...) ((logger) ? (logger)->writeSendErrorLog(__FILENAME__ , (int)__LINE__, __FUNCTION__, __VA_ARGS__) : 0)
+#define fatalErrorLog(logger, exit_code, ...) ((logger) ? (logger)->writeFatalErrorLog(__FILENAME__ , (int)__LINE__, __FUNCTION__, exit_code, __VA_ARGS__) : 0)
 #define assertSendErrorLog(c, logger, ...) ((c) || sendErrorLog(logger, "Assert " #c " error : " __VA_ARGS__))
 #define assertFatalErrorLog(c, logger, exit_code, ...) ((c) || fatalErrorLog(logger, exit_code, "Assert " #c " error : " __VA_ARGS__))
 #else

+ 1 - 1
src/core/activation.cpp

@@ -1,6 +1,6 @@
 #include "core-activation.h"
 #include "inter.h"
-#include "init.h"
+#include "core-init.h"
 #include "msg.h"
 #include "code.h"
 

+ 1 - 1
src/core/code.cpp

@@ -1,5 +1,5 @@
 #include "code.h"
-#include "init.h"
+#include "core-init.h"
 
 namespace aFuncore {
     Code::~Code(){

+ 2 - 2
src/core/init.cpp → src/core/core-init.cpp

@@ -1,10 +1,10 @@
 #include <clocale>
-#include "init.h"
+#include "core-init.h"
 
 namespace aFuncore {
     std::string log_path;
     std::string varlib_path;
-    aFuntool::Logger *aFunCoreLogger;
+    aFuntool::Logger *aFunCoreLogger = nullptr;
 
     /**
      * 初始化程序

+ 1 - 1
src/core/inter.cpp

@@ -1,6 +1,6 @@
 #include "inter.h"
 #include "core-activation.h"
-#include "init.h"
+#include "core-init.h"
 #include "msg.h"
 #include "core-exception.h"
 

+ 1 - 1
src/core/lexical.cpp

@@ -4,7 +4,7 @@
  */
 #include <cctype>
 #include "core-parser.h"
-#include "init.h"
+#include "core-init.h"
 #include "inter.h"
 
 #ifndef isascii

+ 1 - 1
src/core/object-value.cpp

@@ -1,6 +1,6 @@
 #include "object-value.h"
 #include "inter.h"
-#include "init.h"
+#include "core-init.h"
 
 namespace aFuncore {
     Var::Var(Object *data_, Inter &inter) : Object("Var", inter), data{data_}, env{inter.getEnvironment()}{

+ 1 - 1
src/core/object.cpp

@@ -1,6 +1,6 @@
 #include "object.h"
 #include "inter.h"
-#include "init.h"
+#include "core-init.h"
 
 namespace aFuncore {
     Object::Object(std::string type_, Inter &inter)

+ 1 - 1
src/core/reader.cpp

@@ -1,4 +1,4 @@
-#include "init.h"
+#include "core-init.h"
 #include "reader.h"
 
 namespace aFuncore {

+ 1 - 1
src/core/syntactic.cpp

@@ -1,5 +1,5 @@
 #include "core-parser.h"
-#include "init.h"
+#include "core-init.h"
 
 namespace aFuncore {
     bool Parser::getToken() {

+ 1 - 1
src/core/varlist.cpp

@@ -1,7 +1,7 @@
 #include "varlist.h"
 #include "object-value.h"
 #include "inter.h"
-#include "init.h"
+#include "core-init.h"
 
 namespace aFuncore {
     VarList::VarList(VarList *varlist){

+ 14 - 0
src/runtime/rt-init.cpp

@@ -0,0 +1,14 @@
+#include "rt-init.h"
+
+namespace aFunrt {
+    aFuntool::Logger *aFunLogger = nullptr;
+
+    bool aFunInit(aFunInitInfo *info) {
+        if (!aFuncore::aFunCoreInit(info))
+            return false;
+        static aFuntool::Logger logger{info->factor, "aFunlang", info->lang_level};
+        aFunLogger = &logger;
+        return true;
+    }
+
+}

+ 3 - 2
src/runtime/rt-reader.cpp

@@ -1,4 +1,5 @@
 #include "rt-reader.h"
+#include "rt-init.h"
 
 namespace aFunrt {
     size_t StringReader::readText(char *dest, size_t read_len, ReadMode &mode) {
@@ -31,10 +32,10 @@ namespace aFunrt {
                     mode = read_mode_error;
                     return 0;
                 }
-//                writeTrackLog(aFunRTLogger, "Parser utf-8 with BOM");
+                trackLog(aFunLogger, "Parser utf-8 with BOM");
             } else {
                 ungetc(ch, file);
-//                writeTrackLog(aFunRTLogger, "Parser utf-8 without BOM");
+                trackLog(aFunLogger, "Parser utf-8 without BOM");
             }
         }
 

+ 6 - 6
src/tool/log.cpp

@@ -370,7 +370,7 @@ namespace aFuntool {
 #if aFunWriteTrack
         va_list ap;
         va_start(ap, format);
-        return this->factor_.newLog(this, aFunConsoleTrack, log_track, file, line, func, format, ap);
+        return factor_.newLog(this, aFunConsoleTrack, log_track, file, line, func, format, ap);
 #endif
     }
 
@@ -381,7 +381,7 @@ namespace aFuntool {
 #if aFunWriteDebug
         va_list ap;
         va_start(ap, format);
-        return this->factor_.newLog(this, aFunConsoleDebug, log_debug, file, line, func, format, ap);
+        return factor_.newLog(this, aFunConsoleDebug, log_debug, file, line, func, format, ap);
 #endif
     }
 
@@ -392,7 +392,7 @@ namespace aFuntool {
 #if aFunWriteInfo
         va_list ap;
         va_start(ap, format);
-        return this->factor_.newLog(this, aFunConsoleInfo, log_info, file, line, func, format, ap);
+        return factor_.newLog(this, aFunConsoleInfo, log_info, file, line, func, format, ap);
 #endif
     }
 
@@ -403,7 +403,7 @@ namespace aFuntool {
 #if !aFunIgnoreWarning
         va_list ap;
         va_start(ap, format);
-        return this->factor_.newLog(this, aFunConsoleWarning, log_warning, file, line, func, format, ap);
+        return factor_.newLog(this, aFunConsoleWarning, log_warning, file, line, func, format, ap);
 #endif
     }
 
@@ -414,7 +414,7 @@ namespace aFuntool {
 #if !aFunIgnoreError
         va_list ap;
         va_start(ap, format);
-        return this->factor_.newLog(this, aFunConsoleError, log_error, file, line, func, format, ap);
+        return factor_.newLog(this, aFunConsoleError, log_error, file, line, func, format, ap);
 #endif
     }
 
@@ -426,7 +426,7 @@ namespace aFuntool {
 #if !aFunIgnoreSendError
         va_list ap;
         va_start(ap, format);
-        this->factor_.newLog(this, aFunConsoleSendError, log_send_error, file, line, func, format, ap);
+        factor_.newLog(this, aFunConsoleSendError, log_send_error, file, line, func, format, ap);
 #endif
 
         if (this->exit_)

+ 2 - 0
test/src/CMakeLists.txt

@@ -34,4 +34,6 @@ add_new_test(core-up-msg COMMAND "$<TARGET_FILE:core-up-msg>")
 add_new_test(core-reader COMMAND "$<TARGET_FILE:core-reader>")
 add_new_test(core-lexical COMMAND "$<TARGET_FILE:core-lexical>")
 
+add_new_test(rt-syntactic COMMAND "$<TARGET_FILE:rt-syntactic>")
+
 add_new_test(run-code COMMAND "$<TARGET_FILE:run-code>")

+ 2 - 6
test/src/core-init.cpp

@@ -1,4 +1,4 @@
-#include "init.h"
+#include "core-init.h"
 using namespace aFuncore;
 using namespace aFuntool;
 
@@ -10,11 +10,7 @@ int main() {
     }
 
     aFuntool::LogFactory factor {};
-    aFuncore::InitInfo info = {.base_dir=base_path,
-                               .factor=factor,
-                               .log_asyn=true,
-                               .level=log_debug,
-    };
+    aFuncore::InitInfo info {base_path, factor, true, log_debug};
 
     if (!aFunCoreInit(&info)) {
         printf_stderr(0, "aFunlang init error.");

+ 1 - 1
test/src/core-syntactic.cpp → test/src/rt-syntactic.cpp

@@ -7,7 +7,7 @@ const char *str2 = "{if true [HelloWorld (10)\n";
 class ConsoleReader : public aFuncore::Reader {
 public:
     size_t STDIN_MAX_SIZE = 1024;
-    ConsoleReader(std::function<bool()> interrupt_, const aFuntool::FilePath &path_="console.aun");
+    explicit ConsoleReader(std::function<bool()> interrupt_, const aFuntool::FilePath &path_="console.aun");
     size_t readText(char *dest, size_t read_len, ReadMode &mode) override;
 private:
     char *data;

+ 1 - 5
test/src/run-code.cpp

@@ -291,11 +291,7 @@ int main() {
         }
 
         aFuntool::LogFactory factor{};
-        aFuncore::InitInfo info = {.base_dir=base_path,
-            .factor=factor,
-            .log_asyn=true,
-            .level=log_debug,
-        };
+        aFuncore::InitInfo info{base_path, factor, true, log_debug};
 
         if (!aFunCoreInit(&info)) {
             printf_stderr(0, "aFunlang init error.");