env.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  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.aun.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. static void initCallFuncInfo(CallFuncInfo *cfi, af_Environment *env) {
  862. cfi->mark = env->activity->mark;
  863. cfi->belong = env->activity->belong;
  864. cfi->func = env->activity->func;
  865. cfi->var_list = env->activity->var_list; // 传var_list而非vsl
  866. cfi->call_type = env->activity->call_type;
  867. cfi->is_gc_call = env->activity->is_gc_call;
  868. cfi->is_literal = env->activity->is_literal;
  869. cfi->is_obj_func = env->activity->is_obj_func;
  870. cfi->is_macro_call = env->activity->is_macro_call;
  871. }
  872. /*
  873. * 函数名: setFuncActivityToNormal
  874. * 目标: 获取下一步需要运行的结果
  875. * 返回 (0) 表示无下一步
  876. * 返回 (-1) 表示运行C函数, 并且设置了 process_msg_first
  877. * 返回 (1) 表示下一步运行Code
  878. * 返回 (2) 表示遇到未被替换的动态代码块
  879. */
  880. int setFuncActivityToNormal(af_Environment *env){ // 获取函数的函数体
  881. int re;
  882. af_FuncBody *body = env->activity->body_next;
  883. env->activity->status = act_func_normal;
  884. setActivityBtNext(NULL, env->activity);
  885. if (body == NULL) // 已经没有下一步了 (原msg不释放)
  886. return 0;
  887. af_Activity *activity = env->activity; // 防止在函数调用期间env->activity被修改
  888. env->activity->body_next = body->next;
  889. switch (body->type) {
  890. case func_body_c: {
  891. CallFuncInfo cfi;
  892. initCallFuncInfo(&cfi, env);
  893. af_FuncBody *new = GET_SYMBOL(body->c_func)(&cfi, env);
  894. activity->process_msg_first++; // 处理C函数通过msg_down返回的结果
  895. pushDynamicFuncBody(new, body);
  896. activity->body_next = body->next; // 添加新元素后要重新设定body_next的位置
  897. re = -1;
  898. break;
  899. }
  900. case func_body_import:
  901. if (!pushImportActivity(body->code, NULL, NULL, env)) {
  902. pushMessageDown(makeERRORMessage(IMPORT_ERROR, IMPORT_OBJ_ERROR, env), env);
  903. activity->process_msg_first++;
  904. re = 2;
  905. break;
  906. }
  907. re = 1;
  908. break;
  909. case func_body_code:
  910. setActivityBtStart(body->code, env->activity);
  911. re = 1;
  912. break;
  913. default:
  914. case func_body_dynamic:
  915. pushMessageDown(makeERRORMessage(RUN_ERROR, FUNCBODY_ERROR_INFO, env), env);
  916. activity->process_msg_first++;
  917. re = 2;
  918. break;
  919. }
  920. if (activity->body_next == NULL) // 最后一个aFunBody
  921. freeMark(activity);
  922. return re;
  923. }
  924. /*
  925. * 函数名: runTopMessageProcess
  926. * 目标: 运行顶层信息处理器
  927. */
  928. void runTopMessageProcess(bool is_gc, af_Environment *env) {
  929. af_Message **pmsg = &env->activity->msg_down;
  930. while (*pmsg != NULL) {
  931. af_TopMsgProcess *mp = findTopMsgProcessFunc((*pmsg)->type, env);
  932. if (mp != NULL) {
  933. GET_SYMBOL(mp->func)(*pmsg, is_gc, env);
  934. *pmsg = freeMessage(*pmsg);
  935. } else
  936. pmsg = &((*pmsg)->next);
  937. }
  938. }
  939. static void freeMark(af_Activity *activity) {
  940. if (activity->type == act_func && activity->func != NULL && activity->mark != NULL) {
  941. obj_funcFreeMask *func = findAPI("obj_funcFreeMask", activity->func->data->api);
  942. if (func != NULL)
  943. func(activity->func->data->id, activity->func, activity->mark);
  944. activity->mark = NULL;
  945. }
  946. }
  947. void popActivity(bool is_normal, af_Message *msg, af_Environment *env) {
  948. if (env->activity->type == act_func || env->activity->type == act_top || env->activity->type == act_top_import) {
  949. if (msg != NULL && env->activity->return_first) { // msg有内容, 并且设定了返回首位, 则清除msg内容, 并压入首位(压入的代码在下面)
  950. if (EQ_STR(msg->type, "NORMAL")) {
  951. gc_delReference(*(af_Object **) msg->msg);
  952. freeMessage(msg);
  953. msg = NULL;
  954. }
  955. } else if (env->activity->return_first) { // msg无内容, 并且设定了返回首位, 则检查msg_down是否有normal, 有则清除
  956. if (env->activity->msg_down != NULL && EQ_STR(env->activity->msg_down->type, "NORMAL")) {
  957. af_Message *tmp = getFirstMessage(env);
  958. gc_delReference(*(af_Object **) (tmp->msg));
  959. freeMessage(tmp);
  960. }
  961. }
  962. if (msg == NULL && env->activity->return_first) { // 如果首位
  963. if (env->activity->return_obj == NULL)
  964. msg = makeERRORMessage(RUN_ERROR, RETURN_OBJ_NOT_FOUND_INFO, env);
  965. else
  966. msg = makeNORMALMessage(env->activity->return_obj);
  967. }
  968. }
  969. if (msg != NULL)
  970. pushMessageDown(msg, env);
  971. if (env->activity->type == act_top_import && /* import模式, 并且msg_down中有normal, 则把normal替换为belong */
  972. env->activity->msg_down != NULL && EQ_STR(env->activity->msg_down->type, "NORMAL")) {
  973. af_Message *tmp = getFirstMessage(env);
  974. pushMessageDown(makeIMPORTMessage(env->activity->import_mark, env->activity->belong), env); // 压入belong作为msg
  975. pushMessageDown(tmp, env);
  976. }
  977. if (!is_normal)
  978. freeMark(env->activity); // 遇到非正常退出时, 释放`mark`
  979. if (env->activity->type == act_top || env->activity->type == act_gc) // 顶层或gc层
  980. runTopMessageProcess((env->activity->type == act_gc), env);
  981. else {
  982. connectMessage(&(env->activity->msg_down), env->activity->prev->msg_down);
  983. env->activity->prev->msg_down = env->activity->msg_down;
  984. env->activity->msg_down = NULL;
  985. /* popActivity必然设定process_msg_first */
  986. env->activity->prev->process_msg_first++;
  987. }
  988. if (env->activity->type != act_top)
  989. env->activity = freeActivity(env->activity);
  990. else
  991. freeActivityTop(env->activity); // activity不被释放
  992. }
  993. static af_LiteralRegex *makeLiteralRegex(char *pattern, char *func, bool in_protect) {
  994. af_Regex *rg = makeRegex(pattern);
  995. if (rg == NULL)
  996. return NULL;
  997. af_LiteralRegex *lr = calloc(1, sizeof(af_LiteralRegex));
  998. lr->rg = rg;
  999. lr->func = strCopy(func);
  1000. lr->in_protect = in_protect;
  1001. return lr;
  1002. }
  1003. static af_LiteralRegex *freeLiteralRegex(af_LiteralRegex *lr) {
  1004. af_LiteralRegex *next = lr->next;
  1005. freeRegex(lr->rg);
  1006. free(lr->func);
  1007. free(lr);
  1008. return next;
  1009. }
  1010. static void freeAllLiteralRegex(af_LiteralRegex *lr) {
  1011. while (lr != NULL)
  1012. lr = freeLiteralRegex(lr);
  1013. }
  1014. bool pushLiteralRegex(char *pattern, char *func, bool in_protect, af_Environment *env) {
  1015. af_LiteralRegex *lr = makeLiteralRegex(pattern, func, in_protect);
  1016. if (lr == NULL)
  1017. return false;
  1018. lr->next = env->core->lr;
  1019. env->core->lr = lr;
  1020. return true;
  1021. }
  1022. /*
  1023. * 函数名: checkLiteralCode
  1024. * 目标: 检查对象是否为字面量
  1025. * 注意: func被写入函数名, 但不是复制式写入
  1026. */
  1027. bool checkLiteralCode(char *literal, char **func, bool *in_protect, af_Environment *env) {
  1028. for (af_LiteralRegex *lr = env->core->lr; lr != NULL; lr = lr->next) {
  1029. if (matchRegex(literal, lr->rg) == 1) {
  1030. if (func != NULL)
  1031. *func = lr->func; // 不使用复制
  1032. if (in_protect != NULL)
  1033. *in_protect = lr->in_protect;
  1034. return true;
  1035. }
  1036. }
  1037. return false;
  1038. }
  1039. af_ErrorInfo *makeErrorInfo(char *type, char *error, char *note, FileLine line, FilePath path) {
  1040. af_ErrorInfo *ei = calloc(1, sizeof(af_ErrorInfo));
  1041. ei->error_type = strCopy(type);
  1042. ei->error = strCopy(error);
  1043. pushErrorBacktracking(line, path, note, ei);
  1044. return ei;
  1045. }
  1046. void freeErrorInfo(af_ErrorInfo *ei) {
  1047. free(ei->error_type);
  1048. free(ei->error);
  1049. if (ei->obj != NULL)
  1050. gc_delReference(ei->obj);
  1051. freeAllErrorBacktracking(ei->track);
  1052. free(ei);
  1053. }
  1054. static void fprintfNote(FILE *file, char *note) {
  1055. char *ent = NULL;
  1056. while(true) {
  1057. ent = strchr(note, '\n');
  1058. if (ent != NULL)
  1059. *ent = NUL;
  1060. fprintf(file, " #note %s\n", note);
  1061. if (ent == NULL) // 意味着是最后一部分`note`
  1062. break;
  1063. *ent = '\n';
  1064. note = ent + 1;
  1065. }
  1066. }
  1067. void fprintfErrorInfo(FILE *file, af_ErrorInfo *ei) {
  1068. fprintf(file, "Error Traceback (most recent call last):\n");
  1069. for (af_ErrorBacktracking *ebt = ei->track; ebt != NULL; ebt = ebt->next) {
  1070. fprintf(file, " File \"%s\", line %d\n", ebt->file, ebt->line);
  1071. if (ebt->note != NULL)
  1072. fprintfNote(file, ebt->note);
  1073. }
  1074. fprintf(file, "%s: \"%s\"\n", ei->error_type, ei->error);
  1075. fflush(file);
  1076. }
  1077. static af_ErrorBacktracking *makeErrorBacktracking(FileLine line, FilePath file, char *note) {
  1078. af_ErrorBacktracking *ebt = calloc(1, sizeof(af_ErrorBacktracking));
  1079. ebt->line = line;
  1080. if (file == NULL)
  1081. ebt->file = strCopy("unknown.aun.sys");
  1082. else
  1083. ebt->file = strCopy(file);
  1084. if (note != NULL)
  1085. ebt->note = strCopy(note);
  1086. return ebt;
  1087. }
  1088. static af_ErrorBacktracking *freeErrorBacktracking(af_ErrorBacktracking *ebt) {
  1089. af_ErrorBacktracking *next = ebt->next;
  1090. free(ebt->note);
  1091. free(ebt->file);
  1092. free(ebt);
  1093. return next;
  1094. }
  1095. static void freeAllErrorBacktracking(af_ErrorBacktracking *ebt) {
  1096. while(ebt != NULL) {
  1097. ebt = freeErrorBacktracking(ebt);
  1098. }
  1099. }
  1100. void pushErrorBacktracking(FileLine line, FilePath file, char *note, af_ErrorInfo *ei) {
  1101. af_ErrorBacktracking *ebt = makeErrorBacktracking(line, file, note);
  1102. ebt->next = ei->track;
  1103. ei->track = ebt;
  1104. }
  1105. static char *getActivityInfoToBacktracking(af_Activity *activity, bool print_bt_top){
  1106. char *info = NULL;
  1107. if (activity->type == act_gc) {
  1108. info = strJoin(info, "gc-activity;", true, false);
  1109. return info;
  1110. } else if (activity->type == act_top)
  1111. info = strJoin(info, "top-activity;", true, false);
  1112. else if (activity->type == act_top_import)
  1113. info = strJoin(info, "top-import-activity;", true, false);
  1114. else if (activity->is_execution)
  1115. info = strJoin(info, "execution-activity;", true, false);
  1116. else if (activity->is_gc_call)
  1117. info = strJoin(info, "gc-destruct-function-call-activity;", true, false);
  1118. else
  1119. info = strJoin(info, "function-call-activity;", true, false);
  1120. switch (activity->status) {
  1121. case act_func_get:
  1122. info = strJoin(info, "\nfunc-get;", true, false);
  1123. break;
  1124. case act_func_arg:
  1125. info = strJoin(info, "\nfunc-arg;", true, false);
  1126. if (activity->run_in_func)
  1127. info = strJoin(info, " run-in-function-var-space;", true, false);
  1128. break;
  1129. case act_func_normal:
  1130. info = strJoin(info, "\nrun-code;", true, false);
  1131. if (activity->return_first)
  1132. info = strJoin(info, " return-first-result;", true, false);
  1133. break;
  1134. default:
  1135. break;
  1136. }
  1137. if (activity->is_macro_call)
  1138. info = strJoin(info, "\nmacro-call;", true, false);
  1139. if (activity->is_literal)
  1140. info = strJoin(info, "\nliteral-call;", true, false);
  1141. if (activity->is_obj_func)
  1142. info = strJoin(info, "\nobject-function-call;", true, false);
  1143. if (activity->optimization)
  1144. info = strJoin(info, "\ntail-call-Optimization;", true, false);
  1145. info = strJoin(info, "\n", true, false);
  1146. char *print_code = NULL;
  1147. if (!print_bt_top && activity->bt_next != NULL)
  1148. print_code = codeToStr(activity->bt_next, 1);
  1149. else if (activity->bt_top != NULL)
  1150. print_code = codeToStr(activity->bt_top, 1);
  1151. else if (activity->prev == NULL && activity->bt_start != NULL)
  1152. print_code = codeToStr(activity->bt_start, -1);
  1153. if (print_code != NULL) {
  1154. info = strJoin(info, "code: ", true, false);
  1155. info = strJoin(info, print_code, true, true);
  1156. } else
  1157. info = strJoin(info, "sys-err non-code", true, false);
  1158. return info;
  1159. }
  1160. af_ImportInfo *makeImportInfo(char *mark, af_Object *obj) {
  1161. af_ImportInfo *ii = calloc(1, sizeof(af_ImportInfo));
  1162. if (mark != NULL)
  1163. ii->mark = strCopy(mark);
  1164. ii->obj = obj;
  1165. gc_addReference(obj);
  1166. return ii;
  1167. }
  1168. void freeImportInfo(af_ImportInfo *ii) {
  1169. free(ii->mark);
  1170. if (ii->obj != NULL)
  1171. gc_delReference(ii->obj);
  1172. free(ii);
  1173. }
  1174. void setGcMax(size_t max, af_Environment *env) {
  1175. env->core->gc_count_max = max;
  1176. }
  1177. void setGcRun(enum GcRunTime grt, af_Environment *env) {
  1178. env->core->gc_run = grt;
  1179. }
  1180. size_t getGcCount(af_Environment *env) {
  1181. return env->core->gc_count;
  1182. }
  1183. size_t getGcMax(af_Environment *env) {
  1184. return env->core->gc_count_max;
  1185. }
  1186. enum GcRunTime getGcRun(af_Environment *env) {
  1187. return env->core->gc_run;
  1188. }
  1189. af_Object *getCoreGlobal(af_Environment *env) {
  1190. return env->core->global;
  1191. }
  1192. af_Object *getGlobal(af_Environment *env) {
  1193. af_Activity *activity = env->activity;
  1194. for (NULL; activity != NULL; activity = activity->prev) {
  1195. if (activity->type == act_top || activity->type == act_top_import)
  1196. return activity->belong;
  1197. }
  1198. return env->core->global;
  1199. }
  1200. af_Object *getBelong(af_Environment *env) {
  1201. if (env == NULL || env->activity == NULL)
  1202. return NULL;
  1203. return env->activity->belong;
  1204. }
  1205. FilePath getActivityFile(af_Environment *env){
  1206. if (env == NULL || env->activity == NULL)
  1207. return NULL;
  1208. return env->activity->file;
  1209. }
  1210. FileLine getActivityLine(af_Environment *env){
  1211. if (env == NULL || env->activity == NULL)
  1212. return 0;
  1213. return env->activity->line;
  1214. }
  1215. af_Object *getMsgNormalData(af_Message *msg) {
  1216. if (!EQ_STR("NORMAL", msg->type))
  1217. return NULL;
  1218. af_Object *obj = *(af_Object **)msg->msg;
  1219. gc_delReference(obj);
  1220. *(af_Object **)msg->msg = NULL;
  1221. return obj;
  1222. }
  1223. af_ErrorInfo *getMsgErrorInfo(af_Message *msg) {
  1224. if (!EQ_STR("ERROR", msg->type))
  1225. return NULL;
  1226. af_ErrorInfo *ei = *(af_ErrorInfo **)msg->msg;
  1227. return ei;
  1228. }
  1229. af_ImportInfo *getMsgImportInfo(af_Message *msg) {
  1230. if (!EQ_STR("IMPORT", msg->type))
  1231. return NULL;
  1232. af_ImportInfo *ii = *(af_ImportInfo **)msg->msg;
  1233. return ii;
  1234. }
  1235. char *getErrorType(af_ErrorInfo *ei) {
  1236. return ei->error_type;
  1237. }
  1238. char *getError(af_ErrorInfo *ei) {
  1239. return ei->error;
  1240. }
  1241. char *getImportMark(af_ImportInfo *ii) {
  1242. return ii->mark;
  1243. }
  1244. af_Object *getImportObject(af_ImportInfo *ii) {
  1245. af_Object *obj = ii->obj;
  1246. if (obj == NULL)
  1247. return NULL;
  1248. ii->obj = NULL;
  1249. gc_delReference(obj);
  1250. return obj;
  1251. }
  1252. af_VarSpaceListNode *getRunVarSpaceList(af_Environment *env) {
  1253. if (env->activity->type == act_gc)
  1254. return env->activity->var_list;
  1255. else
  1256. return env->activity->vsl;
  1257. }
  1258. int isCoreExit(af_Environment *env) {
  1259. if (env->core->status == core_exit)
  1260. return 1;
  1261. else if (env->core->status == core_stop)
  1262. return -1;
  1263. return 0;
  1264. }