123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- #include "aFuncore.h"
- using namespace aFuncore;
- using namespace aFuntool;
- void printInterEvent(Inter &inter);
- class Func1 : public Function {
- class CallFunc1 : public CallFunction {
- Code &func_code;
- const Code::ByteCode *call_code;
- Inter &inter;
- std::list<ArgCodeList> *acl;
- public:
- CallFunc1(Code &func_code_, const Code::ByteCode *code_, Inter &inter_) : func_code{func_code_}, call_code{code_}, inter{inter_} {
- acl = new std::list<ArgCodeList>;
- if (code_ != nullptr) {
- ArgCodeList agr1 = {code_->getSon()->toNext()};
- acl->push_front(agr1);
- }
- }
- std::list<ArgCodeList> *getArgCodeList(Inter &inter_, Activation &activation, const Code::ByteCode *call) override {
- return acl;
- }
- void runFunction() override {
- if (acl->empty())
- printf_stdout(0, "runFunction No AegCodeList\n");
- else
- printf_stdout(0, "runFunction : %p\n", acl->begin()->ret);
- new ExeActivation(func_code, inter);
- }
- ~CallFunc1() override {
- delete acl;
- }
- };
- Code func_code;
- public:
- explicit Func1(Inter &inter_) : Object("Function", inter_), func_code {"run-code.aun"} {
- func_code.getByteCode()->connect(
- new Code::ByteCode(func_code, Code::ByteCode::block_p,
- new Code::ByteCode(func_code, "test-var", 1), 0));
- }
- ~Func1() override = default;
- CallFunction *getCallFunction(const Code::ByteCode *code, Inter &inter) override {
- return dynamic_cast<CallFunction *>(new CallFunc1(func_code, code, inter));
- }
- bool isInfix() override {return true;}
- void destruct(Inter &gc_inter) override {
- aFuntool::printf_stdout(0, "%p destruct\n", this);
- auto code = Code("run-code.aun");
- code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_p,
- new Code::ByteCode(code, "test-var", 1), 0));
- gc_inter.runCode(code);
- printInterEvent(gc_inter);
- fputs_stdout("\n");
- };
- };
- class Literaler1 : public Literaler {
- Code func_code;
- public:
- explicit Literaler1(Inter &inter_) : Object("Data", inter_), func_code{"run-code.aun"} {
- func_code.getByteCode()->connect(
- new Code::ByteCode(func_code, Code::ByteCode::block_p,
- new Code::ByteCode(func_code, "test-var", 1), 0));
- }
- ~Literaler1() override = default;
- void getObject(const std::string &literal, char prefix, Inter &inter, Activation &activation) override {
- aFuntool::cout << "Literaler1: " << literal << "prefix: " << (prefix == NUL ? '-' : prefix) << "\n";
- new ExeActivation(func_code, inter);
- }
- };
- class CBV1 : public CallBackVar {
- Code func_code;
- public:
- explicit CBV1(Inter &inter_) : Object("CBV1", inter_), func_code{"run-code.aun"} {
- func_code.getByteCode()->connect(
- new Code::ByteCode(func_code, Code::ByteCode::block_p,
- new Code::ByteCode(func_code, "test-var", 1), 0));
- }
- ~CBV1() override = default;
- void callBack(Inter &inter, Activation &activation) override {
- aFuntool::cout << "CallBackVar callback\n";
- new ExeActivation(func_code, inter);
- }
- };
- static void printMessage(const std::string &type, Message *msg, Inter &inter) {
- if (type == "NORMAL") {
- auto *msg_ = dynamic_cast<NormalMessage *>(msg);
- if (msg_ == nullptr)
- return;
- aFuntool::printf_stdout(0, "NORMAL: %p\n", msg_->getObject());
- } else if (type == "ERROR") {
- auto *msg_ = dynamic_cast<ErrorMessage *>(msg);
- if (msg_ == nullptr)
- return;
- int32_t error_std = 0;
- inter.getEnvVarSpace().findNumber("sys:error_std", error_std);
- if (error_std == 0) {
- aFuntool::printf_stderr(0, "Error TrackBack\n");
- for (auto &begin: msg_->getTrackBack())
- aFuntool::printf_stderr(0, " File \"%s\", line %d\n", begin.path.c_str(), begin.line);
- aFuntool::printf_stderr(0, "%s: %s\n", msg_->getErrorType().c_str(), msg_->getErrorInfo().c_str());
- } else {
- aFuntool::printf_stdout(0, "Error TrackBack\n");
- for (auto &begin: msg_->getTrackBack())
- aFuntool::printf_stdout(0, " File \"%s\", line %d\n", begin.path.c_str(), begin.line);
- aFuntool::printf_stdout(0, "%s: %s\n", msg_->getErrorType().c_str(), msg_->getErrorInfo().c_str());
- }
- }
- }
- void printInterEvent(Inter &inter) {
- std::string type;
- for (auto msg = inter.getOutMessageStream().popFrontMessage(type); msg != nullptr; msg = inter.getOutMessageStream().popFrontMessage(type)) {
- printMessage(type, msg, inter);
- delete msg;
- }
- }
- void thread_test(Inter &son) {
- auto code = Code("run-code.aun");
- code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_p,
- new Code::ByteCode(code, "test-var", 1), 0));
- son.runCode(code);
- printInterEvent(son);
- fputs_stdout("\n");
- }
- int Main() {
- Environment env {};
- Inter inter {env};
- auto obj = new Object("Object", inter);
- inter.getGlobalVarlist()->defineVar("test-var", obj);
- aFuntool::cout << "obj: " << obj << "\n";
- obj->delReference();
- auto func = new Func1(inter);
- inter.getGlobalVarlist()->defineVar("test-func", func);
- aFuntool::cout << "func: " << func << "\n";
- func->delReference();
- auto literaler = new Literaler1(inter);
- inter.getGlobalVarlist()->defineVar("test-literaler", literaler);
- aFuntool::cout << "literaler: " << literaler << "\n";
- literaler->delReference();
- auto cbv = new CBV1(inter);
- inter.getGlobalVarlist()->defineVar("test-cbv", cbv);
- aFuntool::cout << "cbv: " << cbv << "\n";
- inter.getEnvVarSpace().setNumber("sys:error_std", 1);
- cbv->delReference();
- auto tmp = new Func1(inter);
- aFuntool::cout << "tmp: " << tmp << "\n\n";
- tmp->delReference();
- aFuntool::cout << "Checking gc ...\n";
- aFuntool::safeSleep(3);
- {
- fputs_stdout("Test-1: block-p & get test-var\n");
- auto code = Code("run-code.aun");
- code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_p,
- new Code::ByteCode(code, "test-var", 1), 0));
- inter.runCode(code);
- printInterEvent(inter);
- fputs_stdout("\n");
- }
- {
- fputs_stdout("Test-2: block-c & run {test-func test-var}\n");
- auto code = Code("run-code.aun");
- auto arg = new Code::ByteCode(code, "test-func", 1);
- arg->connect(new Code::ByteCode(code, "test-var", 1));
- code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_c, arg, 0));
- inter.runCode(code);
- printInterEvent(inter);
- fputs_stdout("\n");
- }
- {
- fputs_stdout("Test-3: block-b & run [test-var test-func]\n");
- auto code = Code("run-code.aun");
- auto arg = new Code::ByteCode(code, "test-var", 1);
- arg->connect(new Code::ByteCode(code, "test-func", 1));
- code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_b, arg, 0));
- inter.runCode(code);
- printInterEvent(inter);
- fputs_stdout("\n");
- }
- {
- fputs_stdout("Test-4: test-literaler\n");
- inter.pushLiteral("data[0-9]", "test-literaler", false);
- auto code = Code("run-code.aun");
- code.getByteCode()->connect(new Code::ByteCode(code, "data4", 1));
- inter.runCode(code);
- printInterEvent(inter);
- fputs_stdout("\n");
- }
- {
- fputs_stdout("Test-5: test-cbv\n");
- auto code = Code("run-code.aun");
- code.getByteCode()->connect(new Code::ByteCode(code, "test-cbv", 1));
- inter.runCode(code);
- printInterEvent(inter);
- fputs_stdout("\n");
- }
- {
- fputs_stdout("Test-6: run-function\n");
- inter.runCode(func);
- printInterEvent(inter);
- fputs_stdout("\n");
- }
- {
- /* 多线程 */
- fputs_stdout("Test-7: thread\n");
- Inter son{inter};
- std::thread thread{thread_test, std::ref(son)};
- {
- auto code = Code("run-code.aun");
- code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_p,
- new Code::ByteCode(code, "test-var", 1), 0));
- inter.runCode(code);
- printInterEvent(inter);
- fputs_stdout("\n");
- }
- thread.join();
- }
- /* 执行错误的代码 */
- {
- auto code = Code("run-code.aun");
- code.getByteCode()->connect(new Code::ByteCode(code, "test-not-var", 1));
- inter.runCode(code);
- printInterEvent(inter);
- fputs_stdout("\n");
- }
- /* 不会执行的代码 */
- inter.setInterExit();
- {
- auto code = Code("run-code.aun");
- code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_p,
- new Code::ByteCode(code, "test-var", 1), 0));
- inter.runCode(code);
- printInterEvent(inter);
- }
- return 0;
- }
- int main() {
- int exit_code = 0;
- try {
- std::string base_path = getExedir(1);
- if (base_path.empty()) {
- printf_stderr(0, "aFunlang init error.");
- aFunExitReal(EXIT_FAILURE);
- }
- aFuntool::LogFactory factor{};
- aFuncore::InitInfo info = {.base_dir=base_path,
- .factor=factor,
- .log_asyn=true,
- .level=log_debug,
- };
- if (!aFunCoreInit(&info)) {
- printf_stderr(0, "aFunlang init error.");
- aFunExitReal(EXIT_FAILURE);
- }
- exit_code = Main();
- aFunExit(exit_code);
- } catch (aFuntool::Exit &e) {
- exit_code = e.getExitCode();
- }
- aFunExitReal(exit_code);
- }
|