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

refactor & feat: 支持派生inter

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

+ 2 - 2
include/core/inter.h

@@ -13,8 +13,8 @@ namespace aFuncore {
     class AFUN_CORE_EXPORT Inter {
     public:
         explicit Inter(int argc=0, char **argv=nullptr, ExitMode em=em_activity);
+        Inter(const Inter &base_inter, ExitMode em=em_activity);
         ~Inter();
-        Inter(const Inter &)=delete;
         Inter &operator=(const Inter &)=delete;
 
         void enable();
@@ -59,7 +59,7 @@ namespace aFuncore {
         friend Activation::Activation(Inter &inter_);
 
         struct LiteralRegex;
-        std::list<LiteralRegex> *literal;
+        std::list<LiteralRegex> literal;
 
         /* 配置信息记录器 */
         EnvVarSpace &envvar;

+ 1 - 1
include/tool/dlc.h

@@ -60,7 +60,7 @@ namespace aFuntool {
         inline int operator++(int);
         inline int operator--(int);
 
-        AFUN_TOOL_EXPORT static void dlcExit();
+        static void dlcExit();
 
         class AFUN_TOOL_EXPORT Handle {
             friend class DlcHandle;

+ 1 - 1
include/tool/dlc.template.h

@@ -55,7 +55,7 @@ namespace aFuntool {
                 (*handle_)--;
         }
 
-        const SYMBOL *getSymbol() const {
+        SYMBOL *getSymbol() const {
             return symbol_;
         }
 

+ 2 - 2
include/tool/regex.h

@@ -20,8 +20,8 @@ namespace aFuntool {
         Regex &operator=(Regex &&regex)=delete;
         ~Regex();
 
-        int match(const char *subject);
-        inline int match(const std::string &subject);
+        int match(const char *subject) const;
+        inline int match(const std::string &subject) const;
     };
 }
 

+ 1 - 1
include/tool/regex.inline.h

@@ -4,7 +4,7 @@
 #include "regex.h"
 
 namespace aFuntool {
-    inline int Regex::match(const std::string &subject){
+    inline int Regex::match(const std::string &subject) const {
         return match(subject.c_str());
     }
 

+ 34 - 17
src/core/inter.cpp

@@ -18,7 +18,6 @@ Inter::Inter(int argc, char **argv, ExitMode em)
     gc->varspace = nullptr;
 
     activation = nullptr;
-    literal = new std::list<LiteralRegex>;
 
     envvar.setNumber("sys:gc-runtime", 2);
     envvar.setString("sys:prefix", "''");  // 引用,顺序执行
@@ -46,6 +45,31 @@ Inter::Inter(int argc, char **argv, ExitMode em)
     status = inter_init;
 }
 
+Inter::Inter(const Inter &base_inter, ExitMode em)
+        : base{base_inter.base}, is_derive{true}, out{}, in{}, envvar{base_inter.base.envvar} {
+    status = inter_creat;
+
+    gc = base.gc;
+
+    activation = nullptr;
+
+    for(auto &i : base.literal)
+        literal.push_back(i);
+
+    result = nullptr;
+    son_inter = nullptr;
+    base.son_inter->push_back(this);
+
+    exit_flat = ef_none;
+    exit_mode = em;
+
+    protect = base.protect;  // 放到最后
+    global = base.global;  // 放到最后
+    global_varlist = base.global_varlist;
+
+    status = inter_init;
+}
+
 Inter::~Inter(){
     if (!is_derive) {
         delete global_varlist;
@@ -54,7 +78,6 @@ Inter::~Inter(){
         Var::destruct(gc->var);
         VarSpace::destruct(gc->varspace);
 
-        delete literal;
         delete gc;
         delete son_inter;
         delete &envvar;
@@ -131,15 +154,12 @@ bool Inter::runCode(Code *code){
  * @return
  */
 bool Inter::checkLiteral(const std::string &element) const {
-    if (literal->empty())
+    if (literal.empty())
         return false;
 
-    auto it = literal->begin();
-    auto end = literal->end();
-
-    for(NULL;it != end;it++){
+    for(auto &i : literal){
         try {
-            if (it->rg.match(element) != 1)
+            if (i.rg.match(element) != 1)
                 continue;
             return true;
         } catch (RegexException &e) {
@@ -157,18 +177,15 @@ bool Inter::checkLiteral(const std::string &element) const {
  * @return
  */
 bool Inter::checkLiteral(const std::string &element, std::string &literaler, bool &in_protect) const {
-    if (literal->empty())
+    if (literal.empty())
         return false;
 
-    auto it = literal->begin();
-    auto end = literal->end();
-
-    for(NULL;it != end;it++){
+    for(auto &i : literal){
         try {
-            if (it->rg.match(element) != 1)
+            if (i.rg.match(element) != 1)
                 continue;
-            literaler = it->literaler;
-            in_protect = it->in_protect;
+            literaler = i.literaler;
+            in_protect = i.in_protect;
             return true;
         } catch (RegexException &e) {
             continue;
@@ -179,7 +196,7 @@ bool Inter::checkLiteral(const std::string &element, std::string &literaler, boo
 
 bool Inter::pushLiteral(const std::string &pattern, const std::string &literaler, bool in_protect){
     try {
-        literal->push_front({Regex(pattern), pattern, literaler, in_protect});
+        literal.push_front({Regex(pattern), pattern, literaler, in_protect});
     } catch (RegexException &e) {
         return false;
     }

+ 1 - 1
src/tool/regex.cpp

@@ -53,7 +53,7 @@ aFuntool::Regex::~Regex() {
  * 返回  (0) - 不可完全匹配或不可匹配
  * 返回 (>0) - 失败
  */
-int aFuntool::Regex::match(const char *subject) {
+int aFuntool::Regex::match(const char *subject) const {
     if (!isCharUTF8(subject))
         throw RegexException("Subject not utf-8");
 

+ 9 - 0
test/src/run-code.cpp

@@ -160,5 +160,14 @@ int main() {
         fputs_stdout("\n");
     }
 
+    {
+        Inter son {inter};
+        auto code = (Code::create(0, "run-code.aun"));
+        code->connect(Code::create("test-not-var", 1));
+        son.runCode(code);
+        Code::destruct(code);
+        fputs_stdout("\n");
+    }
+
     return 0;
 }