run.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "aFun.h"
  2. #include "run.h"
  3. #include "__env.h"
  4. #include "__object.h"
  5. #include "__var.h"
  6. #include "__gc.h"
  7. #include "__code.h"
  8. /* Code 执行函数 */
  9. static void codeVariable(af_Code *code, af_Environment *env);
  10. static void codeLiteral(af_Code *code, af_Environment *env);
  11. static void codeBlock(af_Code *code, af_Environment *env);
  12. /* 工具函数 */
  13. static bool checkInMsgType(char *type, af_Environment *env);
  14. static void popLastActivity(af_Message *msg, af_Environment *env) ;
  15. static void codeVariable(af_Code *code, af_Environment *env) {
  16. af_Var *var = findVarFromVarList(code->variable.name, env->activity->vsl);
  17. af_Message *msg;
  18. if (var != NULL) {
  19. af_Object *obj = var->vn->obj;
  20. msg = makeMessage("NORMAL", sizeof(af_Object *));
  21. *((af_Object **)msg->msg) = obj;
  22. gc_addReference(obj);
  23. printf("Get Variable %s : %p\n", code->variable.name, obj);
  24. } else {
  25. msg = makeMessage("ERROR-STR", 0);
  26. printf("Variable not found: %s\n", code->variable.name);
  27. }
  28. pushMessageDown(msg, env);
  29. env->activity->bt_next = env->activity->bt_next->next;
  30. }
  31. static void codeLiteral(af_Code *code, af_Environment *env) {
  32. af_Object *obj = makeObject("Literal", true, makeObjectAPI(), true, NULL, NULL, env);
  33. af_Message *msg = makeMessage("NORMAL", sizeof(af_Object *));
  34. *((af_Object **)msg->msg) = obj;
  35. gc_addReference(obj);
  36. pushMessageDown(msg, env);
  37. printf("Literal %s(%s) : %p\n", code->literal.func, code->literal.literal_data, obj);
  38. env->activity->bt_next = env->activity->bt_next->next;
  39. }
  40. static void codeBlock(af_Code *code, af_Environment *env) {
  41. if (code->prefix == env->core->prefix[B_EXEC] && code->block.type == parentheses) // 顺序执行, 返回尾项
  42. pushExecutionActivity(code, false, env);
  43. else if (code->prefix == env->core->prefix[B_EXEC_FIRST] && code->block.type == brackets) // 顺序执行, 返回首项
  44. pushExecutionActivity(code, true, env);
  45. else if (code->prefix == NUL) {
  46. pushFuncActivity(env->activity->bt_next, env);
  47. if (code->prefix == env->core->prefix[B_MUST_COMMON_ARG])
  48. env->activity->must_common_arg = true;
  49. else if (code->prefix == env->core->prefix[B_NOT_STRICT])
  50. env->activity->not_strict = true;
  51. } else
  52. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  53. }
  54. static bool checkInMsgType(char *type, af_Environment *env) {
  55. if (env->activity->msg_type == NULL)
  56. return false;
  57. for (char *msg_type_node = *env->activity->msg_type; msg_type_node != NULL; msg_type_node++) {
  58. if (EQ_STR(type, msg_type_node))
  59. return true;
  60. }
  61. return false;
  62. }
  63. static void popLastActivity(af_Message *msg, af_Environment *env) {
  64. do { // 如果返回一级后仍是执行完成则继续返回
  65. if (env->activity->prev == NULL)
  66. printf("top finished\n");
  67. if (env->activity->return_first) {
  68. if (msg != NULL) {
  69. gc_delReference(*(af_Object **)msg->msg);
  70. freeMessage(msg);
  71. }
  72. if (env->activity->return_obj == NULL)
  73. msg = makeMessage("ERROR-STR", 0);
  74. else {
  75. msg = makeMessage("NORMAL", sizeof(af_Object *));
  76. *(af_Object **)msg->msg = env->activity->return_obj; // env->activity->return_obj本来就有一个gc_Reference
  77. env->activity->return_obj = NULL;
  78. }
  79. }
  80. popActivity(msg, env);
  81. msg = NULL; // 随后几次执行popActivity时不需要压入新的msg
  82. } while (env->activity != NULL && env->activity->bt_next == NULL);
  83. }
  84. bool iterCode(af_Code *code, af_Environment *env) {
  85. if (!addTopActivity(code, env))
  86. return false;
  87. while (env->activity != NULL) {
  88. af_Message *msg = NULL;
  89. bool run_code = false;
  90. if (env->activity->status == act_arg && env->activity->run_in_func && env->activity->func_var_list != NULL)
  91. env->activity->vsl = env->activity->func_var_list;
  92. else
  93. env->activity->vsl = env->activity->var_list;
  94. if (env->activity->bt_next != NULL) {
  95. run_code = true;
  96. if (!env->activity->in_call) {
  97. switch (env->activity->bt_next->type) {
  98. case literal:
  99. codeLiteral(env->activity->bt_next, env);
  100. break;
  101. case variable:
  102. codeVariable(env->activity->bt_next, env);
  103. break;
  104. case block:
  105. codeBlock(env->activity->bt_next, env);
  106. continue; // 该步骤没有任何实质性运算
  107. default:
  108. break; // 错误
  109. }
  110. } else
  111. env->activity->in_call = false;
  112. if (env->activity->msg_down == NULL) // 若未获得 msg
  113. msg = makeMessage("ERROR-STR", 0);
  114. else
  115. msg = getFirstMessage(env);
  116. if (!EQ_STR(msg->type, "NORMAL")) { // 若msg为非正常值
  117. pushMessageDown(msg, env); // msg不弹出
  118. if (env->activity->status != act_normal || !checkInMsgType(msg->type, env)) { // 非normal模式, 或normal模式不匹配该msg
  119. if (env->activity->return_obj != NULL)
  120. gc_delReference(env->activity->return_obj);
  121. env->activity->return_obj = NULL;
  122. popLastActivity(NULL, env); // msg 已经 push进去了
  123. continue;
  124. }
  125. } else if (env->activity->return_first && env->activity->return_obj == NULL) { // 设置return_first
  126. env->activity->return_obj = *(af_Object **)msg->msg;
  127. gc_addReference(env->activity->return_obj);
  128. }
  129. }
  130. switch (env->activity->status) {
  131. case act_normal:
  132. if (!run_code) {
  133. msg = makeMessage("ERROR-STR", 0); // 无代码可运行
  134. popLastActivity(msg, env);
  135. } else if (env->activity->bt_next == NULL) { // 执行完成
  136. if (setFuncActivityToNormal(true, env))
  137. goto run_continue; // 继续运行
  138. else
  139. popLastActivity(msg, env);
  140. } else if (env->activity->bt_next->type == block && env->activity->bt_next->block.type == parentheses
  141. && env->activity->bt_next->prefix == NUL) { // 类前缀调用
  142. env->activity->parentheses_call = *(af_Object **)(msg->msg);
  143. freeMessage(msg);
  144. } else {
  145. run_continue:
  146. gc_delReference(*(af_Object **)(msg->msg)); // msg->msg是一个指针, 这个指针的内容是一个af_Object *
  147. freeMessage(msg);
  148. }
  149. break;
  150. case act_func: {
  151. if (!run_code) {
  152. msg = makeMessage("ERROR-STR", 0); // 无代码可运行
  153. popLastActivity(msg, env);
  154. } else {
  155. af_Object *func = *(af_Object **)(msg->msg); // func仍保留了msg的gc计数
  156. freeMessage(msg);
  157. setFuncActivityToArg(func, env); // 该函数会设定bt_next到arg计算的bt上
  158. gc_delReference(func); // 释放计数
  159. }
  160. break;
  161. }
  162. case act_arg: {
  163. if (!run_code) {
  164. act_arg_end:
  165. setFuncActivityAddVar(true, false, NULL, env);
  166. if (!setFuncActivityToNormal(true, env)) {
  167. msg = makeMessage("ERROR-STR", 0); // 无代码可运行
  168. popLastActivity(msg, env);
  169. }
  170. } else {
  171. env->activity->acl_next->result = *(af_Object **)(msg->msg);
  172. freeMessage(msg);
  173. if (env->activity->acl_next->next == NULL)
  174. goto act_arg_end; // 参数设定结束
  175. env->activity->acl_next = env->activity->acl_next->next;
  176. env->activity->bt_next = env->activity->acl_next->code;
  177. }
  178. break;
  179. }
  180. default:
  181. break;
  182. }
  183. }
  184. return true;
  185. }