2
0
Эх сурвалжийг харах

refactor & feat: 定义rt-reader

SongZihuan 3 жил өмнө
parent
commit
d8e4fd3e57

+ 10 - 20
cmake/aFunHeader.cmake

@@ -9,30 +9,20 @@ generate_export_header(core-shared
                        EXPORT_FILE_NAME "${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}/aFunCoreExport.h"  # 导出的位置
                        BASE_NAME "AFUN_CORE")
 
-#generate_export_header(rt-shared
-#                       EXPORT_FILE_NAME "${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}/aFunlangExport.h"  # 导出的位置
-#                       BASE_NAME "AFUN_LANG")
+generate_export_header(rt-shared
+                       EXPORT_FILE_NAME "${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}/aFunlangExport.h"  # 导出的位置
+                       BASE_NAME "AFUN_LANG")
 
 target_compile_definitions(tool-static PUBLIC AFUN_TOOL_STATIC_DEFINE=1)  # 静态库需要定义 AFUN_TOOL_STATIC_DEFINE
 target_compile_definitions(core-static PUBLIC AFUN_CORE_STATIC_DEFINE=1)
-#target_compile_definitions(rt-static PUBLIC AFUN_LANG_STATIC_DEFINE=1)
-#
-set_property(TARGET tool-shared core-shared
+target_compile_definitions(rt-static PUBLIC AFUN_LANG_STATIC_DEFINE=1)
+
+set_property(TARGET tool-shared core-shared rt-shared
              PROPERTY C_VISIBILITY_PRESET "hidden")
-set_property(TARGET tool-shared core-shared
+set_property(TARGET tool-shared core-shared rt-shared
              PROPERTY VISIBILITY_INLINES_HIDDEN TRUE)
 
-set_property(TARGET tool-static core-static
+set_property(TARGET tool-static core-static rt-static
              PROPERTY C_VISIBILITY_PRESET "default")
-set_property(TARGET tool-static core-static
-             PROPERTY VISIBILITY_INLINES_HIDDEN FALSE)
-
-#set_property(TARGET tool-shared core-shared rt-shared
-#             PROPERTY C_VISIBILITY_PRESET "hidden")
-#set_property(TARGET tool-shared core-shared rt-shared
-#             PROPERTY VISIBILITY_INLINES_HIDDEN TRUE)
-#
-#set_property(TARGET tool-static core-static rt-static
-#             PROPERTY C_VISIBILITY_PRESET "default")
-#set_property(TARGET tool-static core-static
-#             PROPERTY VISIBILITY_INLINES_HIDDEN FALSE)
+set_property(TARGET tool-static core-static rt-static
+             PROPERTY VISIBILITY_INLINES_HIDDEN FALSE)

+ 4 - 2
include/core/aFuncore.h

@@ -3,13 +3,15 @@
 
 #include "init.h"
 #include "code.h"
-#include "inter.h"
-#include "object.h"
 #include "msg.h"
 #include "env-var.h"
+#include "inter.h"
+#include "object.h"
 #include "object-value.h"
 #include "varlist.h"
 #include "core-activation.h"
 #include "core-exception.h"
+#include "reader.h"
+#include "core-parser.h"
 
 #endif //AFUN_AFUNCORE_H

+ 1 - 1
include/core/core-activation.h

@@ -26,7 +26,7 @@ namespace aFuncore {
 
         virtual ActivationStatus getCode(const Code::ByteCode *&code) = 0;
         virtual void runCode(const Code::ByteCode *code);
-        virtual inline void endRun();
+        virtual void endRun();
 
         [[nodiscard]] inline VarList *getVarlist() const;
         [[nodiscard]] inline UpMessage &getUpStream();

+ 0 - 4
include/core/core-activation.inline.h

@@ -4,10 +4,6 @@
 #include "core-activation.h"
 
 namespace aFuncore {
-    inline void Activation::endRun() {
-
-    }
-
     inline VarList *Activation::getVarlist() const{
         return varlist;
     }

+ 4 - 4
include/core/object-value.h

@@ -17,8 +17,8 @@ namespace aFuncore {
         Var(Object *data_, Environment &env_);
         ~Var() override = default;
 
-        [[nodiscard]] inline virtual Object *getData();
-        virtual void inline setData(Object *data_);
+        [[nodiscard]] virtual Object *getData();
+        virtual void setData(Object *data_);
         void linkObject(std::queue<Object *> &queue) override;
 
     private:
@@ -84,7 +84,7 @@ namespace aFuncore {
         class AFUN_CORE_EXPORT CallFunction;
 
         virtual CallFunction *getCallFunction(const Code::ByteCode *code, Inter &inter) = 0;
-        virtual inline bool isInfix();
+        virtual bool isInfix();
     };
 
     class AFUN_CORE_EXPORT Function::CallFunction {
@@ -112,7 +112,7 @@ namespace aFuncore {
 
     class AFUN_CORE_EXPORT CallBackVar : public virtual Object {
     public:
-        virtual inline bool isCallBack(Inter &inter, Activation &activation);
+        virtual bool isCallBack(Inter &inter, Activation &activation);
         virtual void callBack(Inter &inter, Activation &activation) = 0;
     };
 };

+ 0 - 18
include/core/object-value.inline.h

@@ -3,16 +3,6 @@
 #include "object-value.h"
 
 namespace aFuncore {
-    inline Object *Var::getData() {
-        std::unique_lock<std::mutex> mutex{lock};
-        return data;
-    }
-
-    inline void Var::setData(Object *data_) {
-        std::unique_lock<std::mutex> mutex{lock};
-        data = data_;
-    }
-
     inline size_t VarSpace::getCount() {
         std::unique_lock<std::mutex> mutex{lock};
         return var.size();
@@ -38,14 +28,6 @@ namespace aFuncore {
     inline bool ProtectVarSpace::setProtect(bool protect) {
         bool ret = is_protect; is_protect = protect; return ret;
     }
-
-    inline bool Function::isInfix() {
-        return false;
-    }
-
-    inline bool CallBackVar::isCallBack(Inter &inter, Activation &activation) {
-        return true;
-    }
 };
 
 #endif //AFUN_OBJECT_VALUE_INLINE_H

+ 6 - 0
include/runtime/aFunrt.h

@@ -0,0 +1,6 @@
+#ifndef AFUN_AFUNRT_H
+#define AFUN_AFUNRT_H
+
+#include "rt-reader.h"
+
+#endif //AFUN_AFUNRT_H

+ 20 - 0
include/runtime/rt-reader.h

@@ -0,0 +1,20 @@
+#ifndef AFUN_RT_READER_H
+#define AFUN_RT_READER_H
+#include "aFuncore.h"
+
+namespace aFunrt {
+    class ReaderString : public aFuncore::Reader {
+    public:
+        inline ReaderString(std::string str_, const aFuntool::FilePath &path_);
+
+        size_t readText(char *dest, size_t read_len, ReadMode &mode) override;
+    private:
+        std::string str;
+        size_t index;
+        size_t len;
+    };
+}
+
+#include "rt-reader.inline.h"
+
+#endif //AFUN_RT_READER_H

+ 13 - 0
include/runtime/rt-reader.inline.h

@@ -0,0 +1,13 @@
+#ifndef AFUN_RT_READER_INLINE_H
+#define AFUN_RT_READER_INLINE_H
+#include "rt-reader.h"
+
+namespace aFunrt {
+    inline ReaderString::ReaderString(std::string str_, const aFuntool::FilePath &path_)
+        : Reader{path_, 0}, str{std::move(str_)} {
+        index = 0;
+        len = str.size();
+    }
+}
+
+#endif //AFUN_RT_READER_INLINE_H

+ 1 - 1
include/tool/tool-exception.h

@@ -7,7 +7,7 @@ namespace aFuntool {
         std::string message;
     public:
         inline explicit aFunException(std::string msg);
-        inline virtual const char *what();
+        virtual const char *what();
     };
 
     class aFuntoolException : public aFunException {

+ 0 - 4
include/tool/tool-exception.inline.h

@@ -8,10 +8,6 @@ namespace aFuntool {
 
     }
 
-    inline const char *aFunException::what() {
-        return message.c_str();
-    }
-
     inline aFuntoolException::aFuntoolException(const std::string &msg) : aFunException{msg} {
 
     }

+ 1 - 2
src/CMakeLists.txt

@@ -2,7 +2,6 @@
 set(install_include_tool ${INSTALL_INCLUDEDIR})
 
 set(build_include_core ${PROJECT_SOURCE_DIR}/include/core)
-set(build_include_core_info ${PROJECT_SOURCE_DIR}/include/core/info)
 set(install_include_core ${INSTALL_INCLUDEDIR})
 
 set(build_include_runtime ${PROJECT_SOURCE_DIR}/include/runtime)
@@ -26,7 +25,7 @@ set(install_include $<INSTALL_INTERFACE:${INSTALL_INCLUDEDIR}>)
 
 add_subdirectory(tool)
 add_subdirectory(core)  # core 依赖 tool
-#add_subdirectory(runtime)  # runtime 依赖 core
+add_subdirectory(runtime)  # runtime 依赖 core
 
 # source在子目录中被使用, 为了避免子目录访问到source, 子目录将在此前面被执行
 #file(GLOB source

+ 0 - 11
src/core/CMakeLists.txt

@@ -11,11 +11,6 @@ file(GLOB public_h
      RELATIVE "${build_include_core}"
      "${build_include_core}/*.h")
 
-file(GLOB public_h_info
-     LIST_DIRECTORIES FALSE
-     RELATIVE "${build_include_core_info}"
-     "${build_include_core_info}/*.h")
-
 set(public_h_build)
 set(public_h_install)
 
@@ -25,12 +20,6 @@ foreach(h IN LISTS public_h)
     list(APPEND public_h_install "$<INSTALL_INTERFACE:${install_include_core}/${h}>")
 endforeach()
 
-foreach(h IN LISTS public_h_info)
-    file(RELATIVE_PATH _path ${CMAKE_CURRENT_LIST_DIR} "${build_include_core_info}/${h}")
-    list(APPEND public_h_build   "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${_path}>")  # 相对路径的转换, 此处只能使用相对路径
-    list(APPEND public_h_install "$<INSTALL_INTERFACE:${install_include_core}/${h}>")
-endforeach()
-
 add_library(core-shared SHARED "")  # core和tool动态库
 add_library(core-static STATIC "")  # core和tool静态库
 

+ 4 - 0
src/core/activation.cpp

@@ -45,6 +45,10 @@ namespace aFuncore {
         delete varlist;
     }
 
+    void Activation::endRun() {
+
+    }
+
     /**
      * 运行代码
      * @param code

+ 18 - 0
src/core/object-value.cpp

@@ -24,6 +24,16 @@ namespace aFuncore {
         queue.push(getData());
     }
 
+    Object *Var::getData() {
+        std::unique_lock<std::mutex> mutex{lock};
+        return data;
+    }
+
+    void Var::setData(Object *data_) {
+        std::unique_lock<std::mutex> mutex{lock};
+        data = data_;
+    }
+
     /**
      * 访问指定变量
      * @param name 变量名
@@ -152,4 +162,12 @@ namespace aFuncore {
             return findVar(name) ? vof_fail : vof_not_var;
         return VarSpace::delVar(name);
     }
+
+    bool Function::isInfix() {
+        return false;
+    }
+
+    bool CallBackVar::isCallBack(Inter &inter, Activation &activation) {
+        return true;
+    }
 }

+ 1 - 14
src/runtime/CMakeLists.txt

@@ -2,22 +2,10 @@
      LIST_DIRECTORIES FALSE
      ${CMAKE_CURRENT_LIST_DIR}/*.cpp)
 
-file(GLOB source_tool
-     LIST_DIRECTORIES FALSE
-     ${CMAKE_CURRENT_LIST_DIR}/*/*.cpp)
-
 file(GLOB private_h
      LIST_DIRECTORIES FALSE
      ${CMAKE_CURRENT_LIST_DIR}/*.h)
 
-file(GLOB private_tool_h
-     LIST_DIRECTORIES FALSE
-     ${CMAKE_CURRENT_LIST_DIR}/*/*.h)
-
-file(GLOB private_h_core
-     LIST_DIRECTORIES FALSE
-     ${CMAKE_CURRENT_LIST_DIR}/../core/*.h)  # 需要使用 core 的特定头文件 (高级开发)
-
 file(GLOB public_h
      LIST_DIRECTORIES FALSE
      RELATIVE "${build_include_runtime}"
@@ -32,13 +20,12 @@ foreach(h IN LISTS public_h)
     list(APPEND public_h_install "$<INSTALL_INTERFACE:${install_include_runtime}/${h}>")
 endforeach()
 
-
 add_library(rt-shared SHARED "")  # xx表示均为动态链接 core-share-t
 add_library(rt-static STATIC "")  # ct表示均静态链接 core-static
 
 foreach(tgt rt-shared rt-static)
     target_sources(${tgt}
-                   PRIVATE ${source} ${source_tool} ${private_h} ${private_tool_h} ${private_h_core}
+                   PRIVATE ${source} ${private_h}
                    PUBLIC ${public_h_build} ${public_h_install})
     target_include_directories(${tgt}
                                PRIVATE

+ 17 - 0
src/runtime/rt-reader.cpp

@@ -0,0 +1,17 @@
+#include "rt-reader.h"
+
+namespace aFunrt {
+    size_t ReaderString::readText(char *dest, size_t read_len, ReadMode &mode) {
+        if (index == len)  // 读取到末尾
+            return 0;
+
+        if (index + read_len > len) {  // 超出长度范围
+            read_len = len - index;
+            mode = read_mode_finished;
+        }
+
+        memcpy(dest, str.c_str() + index, read_len);
+        index += read_len;
+        return read_len;
+    }
+}

+ 7 - 0
src/tool/exception.cpp

@@ -0,0 +1,7 @@
+#include "tool-exception.h"
+
+namespace aFuntool {
+    const char *aFunException::what() {
+        return message.c_str();
+    }
+}

+ 0 - 0
test/af/test1.aub → test/bytecode/test1.aub


+ 0 - 0
test/af/test1.aun → test/bytecode/test1.aun


+ 0 - 0
test/af/test2.aub → test/bytecode/test2.aub


+ 1 - 1
test/lib/CMakeLists.txt

@@ -5,7 +5,7 @@ foreach(src IN LISTS src_list)
     cmake_path(GET src STEM file_name)
     add_library(${file_name} SHARED ${src})
     set_target_properties(${file_name}
-                          PROPERTIES OUTPUT_NAME "testlib_${file_name}")
+                          PROPERTIES OUTPUT_NAME "test-lib-${file_name}")
     define_FILENAME(${file_name})
     unset(file_name)
 endforeach()

+ 1 - 1
test/src/CMakeLists.txt

@@ -5,7 +5,7 @@ foreach(src IN LISTS src_list)
     cmake_path(GET src STEM file_name)
     add_executable(${file_name})
     target_sources(${file_name} PRIVATE ${src})
-    target_link_libraries(${file_name} PUBLIC tool-static core-static)  # 链接静态库 (导出所有符号)
+    target_link_libraries(${file_name} PUBLIC tool-static core-static rt-static)  # 链接静态库 (导出所有符号)
     set_target_properties(${file_name}
                           PROPERTIES OUTPUT_NAME "test-${file_name}")
     target_compile_definitions(${file_name} PRIVATE IN_CTEST)

+ 3 - 28
test/src/core-syntactic.cpp

@@ -1,37 +1,12 @@
 #include <cstdio>
-#include "core-parser.h"
+#include "aFunrt.h"
 
 const char *str = "{if true [HelloWorld (10)]}\n";
 const char *str2 = "{if true [HelloWorld (10)\n";
 
-class ReaderString : public aFuncore::Reader {
-    std::string str;
-    size_t index;
-    size_t len;
-public:
-    ReaderString(std::string str_, const aFuntool::FilePath &path_) : Reader{path_, 0}, str{std::move(str_)} {
-        index = 0;
-        len = str.size();
-    }
-
-    size_t readText(char *dest, size_t read_len, ReadMode &mode) override {
-        if (index == len)  // 读取到末尾
-            return 0;
-
-        if (index + read_len > len) {  // 超出长度范围
-            read_len = len - index;
-            mode = read_mode_finished;
-        }
-
-        memcpy(dest, str.c_str() + index, read_len);
-        index += read_len;
-        return read_len;
-    }
-};
-
 int main() {
     {
-        auto reader = ReaderString(str, "str");
+        auto reader = aFunrt::ReaderString(str, "str");
         auto parser = aFuncore::Parser(reader);
         auto code = aFuncore::Code("test.aun");
         bool ret = parser.parserCode(code);
@@ -41,7 +16,7 @@ int main() {
     }
 
     {
-        auto reader = ReaderString(str2, "str2");
+        auto reader = aFunrt::ReaderString(str2, "str2");
         auto parser = aFuncore::Parser(reader);
         auto code = aFuncore::Code("test2.aun");
         parser.parserCode(code);