2
0

env.c 54 KB

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