1
0

inter.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "core_inter.h"
  2. #include "core_activation.h"
  3. #include "core_logger.h"
  4. #include "core_exception.h"
  5. namespace aFuncore {
  6. Inter::Inter(Environment &env_)
  7. : status{inter_init}, env{env_}, activation{nullptr}, out{}, in{} {
  8. env++;
  9. }
  10. Inter::Inter(const Inter &base_inter)
  11. : status{inter_init}, env{base_inter.env}, activation{nullptr}, out{}, in{}{
  12. for (auto &i: base_inter.literal)
  13. literal.push_back(i);
  14. env++;
  15. }
  16. Inter::~Inter(){
  17. env--;
  18. }
  19. /**
  20. * 使能 (激活解释器)
  21. */
  22. void Inter::enable(){
  23. if (status == inter_init) {
  24. status = inter_normal;
  25. }
  26. }
  27. /**
  28. * 运行代码(直接运行activation)
  29. * @return
  30. */
  31. bool Inter::runCode(){
  32. if (status == inter_stop)
  33. status = inter_normal;
  34. while (activation != nullptr) {
  35. if (isInterStop()) {
  36. while (activation != nullptr)
  37. delete popActivation();
  38. return false;
  39. }
  40. const aFuncode::Code::ByteCode *code = nullptr;
  41. Activation::ActivationStatus as = activation->getCode(code);
  42. switch (as) {
  43. case Activation::as_end: {
  44. delete popActivation();
  45. break;
  46. }
  47. case Activation::as_run:
  48. activation->runCode(code);
  49. break;
  50. case Activation::as_end_run:
  51. activation->endRun();
  52. break;
  53. default:
  54. errorLog(aFunCoreLogger, "Error activation status.");
  55. delete popActivation();
  56. break;
  57. }
  58. }
  59. return true;
  60. }
  61. /**
  62. * 检查字面量是否匹配
  63. * @param element 字面量
  64. * @return
  65. */
  66. bool Inter::checkLiteral(const std::string &element) const{
  67. if (literal.empty())
  68. return false;
  69. for (auto &i: literal) {
  70. try {
  71. if (i.rg.match(element) != 1)
  72. continue;
  73. return true;
  74. } catch (aFuntool::RegexException &) {
  75. continue;
  76. }
  77. }
  78. return false;
  79. }
  80. /**
  81. * 检查字面量正则匹配
  82. * @param element 字面量
  83. * @param literaler 函数
  84. * @param in_protect 是否保护空间
  85. * @return
  86. */
  87. bool Inter::checkLiteral(const std::string &element, std::string &literaler, bool &in_protect) const{
  88. if (literal.empty())
  89. return false;
  90. for (auto &i: literal) {
  91. try {
  92. if (i.rg.match(element) != 1)
  93. continue;
  94. literaler = i.literaler;
  95. in_protect = i.in_protect;
  96. return true;
  97. } catch (aFuntool::RegexException &) {
  98. continue;
  99. }
  100. }
  101. return false;
  102. }
  103. bool Inter::pushLiteral(const std::string &pattern, const std::string &literaler, bool in_protect){
  104. try {
  105. literal.push_front({aFuntool::Regex(pattern), pattern, literaler, in_protect});
  106. } catch (aFuntool::RegexException &) {
  107. return false;
  108. }
  109. return true;
  110. }
  111. }