env.c 43 KB

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