Explorar o código

refactor & fix: 取消尾调用优化

设计层面的抉择,导致尾调用优化在aFun中无法实现,且意义不大
现取消尾调用优化
SongZihuan %!s(int64=3) %!d(string=hai) anos
pai
achega
4e212d4025
Modificáronse 3 ficheiros con 23 adicións e 31 borrados
  1. 0 4
      include/core/activation.hpp
  2. 8 21
      src/core/activation.cpp
  3. 15 6
      test/src/run-code.cpp

+ 0 - 4
include/core/activation.hpp

@@ -25,7 +25,6 @@ namespace aFuncore {
         virtual ~Activation();
 
         virtual ActivationStatus getCode(Code *&code)=0;
-        virtual bool onTail()=0;
         virtual void runCode(Code *code);
 
         [[nodiscard]] VarList *getVarlist() const {return varlist;}
@@ -41,7 +40,6 @@ namespace aFuncore {
     public:
         explicit ExeActivation(Code *code, Inter *inter_) : Activation(inter_), start{code}, next{code} {}
         ActivationStatus getCode(Code *&code) override;
-        bool onTail() override {return next == nullptr;}
         [[nodiscard]] Code *getStart() const {return start;}
     };
 
@@ -49,7 +47,6 @@ namespace aFuncore {
     public:
         explicit TopActivation(Code *code, Inter *inter_);
         ~TopActivation() override;
-        bool onTail() override {return false;}
     };
 
     class FuncActivation : public Activation {
@@ -72,7 +69,6 @@ namespace aFuncore {
         explicit FuncActivation(Code *code, Inter *inter_) : Activation(inter_), call{code,} {}
         ~FuncActivation() override;
         ActivationStatus getCode(Code *&code) override;
-        bool onTail() override {return on_tail;}
     };
 }
 

+ 8 - 21
src/core/activation.cpp

@@ -11,32 +11,17 @@ using namespace aFuntool;
 
 /**
  * 创建基本Activation
- * 若上层Activation已到结尾则尾调用优化
  * 自动继承上层VarList和UpMessage
  * 自动压入inter
  * @param inter_
  */
 Activation::Activation(Inter *inter_) : inter{inter_}, line{0} {
     Activation *prev_ = inter->getActivation();
-    if (prev_ != nullptr && prev_->onTail()) {
-        prev = prev_->prev;
-        up = prev_->up;
-        down = prev_->down;
-        old_varlist = prev_->old_varlist;
-        varlist = prev_->varlist;
-
-        prev_->up = nullptr;
-        prev_->down = nullptr;
-        prev_->old_varlist = nullptr;
-        prev_->varlist = nullptr;
-        delete prev_;
-    } else {
-        prev = prev_;
-        old_varlist = prev ? prev->varlist : nullptr;
-        varlist = old_varlist;
-        down = new DownMessage();
-        up = new UpMessage(prev ? prev->up : nullptr);
-    }
+    prev = prev_;
+    old_varlist = prev ? prev->varlist : nullptr;
+    varlist = old_varlist;
+    down = new DownMessage();
+    up = new UpMessage(prev ? prev->up : nullptr);
     inter->pushActivation(this);
 }
 
@@ -186,5 +171,7 @@ ActivationStatus FuncActivation::getCode(Code *&code){
     }
 
     on_tail = true;
-    return call_func->runFunction();
+    if (call_func->runFunction() == as_run)
+        return inter->getActivation()->getCode(code);
+    return as_end;
 }

+ 15 - 6
test/src/run-code.cpp

@@ -10,11 +10,12 @@ using namespace aFuntool;
 
 class Func1 : public Function {
     class CallFunc1 : public CallFunction {
+        Code *func_code;
         Code *code;
         Inter *inter;
         std::list<ArgCodeList> *acl;
     public:
-        CallFunc1(Code *code_, Inter *inter_) : code{code_}, inter{inter_} {
+        CallFunc1(Code *func_code_, Code *code_, Inter *inter_) : func_code{func_code_}, code{code_}, inter{inter_} {
             acl = new std::list<ArgCodeList>;
             ArgCodeList agr1 = {code_->getSon()->toNext()};
             acl->push_front(agr1);
@@ -26,9 +27,8 @@ class Func1 : public Function {
 
         ActivationStatus runFunction() override {
             printf_stdout(0, "runFunction : %p\n", acl->begin()->ret);
-            auto *none = new Object("None", inter);
-            inter->getActivation()->getDownStream()->pushMessage(new NormalMessage(none));
-            return aFuncore::as_end;
+            new ExeActivation(func_code, inter);
+            return aFuncore::as_run;
         }
 
         ~CallFunc1() override {
@@ -36,10 +36,19 @@ class Func1 : public Function {
         }
     };
 
+    Code *func_code;
 public:
-    explicit Func1(Inter *inter_) : Function("Function", inter_) {}
+    explicit Func1(Inter *inter_) : Function("Function", inter_) {
+        func_code = (new Code(0, "run-code.aun"));
+        func_code->connect(new Code(block_p, new Code("test-var", 1), 0));
+    }
+
+    ~Func1() {
+        func_code->destructAll();
+    }
+
     CallFunction *getCallFunction(Code *code, Inter *inter) override {
-        return dynamic_cast<CallFunction *>(new CallFunc1(code, inter));
+        return dynamic_cast<CallFunction *>(new CallFunc1(func_code, code, inter));
     }
 };