Przeglądaj źródła

slice使用a[:2:]的用法

SongZihuan 5 lat temu
rodzic
commit
02ee9f9e1f
4 zmienionych plików z 34 dodań i 7 usunięć
  1. 1 0
      README.md
  2. 10 4
      inter/cfunc.c
  3. 16 0
      inter/interpreter.c
  4. 7 3
      paser/syntax.c

+ 1 - 0
README.md

@@ -77,5 +77,6 @@ return x n  函数返回值x,返回n层
 * 调整了运算优先级,修改了无法读取``5 ** -2``而可以读取``5 ** (-2)``的bug
 * while和for循环新增else语句
 * try...except增加了else和finally语句,并且允许``except``,``except as``,``else``,``finally``为可选语句
+* 实现了slice赋None值(本质是依靠了虚解包)
 ## 关于GWARF
 最后更新时间 : 2020年05月08日 广州

+ 10 - 4
inter/cfunc.c

@@ -134,6 +134,7 @@ GWARF_result get_object(parameter *tmp_s, char *name, var_list *the_var, inter *
 
 
 GWARF_result to_error(char *error_info, char *error_type, inter *global_inter){  // 把GWARF_value封装成error
+    printf("%s(%s)\n", error_type, error_info);
     GWARF_result func_result, return_result = GWARF_result_reset;
     GWARF_value tmp_value = GWARF_value_reset;
     var_list *the_var = make_var_base(global_inter->global_var);
@@ -1884,12 +1885,17 @@ GWARF_result tuple_official_func(func *the_func, parameter *tmp_s, var_list *the
                     GWARF_result tmp = get__value__(&(end_result.value), the_var, global_inter);
                     error_space(tmp, return_result, return_value);
                     
-                    tmp = to_int(tmp.value, the_var, global_inter);
-                    error_space(tmp, return_result, return_value);
-                    
-                    end = tmp.value.value.int_value;
+                    if(tmp.value.type != NULL_value){
+                        tmp = to_int(tmp.value, the_var, global_inter);
+                        error_space(tmp, return_result, return_value);
+                        end = tmp.value.value.int_value;
+                    }
+                    else{
+                        goto not_last;
+                    }
                 }
                 else{
+                    not_last:
                     end = len;
                 }
                 if(len < start || len < end || end < start){

+ 16 - 0
inter/interpreter.c

@@ -477,7 +477,9 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
                 if(tmp != NULL){
                     get.value = tmp->value;
                     get.father = &base_the_var;  // 设置father
+                    puts("case down");
                     return_value = call_back_core(get, the_var, (the_statement->code).down.child_var, global_inter);
+                    printf("return_value.u = %d\n", return_value.u);
                 }
                 else{
                     return_value = to_error("Don't Support Down Number", "TypeException", global_inter);
@@ -2404,6 +2406,7 @@ GWARF_result call_back_core(GWARF_result get, var_list *the_var, parameter *tmp_
 
         if(func_->type == customize){  // 用户定义的方法
             // 赋值self
+            puts("WWSSSSS");
             GWARF_result father = GWARF_result_reset;
             if(func_->is_class == action){
                 if(get.father != NULL && get.father->type == OBJECT_value){
@@ -2443,6 +2446,19 @@ GWARF_result call_back_core(GWARF_result get, var_list *the_var, parameter *tmp_
                 out:
                 ;
             }
+            puts("WFWFSSSSSSSSSSSSSSSSSSSSSs");
+            if(tmp_s->next == NULL){
+                puts("tmp_s->next == NULL");
+            }
+            else{
+                puts("tmp_s->next != NULL");
+            }
+            if(tmp_x->next == NULL){
+                puts("tmp_x->next == NULL");
+            }
+            else{
+                puts("tmp_x->next != NULL");
+            }
             GWARF_result tmp_return = login_var(the_var, old_var_list, tmp_x, tmp_s, global_inter);
             if(tmp_return.u != statement_end){
                 the_var = free_var_list(the_var);  // free the new var

+ 7 - 3
paser/syntax.c

@@ -637,7 +637,7 @@ void formal_parameter(p_status *status, token_node *list){  // 因试分解
             new_status.not_match_tuple = true;
             token tmp_next;
             get_right_token(&new_status, list, top_exp, tmp_next);
-            if(tmp_next.type != NON_top_exp){  // 结尾分号 -> 虚解包
+            if(tmp_next.type != NON_top_exp){  // 结尾分号 -> 虚解包,或者slice填充None值
                 if(!status->is_args){
                     back_again(list, tmp_next);
                     next.type = NON_top_exp;
@@ -2638,7 +2638,10 @@ void call_down(p_status *status, token_node *list){  // 因试分解
             if(rb_t.type == RI_PASER || rb_t.type == COMMA_PASER){  // a[1,2,3,4]模式
                 back_one_token(list, parameter_t);
                 back_again(list, rb_t);
-                get_base_token(status,list,formal_parameter,parameter_t);
+                p_status new_status;
+                new_status = *status;
+                reset_status(new_status);
+                get_base_token(&new_status,list,formal_parameter,parameter_t);
                 if(parameter_t.type != NON_parameter){
                     paser_error("Don't get formal_parameter");
                 }
@@ -2648,11 +2651,12 @@ void call_down(p_status *status, token_node *list){  // 因试分解
                 code_tmp->code.down.base_var = left.data.statement_value;
                 code_tmp->code.down.child_var = p_list;
             }
-            else if(rb_t.type == COLON_PASER){  // a[1,2,3,4]模式
+            else if(rb_t.type == COLON_PASER){  // a[1:2:3:4]模式
                 back_one_token(list, parameter_t);
                 back_again(list, rb_t);
                 p_status new_status;
                 new_status = *status;
+                reset_status(new_status);  // 避免正在dict和list模式中
                 new_status.is_slice = true;
                 get_base_token(&new_status,list,formal_parameter,parameter_t);
                 if(parameter_t.type != NON_parameter){