瀏覽代碼

fix: 字典的key忽略变量

当字典的key为变量时, 对变量求值
SongZihuan 4 年之前
父節點
當前提交
ab28478cd4
共有 5 個文件被更改,包括 27 次插入16 次删除
  1. 2 2
      include/parameter.h
  2. 0 2
      main.c
  3. 20 6
      src/parameter.c
  4. 2 2
      src/runcall.c
  5. 3 4
      src/runoperation.c

+ 2 - 2
include/parameter.h

@@ -65,8 +65,8 @@ Argument *dictToArgument(LinkValue *dict_value, INTER_FUNCTIONSIG_CORE);
 
 ResultType setParameterCore(Argument *call, Parameter *function_base, VarList *function_var, INTER_FUNCTIONSIG_NOT_ST);
 ResultType setParameter(Parameter *call_base, Parameter *function_base, VarList *function_var, LinkValue *function_father, INTER_FUNCTIONSIG_NOT_ST);
-ResultType iterParameter(Parameter *call, Argument **base_ad, INTER_FUNCTIONSIG_NOT_ST);
-Argument *getArgument(Parameter *call, INTER_FUNCTIONSIG_NOT_ST);
+ResultType iterParameter(Parameter *call, Argument **base_ad, bool is_dict, INTER_FUNCTIONSIG_NOT_ST);
+Argument * getArgument(Parameter *call, bool is_dict, INTER_FUNCTIONSIG_NOT_ST);
 
 ResultType defaultParameter(Parameter **function_ad, NUMBER_TYPE *num, INTER_FUNCTIONSIG_NOT_ST);
 ResultType argumentToVar(Argument **call_ad, NUMBER_TYPE *num, INTER_FUNCTIONSIG_NOT_ST);

+ 0 - 2
main.c

@@ -19,12 +19,10 @@ int main(int argc, char *argv[]) {
 
 
 /**
- * TODO-szh 类封装
  * TODO-szh 类super语句
  * TODO-szh const声明
  * TODO-szh import语句
  * TODO-szh 生成语法树
  * TODO-szh 取反符号 -
  * TODO-szh 字面量后缀
- * TODO-szh dict设置: 忽略变量、设置权限、设置father
  */

+ 20 - 6
src/parameter.c

@@ -378,7 +378,7 @@ ResultType argumentToParameter(Argument **call_ad, Parameter **function_ad, VarL
  * @param var_list
  * @return
  */
-ResultType iterParameter(Parameter *call, Argument **base_ad, INTER_FUNCTIONSIG_NOT_ST){
+ResultType iterParameter(Parameter *call, Argument **base_ad, bool is_dict, INTER_FUNCTIONSIG_NOT_ST){
     Argument *base = *base_ad;
     setResultCore(result);
 
@@ -388,8 +388,22 @@ ResultType iterParameter(Parameter *call, Argument **base_ad, INTER_FUNCTIONSIG_
 
         if (call->type == value_par)
             base = connectValueArgument(result->value, base);
-        else if (call->type == name_par)
-            base = connectStatementNameArgument(result->value, call->data.name, base);
+        else if (call->type == name_par){
+            if (is_dict){
+                LinkValue *value = result->value;
+                setResultCore(result);
+                if(operationSafeInterStatement(CALL_INTER_FUNCTIONSIG(call->data.name, var_list, result, father))) {
+                    gcFreeTmpLink(&value->gc_status);
+                    goto return_;
+                }
+                char *name_str = getNameFromValue(result->value->value, CALL_INTER_FUNCTIONSIG_CORE(var_list));
+                base = connectCharNameArgument(value, result->value, name_str, base);
+                memFree(name_str);
+                gcFreeTmpLink(&value->gc_status);
+            }
+            else
+                base = connectStatementNameArgument(result->value, call->data.name, base);
+        }
         else if (call->type == args_par){
             Argument *tmp_at = listToArgument(result->value, CALL_INTER_FUNCTIONSIG_CORE(var_list));
             base = connectArgument(tmp_at, base);
@@ -407,11 +421,11 @@ ResultType iterParameter(Parameter *call, Argument **base_ad, INTER_FUNCTIONSIG_
     return result->type;
 }
 
-Argument *getArgument(Parameter *call, INTER_FUNCTIONSIG_NOT_ST){
+Argument * getArgument(Parameter *call, bool is_dict, INTER_FUNCTIONSIG_NOT_ST) {
     Argument *new_arg = NULL;
     freeResult(result);
 
-    iterParameter(call, &new_arg, CALL_INTER_FUNCTIONSIG_NOT_ST (var_list, result, father));
+    iterParameter(call, &new_arg, is_dict, CALL_INTER_FUNCTIONSIG_NOT_ST (var_list, result, father));
     return new_arg;
 }
 
@@ -434,7 +448,7 @@ Argument *getArgument(Parameter *call, INTER_FUNCTIONSIG_NOT_ST){
 ResultType setParameter(Parameter *call_base, Parameter *function_base, VarList *function_var, LinkValue *function_father, INTER_FUNCTIONSIG_NOT_ST) {
     Argument *call = NULL;
     setResultCore(result);
-    call = getArgument(call_base, CALL_INTER_FUNCTIONSIG_NOT_ST (var_list, result, father));
+    call = getArgument(call_base, false, CALL_INTER_FUNCTIONSIG_NOT_ST (var_list, result, father));
     if (!run_continue(result)) {
         freeArgument(call, false);
         return result->type;

+ 2 - 2
src/runcall.c

@@ -7,7 +7,7 @@ ResultType setClass(INTER_FUNCTIONSIG) {
     VarList *father_var = NULL;
     setResultCore(result);
 
-    call = getArgument(st->u.set_class.father, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, father));
+    call = getArgument(st->u.set_class.father, false, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, father));
     if (!run_continue(result))
         goto error_;
 
@@ -122,7 +122,7 @@ ResultType callFunction(LinkValue *function_value, Parameter *parameter, INTER_F
     gcAddTmp(&function_var->hashtable->gc_status);
     runFREEZE(inter, var_list, function_var, true);
 
-    setParameter(parameter, function_value->value->data.function.pt, function_var, function_value, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, father));  // TODO-szh 设置双father
+    setParameter(parameter, function_value->value->data.function.pt, function_var, function_value, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, father));
     if (!run_continue(result)) {
         gcAddTmp(&function_var->hashtable->gc_status);
         runFREEZE(inter, var_list, function_var, false);

+ 3 - 4
src/runoperation.c

@@ -204,7 +204,7 @@ ResultType assCore(Statement *name, LinkValue *value, INTER_FUNCTIONSIG_NOT_ST){
         Statement *tmp_st = makeBaseLinkValueStatement(value, name->line, name->code_file);
 
         pt = makeArgsParameter(tmp_st);
-        call = getArgument(pt, CALL_INTER_FUNCTIONSIG_NOT_ST (var_list, result, father));
+        call = getArgument(pt, false, CALL_INTER_FUNCTIONSIG_NOT_ST (var_list, result, father));
         if (!run_continue(result)) {
             freeArgument(call, false);
             freeParameter(pt, true);
@@ -327,7 +327,7 @@ ResultType getList(INTER_FUNCTIONSIG) {
     Argument *at_tmp = NULL;
 
     freeResult(result);
-    at = getArgument(st->u.base_list.list, CALL_INTER_FUNCTIONSIG_NOT_ST (var_list, result, father));
+    at = getArgument(st->u.base_list.list, false, CALL_INTER_FUNCTIONSIG_NOT_ST (var_list, result, father));
     at_tmp = at;
     if (!run_continue(result)){
         freeArgument(at_tmp, true);
@@ -341,13 +341,12 @@ ResultType getList(INTER_FUNCTIONSIG) {
     return result->type;
 }
 
-// TODO-szh 设置字典key为变量时直接取值
 ResultType getDict(INTER_FUNCTIONSIG) {
     Argument *at = NULL;
     Argument *at_tmp = NULL;
 
     freeResult(result);
-    at = getArgument(st->u.base_dict.dict, CALL_INTER_FUNCTIONSIG_NOT_ST (var_list, result, father));
+    at = getArgument(st->u.base_dict.dict, true, CALL_INTER_FUNCTIONSIG_NOT_ST (var_list, result, father));
     at_tmp = at;
     if (!run_continue(result)){
         freeArgument(at_tmp, false);