|
@@ -8,16 +8,16 @@
|
|
|
#include "__code.h"
|
|
|
|
|
|
/* Code 执行函数 */
|
|
|
-static af_Code *codeVariable(af_Code *code, af_Environment *env);
|
|
|
-static af_Code *codeLiteral(af_Code *code, af_Environment *env);
|
|
|
-static void codeBlock(af_Code *bt, af_Environment *env);
|
|
|
+static void codeVariable(af_Code *code, af_Environment *env);
|
|
|
+static void codeLiteral(af_Code *code, af_Environment *env);
|
|
|
+static void codeBlock(af_Code *code, af_Environment *env);
|
|
|
|
|
|
/* 工具函数 */
|
|
|
static bool checkInMsgType(char *type, af_Environment *env);
|
|
|
static void popLastActivity(af_Message *msg, af_Environment *env) ;
|
|
|
|
|
|
-static af_Code *codeVariable(af_Code *code, af_Environment *env) {
|
|
|
- af_Var *var = findVarFromVarList(code->variable.name, env->activity->var_list);
|
|
|
+static void codeVariable(af_Code *code, af_Environment *env) {
|
|
|
+ af_Var *var = findVarFromVarList(code->variable.name, env->activity->vsl);
|
|
|
af_Message *msg;
|
|
|
|
|
|
if (var != NULL) {
|
|
@@ -32,32 +32,34 @@ static af_Code *codeVariable(af_Code *code, af_Environment *env) {
|
|
|
}
|
|
|
|
|
|
pushMessageDown(msg, env);
|
|
|
- return code->next;
|
|
|
+ env->activity->bt_next = env->activity->bt_next->next;
|
|
|
}
|
|
|
|
|
|
-static af_Code *codeLiteral(af_Code *code, af_Environment *env) {
|
|
|
- af_Message *msg;
|
|
|
+static void codeLiteral(af_Code *code, af_Environment *env) {
|
|
|
af_Object *obj = makeObject("Literal", 0, true, true, NULL, NULL, env);
|
|
|
- printf("Literal %s(%s) : %p\n", code->literal.func, code->literal.literal_data, obj);
|
|
|
- msg = makeMessage("NORMAL", sizeof(af_Object *));
|
|
|
+ af_Message *msg = makeMessage("NORMAL", sizeof(af_Object *));
|
|
|
*((af_Object **)msg->msg) = obj;
|
|
|
gc_addReference(obj);
|
|
|
pushMessageDown(msg, env);
|
|
|
- return code->next;
|
|
|
+
|
|
|
+ printf("Literal %s(%s) : %p\n", code->literal.func, code->literal.literal_data, obj);
|
|
|
+ env->activity->bt_next = env->activity->bt_next->next;
|
|
|
}
|
|
|
|
|
|
-static void codeBlock(af_Code *bt, af_Environment *env) {
|
|
|
- if (bt->prefix == env->core->prefix[B_EXEC] && bt->block.type == parentheses) // 顺序执行, 返回尾项
|
|
|
- pushExecutionActivity(bt, false, env);
|
|
|
- else if (bt->prefix == env->core->prefix[B_EXEC_FIRST] && bt->block.type == brackets) // 顺序执行, 返回首项
|
|
|
- pushExecutionActivity(bt, true, env);
|
|
|
- else {
|
|
|
+static void codeBlock(af_Code *code, af_Environment *env) {
|
|
|
+ if (code->prefix == env->core->prefix[B_EXEC] && code->block.type == parentheses) // 顺序执行, 返回尾项
|
|
|
+ pushExecutionActivity(code, false, env);
|
|
|
+ else if (code->prefix == env->core->prefix[B_EXEC_FIRST] && code->block.type == brackets) // 顺序执行, 返回首项
|
|
|
+ pushExecutionActivity(code, true, env);
|
|
|
+ else if (code->prefix == NUL) {
|
|
|
pushFuncActivity(env->activity->bt_next, env);
|
|
|
- if (bt->prefix == env->core->prefix[B_MUST_COMMON_ARG])
|
|
|
+ if (code->prefix == env->core->prefix[B_MUST_COMMON_ARG])
|
|
|
env->activity->must_common_arg = true;
|
|
|
- else if (bt->prefix == env->core->prefix[B_NOT_STRICT])
|
|
|
+ else if (code->prefix == env->core->prefix[B_NOT_STRICT])
|
|
|
env->activity->not_strict = true;
|
|
|
- }
|
|
|
+ } else
|
|
|
+ pushMessageDown(makeMessage("ERROR-STR", 0), env);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
static bool checkInMsgType(char *type, af_Environment *env) {
|
|
@@ -95,67 +97,102 @@ static void popLastActivity(af_Message *msg, af_Environment *env) {
|
|
|
|
|
|
bool iterCode(af_Code *code, af_Environment *env) {
|
|
|
if (!addTopActivity(code, env))
|
|
|
- return false;
|
|
|
+ return false;
|
|
|
|
|
|
while (env->activity != NULL) {
|
|
|
- af_Message *msg;
|
|
|
- switch (env->activity->bt_next->type) {
|
|
|
- case literal:
|
|
|
- env->activity->bt_next = codeLiteral(env->activity->bt_next, env);
|
|
|
- break;
|
|
|
- case variable:
|
|
|
- env->activity->bt_next = codeVariable(env->activity->bt_next, env);
|
|
|
- break;
|
|
|
- case block:
|
|
|
- codeBlock(env->activity->bt_next, env);
|
|
|
- continue; // 该步骤没有任何实质性运算
|
|
|
- default:
|
|
|
- break; // 错误
|
|
|
- }
|
|
|
-
|
|
|
- if (env->activity->msg_down == NULL) {
|
|
|
- msg = makeMessage("ERROR-STR", 0);
|
|
|
- } else
|
|
|
- msg = getFirstMessage(env);
|
|
|
+ af_Message *msg = NULL;
|
|
|
+ bool run_code = false;
|
|
|
+
|
|
|
+ if (env->activity->status == act_arg && env->activity->run_in_func && env->activity->func_var_list != NULL)
|
|
|
+ env->activity->vsl = env->activity->func_var_list;
|
|
|
+ else
|
|
|
+ env->activity->vsl = env->activity->var_list;
|
|
|
+
|
|
|
+ if (env->activity->bt_next != NULL) {
|
|
|
+ run_code = true;
|
|
|
+ switch (env->activity->bt_next->type) {
|
|
|
+ case literal:
|
|
|
+ codeLiteral(env->activity->bt_next, env);
|
|
|
+ break;
|
|
|
+ case variable:
|
|
|
+ codeVariable(env->activity->bt_next, env);
|
|
|
+ break;
|
|
|
+ case block:
|
|
|
+ codeBlock(env->activity->bt_next, env);
|
|
|
+ continue; // 该步骤没有任何实质性运算
|
|
|
+ default:
|
|
|
+ break; // 错误
|
|
|
+ }
|
|
|
|
|
|
- if (!EQ_STR(msg->type, "NORMAL")) {
|
|
|
- pushMessageDown(msg, env);
|
|
|
- if (env->activity->status != act_normal || !checkInMsgType(msg->type, env)) {
|
|
|
- if (env->activity->return_obj != NULL)
|
|
|
- gc_delReference(env->activity->return_obj);
|
|
|
- env->activity->return_obj = NULL;
|
|
|
- popLastActivity(NULL, env); // msg 已经 push进去了
|
|
|
- continue;
|
|
|
- } else {
|
|
|
- env->activity->bt_next = NULL;
|
|
|
- // TODO-szh 切换函数
|
|
|
+ if (env->activity->msg_down == NULL) // 若未获得 msg
|
|
|
+ msg = makeMessage("ERROR-STR", 0);
|
|
|
+ else
|
|
|
+ msg = getFirstMessage(env);
|
|
|
+
|
|
|
+ if (!EQ_STR(msg->type, "NORMAL")) { // 若msg为非正常值
|
|
|
+ pushMessageDown(msg, env); // msg不弹出
|
|
|
+ if (env->activity->status != act_normal || !checkInMsgType(msg->type, env)) { // 非normal模式, 或normal模式不匹配该msg
|
|
|
+ if (env->activity->return_obj != NULL)
|
|
|
+ gc_delReference(env->activity->return_obj);
|
|
|
+ env->activity->return_obj = NULL;
|
|
|
+ popLastActivity(NULL, env); // msg 已经 push进去了
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ } else if (env->activity->return_first && env->activity->return_obj == NULL) { // 设置return_first
|
|
|
+ env->activity->return_obj = *(af_Object **)msg->msg;
|
|
|
+ gc_addReference(env->activity->return_obj);
|
|
|
}
|
|
|
- } else if (env->activity->return_first && env->activity->return_obj == NULL) {
|
|
|
- env->activity->return_obj = *(af_Object **)msg->msg;
|
|
|
- gc_addReference(env->activity->return_obj);
|
|
|
}
|
|
|
|
|
|
switch (env->activity->status) {
|
|
|
case act_normal:
|
|
|
- if (env->activity->bt_next == NULL) { // 执行完成
|
|
|
+ if (!run_code) {
|
|
|
+ msg = makeMessage("ERROR-STR", 0); // 无代码可运行
|
|
|
popLastActivity(msg, env);
|
|
|
+ } else if (env->activity->bt_next == NULL) { // 执行完成
|
|
|
+ if (setFuncActivityToNormal(true, env))
|
|
|
+ goto run_continue; // 继续运行
|
|
|
+ else
|
|
|
+ popLastActivity(msg, env);
|
|
|
+ } else if (env->activity->bt_next->type == block && env->activity->bt_next->block.type == parentheses
|
|
|
+ && env->activity->bt_next->prefix == NUL) { // 类前缀调用
|
|
|
+ env->activity->parentheses_call = *(af_Object **)(msg->msg);
|
|
|
+ freeMessage(msg);
|
|
|
} else {
|
|
|
+ run_continue:
|
|
|
gc_delReference(*(af_Object **)(msg->msg)); // msg->msg是一个指针, 这个指针的内容是一个af_Object *
|
|
|
freeMessage(msg);
|
|
|
}
|
|
|
break;
|
|
|
case act_func: {
|
|
|
- af_Object *func = *(af_Object **)(msg->msg); // func仍保留了msg的gc计数
|
|
|
- freeMessage(msg);
|
|
|
- setFuncActivityToArg(func, env); // 该函数会设定bt_next到arg计算的bt上
|
|
|
- gc_delReference(func); // 释放计数
|
|
|
+ if (!run_code) {
|
|
|
+ msg = makeMessage("ERROR-STR", 0); // 无代码可运行
|
|
|
+ popLastActivity(msg, env);
|
|
|
+ } else {
|
|
|
+ af_Object *func = *(af_Object **)(msg->msg); // func仍保留了msg的gc计数
|
|
|
+ freeMessage(msg);
|
|
|
+ setFuncActivityToArg(func, env); // 该函数会设定bt_next到arg计算的bt上
|
|
|
+ gc_delReference(func); // 释放计数
|
|
|
+ }
|
|
|
break;
|
|
|
}
|
|
|
- case act_arg: { // TODO-szh 暂时跳过参数设定
|
|
|
- setFuncActivityAddVar(NULL, true, false, NULL, env);
|
|
|
- setFuncActivityToNormal(env->activity->bt_next, env);
|
|
|
- gc_delReference(*(af_Object **)(msg->msg)); // 释放计数
|
|
|
- freeMessage(msg);
|
|
|
+ case act_arg: {
|
|
|
+ if (!run_code) {
|
|
|
+ act_arg_end:
|
|
|
+ setFuncActivityAddVar(true, false, NULL, env);
|
|
|
+ if (!setFuncActivityToNormal(true, env)) {
|
|
|
+ msg = makeMessage("ERROR-STR", 0); // 无代码可运行
|
|
|
+ popLastActivity(msg, env);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ env->activity->acl_next->result = *(af_Object **)(msg->msg);
|
|
|
+ freeMessage(msg);
|
|
|
+ if (env->activity->acl_next->next == NULL)
|
|
|
+ goto act_arg_end; // 参数设定结束
|
|
|
+
|
|
|
+ env->activity->acl_next = env->activity->acl_next->next;
|
|
|
+ env->activity->bt_next = env->activity->acl_next->code;
|
|
|
+ }
|
|
|
break;
|
|
|
}
|
|
|
default:
|
|
@@ -163,5 +200,6 @@ bool iterCode(af_Code *code, af_Environment *env) {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
return true;
|
|
|
}
|