run-code.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include "aFuncore.h"
  2. using namespace aFuncore;
  3. using namespace aFuntool;
  4. void printInterEvent(Inter &inter);
  5. class Func1 : public Function {
  6. class CallFunc1 : public CallFunction {
  7. Code &func_code;
  8. const Code::ByteCode *call_code;
  9. Inter &inter;
  10. std::list<ArgCodeList> *acl;
  11. public:
  12. CallFunc1(Code &func_code_, const Code::ByteCode *code_, Inter &inter_) : func_code{func_code_}, call_code{code_}, inter{inter_} {
  13. acl = new std::list<ArgCodeList>;
  14. if (code_ != nullptr) {
  15. ArgCodeList agr1 = {code_->getSon()->toNext()};
  16. acl->push_front(agr1);
  17. }
  18. }
  19. std::list<ArgCodeList> *getArgCodeList(Inter &inter_, Activation &activation, const Code::ByteCode *call) override {
  20. return acl;
  21. }
  22. void runFunction() override {
  23. if (acl->empty())
  24. printf_stdout(0, "runFunction No AegCodeList\n");
  25. else
  26. printf_stdout(0, "runFunction : %p\n", acl->begin()->ret);
  27. new ExeActivation(func_code, inter);
  28. }
  29. ~CallFunc1() override {
  30. delete acl;
  31. }
  32. };
  33. Code func_code;
  34. public:
  35. explicit Func1(Inter &inter_) : Object("Function", inter_), func_code {"run-code.aun"} {
  36. func_code.getByteCode()->connect(
  37. new Code::ByteCode(func_code, Code::ByteCode::block_p,
  38. new Code::ByteCode(func_code, "test-var", 1), 0));
  39. }
  40. ~Func1() override = default;
  41. CallFunction *getCallFunction(const Code::ByteCode *code, Inter &inter) override {
  42. return dynamic_cast<CallFunction *>(new CallFunc1(func_code, code, inter));
  43. }
  44. bool isInfix() override {return true;}
  45. void destruct(Inter &gc_inter) override {
  46. aFuntool::printf_stdout(0, "%p destruct\n", this);
  47. auto code = Code("run-code.aun");
  48. code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_p,
  49. new Code::ByteCode(code, "test-var", 1), 0));
  50. gc_inter.runCode(code);
  51. printInterEvent(gc_inter);
  52. };
  53. };
  54. class Literaler1 : public Literaler {
  55. Code func_code;
  56. public:
  57. explicit Literaler1(Inter &inter_) : Object("Data", inter_), func_code{"run-code.aun"} {
  58. func_code.getByteCode()->connect(
  59. new Code::ByteCode(func_code, Code::ByteCode::block_p,
  60. new Code::ByteCode(func_code, "test-var", 1), 0));
  61. }
  62. ~Literaler1() override = default;
  63. void getObject(const std::string &literal, char prefix, Inter &inter, Activation &activation) override {
  64. aFuntool::cout << "Literaler1: " << literal << "prefix: " << (prefix == NUL ? '-' : prefix) << "\n";
  65. new ExeActivation(func_code, inter);
  66. }
  67. };
  68. class CBV1 : public CallBackVar {
  69. Code func_code;
  70. public:
  71. explicit CBV1(Inter &inter_) : Object("CBV1", inter_), func_code{"run-code.aun"} {
  72. func_code.getByteCode()->connect(
  73. new Code::ByteCode(func_code, Code::ByteCode::block_p,
  74. new Code::ByteCode(func_code, "test-var", 1), 0));
  75. }
  76. ~CBV1() override = default;
  77. void callBack(Inter &inter, Activation &activation) override {
  78. aFuntool::cout << "CallBackVar callback\n";
  79. new ExeActivation(func_code, inter);
  80. }
  81. };
  82. static void printMessage(const std::string &type, Message *msg, Inter &inter) {
  83. if (type == "NORMAL") {
  84. auto *msg_ = dynamic_cast<NormalMessage *>(msg);
  85. if (msg_ == nullptr)
  86. return;
  87. aFuntool::printf_stdout(0, "NORMAL: %p\n", msg_->getObject());
  88. } else if (type == "ERROR") {
  89. auto *msg_ = dynamic_cast<ErrorMessage *>(msg);
  90. if (msg_ == nullptr)
  91. return;
  92. int32_t error_std = 0;
  93. inter.getEnvVarSpace().findNumber("sys:error_std", error_std);
  94. if (error_std == 0) {
  95. aFuntool::printf_stderr(0, "Error TrackBack\n");
  96. for (auto &begin: msg_->getTrackBack())
  97. aFuntool::printf_stderr(0, " File \"%s\", line %d\n", begin.path.c_str(), begin.line);
  98. aFuntool::printf_stderr(0, "%s: %s\n", msg_->getErrorType().c_str(), msg_->getErrorInfo().c_str());
  99. } else {
  100. aFuntool::printf_stdout(0, "Error TrackBack\n");
  101. for (auto &begin: msg_->getTrackBack())
  102. aFuntool::printf_stdout(0, " File \"%s\", line %d\n", begin.path.c_str(), begin.line);
  103. aFuntool::printf_stdout(0, "%s: %s\n", msg_->getErrorType().c_str(), msg_->getErrorInfo().c_str());
  104. }
  105. }
  106. }
  107. void printInterEvent(Inter &inter) {
  108. std::string type;
  109. for (auto msg = inter.getOutMessageStream().popFrontMessage(type); msg != nullptr; msg = inter.getOutMessageStream().popFrontMessage(type)) {
  110. printMessage(type, msg, inter);
  111. delete msg;
  112. }
  113. }
  114. void thread_test(Inter &son) {
  115. auto code = Code("run-code.aun");
  116. code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_p,
  117. new Code::ByteCode(code, "test-var", 1), 0));
  118. son.runCode(code);
  119. printInterEvent(son);
  120. fputs_stdout("\n");
  121. }
  122. int Main() {
  123. Environment env {};
  124. Inter inter {env};
  125. auto obj = new Object("Object", inter);
  126. inter.getGlobalVarlist()->defineVar("test-var", obj);
  127. aFuntool::cout << "obj: " << obj << "\n";
  128. obj->delReference();
  129. auto func = new Func1(inter);
  130. inter.getGlobalVarlist()->defineVar("test-func", func);
  131. aFuntool::cout << "func: " << func << "\n";
  132. func->delReference();
  133. auto literaler = new Literaler1(inter);
  134. inter.getGlobalVarlist()->defineVar("test-literaler", literaler);
  135. aFuntool::cout << "literaler: " << literaler << "\n";
  136. literaler->delReference();
  137. auto cbv = new CBV1(inter);
  138. inter.getGlobalVarlist()->defineVar("test-cbv", cbv);
  139. aFuntool::cout << "cbv: " << cbv << "\n";
  140. inter.getEnvVarSpace().setNumber("sys:error_std", 1);
  141. cbv->delReference();
  142. auto tmp = new Func1(inter);
  143. aFuntool::cout << "tmp: " << tmp << "\n\n";
  144. tmp->delReference();
  145. aFuntool::cout << "Checking gc ...\n";
  146. for (int i = 0; i <= 12; i++) {
  147. aFuntool::cout << "Wait " << (i * 0.25) << "\n";
  148. aFuntool::safeSleep(0.25);
  149. }
  150. aFuntool::cout << "Check gc finished.\n\n";
  151. {
  152. fputs_stdout("Test-1: block-p & get test-var\n");
  153. auto code = Code("run-code.aun");
  154. code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_p,
  155. new Code::ByteCode(code, "test-var", 1), 0));
  156. inter.runCode(code);
  157. printInterEvent(inter);
  158. fputs_stdout("\n");
  159. }
  160. {
  161. fputs_stdout("Test-2: block-c & run {test-func test-var}\n");
  162. auto code = Code("run-code.aun");
  163. auto arg = new Code::ByteCode(code, "test-func", 1);
  164. arg->connect(new Code::ByteCode(code, "test-var", 1));
  165. code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_c, arg, 0));
  166. inter.runCode(code);
  167. printInterEvent(inter);
  168. fputs_stdout("\n");
  169. }
  170. {
  171. fputs_stdout("Test-3: block-b & run [test-var test-func]\n");
  172. auto code = Code("run-code.aun");
  173. auto arg = new Code::ByteCode(code, "test-var", 1);
  174. arg->connect(new Code::ByteCode(code, "test-func", 1));
  175. code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_b, arg, 0));
  176. inter.runCode(code);
  177. printInterEvent(inter);
  178. fputs_stdout("\n");
  179. }
  180. {
  181. fputs_stdout("Test-4: test-literaler\n");
  182. inter.pushLiteral("data[0-9]", "test-literaler", false);
  183. auto code = Code("run-code.aun");
  184. code.getByteCode()->connect(new Code::ByteCode(code, "data4", 1));
  185. inter.runCode(code);
  186. printInterEvent(inter);
  187. fputs_stdout("\n");
  188. }
  189. {
  190. fputs_stdout("Test-5: test-cbv\n");
  191. auto code = Code("run-code.aun");
  192. code.getByteCode()->connect(new Code::ByteCode(code, "test-cbv", 1));
  193. inter.runCode(code);
  194. printInterEvent(inter);
  195. fputs_stdout("\n");
  196. }
  197. {
  198. fputs_stdout("Test-6: run-function\n");
  199. inter.runCode(func);
  200. printInterEvent(inter);
  201. fputs_stdout("\n");
  202. }
  203. {
  204. /* 多线程 */
  205. fputs_stdout("Test-7: thread\n");
  206. Inter son{inter};
  207. std::thread thread{thread_test, std::ref(son)};
  208. {
  209. auto code = Code("run-code.aun");
  210. code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_p,
  211. new Code::ByteCode(code, "test-var", 1), 0));
  212. inter.runCode(code);
  213. printInterEvent(inter);
  214. fputs_stdout("\n");
  215. }
  216. thread.join();
  217. }
  218. /* 执行错误的代码 */
  219. {
  220. auto code = Code("run-code.aun");
  221. code.getByteCode()->connect(new Code::ByteCode(code, "test-not-var", 1));
  222. inter.runCode(code);
  223. printInterEvent(inter);
  224. fputs_stdout("\n");
  225. }
  226. /* 不会执行的代码 */
  227. inter.setInterExit();
  228. {
  229. auto code = Code("run-code.aun");
  230. code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_p,
  231. new Code::ByteCode(code, "test-var", 1), 0));
  232. inter.runCode(code);
  233. printInterEvent(inter);
  234. }
  235. return 0;
  236. }
  237. int main() {
  238. int exit_code = 0;
  239. try {
  240. std::string base_path = getExedir(1);
  241. if (base_path.empty()) {
  242. printf_stderr(0, "aFunlang init error.");
  243. aFunExitReal(EXIT_FAILURE);
  244. }
  245. aFuntool::LogFactory factor{};
  246. aFuncore::InitInfo info{base_path, factor, true, log_debug};
  247. if (!aFunCoreInit(&info)) {
  248. printf_stderr(0, "aFunlang init error.");
  249. aFunExitReal(EXIT_FAILURE);
  250. }
  251. exit_code = Main();
  252. aFunExit(exit_code);
  253. } catch (aFuntool::Exit &e) {
  254. exit_code = e.getExitCode();
  255. }
  256. aFunExitReal(exit_code);
  257. }