env.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. #include "__env.h"
  2. /* 核心创建和释放 */
  3. static af_Core *makeCore(void);
  4. static void freeCore(af_Core *core);
  5. /* 核心初始化 */
  6. static bool enableCore(af_Core *core);
  7. static void checkInherit(af_Inherit **ih, af_Object *obj);
  8. /* 活动记录器创建和释放 */
  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. /* 环境变量创建与释放 */
  14. static af_EnvVar *makeEnvVar(char *name, char *data);
  15. static af_EnvVar *freeEnvVar(af_EnvVar *var);
  16. static void freeAllEnvVar(af_EnvVar *var);
  17. static void freeEnvVarSpace(af_EnvVarSpace *evs);
  18. /* 顶层消息处理器创建与释放 */
  19. static af_TopMsgProcess *makeTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func);
  20. static af_TopMsgProcess *freeTopMsgProcess(af_TopMsgProcess *mp);
  21. static void freeAllTopMsgProcess(af_TopMsgProcess *mp);
  22. /* 顶层消息处理器 处理函数 */
  23. static void *findTopMsgProcessFunc(char *type, af_Environment *env);
  24. static void runTopMessageProcess(af_Environment *env);
  25. static af_Core *makeCore(void) {
  26. af_Core *core = calloc(sizeof(af_Core), 1);
  27. core->in_init = true;
  28. core->protect = makeVarSpace();
  29. addVarSpaceGCByCore(core->protect, core);
  30. gc_addReference(core->protect); // protect被外部引用, 由gc管理, 此处标记一个Reference
  31. core->prefix[L_NOT_REPEAT] = '\'';
  32. core->prefix[V_QUOTE] = '\'';
  33. core->prefix[B_EXEC] = '\'';
  34. core->prefix[B_EXEC_FIRST] = ',';
  35. return core;
  36. }
  37. static void freeCore(af_Core *core) {
  38. if (core->object != NULL)
  39. gc_delReference(core->object);
  40. if (core->global != NULL)
  41. gc_delReference(core->global);
  42. gc_delReference(core->protect);
  43. gc_freeAllValue(core);
  44. free(core);
  45. }
  46. char setPrefix(size_t name, char prefix, af_Environment *env) {
  47. char old = env->core->prefix[name];
  48. if (name >= PREFIX_SIZE || prefix == NUL)
  49. return NUL;
  50. env->core->prefix[name] = prefix;
  51. return old;
  52. }
  53. char getPrefix(size_t name, af_Environment *env) {
  54. return env->core->prefix[name];
  55. }
  56. /*
  57. * 函数名: getBaseObjectFromCore
  58. * 目标: 从VarSpace中获取一个量
  59. * 作用: 用于init初始化时在保护空间获得一些初始化对象
  60. */
  61. af_Object *getBaseObjectFromCore(char *name, af_Core *core) {
  62. af_Var *var = findVarFromVarSpace(name, core->protect);
  63. if (var != NULL)
  64. return var->vn->obj;
  65. return NULL;
  66. }
  67. /*
  68. * 函数名: getBaseObject
  69. * 目标: getBaseObjectFromCore的对外接口
  70. */
  71. af_Object *getBaseObject(char *name, af_Environment *env) {
  72. return getBaseObjectFromCore(name, env->core);
  73. }
  74. static void checkInherit(af_Inherit **ih, af_Object *obj) {
  75. while (*ih != NULL) {
  76. if ((*ih)->obj->data == obj->data) {
  77. if ((*ih)->next == NULL && (*ih)->obj == obj) // 最后一个就是obj
  78. return; // 不需要任何更改
  79. *ih = freeInherit(*ih); // 释放该ih
  80. } else
  81. ih = &((*ih)->next);
  82. }
  83. *ih = makeInherit(obj);
  84. }
  85. static bool enableCore(af_Core *core) {
  86. af_Object *object = getBaseObjectFromCore("object", core);
  87. af_Object *global = getBaseObjectFromCore("global", core);
  88. if (global == NULL || global->belong != NULL)
  89. return false; // global未找到 或其有属对象
  90. if (object == NULL || object->data->inherit != NULL || !object->data->allow_inherit)
  91. return false; // object未找到 或其继承自其他对象 或其不可被继承
  92. core->global = global;
  93. core->object = object;
  94. addVarSpaceGCByCore(global->data->var_space, core);
  95. for (af_Object *obj = core->object; obj != NULL; obj = obj->gc.next) {
  96. if (obj == global)
  97. continue;
  98. if (obj->belong == NULL)
  99. obj->belong = global;
  100. }
  101. for (af_ObjectData *od = core->gc_ObjectData; od != NULL; od = od->gc.next) {
  102. if (od == object->data)
  103. continue;
  104. checkInherit(&od->inherit, object);
  105. }
  106. gc_addReference(object);
  107. gc_addReference(global);
  108. addVarSpaceGCByCore(global->data->var_space, core); // global的vs是全局作用空间, 被外部引用, 所以由gc管理 (不需要要标记Reference, global已经标记了)
  109. core->in_init = false;
  110. return true;
  111. }
  112. static af_Activity *makeActivity(af_Code *bt_top, af_Code *bt_start, bool return_first, af_Message *msg_up,
  113. af_VarSpaceListNode *vsl, af_Object *belong, af_Object *func) {
  114. af_Activity *activity = calloc(sizeof(af_Activity), 1);
  115. activity->status = act_func;
  116. activity->msg_up = msg_up;
  117. activity->msg_up_count = 0;
  118. activity->var_list = vsl;
  119. activity->new_vs_count = 0;
  120. activity->belong = belong;
  121. activity->func = func;
  122. gc_addReference(belong);
  123. if (func != NULL)
  124. gc_addReference(func);
  125. activity->bt_top = bt_top;
  126. activity->bt_start = bt_start;
  127. activity->bt_next = bt_start;
  128. activity->return_first = return_first;
  129. return activity;
  130. }
  131. static af_Activity *freeActivity(af_Activity *activity) {
  132. af_Activity *prev = activity->prev;
  133. af_VarSpaceListNode *vs = activity->var_list;
  134. af_Message *msg_up = activity->msg_up;
  135. gc_delReference(activity->belong);
  136. if (activity->func != NULL)
  137. gc_delReference(activity->func);
  138. freeAllMessage(activity->msg_down); // msg转移后需要将对应成员设置为NULL
  139. for (int i = activity->msg_up_count; i > 0; i--) {
  140. if (msg_up == NULL) // 发生了错误
  141. break;
  142. msg_up = freeMessage(msg_up);
  143. }
  144. // vsl 是引用自 var_list和func_var_list的 故不释放
  145. // func_var_list 是引用自函数的 故不释放
  146. for (int i = activity->new_vs_count; i > 0; i--) {
  147. if (vs == NULL) // 发生了错误
  148. break;
  149. vs = popLastVarList(vs);
  150. }
  151. if (activity->return_obj != NULL)
  152. gc_delReference(activity->return_obj);
  153. if (activity->parentheses_call != NULL)
  154. gc_delReference(activity->parentheses_call);
  155. freeAllArgCodeList(activity->acl_start);
  156. if (activity->fi != NULL)
  157. freeFuncInfo(activity->fi);
  158. free(activity);
  159. return prev;
  160. }
  161. static void freeAllActivity(af_Activity *activity) {
  162. while (activity != NULL)
  163. activity = freeActivity(activity);
  164. }
  165. af_Message *makeMessage(char *type, size_t size) {
  166. af_Message *msg = calloc(sizeof(af_Message), 1);
  167. msg->type = strCopy(type);
  168. if (size != 0)
  169. msg->msg = calloc(size, 1);
  170. msg->size = size;
  171. return msg;
  172. }
  173. af_Message *freeMessage(af_Message *msg) {
  174. af_Message *next = msg->next;
  175. free(msg->type);
  176. free(msg->msg);
  177. free(msg);
  178. return next;
  179. }
  180. void freeAllMessage(af_Message *msg) {
  181. while (msg != NULL)
  182. msg = freeMessage(msg);
  183. }
  184. void pushMessageUp(af_Message *msg, af_Environment *env) {
  185. msg->next = env->activity->msg_up;
  186. env->activity->msg_up = msg;
  187. env->activity->msg_up_count++;
  188. }
  189. void pushMessageDown(af_Message *msg, af_Environment *env) {
  190. msg->next = env->activity->msg_down;
  191. env->activity->msg_down = msg;
  192. }
  193. void *popMessageUpData(char *type, af_Environment *env) {
  194. for (af_Message **pmsg = &env->activity->msg_up; *pmsg != NULL; pmsg = &((*pmsg)->next)) {
  195. if (EQ_STR((*pmsg)->type, type))
  196. return (*pmsg)->msg; // msg_up是只读的
  197. }
  198. return NULL;
  199. }
  200. af_Message *popMessageUp(af_Environment *env) {
  201. if (env->activity->new_vs_count == 0 || env->activity->msg_up == NULL)
  202. return NULL;
  203. af_Message *msg = env->activity->msg_up;
  204. env->activity->msg_up = msg->next;
  205. msg->next = NULL;
  206. env->activity->new_vs_count--;
  207. return msg;
  208. }
  209. /*
  210. * 函数名: getMessageData
  211. * 目标: 获取`msg`的数据, 对外API
  212. */
  213. void *getMessageData(af_Message *msg) {
  214. return msg->msg;
  215. }
  216. af_Message *popMessageDown(char *type, af_Environment *env) {
  217. for (af_Message **pmsg = &env->activity->msg_down; *pmsg != NULL; pmsg = &((*pmsg)->next)) {
  218. if (EQ_STR((*pmsg)->type, type)) {
  219. af_Message *msg = *pmsg;
  220. *pmsg = msg->next;
  221. msg->next = NULL;
  222. return msg;
  223. }
  224. }
  225. return NULL;
  226. }
  227. af_Message *getFirstMessage(af_Environment *env) {
  228. af_Message *msg = env->activity->msg_down;
  229. env->activity->msg_down = msg->next;
  230. msg->next = NULL;
  231. return msg;
  232. }
  233. void connectMessage(af_Message **base, af_Message *msg) {
  234. while (*base != NULL)
  235. base = &((*base)->next);
  236. *base = msg;
  237. }
  238. static af_EnvVar *makeEnvVar(char *name, char *data) {
  239. af_EnvVar *var = calloc(sizeof(af_EnvVar), 1);
  240. var->name = strCopy(name);
  241. var->data = strCopy(data);
  242. return var;
  243. }
  244. static af_EnvVar *freeEnvVar(af_EnvVar *var) {
  245. af_EnvVar *next = var->next;
  246. free(var->data);
  247. free(var->name);
  248. free(var);
  249. return next;
  250. }
  251. static void freeAllEnvVar(af_EnvVar *var) {
  252. while (var != NULL)
  253. var = freeEnvVar(var);
  254. }
  255. static af_EnvVarSpace *makeEnvVarSpace(void) {
  256. af_EnvVarSpace *esv = calloc(sizeof(af_EnvVarSpace), 1);
  257. return esv;
  258. }
  259. static void freeEnvVarSpace(af_EnvVarSpace *evs) {
  260. for (int i = 0; i < ENV_VAR_HASH_SIZE; i++)
  261. freeAllEnvVar(evs->var[i]);
  262. free(evs);
  263. }
  264. void setEnvVar(char *name, char *data, af_Environment *env) {
  265. time33_t index = time33(name) % ENV_VAR_HASH_SIZE;
  266. af_EnvVar **pvar = &env->esv->var[index];
  267. for (NULL; *pvar != NULL; pvar = &((*pvar)->next)) {
  268. if (EQ_STR((*pvar)->name, name)) {
  269. free((*pvar)->data);
  270. (*pvar)->data = strCopy(data);
  271. return;
  272. }
  273. }
  274. *pvar = makeEnvVar(name, data);
  275. }
  276. char *findEnvVar(char *name, af_Environment *env) {
  277. time33_t index = time33(name) % ENV_VAR_HASH_SIZE;
  278. af_EnvVar **pvar = &env->esv->var[index];
  279. for (NULL; *pvar != NULL; pvar = &((*pvar)->next)) {
  280. if (EQ_STR((*pvar)->name, name))
  281. return (*pvar)->data;
  282. }
  283. return NULL;
  284. }
  285. void mp_NORMAL(af_Message *msg, af_Environment *env) {
  286. if (msg->msg == NULL || *(af_Object **)msg->msg == NULL) {
  287. printf("msg: %p error\n", msg->msg);
  288. return;
  289. }
  290. gc_delReference(*(af_Object **)msg->msg);
  291. printf("NORMAL Point: %p\n", *(af_Object **)msg->msg);
  292. }
  293. af_Environment *makeEnvironment(void) {
  294. af_Environment *env = calloc(sizeof(af_Environment), 1);
  295. DLC_SYMBOL(TopMsgProcessFunc) func = MAKE_SYMBOL(mp_NORMAL, TopMsgProcessFunc);
  296. env->core = makeCore();
  297. env->esv = makeEnvVarSpace();
  298. addTopMsgProcess("NORMAL", func, env);
  299. FREE_SYMBOL(func);
  300. return env;
  301. }
  302. bool addTopActivity(af_Code *code, af_Environment *env) {
  303. if (env->activity != NULL)
  304. return false;
  305. env->activity = makeActivity(NULL, code, false, NULL, NULL, env->core->global, NULL);
  306. env->activity->new_vs_count = 2;
  307. env->activity->var_list = makeVarSpaceList(env->core->global->data->var_space);
  308. env->activity->var_list->next = makeVarSpaceList(env->core->protect);
  309. env->activity->status = act_normal;
  310. return true;
  311. }
  312. bool enableEnvironment(af_Environment *env) {
  313. return enableCore(env->core);
  314. }
  315. void freeEnvironment(af_Environment *env) {
  316. freeCore(env->core);
  317. freeAllActivity(env->activity);
  318. freeEnvVarSpace(env->esv);
  319. freeAllTopMsgProcess(env->process);
  320. free(env);
  321. }
  322. bool addVarToProtectVarSpace(af_Var *var, af_Environment *env) {
  323. return addVarToVarSpace(var, env->core->protect);
  324. }
  325. static af_TopMsgProcess *makeTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func) {
  326. af_TopMsgProcess *mp = calloc(sizeof(af_TopMsgProcess), 1);
  327. mp->type = strCopy(type);
  328. mp->func = COPY_SYMBOL(func, TopMsgProcessFunc);
  329. return mp;
  330. }
  331. static af_TopMsgProcess *freeTopMsgProcess(af_TopMsgProcess *mp) {
  332. af_TopMsgProcess *next = mp->next;
  333. free(mp->type);
  334. FREE_SYMBOL(mp->func);
  335. free(mp);
  336. return next;
  337. }
  338. static void freeAllTopMsgProcess(af_TopMsgProcess *mp) {
  339. while (mp != NULL)
  340. mp = freeTopMsgProcess(mp);
  341. }
  342. static void *findTopMsgProcessFunc(char *type, af_Environment *env) {
  343. af_TopMsgProcess *mp = env->process;
  344. for (NULL; mp != NULL; mp = mp->next) {
  345. if (EQ_STR(type, mp->type))
  346. return mp;
  347. }
  348. return NULL;
  349. }
  350. void addTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func,
  351. af_Environment *env) {
  352. af_TopMsgProcess *mp = makeTopMsgProcess(type, func);
  353. mp->next = env->process;
  354. env->process = mp;
  355. }
  356. bool changeTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func,
  357. af_Environment *env) {
  358. af_TopMsgProcess *mp = findTopMsgProcessFunc(type, env);
  359. if (mp == NULL)
  360. return false;
  361. FREE_SYMBOL(mp->func);
  362. mp->func = COPY_SYMBOL(func, TopMsgProcessFunc);
  363. return true;
  364. }
  365. bool pushExecutionActivity(af_Code *bt, bool return_first, af_Environment *env) {
  366. af_Code *next;
  367. if (!getCodeBlockNext(bt, &next))
  368. return false;
  369. env->activity->bt_next = next;
  370. if (next == NULL && env->activity->body == NULL) {
  371. printf("Tail tone recursive optimization\n");
  372. env->activity->bt_top = bt;
  373. env->activity->bt_start = bt->next;
  374. env->activity->bt_next = bt->next;
  375. if (!env->activity->return_first) // 若原本就有设置 return_first 则没有在设置的必要了, 因为该执行不会被返回
  376. env->activity->return_first = return_first;
  377. } else {
  378. af_Activity *activity = makeActivity(bt, bt->next, return_first, env->activity->msg_up,
  379. env->activity->var_list, env->activity->belong,
  380. env->activity->func);
  381. activity->prev = env->activity;
  382. env->activity = activity;
  383. }
  384. env->activity->status = act_normal;
  385. return true;
  386. }
  387. bool pushFuncActivity(af_Code *bt, af_Environment *env) {
  388. af_Code *next;
  389. af_Object **parentheses_call = &env->activity->parentheses_call;
  390. if (!getCodeBlockNext(bt, &next))
  391. return false;
  392. env->activity->bt_next = next;
  393. if (next == NULL && env->activity->body == NULL) {
  394. printf("Tail tone recursive optimization\n");
  395. env->activity->bt_top = bt;
  396. env->activity->bt_start = bt->next;
  397. env->activity->bt_next = bt->next;
  398. // 保持原有的return_false
  399. } else {
  400. af_Activity *activity = makeActivity(bt, bt->next, false, env->activity->msg_up,
  401. env->activity->var_list, env->activity->belong, env->activity->func);
  402. activity->prev = env->activity;
  403. env->activity = activity;
  404. }
  405. env->activity->call_type = env->activity->bt_top->block.type;
  406. if (env->activity->call_type == parentheses) { // 对于类前缀调用, 已经获得func的实际值了
  407. setFuncActivityToArg(*parentheses_call, env);
  408. gc_delReference(*parentheses_call);
  409. *parentheses_call = NULL;
  410. } else
  411. env->activity->status = act_func;
  412. return true;
  413. }
  414. bool setFuncActivityToArg(af_Object *func, af_Environment *env) {
  415. af_Object *belong = getBelongObject(func, env);
  416. gc_delReference(env->activity->belong);
  417. if (env->activity->func != NULL)
  418. gc_delReference(env->activity->func);
  419. gc_addReference(func);
  420. gc_addReference(belong);
  421. env->activity->func = func;
  422. env->activity->belong = belong;
  423. env->activity->status = act_arg;
  424. env->activity->func_var_list = env->activity->var_list; // 设置vsl [桩]
  425. env->activity->acl_start = NULL; // 设置acl [桩]
  426. env->activity->acl_next = env->activity->acl_start;
  427. env->activity->bt_next = NULL;
  428. return true;
  429. }
  430. void testFunc(af_Environment *env) { // 测试用函数
  431. printf("I am testFunc\n");
  432. }
  433. bool setFuncActivityAddVar(bool new_vsl, bool is_protect, char **msg_type, af_Environment *env) {
  434. if (!new_vsl && is_protect)
  435. return false;
  436. { // 获取FuncInfo [桩]
  437. env->activity->fi = makeFuncInfo(normal_scope, not_embedded, false, false); // 获取FuncInfo [桩]
  438. makeCodeFuncBodyToFuncInfo(env->activity->bt_start, false, NULL, env->activity->fi);
  439. DLC_SYMBOL(callFuncBody) func = MAKE_SYMBOL(testFunc, callFuncBody);
  440. makeCFuncBodyToFuncInfo(func, NULL, env->activity->fi);
  441. FREE_SYMBOL(func);
  442. }
  443. env->activity->body = env->activity->fi->body;
  444. // 桩函数: 目前func_var_list引用自var_list, 故var_list不释放 [桩]
  445. //af_VarSpaceListNode *vs = env->activity->var_list;
  446. //for (int i = env->activity->new_vs_count; i > 0; i--) {
  447. // if (vs == NULL) // 发生了错误
  448. // return false;
  449. // vs = popLastVarList(vs);
  450. //}
  451. env->activity->var_list = env->activity->func_var_list;
  452. env->activity->func_var_list = NULL;
  453. // env->activity->new_vs_count = 0; // 目前func_var_list引用自var_list, 故不设置此值 [桩]
  454. if (new_vsl) {
  455. env->activity->var_list = pushNewVarList(env->activity->var_list);
  456. env->activity->new_vs_count++;
  457. }
  458. runArgList(NULL, env->activity->var_list);
  459. env->activity->var_list->vs->is_protect = is_protect;
  460. return true;
  461. }
  462. bool setFuncActivityToNormal(bool is_first, af_Environment *env) { // 获取函数的函数体
  463. env->activity->status = act_normal;
  464. env->activity->bt_next = NULL;
  465. while (env->activity->body != NULL) {
  466. if (env->activity->body->type == func_body_c) {
  467. GET_SYMBOL(env->activity->body->c_func)(env);
  468. env->activity->body = env->activity->body->next;
  469. } else {
  470. env->activity->bt_start = env->activity->body->code;
  471. env->activity->bt_next = env->activity->body->code;
  472. env->activity->body = env->activity->body->next;
  473. return true;
  474. }
  475. }
  476. return false;
  477. }
  478. /*
  479. * 函数名: runTopMessageProcess
  480. * 目标: 运行顶层信息处理器
  481. */
  482. static void runTopMessageProcess(af_Environment *env) {
  483. af_Message **pmsg = &env->activity->msg_down;
  484. while (*pmsg != NULL) {
  485. af_TopMsgProcess *mp = findTopMsgProcessFunc((*pmsg)->type, env);
  486. if (mp != NULL) {
  487. GET_SYMBOL(mp->func)(*pmsg, env);
  488. *pmsg = freeMessage(*pmsg);
  489. } else
  490. pmsg = &((*pmsg)->next);
  491. }
  492. }
  493. void popActivity(af_Message *msg, af_Environment *env) {
  494. if (env->activity->prev != NULL) {
  495. af_Message *new_msg;
  496. if (msg != NULL) {
  497. new_msg = msg;
  498. msg->next = env->activity->msg_down;
  499. } else
  500. new_msg = env->activity->msg_down;
  501. env->activity->msg_down = NULL;
  502. connectMessage(&new_msg, env->activity->prev->msg_down);
  503. env->activity->prev->msg_down = new_msg;
  504. } else { // 到顶
  505. if (msg != NULL) {
  506. msg->next = env->activity->msg_down;
  507. env->activity->msg_down = msg;
  508. }
  509. runTopMessageProcess(env);
  510. }
  511. env->activity = freeActivity(env->activity);
  512. }