env.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. #include "__env.h"
  2. /* Core 创建和释放 */
  3. static af_Core *makeCore(void);
  4. static void freeCore(af_Core *core);
  5. /* Core 初始化 */
  6. static bool enableCore(af_Core *core);
  7. static void checkInherit(af_Inherit **ih, af_Object *obj);
  8. /* Activity 创建和释放 */
  9. static af_Activity *makeActivity(af_Code *bt_top, af_Code *bt_start, bool return_first, af_Message *msg_up,
  10. af_VarSpaceListNode *vsl, af_Object *belong, af_Object *func);
  11. static af_Activity *freeActivity(af_Activity *activity);
  12. static void freeAllActivity(af_Activity *activity);
  13. static void clearActivity(af_Activity *activity);
  14. /* Activity 相关处理函数 */
  15. static void freeMark(af_Environment *env);
  16. static void newActivity(af_Code *bt, const af_Code *next, bool return_first, af_Environment *env);
  17. static void freeMarkByActivity(af_Activity *activity);
  18. /* 环境变量 创建与释放 */
  19. static af_EnvVar *makeEnvVar(char *name, char *data);
  20. static af_EnvVar *freeEnvVar(af_EnvVar *var);
  21. static void freeAllEnvVar(af_EnvVar *var);
  22. static void freeEnvVarSpace(af_EnvVarSpace *evs);
  23. /* 顶层消息处理器 创建与释放 */
  24. static af_TopMsgProcess *makeTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func);
  25. static af_TopMsgProcess *freeTopMsgProcess(af_TopMsgProcess *mp);
  26. static void freeAllTopMsgProcess(af_TopMsgProcess *mp);
  27. /* 顶层消息处理器 处理函数 */
  28. static void *findTopMsgProcessFunc(char *type, af_Environment *env);
  29. static void runTopMessageProcess(af_Environment *env);
  30. /* LiteralData 创建与释放 */
  31. static af_LiteralDataList *makeLiteralDataList(char *data);
  32. static af_LiteralDataList *freeLiteralData_Pri(af_LiteralDataList *ld);
  33. static af_Core *makeCore(void) {
  34. af_Core *core = calloc(sizeof(af_Core), 1);
  35. core->in_init = true;
  36. core->protect = makeVarSpace();
  37. addVarSpaceGCByCore(core->protect, core);
  38. gc_addReference(core->protect); // protect被外部引用, 由gc管理, 此处标记一个Reference
  39. core->prefix[V_QUOTE] = '\'';
  40. core->prefix[B_EXEC] = '\'';
  41. core->prefix[B_EXEC_FIRST] = ',';
  42. return core;
  43. }
  44. static void freeCore(af_Core *core) {
  45. if (core->object != NULL)
  46. gc_delReference(core->object);
  47. if (core->global != NULL)
  48. gc_delReference(core->global);
  49. gc_delReference(core->protect);
  50. printGCByCode(core);
  51. gc_freeAllValue(core);
  52. free(core);
  53. }
  54. char setPrefix(size_t name, char prefix, af_Environment *env) {
  55. char old = env->core->prefix[name];
  56. if (name >= PREFIX_SIZE || prefix == NUL)
  57. return NUL;
  58. env->core->prefix[name] = prefix;
  59. return old;
  60. }
  61. char getPrefix(size_t name, af_Environment *env) {
  62. return env->core->prefix[name];
  63. }
  64. af_VarSpace *getProtectVarSpace(af_Environment *env) {
  65. return env->core->protect;
  66. }
  67. /*
  68. * 函数名: getBaseObjectFromCore
  69. * 目标: 从VarSpace中获取一个量
  70. * 作用: 用于init初始化时在保护空间获得一些初始化对象
  71. */
  72. af_Object *getBaseObjectFromCore(char *name, af_Core *core) {
  73. af_Var *var = findVarFromVarSpace(name, core->protect);
  74. if (var != NULL)
  75. return var->vn->obj;
  76. return NULL;
  77. }
  78. /*
  79. * 函数名: getBaseObject
  80. * 目标: getBaseObjectFromCore的对外接口
  81. */
  82. af_Object *getBaseObject(char *name, af_Environment *env) {
  83. return getBaseObjectFromCore(name, env->core);
  84. }
  85. static void checkInherit(af_Inherit **ih, af_Object *obj) {
  86. while (*ih != NULL) {
  87. if ((*ih)->obj->data == obj->data) {
  88. if ((*ih)->next == NULL && (*ih)->obj == obj) // 最后一个就是obj
  89. return; // 不需要任何更改
  90. *ih = freeInherit(*ih); // 释放该ih
  91. } else
  92. ih = &((*ih)->next);
  93. }
  94. *ih = makeInherit(obj);
  95. }
  96. static bool enableCore(af_Core *core) {
  97. af_Object *object = getBaseObjectFromCore("object", core);
  98. af_Object *global = getBaseObjectFromCore("global", core);
  99. if (global == NULL || global->belong != NULL)
  100. return false; // global未找到 或其有属对象
  101. if (object == NULL || object->data->inherit != NULL || !object->data->allow_inherit)
  102. return false; // object未找到 或其继承自其他对象 或其不可被继承
  103. core->global = global;
  104. core->object = object;
  105. addVarSpaceGCByCore(global->data->var_space, core);
  106. for (af_Object *obj = core->object; obj != NULL; obj = obj->gc.next) {
  107. if (obj == global)
  108. continue;
  109. if (obj->belong == NULL)
  110. obj->belong = global;
  111. }
  112. for (af_ObjectData *od = core->gc_ObjectData; od != NULL; od = od->gc.next) {
  113. if (od == object->data)
  114. continue;
  115. checkInherit(&od->inherit, object);
  116. }
  117. gc_addReference(object);
  118. gc_addReference(global);
  119. addVarSpaceGCByCore(global->data->var_space, core); // global的vs是全局作用空间, 被外部引用, 所以由gc管理 (不需要要标记Reference, global已经标记了)
  120. core->in_init = false;
  121. return true;
  122. }
  123. static af_Activity *makeActivity(af_Code *bt_top, af_Code *bt_start, bool return_first, af_Message *msg_up,
  124. af_VarSpaceListNode *vsl, af_Object *belong, af_Object *func) {
  125. af_Activity *activity = calloc(sizeof(af_Activity), 1);
  126. activity->status = act_func;
  127. activity->msg_up = msg_up;
  128. activity->msg_up_count = 0;
  129. activity->var_list = vsl;
  130. activity->new_vs_count = 0;
  131. activity->belong = belong;
  132. activity->func = func;
  133. gc_addReference(belong);
  134. if (func != NULL)
  135. gc_addReference(func);
  136. activity->bt_top = bt_top;
  137. activity->bt_start = bt_start;
  138. activity->bt_next = bt_start;
  139. activity->return_first = return_first;
  140. return activity;
  141. }
  142. static af_Activity *freeActivity(af_Activity *activity) {
  143. af_Activity *prev = activity->prev;
  144. gc_delReference(activity->belong);
  145. if (activity->func != NULL)
  146. gc_delReference(activity->func);
  147. freeAllMessage(activity->msg_down); // msg转移后需要将对应成员设置为NULL
  148. freeMessageCount(activity->msg_up_count, activity->msg_up);
  149. // vsl 是引用自 var_list和func_var_list的 故不释放
  150. // func_var_list 是引用自函数的 故不释放
  151. freeVarSpaceListCount(activity->new_vs_count, activity->var_list);
  152. freeVarSpaceListCount(activity->macro_vs_count, activity->macro_vsl);
  153. if (activity->return_obj != NULL)
  154. gc_delReference(activity->return_obj);
  155. if (activity->parentheses_call != NULL)
  156. gc_delReference(activity->parentheses_call);
  157. freeAllArgCodeList(activity->acl_start);
  158. if (activity->fi != NULL)
  159. freeFuncInfo(activity->fi);
  160. freeAllLiteralData(activity->ld);
  161. free(activity);
  162. return prev;
  163. }
  164. static void freeAllActivity(af_Activity *activity) {
  165. while (activity != NULL)
  166. activity = freeActivity(activity);
  167. }
  168. static void clearActivity(af_Activity *activity) {
  169. freeMarkByActivity(activity);
  170. freeVarSpaceListCount(activity->macro_vs_count, activity->macro_vsl);
  171. freeAllArgCodeList(activity->acl_start);
  172. if (activity->fi != NULL)
  173. freeFuncInfo(activity->fi);
  174. if (activity->parentheses_call != NULL) {
  175. gc_delReference(activity->parentheses_call);
  176. activity->parentheses_call = NULL;
  177. }
  178. activity->func_var_list = NULL;
  179. activity->bt_top = NULL;
  180. activity->bt_start = NULL;
  181. activity->bt_next = NULL;
  182. activity->acl_start = NULL;
  183. activity->acl_done = NULL;
  184. activity->fi = NULL;
  185. activity->body_next = NULL;
  186. }
  187. /*
  188. * 函数名: makeLiteralDataList
  189. * 目标: 生成一个 af_LiteralDataList
  190. * 注意: char *data 要求传入一个已经被复制的data值
  191. * makeLiteralDataList是内部函数, 属于可控函数, 因此data在函数内部不再复制
  192. */
  193. static af_LiteralDataList *makeLiteralDataList(char *data) {
  194. af_LiteralDataList *ld = calloc(sizeof(af_LiteralDataList), 1);
  195. ld->literal_data = data;
  196. return ld;
  197. }
  198. static af_LiteralDataList *freeLiteralData_Pri(af_LiteralDataList *ld) {
  199. af_LiteralDataList *next = ld->next;
  200. free(ld->literal_data);
  201. free(ld);
  202. return next;
  203. }
  204. void freeAllLiteralData(af_LiteralDataList *ld) {
  205. while (ld != NULL)
  206. ld = freeLiteralData_Pri(ld);
  207. }
  208. void pushLiteralData(char *data, af_Environment *env) {
  209. af_LiteralDataList *ld = makeLiteralDataList(data);
  210. ld->next = env->activity->ld;
  211. env->activity->ld = ld;
  212. }
  213. af_Message *makeMessage(char *type, size_t size) {
  214. af_Message *msg = calloc(sizeof(af_Message), 1);
  215. msg->type = strCopy(type);
  216. if (size != 0)
  217. msg->msg = calloc(size, 1);
  218. msg->size = size;
  219. return msg;
  220. }
  221. af_Message *freeMessage(af_Message *msg) {
  222. af_Message *next = msg->next;
  223. free(msg->type);
  224. free(msg->msg);
  225. free(msg);
  226. return next;
  227. }
  228. void freeAllMessage(af_Message *msg) {
  229. while (msg != NULL)
  230. msg = freeMessage(msg);
  231. }
  232. bool freeMessageCount(size_t count, af_Message *msg) {
  233. for (size_t i = count; i > 0; i--) {
  234. if (msg == NULL) // 发生了错误
  235. return false;
  236. msg = freeMessage(msg);
  237. }
  238. return true;
  239. }
  240. void pushMessageUp(af_Message *msg, af_Environment *env) {
  241. msg->next = env->activity->msg_up;
  242. env->activity->msg_up = msg;
  243. env->activity->msg_up_count++;
  244. }
  245. void pushMessageDown(af_Message *msg, af_Environment *env) {
  246. msg->next = env->activity->msg_down;
  247. env->activity->msg_down = msg;
  248. }
  249. void *popMessageUpData(char *type, af_Environment *env) {
  250. for (af_Message **pmsg = &env->activity->msg_up; *pmsg != NULL; pmsg = &((*pmsg)->next)) {
  251. if (EQ_STR((*pmsg)->type, type))
  252. return (*pmsg)->msg; // msg_up是只读的
  253. }
  254. return NULL;
  255. }
  256. af_Message *popMessageUp(af_Environment *env) {
  257. if (env->activity->msg_up_count == 0 || env->activity->msg_up == NULL)
  258. return NULL;
  259. af_Message *msg = env->activity->msg_up;
  260. env->activity->msg_up = msg->next;
  261. msg->next = NULL;
  262. env->activity->msg_up_count--;
  263. return msg;
  264. }
  265. /*
  266. * 函数名: getMessageData
  267. * 目标: 获取`msg`的数据, 对外API
  268. */
  269. void *getMessageData(af_Message *msg) {
  270. return msg->msg;
  271. }
  272. af_Message *popMessageDown(char *type, af_Environment *env) {
  273. for (af_Message **pmsg = &env->activity->msg_down; *pmsg != NULL; pmsg = &((*pmsg)->next)) {
  274. if (EQ_STR((*pmsg)->type, type)) {
  275. af_Message *msg = *pmsg;
  276. *pmsg = msg->next;
  277. msg->next = NULL;
  278. return msg;
  279. }
  280. }
  281. return NULL;
  282. }
  283. af_Message *getFirstMessage(af_Environment *env) {
  284. af_Message *msg = env->activity->msg_down;
  285. env->activity->msg_down = msg->next;
  286. msg->next = NULL;
  287. return msg;
  288. }
  289. void connectMessage(af_Message **base, af_Message *msg) {
  290. while (*base != NULL)
  291. base = &((*base)->next);
  292. *base = msg;
  293. }
  294. static af_EnvVar *makeEnvVar(char *name, char *data) {
  295. af_EnvVar *var = calloc(sizeof(af_EnvVar), 1);
  296. var->name = strCopy(name);
  297. var->data = strCopy(data);
  298. return var;
  299. }
  300. static af_EnvVar *freeEnvVar(af_EnvVar *var) {
  301. af_EnvVar *next = var->next;
  302. free(var->data);
  303. free(var->name);
  304. free(var);
  305. return next;
  306. }
  307. static void freeAllEnvVar(af_EnvVar *var) {
  308. while (var != NULL)
  309. var = freeEnvVar(var);
  310. }
  311. static af_EnvVarSpace *makeEnvVarSpace(void) {
  312. af_EnvVarSpace *esv = calloc(sizeof(af_EnvVarSpace), 1);
  313. return esv;
  314. }
  315. static void freeEnvVarSpace(af_EnvVarSpace *evs) {
  316. for (int i = 0; i < ENV_VAR_HASH_SIZE; i++)
  317. freeAllEnvVar(evs->var[i]);
  318. free(evs);
  319. }
  320. void setEnvVar(char *name, char *data, af_Environment *env) {
  321. time33_t index = time33(name) % ENV_VAR_HASH_SIZE;
  322. af_EnvVar **pvar = &env->esv->var[index];
  323. for (NULL; *pvar != NULL; pvar = &((*pvar)->next)) {
  324. if (EQ_STR((*pvar)->name, name)) {
  325. free((*pvar)->data);
  326. (*pvar)->data = strCopy(data);
  327. return;
  328. }
  329. }
  330. *pvar = makeEnvVar(name, data);
  331. }
  332. char *findEnvVar(char *name, af_Environment *env) {
  333. time33_t index = time33(name) % ENV_VAR_HASH_SIZE;
  334. af_EnvVar **pvar = &env->esv->var[index];
  335. for (NULL; *pvar != NULL; pvar = &((*pvar)->next)) {
  336. if (EQ_STR((*pvar)->name, name))
  337. return (*pvar)->data;
  338. }
  339. return NULL;
  340. }
  341. void mp_NORMAL(af_Message *msg, af_Environment *env) {
  342. if (msg->msg == NULL || *(af_Object **)msg->msg == NULL) {
  343. printf("msg: %p error\n", msg->msg);
  344. return;
  345. }
  346. gc_delReference(*(af_Object **)msg->msg);
  347. printf("NORMAL Point: %p\n", *(af_Object **)msg->msg);
  348. }
  349. af_Environment *makeEnvironment(void) {
  350. af_Environment *env = calloc(sizeof(af_Environment), 1);
  351. DLC_SYMBOL(TopMsgProcessFunc) func = MAKE_SYMBOL(mp_NORMAL, TopMsgProcessFunc);
  352. env->core = makeCore();
  353. env->esv = makeEnvVarSpace();
  354. addTopMsgProcess("NORMAL", func, env);
  355. FREE_SYMBOL(func);
  356. return env;
  357. }
  358. bool addTopActivity(af_Code *code, af_Environment *env) {
  359. if (env->activity != NULL)
  360. return false;
  361. env->activity = makeActivity(NULL, code, false, NULL, NULL, env->core->global, NULL);
  362. env->activity->new_vs_count = 2;
  363. env->activity->var_list = makeVarSpaceList(env->core->global->data->var_space);
  364. env->activity->var_list->next = makeVarSpaceList(env->core->protect);
  365. env->activity->status = act_normal;
  366. return true;
  367. }
  368. bool enableEnvironment(af_Environment *env) {
  369. return enableCore(env->core);
  370. }
  371. void freeEnvironment(af_Environment *env) {
  372. freeCore(env->core);
  373. freeAllActivity(env->activity);
  374. freeEnvVarSpace(env->esv);
  375. freeAllTopMsgProcess(env->process);
  376. free(env);
  377. }
  378. bool addVarToProtectVarSpace(af_Var *var, af_Environment *env) {
  379. return addVarToVarSpace(var, env->core->protect);
  380. }
  381. static af_TopMsgProcess *makeTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func) {
  382. af_TopMsgProcess *mp = calloc(sizeof(af_TopMsgProcess), 1);
  383. mp->type = strCopy(type);
  384. mp->func = COPY_SYMBOL(func, TopMsgProcessFunc);
  385. return mp;
  386. }
  387. static af_TopMsgProcess *freeTopMsgProcess(af_TopMsgProcess *mp) {
  388. af_TopMsgProcess *next = mp->next;
  389. free(mp->type);
  390. FREE_SYMBOL(mp->func);
  391. free(mp);
  392. return next;
  393. }
  394. static void freeAllTopMsgProcess(af_TopMsgProcess *mp) {
  395. while (mp != NULL)
  396. mp = freeTopMsgProcess(mp);
  397. }
  398. static void *findTopMsgProcessFunc(char *type, af_Environment *env) {
  399. af_TopMsgProcess *mp = env->process;
  400. for (NULL; mp != NULL; mp = mp->next) {
  401. if (EQ_STR(type, mp->type))
  402. return mp;
  403. }
  404. return NULL;
  405. }
  406. void addTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func,
  407. af_Environment *env) {
  408. af_TopMsgProcess *mp = makeTopMsgProcess(type, func);
  409. mp->next = env->process;
  410. env->process = mp;
  411. }
  412. bool changeTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func,
  413. af_Environment *env) {
  414. af_TopMsgProcess *mp = findTopMsgProcessFunc(type, env);
  415. if (mp == NULL)
  416. return false;
  417. FREE_SYMBOL(mp->func);
  418. mp->func = COPY_SYMBOL(func, TopMsgProcessFunc);
  419. return true;
  420. }
  421. static void newActivity(af_Code *bt, const af_Code *next, bool return_first, af_Environment *env){
  422. if (next == NULL && env->activity->body_next == NULL) {
  423. printf("Tail tone recursive optimization\n");
  424. clearActivity(env->activity);
  425. env->activity->bt_top = bt;
  426. if (!env->activity->return_first) // 若原本就有设置 return_first 则没有在设置的必要了, 因为该执行不会被返回
  427. env->activity->return_first = return_first;
  428. } else {
  429. af_Activity *activity = makeActivity(bt, NULL, return_first, env->activity->msg_up,
  430. env->activity->var_list, env->activity->belong,
  431. env->activity->func);
  432. activity->prev = env->activity;
  433. env->activity = activity;
  434. }
  435. }
  436. bool pushExecutionActivity(af_Code *bt, bool return_first, af_Environment *env) {
  437. af_Code *next;
  438. if (!getCodeBlockNext(bt, &next)) {
  439. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  440. return false;
  441. }
  442. env->activity->bt_next = next;
  443. newActivity(bt, next, return_first, env);
  444. env->activity->bt_start = bt->next;
  445. env->activity->bt_next = bt->next;
  446. env->activity->status = act_normal;
  447. return true;
  448. }
  449. bool pushFuncActivity(af_Code *bt, af_Environment *env) {
  450. af_Code *next;
  451. af_Code *func;
  452. af_Object *parentheses_call = env->activity->parentheses_call;
  453. if (parentheses_call != NULL)
  454. gc_delReference(parentheses_call);
  455. env->activity->parentheses_call = NULL;
  456. if (!getCodeBlockNext(bt, &next)) {
  457. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  458. return false;
  459. }
  460. if (bt->block.type == curly) { // 大括号
  461. if (bt->block.elements == 0) {
  462. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  463. return false;
  464. } else
  465. func = bt->next;
  466. } else if (bt->block.type == brackets) { // 暂时不考虑中括号
  467. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  468. return false;
  469. } else
  470. func = NULL; // 小括号则不在需要匹配
  471. env->activity->bt_next = next;
  472. newActivity(bt, next, false, env);
  473. env->activity->bt_start = func;
  474. env->activity->bt_next = func;
  475. env->activity->call_type = env->activity->bt_top->block.type;
  476. env->activity->status = act_func;
  477. if (env->activity->call_type == parentheses) // 对于类前缀调用, 已经获得func的实际值了
  478. return setFuncActivityToArg(parentheses_call, env);
  479. return true;
  480. }
  481. bool pushLiteralActivity(af_Code *bt, af_Object *func, af_Environment *env) {
  482. char *literal_data = strCopy(bt->literal.literal_data); // newActivity可能会导致code和literal_data释放
  483. env->activity->bt_next = bt->next;
  484. /* 隐式调用不设置 bt_top */
  485. newActivity(NULL, bt->next, false, env); // 如果原activity也是字面量, 则不进行尾调递归优化
  486. env->activity->is_literal = true;
  487. pushLiteralData(literal_data, env);
  488. return setFuncActivityToArg(func, env);
  489. }
  490. bool pushVariableActivity(af_Code *bt, af_Object *func, af_Environment *env) {
  491. env->activity->bt_next = bt->next;
  492. /* 隐式调用不设置 bt_top */
  493. newActivity(bt, bt->next, false, env);
  494. return setFuncActivityToArg(func, env);
  495. }
  496. bool pushMacroFuncActivity(af_Object *func, af_Environment *env) {
  497. /* Macro是隐式调用, bt不移动 */
  498. /* 沿用activity */
  499. printf("Run macro\n");
  500. if (!freeVarSpaceListCount(env->activity->new_vs_count, env->activity->var_list)) { // 释放外部变量空间
  501. env->activity->new_vs_count = 0;
  502. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  503. return false;
  504. }
  505. env->activity->var_list = env->activity->macro_vsl;
  506. env->activity->new_vs_count = env->activity->macro_vs_count;
  507. env->activity->macro_vs_count = 0;
  508. clearActivity(env->activity); /* 隐式调用不设置 bt_top */
  509. return setFuncActivityToArg(func, env);
  510. }
  511. bool setFuncActivityToArg(af_Object *func, af_Environment *env) {
  512. obj_funcGetArgCodeList *get_acl = findAPI("obj_funcGetArgCodeList", func->data->api);
  513. obj_funcGetVarList *get_var_list = findAPI("obj_funcGetVarList", func->data->api);
  514. if (get_var_list == NULL || get_acl == NULL) {
  515. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  516. return false;
  517. }
  518. af_Object *belong = getBelongObject(func, env);
  519. gc_delReference(env->activity->belong);
  520. if (env->activity->func != NULL)
  521. gc_delReference(env->activity->func);
  522. gc_addReference(func);
  523. gc_addReference(belong);
  524. env->activity->func = func;
  525. env->activity->belong = belong;
  526. env->activity->status = act_arg;
  527. /* 遇到错误时 get_acl 和 get_var_list 要自行设定msg */
  528. if (!get_acl(&env->activity->acl_start, func, env->activity->bt_top, &env->activity->mark, env)) // 设置acl
  529. return false;
  530. if (!get_var_list(&env->activity->func_var_list, func, env->activity->mark, env)) // 设置 func_var_list
  531. return false;
  532. env->activity->acl_done = env->activity->acl_start;
  533. if (env->activity->acl_done != NULL)
  534. env->activity->bt_next = env->activity->acl_done->code;
  535. else
  536. env->activity->bt_next = NULL;
  537. return true;
  538. }
  539. bool setFuncActivityAddVar(af_Environment *env){
  540. obj_funcGetInfo *get_info = findAPI("obj_funcGetInfo", env->activity->func->data->api);
  541. obj_funcGetArgList *get_arg_list = findAPI("obj_funcGetArgList", env->activity->func->data->api);
  542. af_ArgList *al;
  543. if (get_info == NULL || get_arg_list == NULL) {
  544. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  545. return false;
  546. }
  547. if (!get_info(&env->activity->fi, env->activity->func, env->activity->bt_top, env->activity->mark, env))
  548. return false;
  549. if (env->activity->fi == NULL) {
  550. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  551. return false;
  552. }
  553. if (env->activity->fi->scope == super_pure_scope && env->activity->fi->scope == super_embedded) {
  554. /* 超纯函数和超内嵌函数不得搭配使用 */
  555. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  556. return false;
  557. }
  558. env->activity->body_next = env->activity->fi->body;
  559. if (env->activity->fi->is_macro) { // 是宏函数则保存变量空间
  560. env->activity->macro_vsl = env->activity->var_list;
  561. env->activity->macro_vs_count = env->activity->new_vs_count;
  562. } else if (env->activity->fi->scope != inline_scope) { // 非内联函数, 释放外部变量空间
  563. if (!freeVarSpaceListCount(env->activity->new_vs_count, env->activity->var_list)) {
  564. pushMessageDown(makeMessage("ERROR-STR", 0), env); // 释放失败
  565. return false;
  566. }
  567. }
  568. if (env->activity->fi->scope == normal_scope) { // 使用函数变量空间
  569. env->activity->var_list = env->activity->func_var_list;
  570. env->activity->new_vs_count = 0;
  571. } else if (env->activity->fi->scope == pure_scope) { // 纯函数只有 protect 变量空间
  572. env->activity->var_list = makeVarSpaceList(env->core->protect);
  573. env->activity->new_vs_count = 0;
  574. } else if (env->activity->fi->scope == super_pure_scope) { // 超纯函数没有变量空间, 因此不得为超内嵌函数(否则var_list就为NULL了)
  575. env->activity->var_list = NULL;
  576. env->activity->new_vs_count = 0;
  577. }
  578. if (env->activity->fi->embedded != super_embedded) { // 不是超内嵌函数则引入一层新的变量空间
  579. env->activity->var_list = pushNewVarList(env->activity->var_list);
  580. env->activity->new_vs_count++;
  581. }
  582. env->activity->func_var_list = NULL;
  583. if (!get_arg_list(&al, env->activity->func, env->activity->acl_start, env->activity->mark, env))
  584. return false;
  585. runArgList(al, env->activity->var_list);
  586. freeAllArgList(al);
  587. if (env->activity->fi->embedded == protect_embedded)
  588. env->activity->var_list->vs->is_protect = true;
  589. int status = setFuncActivityToNormal(env);
  590. if (status == -1) {
  591. popActivity(makeMessage("ERROR-STR", 0), env);
  592. return false;
  593. } else if (status == 0)
  594. env->process_msg_first = true; // 先不弹出activity, 通过act_normal处理msg
  595. return true;
  596. }
  597. int setFuncActivityToNormal(af_Environment *env){ // 获取函数的函数体
  598. env->activity->status = act_normal;
  599. env->activity->bt_next = NULL;
  600. if (env->activity->body_next == NULL) // 已经没有下一步了 (原msg不释放)
  601. return -1;
  602. while (env->activity->body_next != NULL) {
  603. if (env->activity->body_next->type == func_body_c) {
  604. GET_SYMBOL(env->activity->body_next->c_func)(env->activity->mark, env);
  605. env->activity->body_next = env->activity->body_next->next;
  606. } else {
  607. env->activity->bt_start = env->activity->body_next->code;
  608. env->activity->bt_next = env->activity->body_next->code;
  609. env->activity->body_next = env->activity->body_next->next;
  610. return 1; // 仍有下一步
  611. }
  612. }
  613. return 0; // 没有下一步, 但运行了C函数 (原msg释放)
  614. }
  615. /*
  616. * 函数名: runTopMessageProcess
  617. * 目标: 运行顶层信息处理器
  618. */
  619. static void runTopMessageProcess(af_Environment *env) {
  620. af_Message **pmsg = &env->activity->msg_down;
  621. while (*pmsg != NULL) {
  622. af_TopMsgProcess *mp = findTopMsgProcessFunc((*pmsg)->type, env);
  623. if (mp != NULL) {
  624. GET_SYMBOL(mp->func)(*pmsg, env);
  625. *pmsg = freeMessage(*pmsg);
  626. } else
  627. pmsg = &((*pmsg)->next);
  628. }
  629. }
  630. static void freeMarkByActivity(af_Activity *activity) {
  631. if (activity->func != NULL) {
  632. obj_funcFreeMask *func = findAPI("obj_funcFreeMask", activity->func->data->api);
  633. if (func != NULL)
  634. func(activity->mark);
  635. activity->mark = NULL;
  636. }
  637. }
  638. static void freeMark(af_Environment *env) {
  639. if (env->activity->func != NULL) {
  640. obj_funcFreeMask *func = findAPI("obj_funcFreeMask", env->activity->func->data->api);
  641. if (func != NULL)
  642. func(env->activity->mark);
  643. env->activity->mark = NULL;
  644. }
  645. }
  646. void popActivity(af_Message *msg, af_Environment *env) {
  647. env->process_msg_first = true;
  648. if (env->activity->return_first) {
  649. if (msg != NULL) {
  650. gc_delReference(*(af_Object **)msg->msg);
  651. freeMessage(msg);
  652. }
  653. if (env->activity->return_obj == NULL)
  654. msg = makeMessage("ERROR-STR", 0);
  655. else {
  656. msg = makeMessage("NORMAL", sizeof(af_Object *));
  657. *(af_Object **)msg->msg = env->activity->return_obj; // env->activity->return_obj本来就有一个gc_Reference
  658. env->activity->return_obj = NULL;
  659. }
  660. }
  661. freeMark(env);
  662. if (env->activity->prev != NULL) {
  663. af_Message *new_msg;
  664. if (msg != NULL) {
  665. new_msg = msg;
  666. msg->next = env->activity->msg_down;
  667. } else
  668. new_msg = env->activity->msg_down;
  669. env->activity->msg_down = NULL;
  670. connectMessage(&new_msg, env->activity->prev->msg_down);
  671. env->activity->prev->msg_down = new_msg;
  672. } else { // 到顶
  673. if (msg != NULL) {
  674. msg->next = env->activity->msg_down;
  675. env->activity->msg_down = msg;
  676. }
  677. runTopMessageProcess(env);
  678. }
  679. env->activity = freeActivity(env->activity);
  680. }