env.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  1. #include "aFunCore.h"
  2. #include "__env.h"
  3. #include "__global_obj.h"
  4. #include "__run.h"
  5. /* Core 创建和释放 */
  6. static af_Core *makeCore(enum GcRunTime grt);
  7. static void freeCore(af_Environment *env);
  8. /* Activity 创建和释放 */
  9. static af_Activity *makeActivity(af_Message *msg_up, af_VarSpaceListNode *vsl, af_Object *belong);
  10. static af_Activity *makeFuncActivity(af_Code *bt_top, af_Code *bt_start, bool return_first, af_Message *msg_up,
  11. af_VarSpaceListNode *vsl, af_Object *belong, af_Object *func);
  12. static af_Activity *makeTopActivity(af_Code *bt_top, af_Code *bt_start, af_VarSpace *protect, af_Object *belong);
  13. static af_Activity *makeTopImportActivity(af_Code *bt_top, af_Code *bt_start, af_VarSpace *protect, af_Object *belong, char *mark);
  14. static af_Activity *makeGcActivity(gc_DestructList *dl, gc_DestructList **pdl, af_Environment *env);
  15. static af_Activity *freeActivity(af_Activity *activity);
  16. static void freeActivityTop(af_Activity *activity);
  17. static void freeAllActivity(af_Activity *activity);
  18. static void clearActivity(af_Activity *activity);
  19. /* Activity 相关处理函数 */
  20. static void freeMark(af_Activity *activity);
  21. static void newActivity(af_Code *bt, const af_Code *next, bool return_first, af_Environment *env);
  22. /* 环境变量 创建与释放 */
  23. static af_EnvVar *makeEnvVar(char *name, char *data);
  24. static af_EnvVar *freeEnvVar(af_EnvVar *var);
  25. static void freeAllEnvVar(af_EnvVar *var);
  26. static void freeEnvVarSpace(af_EnvVarSpace *evs);
  27. /* 顶层消息处理器 创建与释放 */
  28. static af_TopMsgProcess *makeTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func);
  29. static af_TopMsgProcess *freeTopMsgProcess(af_TopMsgProcess *mp);
  30. static void freeAllTopMsgProcess(af_TopMsgProcess *mp);
  31. /* 顶层消息处理器 处理函数 */
  32. static af_TopMsgProcess *findTopMsgProcessFunc(char *type, af_Environment *env);
  33. /* LiteralData 创建与释放 */
  34. static af_LiteralDataList *makeLiteralDataList(char *data);
  35. static af_LiteralDataList *freeLiteralData_Pri(af_LiteralDataList *ld);
  36. /* LiteralRegex 创建与释放 */
  37. static af_LiteralRegex *makeLiteralRegex(char *pattern, char *func, bool in_protect);
  38. static af_LiteralRegex *freeLiteralRegex(af_LiteralRegex *lr);
  39. static void freeAllLiteralRegex(af_LiteralRegex *lr);
  40. /* af_ErrorBacktracking 创建与释放 */
  41. static af_ErrorBacktracking *makeErrorBacktracking(FileLine line, FilePath file, char *note);
  42. static af_ErrorBacktracking *freeErrorBacktracking(af_ErrorBacktracking *ebt);
  43. static void freeAllErrorBacktracking(af_ErrorBacktracking *ebt);
  44. /* af_ErrorBacktracking 相关函数 */
  45. static char *getActivityInfoToBacktracking(af_Activity *activity, bool print_bt_top);
  46. static void fprintfNote(FILE *file, char *note);
  47. /* 内置顶层消息处理器 */
  48. static void mp_NORMAL(af_Message *msg, bool is_gc, af_Environment *env);
  49. static void mp_ERROR(af_Message *msg, bool is_gc, af_Environment *env);
  50. static void mp_IMPORT(af_Message *msg, bool is_gc, af_Environment *env);
  51. /* 变量检查函数 */
  52. static bool isInfixFunc(af_Code *code, af_Environment *env);
  53. static af_Core *makeCore(enum GcRunTime grt) {
  54. af_Core *core = calloc(1, sizeof(af_Core));
  55. core->status = core_creat;
  56. core->protect = makeVarSpaceByCore(NULL, 3, 3, 3, core);
  57. core->gc_run = grt;
  58. core->gc_count_max = DEFAULT_GC_COUNT_MAX;
  59. return core;
  60. }
  61. /*
  62. * 函数名: freeCore
  63. * 目标: 释放Core
  64. * 因为gc_freeAllValue需要env作为参数, 故使用env作为freeCore的参数
  65. */
  66. static void freeCore(af_Environment *env) {
  67. printGCByCode(env->core);
  68. gc_freeAllValue(env);
  69. freeAllLiteralRegex(env->core->lr);
  70. free(env->core);
  71. }
  72. char setPrefix(size_t name, char prefix, af_Environment *env) {
  73. if (name >= PREFIX_SIZE)
  74. return '-'; // 表示未获取到prefix (NUL在Code中表示无prefix)
  75. char *prefix_ = findEnvVar(ev_sys_prefix, env);
  76. if (prefix_ == NULL || strlen(prefix_) < PREFIX_SIZE)
  77. return '-';
  78. switch (name) {
  79. case E_QUOTE:
  80. if (prefix == NUL && strchr(E_PREFIX, prefix) == NULL)
  81. prefix = '-';
  82. break;
  83. case B_EXEC:
  84. case B_EXEC_FIRST:
  85. if (prefix == NUL && strchr(B_PREFIX, prefix) == NULL)
  86. prefix = '-';
  87. break;
  88. default:
  89. break;
  90. }
  91. char old = prefix_[name];
  92. prefix_[name] = prefix;
  93. return old;
  94. }
  95. char getPrefix(size_t name, af_Environment *env) {
  96. if (name >= PREFIX_SIZE)
  97. return '-'; // 表示未获取到prefix (NUL在Code中表示无prefix)
  98. char *prefix = findEnvVar(ev_sys_prefix, env);
  99. if (prefix == NULL || strlen(prefix) < PREFIX_SIZE)
  100. return '-';
  101. return prefix[name];
  102. }
  103. af_VarSpace *getProtectVarSpace(af_Environment *env) {
  104. return env->core->protect;
  105. }
  106. /*
  107. * 函数名: getBaseObjectFromCore
  108. * 目标: 从VarSpace中获取一个量
  109. * 作用: 用于init初始化时在保护空间获得一些初始化对象
  110. */
  111. af_Object *getBaseObjectFromCore(char *name, af_Core *core) {
  112. af_Var *var = findVarFromVarSpace(name, NULL, core->protect);
  113. if (var != NULL)
  114. return var->vn->obj;
  115. return NULL;
  116. }
  117. /*
  118. * 函数名: getBaseObject
  119. * 目标: getBaseObjectFromCore的对外接口
  120. */
  121. af_Object *getBaseObject(char *name, af_Environment *env) {
  122. return getBaseObjectFromCore(name, env->core);
  123. }
  124. void setCoreStop(af_Environment *env) {
  125. if (env->core->status != core_exit)
  126. env->core->status = core_stop;
  127. }
  128. void setCoreExit(int exit_code, af_Environment *env) {
  129. env->core->status = core_exit;
  130. env->core->exit_code = exit_code;
  131. }
  132. void setCoreNormal(af_Environment *env) {
  133. if (env->core->status == core_exit || env->core->status == core_stop) {
  134. env->core->status = core_normal;
  135. env->core->exit_code = 0;
  136. }
  137. }
  138. static af_Activity *makeActivity(af_Message *msg_up, af_VarSpaceListNode *vsl, af_Object *belong) {
  139. af_Activity *activity = calloc(1, sizeof(af_Activity));
  140. activity->msg_up = msg_up;
  141. activity->msg_up_count = 0;
  142. activity->var_list = vsl;
  143. activity->new_vs_count = 0;
  144. activity->belong = belong;
  145. return activity;
  146. }
  147. static af_Activity *makeFuncActivity(af_Code *bt_top, af_Code *bt_start, bool return_first, af_Message *msg_up,
  148. af_VarSpaceListNode *vsl, af_Object *belong, af_Object *func) {
  149. af_Activity *activity = makeActivity(msg_up, vsl, belong);
  150. activity->type = act_func;
  151. activity->status = act_func_get;
  152. activity->func = func;
  153. setActivityBtTop(bt_top, activity);
  154. setActivityBtStart(bt_start, activity);
  155. activity->return_first = return_first;
  156. return activity;
  157. }
  158. static af_Activity *makeTopActivity(af_Code *bt_top, af_Code *bt_start, af_VarSpace *protect, af_Object *belong) {
  159. af_Activity *activity = makeActivity(NULL, NULL, belong);
  160. activity->type = act_top;
  161. activity->status = act_func_normal;
  162. activity->new_vs_count = 2;
  163. activity->var_list = makeVarSpaceList(belong->data->var_space);
  164. activity->var_list->next = makeVarSpaceList(protect);
  165. setActivityBtTop(bt_top, activity);
  166. setActivityBtStart(bt_start, activity);
  167. return activity;
  168. }
  169. static af_Activity *makeTopImportActivity(af_Code *bt_top, af_Code *bt_start, af_VarSpace *protect, af_Object *belong, char *mark) {
  170. af_Activity *activity = makeTopActivity(bt_top, bt_start, protect, belong);
  171. activity->type = act_top_import;
  172. if (mark != NULL)
  173. activity->import_mark = strCopy(mark);
  174. return activity;
  175. }
  176. static af_Activity *makeGcActivity(gc_DestructList *dl, gc_DestructList **pdl, af_Environment *env) {
  177. af_Activity *activity = makeActivity(NULL, NULL, env->core->global);
  178. activity->type = act_gc;
  179. activity->var_list = makeVarSpaceList(getProtectVarSpace(env));
  180. activity->new_vs_count = 1;
  181. activity->file = strCopy("aFun-gc.af.sys");
  182. activity->line = 0;
  183. activity->dl = dl;
  184. activity->pdl = pdl;
  185. activity->dl_next = dl;
  186. return activity;
  187. }
  188. static af_Activity *freeActivity(af_Activity *activity) {
  189. af_Activity *prev = activity->prev;
  190. freeAllMessage(activity->msg_down); // msg转移后需要将对应成员设置为NULL
  191. freeMessageCount(activity->msg_up_count, activity->msg_up);
  192. freeVarSpaceListCount(activity->new_vs_count, activity->var_list);
  193. free(activity->file);
  194. if (activity->type == act_gc) {
  195. if (activity->dl != NULL)
  196. freeAllDestructList(activity->dl);
  197. } else {
  198. // vsl 是引用自 var_list和func_var_list的 故不释放
  199. // func_var_list 是引用自函数的 故不释放
  200. freeVarSpaceListCount(activity->macro_vs_count, activity->macro_vsl);
  201. freeAllArgCodeList(activity->acl_start);
  202. if (activity->fi != NULL)
  203. freeFuncInfo(activity->fi);
  204. freeAllLiteralData(activity->ld);
  205. free(activity->import_mark);
  206. }
  207. free(activity);
  208. return prev;
  209. }
  210. static void freeActivityTop(af_Activity *activity) {
  211. freeAllMessage(activity->msg_down); // msg转移后需要将对应成员设置为NULL
  212. freeMessageCount(activity->msg_up_count, activity->msg_up);
  213. free(activity->file);
  214. activity->line = 0;
  215. activity->bt_top = NULL;
  216. activity->bt_start = NULL;
  217. activity->bt_next = NULL;
  218. }
  219. static void freeAllActivity(af_Activity *activity) {
  220. while (activity != NULL)
  221. activity = freeActivity(activity);
  222. }
  223. static void clearActivity(af_Activity *activity) {
  224. freeVarSpaceListCount(activity->macro_vs_count, activity->macro_vsl);
  225. /* acl在runArgList之后就被释放了 */
  226. /* acl在FuncBody暂时不释放 */
  227. activity->func_var_list = NULL;
  228. activity->bt_top = NULL;
  229. activity->bt_start = NULL;
  230. activity->bt_next = NULL;
  231. activity->acl_start = NULL;
  232. activity->acl_done = NULL;
  233. activity->body_next = NULL;
  234. free(activity->file);
  235. activity->line = 0;
  236. }
  237. /*
  238. * 函数名: setActivityBtTop
  239. * 目标: 设置activity的bt_top, 并且设置行号
  240. * bt_start和bt_next会被设置为NULL
  241. */
  242. void setActivityBtTop(af_Code *bt_top, af_Activity *activity) {
  243. activity->bt_top = bt_top;
  244. activity->bt_start = NULL;
  245. activity->bt_next = NULL;
  246. if (bt_top != NULL) {
  247. activity->line = bt_top->line;
  248. if (bt_top->path != NULL) {
  249. free(activity->file);
  250. activity->file = strCopy(bt_top->path);
  251. }
  252. } else
  253. activity->line = 0;
  254. }
  255. /*
  256. * 函数名: setActivityBtStart
  257. * 目标: 设置activity的bt_start, 并且设置行号
  258. * bt_next会被设置为NULL
  259. */
  260. void setActivityBtStart(af_Code *bt_start, af_Activity *activity) {
  261. activity->bt_start = bt_start;
  262. activity->bt_next = bt_start;
  263. if (bt_start != NULL) {
  264. activity->line = bt_start->line;
  265. if (bt_start->path != NULL) {
  266. free(activity->file);
  267. activity->file = strCopy(bt_start->path);
  268. }
  269. } else
  270. activity->line = 0;
  271. }
  272. /*
  273. * 函数名: setActivityBtNext
  274. * 目标: 设置activity的bt_next, 并且设置行号
  275. */
  276. void setActivityBtNext(af_Code *bt_next, af_Activity *activity) {
  277. activity->bt_next = bt_next;
  278. if (bt_next != NULL) {
  279. activity->line = bt_next->line;
  280. if (bt_next->path != NULL) {
  281. free(activity->file);
  282. activity->file = strCopy(bt_next->path);
  283. }
  284. } else
  285. activity->line = 0;
  286. }
  287. /*
  288. * 函数名: makeLiteralDataList
  289. * 目标: 生成一个 af_LiteralDataList
  290. * 注意: char *data 要求传入一个已经被复制的data值
  291. * makeLiteralDataList是内部函数, 属于可控函数, 因此data在函数内部不再复制
  292. */
  293. static af_LiteralDataList *makeLiteralDataList(char *data) {
  294. af_LiteralDataList *ld = calloc(1, sizeof(af_LiteralDataList));
  295. ld->literal_data = data;
  296. return ld;
  297. }
  298. static af_LiteralDataList *freeLiteralData_Pri(af_LiteralDataList *ld) {
  299. af_LiteralDataList *next = ld->next;
  300. free(ld->literal_data);
  301. free(ld);
  302. return next;
  303. }
  304. void freeAllLiteralData(af_LiteralDataList *ld) {
  305. while (ld != NULL)
  306. ld = freeLiteralData_Pri(ld);
  307. }
  308. void pushLiteralData(char *data, af_Environment *env) {
  309. af_LiteralDataList *ld = makeLiteralDataList(data);
  310. ld->next = env->activity->ld;
  311. env->activity->ld = ld;
  312. }
  313. af_Message *makeMessage(char *type, size_t size) {
  314. af_Message *msg = calloc(1, sizeof(af_Message));
  315. msg->type = strCopy(type);
  316. if (size != 0)
  317. msg->msg = calloc(1, size);
  318. msg->size = size;
  319. return msg;
  320. }
  321. af_Message *freeMessage(af_Message *msg) {
  322. af_Message *next = msg->next;
  323. free(msg->type);
  324. free(msg->msg);
  325. free(msg);
  326. return next;
  327. }
  328. void freeAllMessage(af_Message *msg) {
  329. while (msg != NULL)
  330. msg = freeMessage(msg);
  331. }
  332. bool freeMessageCount(size_t count, af_Message *msg) {
  333. for (size_t i = count; i > 0; i--) {
  334. if (msg == NULL) // 发生了错误
  335. return false;
  336. msg = freeMessage(msg);
  337. }
  338. return true;
  339. }
  340. void pushMessageUp(af_Message *msg, af_Environment *env) {
  341. msg->next = env->activity->msg_up;
  342. env->activity->msg_up = msg;
  343. env->activity->msg_up_count++;
  344. }
  345. void pushMessageDown(af_Message *msg, af_Environment *env) {
  346. msg->next = env->activity->msg_down;
  347. env->activity->msg_down = msg;
  348. }
  349. void *popMessageUpData(char *type, af_Environment *env) {
  350. for (af_Message **pmsg = &env->activity->msg_up; *pmsg != NULL; pmsg = &((*pmsg)->next)) {
  351. if (EQ_STR((*pmsg)->type, type))
  352. return (*pmsg)->msg; // msg_up是只读的
  353. }
  354. return NULL;
  355. }
  356. af_Message *popMessageUp(af_Environment *env) {
  357. if (env->activity->msg_up_count == 0 || env->activity->msg_up == NULL)
  358. return NULL;
  359. af_Message *msg = env->activity->msg_up;
  360. env->activity->msg_up = msg->next;
  361. msg->next = NULL;
  362. env->activity->msg_up_count--;
  363. return msg;
  364. }
  365. /*
  366. * 函数名: getMessageData
  367. * 目标: 获取`msg`的数据, 对外API
  368. */
  369. void *getMessageData(af_Message *msg) {
  370. return msg->msg;
  371. }
  372. af_Message *popMessageDown(char *type, af_Environment *env) {
  373. for (af_Message **pmsg = &env->activity->msg_down; *pmsg != NULL; pmsg = &((*pmsg)->next)) {
  374. if (EQ_STR((*pmsg)->type, type)) {
  375. af_Message *msg = *pmsg;
  376. *pmsg = msg->next;
  377. msg->next = NULL;
  378. return msg;
  379. }
  380. }
  381. return NULL;
  382. }
  383. af_Message *getFirstMessage(af_Environment *env) {
  384. af_Message *msg = env->activity->msg_down;
  385. env->activity->msg_down = msg->next;
  386. msg->next = NULL;
  387. return msg;
  388. }
  389. void connectMessage(af_Message **base, af_Message *msg) {
  390. while (*base != NULL)
  391. base = &((*base)->next);
  392. *base = msg;
  393. }
  394. af_Message *makeNORMALMessage(af_Object *obj) {
  395. af_Message *msg = makeMessage("NORMAL", sizeof(af_Object *));
  396. *(af_Object **)msg->msg = obj;
  397. gc_addReference(obj);
  398. return msg;
  399. }
  400. af_Message *makeERRORMessage(char *type, char *error, af_Environment *env) {
  401. char *info = getActivityInfoToBacktracking(env->activity, false);
  402. af_ErrorInfo *ei = makeErrorInfo(type, error, info, env->activity->line, env->activity->file);
  403. free(info);
  404. for (af_Activity *activity = env->activity->prev; activity != NULL; activity = activity->prev) {
  405. info = getActivityInfoToBacktracking(activity, true);
  406. pushErrorBacktracking(activity->line, activity->file, info, ei);
  407. free(info);
  408. }
  409. af_Message *msg = makeMessage("ERROR", sizeof(af_ErrorInfo *));
  410. *(af_ErrorInfo **)msg->msg = ei;
  411. return msg;
  412. }
  413. af_Message *makeERRORMessageFormat(char *type, af_Environment *env, const char *format, ...) {
  414. char buf[1024];
  415. va_list ap;
  416. va_start(ap, format);
  417. vsnprintf(buf, 1024, format, ap);
  418. va_end(ap);
  419. return makeERRORMessage(type, buf, env);;
  420. }
  421. af_Message *makeIMPORTMessage(char *mark, af_Object *obj) {
  422. af_Message *msg = makeMessage("IMPORT", sizeof(af_ImportInfo *));
  423. *(af_ImportInfo **)msg->msg = makeImportInfo(mark, obj);
  424. return msg;
  425. }
  426. static af_EnvVar *makeEnvVar(char *name, char *data) {
  427. af_EnvVar *var = calloc(1, sizeof(af_EnvVar));
  428. var->name = strCopy(name);
  429. var->data = strCopy(data);
  430. return var;
  431. }
  432. static af_EnvVar *freeEnvVar(af_EnvVar *var) {
  433. af_EnvVar *next = var->next;
  434. free(var->data);
  435. free(var->name);
  436. free(var);
  437. return next;
  438. }
  439. static void freeAllEnvVar(af_EnvVar *var) {
  440. while (var != NULL)
  441. var = freeEnvVar(var);
  442. }
  443. static af_EnvVarSpace *makeEnvVarSpace(void) {
  444. af_EnvVarSpace *esv = calloc(1, sizeof(af_EnvVarSpace));
  445. return esv;
  446. }
  447. static void freeEnvVarSpace(af_EnvVarSpace *evs) {
  448. for (int i = 0; i < ENV_VAR_HASH_SIZE; i++)
  449. freeAllEnvVar(evs->var[i]);
  450. free(evs);
  451. }
  452. void setEnvVar(char *name, char *data, af_Environment *env) {
  453. time33_t index = time33(name) % ENV_VAR_HASH_SIZE;
  454. af_EnvVar **pvar = &env->esv->var[index];
  455. env->esv->count++;
  456. for (NULL; *pvar != NULL; pvar = &((*pvar)->next)) {
  457. if (EQ_STR((*pvar)->name, name)) {
  458. free((*pvar)->data);
  459. (*pvar)->data = strCopy(data);
  460. return;
  461. }
  462. }
  463. *pvar = makeEnvVar(name, data);
  464. }
  465. char *findEnvVar(char *name, af_Environment *env) {
  466. time33_t index = time33(name) % ENV_VAR_HASH_SIZE;
  467. af_EnvVar **pvar = &env->esv->var[index];
  468. for (NULL; *pvar != NULL; pvar = &((*pvar)->next)) {
  469. if (EQ_STR((*pvar)->name, name))
  470. return (*pvar)->data;
  471. }
  472. return NULL;
  473. }
  474. static void mp_NORMAL(af_Message *msg, bool is_gc, af_Environment *env) {
  475. if (msg->msg == NULL || *(af_Object **)msg->msg == NULL) {
  476. fprintf(stderr, "NORMAL msg: %p error\n", msg->msg);
  477. return;
  478. }
  479. gc_delReference(*(af_Object **)msg->msg);
  480. if (!is_gc)
  481. printf("NORMAL Point: %p\n", *(af_Object **)msg->msg);
  482. }
  483. static void mp_ERROR(af_Message *msg, bool is_gc, af_Environment *env) {
  484. if (msg->msg == NULL || *(af_ErrorInfo **)msg->msg == NULL) {
  485. printf("ERROR msg: %p error\n", msg->msg);
  486. return;
  487. }
  488. if (!is_gc)
  489. fprintfErrorInfo(stdout, *(af_ErrorInfo **)msg->msg);
  490. freeErrorInfo(*(af_ErrorInfo **)msg->msg);
  491. }
  492. static void mp_IMPORT(af_Message *msg, bool is_gc, af_Environment *env) {
  493. if (msg->msg == NULL || *(af_ImportInfo **)msg->msg == NULL) {
  494. printf("IMPORT msg: %p error\n", msg->msg);
  495. return;
  496. }
  497. af_ImportInfo *ii = *(af_ImportInfo **)msg->msg;
  498. if (ii->obj == NULL) {
  499. printf("IMPORT msg: %p do not get obj\n", msg->msg);
  500. return;
  501. }
  502. if (ii->mark != NULL) {
  503. makeVarToProtectVarSpace(ii->mark, 3, 3, 3, ii->obj, env);
  504. printf("IMPORT point: [%s] %p \n", ii->mark, ii->obj);
  505. } else
  506. printf("IMPORT point: <no-name> %p \n", ii->obj);
  507. freeImportInfo(ii);
  508. }
  509. af_Environment *makeEnvironment(enum GcRunTime grt) {
  510. af_Environment *env = calloc(1, sizeof(af_Environment));
  511. env->core = makeCore(grt);
  512. env->esv = makeEnvVarSpace();
  513. /* 生成global对象 */
  514. env->core->global = makeGlobalObject(env);
  515. addVarToProtectVarSpace(makeVar("global", 3, 3, 3, env->core->global, env), env);
  516. /* 设置默认prefix */
  517. char prefix[PREFIX_SIZE + 1] = "";
  518. prefix[E_QUOTE] = '\'';
  519. prefix[B_EXEC] = '\'';
  520. prefix[B_EXEC_FIRST] = ',';
  521. setEnvVar(ev_sys_prefix, prefix, env);
  522. /* 设置NORMAL顶级处理器 */
  523. DLC_SYMBOL(TopMsgProcessFunc) func1 = MAKE_SYMBOL(mp_NORMAL, TopMsgProcessFunc);
  524. addTopMsgProcess("NORMAL", func1, env);
  525. FREE_SYMBOL(func1);
  526. DLC_SYMBOL(TopMsgProcessFunc) func2 = MAKE_SYMBOL(mp_ERROR, TopMsgProcessFunc);
  527. addTopMsgProcess("ERROR", func2, env);
  528. FREE_SYMBOL(func2);
  529. DLC_SYMBOL(TopMsgProcessFunc) func3 = MAKE_SYMBOL(mp_IMPORT, TopMsgProcessFunc);
  530. addTopMsgProcess("IMPORT", func3, env);
  531. FREE_SYMBOL(func3);
  532. env->core->status = core_init;
  533. env->activity = makeTopActivity(NULL, NULL, env->core->protect, env->core->global);
  534. return env;
  535. }
  536. void enableEnvironment(af_Environment *env) {
  537. env->core->protect->is_protect = true;
  538. env->core->status = core_normal;
  539. }
  540. void freeEnvironment(af_Environment *env) {
  541. bool res = true;
  542. if (env->core->status != core_creat)
  543. res = iterDestruct(10, env);
  544. freeAllActivity(env->activity);
  545. freeCore(env);
  546. freeEnvVarSpace(env->esv);
  547. freeAllTopMsgProcess(env->process);
  548. if (!res)
  549. printf("iterDestruct Error\n");
  550. free(env);
  551. }
  552. static af_TopMsgProcess *makeTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func) {
  553. af_TopMsgProcess *mp = calloc(1, sizeof(af_TopMsgProcess));
  554. mp->type = strCopy(type);
  555. mp->func = COPY_SYMBOL(func, TopMsgProcessFunc);
  556. return mp;
  557. }
  558. static af_TopMsgProcess *freeTopMsgProcess(af_TopMsgProcess *mp) {
  559. af_TopMsgProcess *next = mp->next;
  560. free(mp->type);
  561. FREE_SYMBOL(mp->func);
  562. free(mp);
  563. return next;
  564. }
  565. static void freeAllTopMsgProcess(af_TopMsgProcess *mp) {
  566. while (mp != NULL)
  567. mp = freeTopMsgProcess(mp);
  568. }
  569. static af_TopMsgProcess *findTopMsgProcessFunc(char *type, af_Environment *env) {
  570. af_TopMsgProcess *mp = env->process;
  571. for (NULL; mp != NULL; mp = mp->next) {
  572. if (EQ_STR(type, mp->type))
  573. return mp;
  574. }
  575. return NULL;
  576. }
  577. bool addTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func,
  578. af_Environment *env) {
  579. af_TopMsgProcess *mp = findTopMsgProcessFunc(type, env);
  580. if (mp != NULL)
  581. return false;
  582. mp = makeTopMsgProcess(type, func);
  583. mp->next = env->process;
  584. env->process = mp;
  585. return true;
  586. }
  587. static void newActivity(af_Code *bt, const af_Code *next, bool return_first, af_Environment *env){
  588. if (next == NULL && env->activity->body_next == NULL && env->activity->type == act_func) {
  589. printf("Tail tone recursive optimization\n");
  590. clearActivity(env->activity);
  591. env->activity->optimization = true;
  592. setActivityBtTop(bt, env->activity);
  593. if (!env->activity->return_first) // 若原本就有设置 return_first 则没有在设置的必要了, 因为该执行不会被返回
  594. env->activity->return_first = return_first;
  595. } else {
  596. af_Activity *activity = makeFuncActivity(bt, NULL, return_first, env->activity->msg_up,
  597. env->activity->var_list, env->activity->belong,
  598. env->activity->func);
  599. activity->prev = env->activity;
  600. env->activity = activity;
  601. }
  602. }
  603. /*
  604. * 函数名: isInfixFunc
  605. * 目标: 检查是否中缀调用函数
  606. */
  607. static bool isInfixFunc(af_Code *code, af_Environment *env) {
  608. if (code == NULL || code->type != code_element || code->prefix == getPrefix(E_QUOTE, env)) // 检查是否element, 且无引用前缀
  609. return false;
  610. if (checkLiteralCode(code->element.data, NULL, NULL, env)) // 检查是否字面量
  611. return false;
  612. af_Var *var = findVarFromVarList(code->element.data, env->activity->belong, env->activity->var_list);
  613. if (var == NULL)
  614. return false;
  615. obj_isInfixFunc *func = findAPI("obj_isInfixFunc", var->vn->obj->data->api);
  616. if (func == NULL)
  617. return false;
  618. return func(var->vn->obj->data->id, var->vn->obj);
  619. }
  620. bool pushExecutionActivity(af_Code *bt, bool return_first, af_Environment *env) {
  621. af_Code *next;
  622. next = getCodeNext(bt);
  623. if (bt->type != code_block || bt->block.elements == 0) {
  624. pushMessageDown(makeERRORMessage(SYNTAX_ERROR, NOT_CODE_INFO, env), env);
  625. return false;
  626. }
  627. setActivityBtNext(next, env->activity);
  628. newActivity(bt, next, return_first, env);
  629. setActivityBtStart(bt->next, env->activity);
  630. env->activity->status = act_func_normal;
  631. env->activity->is_execution = true;
  632. return true;
  633. }
  634. bool pushFuncActivity(af_Code *bt, af_Environment *env) {
  635. af_Code *next;
  636. af_Code *func;
  637. af_Object *parentheses_call = env->activity->parentheses_call;
  638. env->activity->parentheses_call = NULL;
  639. next = getCodeNext(bt);
  640. switch (bt->block.type) {
  641. case curly:
  642. if (bt->block.elements == 0) {
  643. pushMessageDown(makeERRORMessage(CALL_ERROR, CURLY_FUNC_BODY_INFO, env), env);
  644. return false;
  645. }
  646. func = bt->next;
  647. break;
  648. case brackets: {
  649. af_Code *code = bt->next;
  650. func = NULL;
  651. for (int i = 0; i < bt->block.elements; i++) {
  652. if (isInfixFunc(code, env)) {
  653. func = code;
  654. break;
  655. }
  656. if ((code = getCodeNext(bt)) == NULL)
  657. break;
  658. }
  659. if (func == NULL) {
  660. pushMessageDown(makeERRORMessage(CALL_ERROR, BRACKETS_FUNC_BODY_INFO, env), env);
  661. return false;
  662. }
  663. break;
  664. }
  665. case parentheses:
  666. func = NULL; // 小括号则不在需要匹配
  667. break;
  668. default:
  669. break;
  670. }
  671. setActivityBtNext(next, env->activity);
  672. newActivity(bt, next, false, env);
  673. setActivityBtStart(func, env->activity);
  674. env->activity->call_type = env->activity->bt_top->block.type;
  675. env->activity->status = act_func_get;
  676. if (env->activity->call_type == parentheses) { // 对于类前缀调用, 已经获得func的实际值了
  677. if (parentheses_call == NULL) {
  678. pushMessageDown(makeERRORMessage(CALL_ERROR, PARENTHESES_FUNC_BODY_INFO, env), env);
  679. return false;
  680. }
  681. return setFuncActivityToArg(parentheses_call, env);
  682. }
  683. return true;
  684. }
  685. bool pushLiteralActivity(af_Code *bt, char *data, af_Object *func, af_Environment *env) {
  686. setActivityBtNext(bt->next, env->activity);
  687. newActivity(bt, bt->next, false, env);
  688. env->activity->is_literal = true;
  689. pushLiteralData(strCopy(data), env); // FuncBody的释放导致code和literal_data释放, 所以要复制
  690. return setFuncActivityToArg(func, env);
  691. }
  692. bool pushVariableActivity(af_Code *bt, af_Object *func, af_Environment *env) {
  693. setActivityBtNext(bt->next, env->activity);
  694. newActivity(bt, bt->next, false, env);
  695. env->activity->is_obj_func = true;
  696. return setFuncActivityToArg(func, env);
  697. }
  698. bool pushMacroFuncActivity(af_Object *func, af_Environment *env) {
  699. /* Macro是隐式调用, bt不移动 */
  700. /* 沿用activity */
  701. printf("Run macro\n");
  702. if (!freeVarSpaceListCount(env->activity->new_vs_count, env->activity->var_list)) { // 释放外部变量空间
  703. env->activity->new_vs_count = 0;
  704. pushMessageDown(makeERRORMessage(RUN_ERROR, FREE_VARSPACE_INFO, env), env);
  705. return false;
  706. }
  707. env->activity->var_list = env->activity->macro_vsl;
  708. env->activity->new_vs_count = env->activity->macro_vs_count;
  709. env->activity->macro_vs_count = 0;
  710. env->activity->is_macro_call = true;
  711. clearActivity(env->activity); /* 隐式调用不设置 bt_top */
  712. return setFuncActivityToArg(func, env);
  713. }
  714. void pushGCActivity(gc_DestructList *dl, gc_DestructList **pdl, af_Environment *env) {
  715. for (af_Activity *tmp = env->activity; tmp != NULL; tmp = tmp->prev) {
  716. if (tmp->type == act_gc) {
  717. *(tmp->pdl) = dl;
  718. tmp->pdl = pdl;
  719. if (tmp->dl_next == NULL) // 原dl_next已经运行到末端
  720. tmp->dl_next = dl;
  721. return;
  722. }
  723. }
  724. /* gc Activity 可能创建为顶层 activity, 故信息不能继承上一级(可能没有上一级) */
  725. af_Activity *activity = makeGcActivity(dl, pdl, env);
  726. activity->prev = env->activity;
  727. env->activity = activity;
  728. }
  729. bool pushImportActivity(af_Code *bt, af_Object **obj, char *mark, af_Environment *env) {
  730. af_Object *tmp = NULL;
  731. if (obj == NULL)
  732. obj = &tmp;
  733. if (*obj == NULL)
  734. *obj = makeGlobalObject(env);
  735. if (*obj == NULL)
  736. return false;
  737. af_Activity *activity = makeTopImportActivity(bt, bt, env->core->protect, *obj, mark);
  738. activity->prev = env->activity;
  739. env->activity = activity;
  740. return true;
  741. }
  742. bool pushDestructActivity(gc_DestructList *dl, af_Environment *env) {
  743. env->activity->dl_next = dl->next;
  744. /* 隐式调用不设置 bt_top */
  745. af_Activity *activity = makeFuncActivity(NULL, NULL, false, env->activity->msg_up,
  746. env->activity->var_list, env->activity->belong, NULL);
  747. activity->prev = env->activity;
  748. env->activity = activity;
  749. env->activity->is_gc_call = true;
  750. return setFuncActivityToArg(dl->func, env);
  751. }
  752. void setArgCodeListToActivity(af_ArgCodeList *acl, af_Environment *env) {
  753. if (acl != NULL) {
  754. setActivityBtStart(acl->code, env->activity);
  755. env->activity->run_in_func = acl->run_in_func;
  756. } else {
  757. setActivityBtStart(NULL, env->activity);
  758. env->activity->run_in_func = false;
  759. }
  760. }
  761. bool setFuncActivityToArg(af_Object *func, af_Environment *env) {
  762. obj_funcGetArgCodeList *get_acl = findAPI("obj_funcGetArgCodeList", func->data->api);
  763. obj_funcGetVarList *get_var_list = findAPI("obj_funcGetVarList", func->data->api);
  764. if (get_var_list == NULL) {
  765. pushMessageDown(makeERRORMessage(TYPE_ERROR, API_NOT_FOUND_INFO(obj_funcGetVarList), env), env);
  766. return false;
  767. }
  768. env->activity->func = func;
  769. env->activity->belong = getBelongObject(func);
  770. env->activity->status = act_func_arg;
  771. /* 遇到错误时 get_acl 和 get_var_list 要自行设定msg */
  772. if (get_acl != NULL) {
  773. if (!get_acl(func->data->id, func, &env->activity->acl_start, env->activity->bt_top, &env->activity->mark, env)) // 设置acl
  774. return false;
  775. } else
  776. env->activity->acl_start = NULL;
  777. if (!get_var_list(func->data->id, func, &env->activity->func_var_list, env->activity->mark, env)) // 设置 func_var_list
  778. return false;
  779. env->activity->acl_done = env->activity->acl_start;
  780. setArgCodeListToActivity(env->activity->acl_start, env);
  781. return true;
  782. }
  783. bool setFuncActivityAddVar(af_Environment *env){
  784. obj_funcGetInfo *get_info = findAPI("obj_funcGetInfo", env->activity->func->data->api);
  785. obj_funcGetArgList *get_arg_list = findAPI("obj_funcGetArgList", env->activity->func->data->api);
  786. if (get_info == NULL) {
  787. pushMessageDown(makeERRORMessage(TYPE_ERROR, API_NOT_FOUND_INFO(obj_funcGetInfo), env), env);
  788. return false;
  789. }
  790. if (env->activity->fi != NULL)
  791. freeFuncInfo(env->activity->fi); // 延迟到这里再释放, 主要是FuncBody中的bt可能会被使用
  792. env->activity->fi = NULL;
  793. env->activity->body_next = NULL;
  794. if (!get_info(env->activity->func->data->id, env->activity->func, &env->activity->fi, env->activity->bt_top, env->activity->mark, env))
  795. return false;
  796. if (env->activity->fi == NULL) {
  797. pushMessageDown(makeERRORMessage(API_RUN_ERROR, API_DONOT_GIVE(FuncInfo), env), env);
  798. return false;
  799. }
  800. if (env->activity->fi->scope == super_pure_scope && env->activity->fi->scope == super_embedded) {
  801. /* 超纯函数和超内嵌函数不得搭配使用 */
  802. pushMessageDown(makeERRORMessage(RUN_ERROR, PURE_EMBEDDED_INFO, env), env);
  803. return false;
  804. }
  805. env->activity->body_next = env->activity->fi->body;
  806. if (env->activity->fi->is_macro) { // 是宏函数则保存变量空间
  807. env->activity->macro_vsl = env->activity->var_list;
  808. env->activity->macro_vs_count = env->activity->new_vs_count;
  809. } else if (env->activity->fi->scope != inline_scope) { // 非内联函数, 释放外部变量空间
  810. if (!freeVarSpaceListCount(env->activity->new_vs_count, env->activity->var_list)) {
  811. pushMessageDown(makeERRORMessage(RUN_ERROR, FREE_VARSPACE_INFO, env), env); // 释放失败
  812. return false;
  813. }
  814. }
  815. if (env->activity->fi->scope == normal_scope) { // 使用函数变量空间
  816. env->activity->var_list = env->activity->func_var_list;
  817. env->activity->new_vs_count = 0;
  818. } else if (env->activity->fi->scope == pure_scope) { // 纯函数只有 protect 变量空间
  819. env->activity->var_list = makeVarSpaceList(env->core->protect);
  820. env->activity->new_vs_count = 0;
  821. } else if (env->activity->fi->scope == super_pure_scope) { // 超纯函数没有变量空间, 因此不得为超内嵌函数(否则var_list就为NULL了)
  822. env->activity->var_list = NULL;
  823. env->activity->new_vs_count = 0;
  824. }
  825. if (env->activity->fi->embedded != super_embedded) { // 不是超内嵌函数则引入一层新的变量空间
  826. /* 新层的变量空间应该属于belong而不是func */
  827. env->activity->var_list = pushNewVarList(env->activity->belong, env->activity->var_list, env);
  828. env->activity->new_vs_count++;
  829. }
  830. env->activity->func_var_list = NULL;
  831. if (env->activity->fi->var_this && env->activity->belong != NULL) {
  832. if (!makeVarToVarSpaceList("this", 3, 3, 3, env->activity->belong,
  833. env->activity->var_list, env->activity->belong, env)) {
  834. pushMessageDown(makeERRORMessage(RUN_ERROR, IMPLICIT_SET_INFO(this), env), env);
  835. return false;
  836. }
  837. }
  838. if (env->activity->fi->var_func && env->activity->func != NULL) {
  839. if (!makeVarToVarSpaceList("func", 3, 3, 3, env->activity->func,
  840. env->activity->var_list, env->activity->belong, env)) {
  841. pushMessageDown(makeERRORMessage(RUN_ERROR, IMPLICIT_SET_INFO(func), env), env);
  842. return false;
  843. }
  844. }
  845. if (get_arg_list != NULL) {
  846. af_ArgList *al;
  847. if (!get_arg_list(env->activity->func->data->id, env->activity->func, &al, env->activity->acl_start, env->activity->mark, env))
  848. return false;
  849. runArgList(al, env->activity->var_list, env);
  850. freeAllArgList(al);
  851. }
  852. if (env->activity->fi->embedded == protect_embedded)
  853. env->activity->var_list->vs->is_protect = true;
  854. freeAllArgCodeList(env->activity->acl_start);
  855. env->activity->acl_start = NULL;
  856. env->activity->acl_done = NULL;
  857. if (setFuncActivityToNormal(env) == 0)
  858. return false; // 运行结束, 且已写入msg
  859. return true;
  860. }
  861. /*
  862. * 函数名: setFuncActivityToNormal
  863. * 目标: 获取下一步需要运行的结果
  864. * 返回 (0) 表示无下一步
  865. * 返回 (-1) 表示运行C函数, 并且设置了 process_msg_first
  866. * 返回 (1) 表示下一步运行Code
  867. * 返回 (2) 表示遇到未被替换的动态代码块
  868. */
  869. int setFuncActivityToNormal(af_Environment *env){ // 获取函数的函数体
  870. int re;
  871. af_FuncBody *body = env->activity->body_next;
  872. env->activity->status = act_func_normal;
  873. setActivityBtNext(NULL, env->activity);
  874. if (body == NULL) // 已经没有下一步了 (原msg不释放)
  875. return 0;
  876. af_Activity *activity = env->activity; // 防止在函数调用期间env->activity被修改
  877. env->activity->body_next = body->next;
  878. switch (body->type) {
  879. case func_body_c: {
  880. af_FuncBody *new = GET_SYMBOL(body->c_func)(env->activity->mark, env);
  881. activity->process_msg_first++; // 处理C函数通过msg_down返回的结果
  882. pushDynamicFuncBody(new, body);
  883. activity->body_next = body->next; // 添加新元素后要重新设定body_next的位置
  884. re = -1;
  885. break;
  886. }
  887. case func_body_import:
  888. if (!pushImportActivity(body->code, NULL, NULL, env)) {
  889. pushMessageDown(makeERRORMessage(IMPORT_ERROR, IMPORT_OBJ_ERROR, env), env);
  890. activity->process_msg_first++;
  891. re = 2;
  892. break;
  893. }
  894. re = 1;
  895. break;
  896. case func_body_code:
  897. setActivityBtStart(body->code, env->activity);
  898. re = 1;
  899. break;
  900. default:
  901. case func_body_dynamic:
  902. pushMessageDown(makeERRORMessage(RUN_ERROR, FUNCBODY_ERROR_INFO, env), env);
  903. activity->process_msg_first++;
  904. re = 2;
  905. break;
  906. }
  907. if (activity->body_next == NULL) // 最后一个aFunBody
  908. freeMark(activity);
  909. return re;
  910. }
  911. /*
  912. * 函数名: runTopMessageProcess
  913. * 目标: 运行顶层信息处理器
  914. */
  915. void runTopMessageProcess(bool is_gc, af_Environment *env) {
  916. af_Message **pmsg = &env->activity->msg_down;
  917. while (*pmsg != NULL) {
  918. af_TopMsgProcess *mp = findTopMsgProcessFunc((*pmsg)->type, env);
  919. if (mp != NULL) {
  920. GET_SYMBOL(mp->func)(*pmsg, is_gc, env);
  921. *pmsg = freeMessage(*pmsg);
  922. } else
  923. pmsg = &((*pmsg)->next);
  924. }
  925. }
  926. static void freeMark(af_Activity *activity) {
  927. if (activity->type == act_func && activity->func != NULL && activity->mark != NULL) {
  928. obj_funcFreeMask *func = findAPI("obj_funcFreeMask", activity->func->data->api);
  929. if (func != NULL)
  930. func(activity->func->data->id, activity->func, activity->mark);
  931. activity->mark = NULL;
  932. }
  933. }
  934. void popActivity(bool is_normal, af_Message *msg, af_Environment *env) {
  935. if (env->activity->type == act_func || env->activity->type == act_top || env->activity->type == act_top_import) {
  936. if (msg != NULL && env->activity->return_first) { // msg有内容, 并且设定了返回首位, 则清除msg内容, 并压入首位(压入的代码在下面)
  937. if (EQ_STR(msg->type, "NORMAL")) {
  938. gc_delReference(*(af_Object **) msg->msg);
  939. freeMessage(msg);
  940. msg = NULL;
  941. }
  942. } else if (env->activity->return_first) { // msg无内容, 并且设定了返回首位, 则检查msg_down是否有normal, 有则清除
  943. if (env->activity->msg_down != NULL && EQ_STR(env->activity->msg_down->type, "NORMAL")) {
  944. af_Message *tmp = getFirstMessage(env);
  945. gc_delReference(*(af_Object **) (tmp->msg));
  946. freeMessage(tmp);
  947. }
  948. }
  949. if (msg == NULL && env->activity->return_first) { // 如果首位
  950. if (env->activity->return_obj == NULL)
  951. msg = makeERRORMessage(RUN_ERROR, RETURN_OBJ_NOT_FOUND_INFO, env);
  952. else
  953. msg = makeNORMALMessage(env->activity->return_obj);
  954. }
  955. }
  956. if (msg != NULL)
  957. pushMessageDown(msg, env);
  958. if (env->activity->type == act_top_import && /* import模式, 并且msg_down中有normal, 则把normal替换为belong */
  959. env->activity->msg_down != NULL && EQ_STR(env->activity->msg_down->type, "NORMAL")) {
  960. af_Message *tmp = getFirstMessage(env);
  961. pushMessageDown(makeIMPORTMessage(env->activity->import_mark, env->activity->belong), env); // 压入belong作为msg
  962. pushMessageDown(tmp, env);
  963. }
  964. if (!is_normal)
  965. freeMark(env->activity); // 遇到非正常退出时, 释放`mark`
  966. if (env->activity->type == act_top || env->activity->type == act_gc) // 顶层或gc层
  967. runTopMessageProcess((env->activity->type == act_gc), env);
  968. else {
  969. connectMessage(&(env->activity->msg_down), env->activity->prev->msg_down);
  970. env->activity->prev->msg_down = env->activity->msg_down;
  971. env->activity->msg_down = NULL;
  972. /* popActivity必然设定process_msg_first */
  973. env->activity->prev->process_msg_first++;
  974. }
  975. if (env->activity->type != act_top)
  976. env->activity = freeActivity(env->activity);
  977. else
  978. freeActivityTop(env->activity); // activity不被释放
  979. }
  980. static af_LiteralRegex *makeLiteralRegex(char *pattern, char *func, bool in_protect) {
  981. af_Regex *rg = makeRegex(pattern);
  982. if (rg == NULL)
  983. return NULL;
  984. af_LiteralRegex *lr = calloc(1, sizeof(af_LiteralRegex));
  985. lr->rg = rg;
  986. lr->func = strCopy(func);
  987. lr->in_protect = in_protect;
  988. return lr;
  989. }
  990. static af_LiteralRegex *freeLiteralRegex(af_LiteralRegex *lr) {
  991. af_LiteralRegex *next = lr->next;
  992. freeRegex(lr->rg);
  993. free(lr->func);
  994. free(lr);
  995. return next;
  996. }
  997. static void freeAllLiteralRegex(af_LiteralRegex *lr) {
  998. while (lr != NULL)
  999. lr = freeLiteralRegex(lr);
  1000. }
  1001. bool pushLiteralRegex(char *pattern, char *func, bool in_protect, af_Environment *env) {
  1002. af_LiteralRegex *lr = makeLiteralRegex(pattern, func, in_protect);
  1003. if (lr == NULL)
  1004. return false;
  1005. lr->next = env->core->lr;
  1006. env->core->lr = lr;
  1007. return true;
  1008. }
  1009. /*
  1010. * 函数名: checkLiteralCode
  1011. * 目标: 检查对象是否为字面量
  1012. * 注意: func被写入函数名, 但不是复制式写入
  1013. */
  1014. bool checkLiteralCode(char *literal, char **func, bool *in_protect, af_Environment *env) {
  1015. for (af_LiteralRegex *lr = env->core->lr; lr != NULL; lr = lr->next) {
  1016. if (matchRegex(literal, lr->rg) == 1) {
  1017. if (func != NULL)
  1018. *func = lr->func; // 不使用复制
  1019. if (in_protect != NULL)
  1020. *in_protect = lr->in_protect;
  1021. return true;
  1022. }
  1023. }
  1024. return false;
  1025. }
  1026. af_ErrorInfo *makeErrorInfo(char *type, char *error, char *note, FileLine line, FilePath path) {
  1027. af_ErrorInfo *ei = calloc(1, sizeof(af_ErrorInfo));
  1028. ei->error_type = strCopy(type);
  1029. ei->error = strCopy(error);
  1030. pushErrorBacktracking(line, path, note, ei);
  1031. return ei;
  1032. }
  1033. void freeErrorInfo(af_ErrorInfo *ei) {
  1034. free(ei->error_type);
  1035. free(ei->error);
  1036. if (ei->obj != NULL)
  1037. gc_delReference(ei->obj);
  1038. freeAllErrorBacktracking(ei->track);
  1039. free(ei);
  1040. }
  1041. static void fprintfNote(FILE *file, char *note) {
  1042. char *ent = NULL;
  1043. while(true) {
  1044. ent = strchr(note, '\n');
  1045. if (ent != NULL)
  1046. *ent = NUL;
  1047. fprintf(file, " #note %s\n", note);
  1048. if (ent == NULL) // 意味着是最后一部分`note`
  1049. break;
  1050. *ent = '\n';
  1051. note = ent + 1;
  1052. }
  1053. }
  1054. void fprintfErrorInfo(FILE *file, af_ErrorInfo *ei) {
  1055. fprintf(file, "Error Traceback (most recent call last):\n");
  1056. for (af_ErrorBacktracking *ebt = ei->track; ebt != NULL; ebt = ebt->next) {
  1057. fprintf(file, " File \"%s\", line %d\n", ebt->file, ebt->line);
  1058. if (ebt->note != NULL)
  1059. fprintfNote(file, ebt->note);
  1060. }
  1061. fprintf(file, "%s: \"%s\"\n", ei->error_type, ei->error);
  1062. fflush(file);
  1063. }
  1064. static af_ErrorBacktracking *makeErrorBacktracking(FileLine line, FilePath file, char *note) {
  1065. af_ErrorBacktracking *ebt = calloc(1, sizeof(af_ErrorBacktracking));
  1066. ebt->line = line;
  1067. if (file == NULL)
  1068. ebt->file = strCopy("unknown.af.sys");
  1069. else
  1070. ebt->file = strCopy(file);
  1071. if (note != NULL)
  1072. ebt->note = strCopy(note);
  1073. return ebt;
  1074. }
  1075. static af_ErrorBacktracking *freeErrorBacktracking(af_ErrorBacktracking *ebt) {
  1076. af_ErrorBacktracking *next = ebt->next;
  1077. free(ebt->note);
  1078. free(ebt->file);
  1079. free(ebt);
  1080. return next;
  1081. }
  1082. static void freeAllErrorBacktracking(af_ErrorBacktracking *ebt) {
  1083. while(ebt != NULL) {
  1084. ebt = freeErrorBacktracking(ebt);
  1085. }
  1086. }
  1087. void pushErrorBacktracking(FileLine line, FilePath file, char *note, af_ErrorInfo *ei) {
  1088. af_ErrorBacktracking *ebt = makeErrorBacktracking(line, file, note);
  1089. ebt->next = ei->track;
  1090. ei->track = ebt;
  1091. }
  1092. static char *getActivityInfoToBacktracking(af_Activity *activity, bool print_bt_top){
  1093. char *info = NULL;
  1094. if (activity->type == act_gc) {
  1095. info = strJoin(info, "gc-activity;", true, false);
  1096. return info;
  1097. } else if (activity->type == act_top)
  1098. info = strJoin(info, "top-activity;", true, false);
  1099. else if (activity->type == act_top_import)
  1100. info = strJoin(info, "top-import-activity;", true, false);
  1101. else if (activity->is_execution)
  1102. info = strJoin(info, "execution-activity;", true, false);
  1103. else if (activity->is_gc_call)
  1104. info = strJoin(info, "gc-destruct-function-call-activity;", true, false);
  1105. else
  1106. info = strJoin(info, "function-call-activity;", true, false);
  1107. switch (activity->status) {
  1108. case act_func_get:
  1109. info = strJoin(info, "\nfunc-get;", true, false);
  1110. break;
  1111. case act_func_arg:
  1112. info = strJoin(info, "\nfunc-arg;", true, false);
  1113. if (activity->run_in_func)
  1114. info = strJoin(info, " run-in-function-var-space;", true, false);
  1115. break;
  1116. case act_func_normal:
  1117. info = strJoin(info, "\nrun-code;", true, false);
  1118. if (activity->return_first)
  1119. info = strJoin(info, " return-first-result;", true, false);
  1120. break;
  1121. default:
  1122. break;
  1123. }
  1124. if (activity->is_macro_call)
  1125. info = strJoin(info, "\nmacro-call;", true, false);
  1126. if (activity->is_literal)
  1127. info = strJoin(info, "\nliteral-call;", true, false);
  1128. if (activity->is_obj_func)
  1129. info = strJoin(info, "\nobject-function-call;", true, false);
  1130. if (activity->optimization)
  1131. info = strJoin(info, "\ntail-call-Optimization;", true, false);
  1132. info = strJoin(info, "\n", true, false);
  1133. char *print_code = NULL;
  1134. if (!print_bt_top && activity->bt_next != NULL)
  1135. print_code = codeToStr(activity->bt_next, 1);
  1136. else if (activity->bt_top != NULL)
  1137. print_code = codeToStr(activity->bt_top, 1);
  1138. else if (activity->prev == NULL && activity->bt_start != NULL)
  1139. print_code = codeToStr(activity->bt_start, -1);
  1140. if (print_code != NULL) {
  1141. info = strJoin(info, "code: ", true, false);
  1142. info = strJoin(info, print_code, true, true);
  1143. } else
  1144. info = strJoin(info, "sys-err non-code", true, false);
  1145. return info;
  1146. }
  1147. af_ImportInfo *makeImportInfo(char *mark, af_Object *obj) {
  1148. af_ImportInfo *ii = calloc(1, sizeof(af_ImportInfo));
  1149. if (mark != NULL)
  1150. ii->mark = strCopy(mark);
  1151. ii->obj = obj;
  1152. gc_addReference(obj);
  1153. return ii;
  1154. }
  1155. void freeImportInfo(af_ImportInfo *ii) {
  1156. free(ii->mark);
  1157. if (ii->obj != NULL)
  1158. gc_delReference(ii->obj);
  1159. free(ii);
  1160. }
  1161. void setGcMax(size_t max, af_Environment *env) {
  1162. env->core->gc_count_max = max;
  1163. }
  1164. void setGcRun(enum GcRunTime grt, af_Environment *env) {
  1165. env->core->gc_run = grt;
  1166. }
  1167. size_t getGcCount(af_Environment *env) {
  1168. return env->core->gc_count;
  1169. }
  1170. size_t getGcMax(af_Environment *env) {
  1171. return env->core->gc_count_max;
  1172. }
  1173. enum GcRunTime getGcRun(af_Environment *env) {
  1174. return env->core->gc_run;
  1175. }
  1176. af_Object *getCoreGlobal(af_Environment *env) {
  1177. return env->core->global;
  1178. }
  1179. af_Object *getGlobal(af_Environment *env) {
  1180. af_Activity *activity = env->activity;
  1181. for (NULL; activity != NULL; activity = activity->prev) {
  1182. if (activity->type == act_top || activity->type == act_top_import)
  1183. return activity->belong;
  1184. }
  1185. return env->core->global;
  1186. }
  1187. af_Object *getBelong(af_Environment *env) {
  1188. if (env == NULL || env->activity == NULL)
  1189. return NULL;
  1190. return env->activity->belong;
  1191. }
  1192. FilePath getActivityFile(af_Environment *env){
  1193. if (env == NULL || env->activity == NULL)
  1194. return NULL;
  1195. return env->activity->file;
  1196. }
  1197. FileLine getActivityLine(af_Environment *env){
  1198. if (env == NULL || env->activity == NULL)
  1199. return 0;
  1200. return env->activity->line;
  1201. }
  1202. af_Object *getMsgNormalData(af_Message *msg) {
  1203. if (!EQ_STR("NORMAL", msg->type))
  1204. return NULL;
  1205. af_Object *obj = *(af_Object **)msg->msg;
  1206. gc_delReference(obj);
  1207. *(af_Object **)msg->msg = NULL;
  1208. return obj;
  1209. }
  1210. af_ErrorInfo *getMsgErrorInfo(af_Message *msg) {
  1211. if (!EQ_STR("ERROR", msg->type))
  1212. return NULL;
  1213. af_ErrorInfo *ei = *(af_ErrorInfo **)msg->msg;
  1214. return ei;
  1215. }
  1216. af_ImportInfo *getMsgImportInfo(af_Message *msg) {
  1217. if (!EQ_STR("IMPORT", msg->type))
  1218. return NULL;
  1219. af_ImportInfo *ii = *(af_ImportInfo **)msg->msg;
  1220. return ii;
  1221. }
  1222. char *getErrorType(af_ErrorInfo *ei) {
  1223. return ei->error_type;
  1224. }
  1225. char *getError(af_ErrorInfo *ei) {
  1226. return ei->error;
  1227. }
  1228. char *getImportMark(af_ImportInfo *ii) {
  1229. return ii->mark;
  1230. }
  1231. af_Object *getImportObject(af_ImportInfo *ii) {
  1232. af_Object *obj = ii->obj;
  1233. if (obj == NULL)
  1234. return NULL;
  1235. ii->obj = NULL;
  1236. gc_delReference(obj);
  1237. return obj;
  1238. }