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