inter.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "inter.h"
  2. #include "activation.h"
  3. #include "init.h"
  4. #include "env-var.h"
  5. #include "var.h"
  6. #include "msg.h"
  7. using namespace aFuncore;
  8. using namespace aFuntool;
  9. Inter::Inter(int argc, char **argv, ExitMode em)
  10. : base{this}, is_derive{false}, status_lock{}, monitor{}, monitor_lock{}, monitor_cond{} {
  11. status = inter_creat;
  12. pthread_mutex_init(&status_lock, nullptr);
  13. gc = new GcRecord;
  14. gc->obj = nullptr;
  15. gc->var = nullptr;
  16. gc->varspace = nullptr;
  17. activation = nullptr;
  18. literal = new std::list<LiteralRegex>;
  19. envvar = new EnvVarSpace();
  20. envvar->setNumber("sys:gc-runtime", 2);
  21. envvar->setString("sys:prefix", "''"); // 引用,顺序执行
  22. envvar->setNumber("sys:exit-code", 0);
  23. envvar->setNumber("sys:argc", argc);
  24. envvar->setNumber("sys:error_std", 0);
  25. for (int i = 0; i < argc; i++) {
  26. char buf[20];
  27. snprintf(buf, 10, "sys:arg%d", i);
  28. envvar->setString(buf, argv[i]);
  29. }
  30. result = nullptr;
  31. son_inter = new std::list<Inter *>;
  32. exit_flat = ef_none;
  33. exit_mode = em;
  34. pthread_mutex_init(&monitor_lock, nullptr);
  35. pthread_cond_init(&monitor_cond, nullptr);
  36. protect = new ProtectVarSpace(this); // 放到最后
  37. global = new VarSpace(this); // 放到最后
  38. global_varlist = new VarList(protect);
  39. global_varlist->push(global);
  40. status = inter_init;
  41. }
  42. Inter::~Inter(){
  43. pthread_mutex_destroy(&status_lock);
  44. pthread_mutex_destroy(&monitor_lock);
  45. pthread_cond_destroy(&monitor_cond);
  46. if (!is_derive) {
  47. delete global_varlist;
  48. Object::destruct(gc->obj);
  49. Var::destruct(gc->var);
  50. VarSpace::destruct(gc->varspace);
  51. for (auto &it : *literal)
  52. delete it.rg;
  53. delete literal;
  54. delete gc;
  55. delete son_inter;
  56. delete envvar;
  57. }
  58. }
  59. /**
  60. * 使能 (激活解释器)
  61. */
  62. void Inter::enable(){
  63. if (status == inter_init) {
  64. protect->setProtect(true);
  65. status = inter_normal;
  66. }
  67. }
  68. /**
  69. * 运行代码(直接运行activation)
  70. * @return
  71. */
  72. bool Inter::runCode(){
  73. while (activation != nullptr) {
  74. Code *code = nullptr;
  75. ActivationStatus as = activation->getCode(code);
  76. switch (as) {
  77. case as_end: {
  78. Activation *prev = activation->toPrev();
  79. delete activation;
  80. activation = prev;
  81. break;
  82. }
  83. case as_run:
  84. activation->runCode(code);
  85. break;
  86. case as_end_run:
  87. activation->endRun();
  88. break;
  89. default:
  90. errorLog(aFunCoreLogger, "Error activation status.");
  91. activation->getDownStream()->pushMessage(new ErrorMessage("RuntimeError", "Error activation status.", activation));
  92. break;
  93. }
  94. if (isExit()) {
  95. while (activation != nullptr) {
  96. Activation *prev = activation->toPrev();
  97. delete activation;
  98. activation = prev;
  99. }
  100. return false;
  101. }
  102. }
  103. return true;
  104. }
  105. /**
  106. * 运行代码
  107. * @param code 代码
  108. * @return
  109. */
  110. bool Inter::runCode(Code *code){
  111. if (activation != nullptr) {
  112. errorLog(aFunCoreLogger, "Run code with activation");
  113. return false;
  114. }
  115. new TopActivation(code, this);
  116. return runCode();
  117. }
  118. /**
  119. * 检查字面量是否匹配
  120. * @param element 字面量
  121. * @return
  122. */
  123. bool Inter::checkLiteral(const std::string &element) const {
  124. if (literal->empty())
  125. return false;
  126. auto it = literal->begin();
  127. auto end = literal->end();
  128. for(NULL;it != end;it++){
  129. try {
  130. if (it->rg->match(element) != 1)
  131. continue;
  132. return true;
  133. } catch (RegexException &e) {
  134. continue;
  135. }
  136. }
  137. return false;
  138. }
  139. /**
  140. * 检查字面量正则匹配
  141. * @param element 字面量
  142. * @param literaler 函数
  143. * @param in_protect 是否保护空间
  144. * @return
  145. */
  146. bool Inter::checkLiteral(const std::string &element, std::string &literaler, bool &in_protect) const {
  147. if (literal->empty())
  148. return false;
  149. auto it = literal->begin();
  150. auto end = literal->end();
  151. for(NULL;it != end;it++){
  152. try {
  153. if (it->rg->match(element) != 1)
  154. continue;
  155. literaler = it->literaler;
  156. in_protect = it->in_protect;
  157. return true;
  158. } catch (RegexException &e) {
  159. continue;
  160. }
  161. }
  162. return false;
  163. }
  164. bool Inter::pushLiteral(const std::string &pattern, const std::string &literaler, bool in_protect){
  165. try {
  166. auto rg = new Regex(pattern);
  167. literal->push_front({rg, pattern, literaler, in_protect});
  168. } catch (RegexException &e) {
  169. return false;
  170. }
  171. return true;
  172. }