normal_activation.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include "rt_normal_activation.h"
  2. #include "rt_exe_activation.h"
  3. #include "rt_func_activation.h"
  4. #include "rt_exception.h"
  5. #include "rt_normal_message.h"
  6. #include "rt_error_message.h"
  7. #include "rt_logger.h"
  8. #include "rt_var_object.h"
  9. #include "rt_varspace_object.h"
  10. #include "rt_func_object.h"
  11. #include "rt_literaler_object.h"
  12. #include "rt_callback_var_object.h"
  13. namespace aFunrt {
  14. /**
  15. * 创建基本Activation
  16. * 自动继承上层VarList和UpMessage
  17. * 自动压入inter
  18. * @param inter_
  19. */
  20. NormalActivation::NormalActivation(aFuncore::Inter &inter_)
  21. : Activation(), inter{inter_}, up{inter_.getActivation() == nullptr ? nullptr : &inter_.getActivation()->getUpStream()}, down{}, line{0} {
  22. auto activation = dynamic_cast<NormalActivation *>(inter_.getActivation());
  23. if (activation != nullptr) {
  24. varlist.connect(activation->getVarlist());
  25. line = inter_.getActivation()->getFileLine();
  26. path = inter_.getActivation()->getFilePath();
  27. } else {
  28. auto global = new VarSpace(inter);
  29. varlist.push(global);
  30. global->delReference();
  31. path = "";
  32. }
  33. }
  34. static void ActivationTopProgress(aFuncore::Message *msg, aFuncore::Inter &inter, NormalActivation &activation){
  35. auto *t = dynamic_cast<TopMessage *>(msg);
  36. if (t)
  37. t->topProgress(inter, activation);
  38. };
  39. /**
  40. * 析构Activation
  41. * 注意: 不会自动从inter中弹出
  42. * 释放Varlist并且将DownMessage压入上层
  43. */
  44. NormalActivation::~NormalActivation(){
  45. if (inter.getActivation() != nullptr)
  46. down.joinMsg(inter.getActivation()->getDownStream());
  47. else
  48. down.forEach(ActivationTopProgress, std::ref(inter), std::ref(*this));
  49. }
  50. void NormalActivation::endRun() {
  51. }
  52. /**
  53. * 运行代码
  54. * @param code
  55. */
  56. void NormalActivation::runCode(const aFuncode::Code::ByteCode *code){
  57. auto code_type = code->getType();
  58. if (code_type == aFuncode::Code::ByteCode::code_start) { // start 不处理 msg
  59. auto *none = new aFuncore::Object("None", inter);
  60. down.pushMessage("NORMAL", new NormalMessage(none));
  61. none->delReference();
  62. } else {
  63. if (code_type == aFuncode::Code::ByteCode::code_element) {
  64. runCodeElement(code);
  65. } else
  66. switch (code->getBlockType()) {
  67. case aFuncode::Code::ByteCode::block_p: // 顺序执行
  68. runCodeBlockP(code);
  69. break;
  70. case aFuncode::Code::ByteCode::block_b:
  71. runCodeBlockB(code);
  72. break;
  73. case aFuncode::Code::ByteCode::block_c:
  74. runCodeBlockC(code);
  75. break;
  76. default:
  77. errorLog(aFunRuntimeLogger, "Error block type.");
  78. break;
  79. }
  80. }
  81. }
  82. void NormalActivation::runCodeElement(const aFuncode::Code::ByteCode *code){
  83. std::string literaler_name;
  84. bool in_protect = false;
  85. aFuncore::Object *obj = nullptr;
  86. if (inter.checkLiteral(code->getElement(), literaler_name, in_protect)) {
  87. if (in_protect)
  88. inter.getEnvVarSpace().findObject(literaler_name, obj);
  89. else {
  90. obj = varlist.findObject(literaler_name);
  91. if (obj == nullptr)
  92. inter.getEnvVarSpace().findObject(literaler_name, obj);
  93. }
  94. auto literaler = dynamic_cast<Literaler *>(obj);
  95. if (literaler != nullptr)
  96. literaler->getObject(code->getElement(), code->getPrefix(), inter, *this);
  97. else
  98. down.pushMessage("ERROR", new ErrorMessage("TypeError", "Error type of literal.", inter));
  99. } else {
  100. obj = varlist.findObject(code->getElement());
  101. if (obj == nullptr)
  102. inter.getEnvVarSpace().findObject(code->getElement(), obj);
  103. if (obj != nullptr) {
  104. auto cbv = dynamic_cast<CallBackVar *>(obj);
  105. if (cbv != nullptr && cbv->isCallBack(inter, *this))
  106. cbv->callBack(inter, *this);
  107. else
  108. down.pushMessage("NORMAL", new NormalMessage(obj));
  109. } else
  110. down.pushMessage("ERROR",
  111. new ErrorMessage("NameError",
  112. std::string("Variable ") + code->getElement() + " not fount.", inter));
  113. }
  114. }
  115. void NormalActivation::runCodeBlockP(const aFuncode::Code::ByteCode *code){
  116. inter.pushActivation(new ExeActivation(code->getSon(), inter));
  117. }
  118. void NormalActivation::runCodeBlockC(const aFuncode::Code::ByteCode *code){
  119. inter.pushActivation(new FuncActivation(code, inter));
  120. }
  121. void NormalActivation::runCodeBlockB(const aFuncode::Code::ByteCode *code){
  122. inter.pushActivation(new FuncActivation(code, inter));
  123. }
  124. aFuncore::UpMessageStream &NormalActivation::getUpStream() {
  125. return up;
  126. }
  127. aFuncore::DownMessageStream &NormalActivation::getDownStream() {
  128. return down;
  129. }
  130. aFuntool::FileLine NormalActivation::getFileLine() {
  131. return line;
  132. }
  133. const aFuntool::FilePath &NormalActivation::getFilePath() {
  134. return path;
  135. }
  136. NormalActivation::VarList::~VarList() {
  137. for (auto &t : varspace)
  138. t->delReference();
  139. }
  140. /**
  141. * 访问变量
  142. * @param name 变量名
  143. * @return
  144. */
  145. Var *NormalActivation::VarList::findVar(const std::string &name){
  146. Var *ret = nullptr;
  147. for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == nullptr; tmp++)
  148. ret = (*tmp)->findVar(name);
  149. return ret;
  150. }
  151. /**
  152. * 定义变量
  153. * 若定义出现redefine则退出报错
  154. * 若出现fail则跳到下一个变量空间尝试定义
  155. * @param name 变量名
  156. * @param data 变量(Object)
  157. * @return
  158. */
  159. bool NormalActivation::VarList::defineVar(const std::string &name, aFuncore::Object *data){
  160. VarSpace::VarOperationFlat ret = VarSpace::vof_fail;
  161. for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == VarSpace::vof_fail; tmp++)
  162. ret = (*tmp)->defineVar(name, data);
  163. return ret == VarSpace::vof_success;
  164. }
  165. /**
  166. * 定义变量
  167. * 若定义出现redefine则退出报错
  168. * 若出现fail则跳到下一个变量空间尝试定义
  169. * @param name 变量名
  170. * @param data 变量(Var)
  171. * @return
  172. */
  173. bool NormalActivation::VarList::defineVar(const std::string &name, Var *data){
  174. VarSpace::VarOperationFlat ret = VarSpace::vof_fail;
  175. for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == VarSpace::vof_fail; tmp++)
  176. ret = (*tmp)->defineVar(name, data);
  177. return ret == VarSpace::vof_success;
  178. }
  179. /**
  180. * 设置变量的值
  181. * 若not_var则跳到下一个变量空间
  182. * 若fail则结束
  183. * @param name 变量名
  184. * @param data 数据
  185. * @return
  186. */
  187. bool NormalActivation::VarList::setVar(const std::string &name, aFuncore::Object *data){
  188. VarSpace::VarOperationFlat ret = VarSpace::vof_not_var;
  189. for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == VarSpace::vof_not_var; tmp++)
  190. ret = (*tmp)->setVar(name, data);
  191. return ret == VarSpace::vof_success;
  192. }
  193. /**
  194. * 删除变量
  195. * 若not_var则跳到下一个变量空间
  196. * 若fail则结束
  197. * @param name
  198. * @return
  199. */
  200. bool NormalActivation::VarList::delVar(const std::string &name){
  201. VarSpace::VarOperationFlat ret = VarSpace::vof_not_var;
  202. for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == VarSpace::vof_not_var; tmp++)
  203. ret = (*tmp)->delVar(name);
  204. return ret == VarSpace::vof_success;
  205. }
  206. void NormalActivation::VarList::clear(){
  207. for (auto &t: varspace)
  208. t->delReference();
  209. varspace.clear();
  210. }
  211. void NormalActivation::VarList::connect(VarList &new_varlist){
  212. for (auto &t: new_varlist.varspace) {
  213. t->addReference();
  214. this->varspace.push_back(t);
  215. }
  216. }
  217. }