فهرست منبع

双目运算符xxx if yyy else zzz

SongZihuan 5 سال پیش
والد
کامیت
842899ef47
5فایلهای تغییر یافته به همراه401 افزوده شده و 351 حذف شده
  1. BIN
      gwarf
  2. 21 0
      inter/interpreter.c
  3. 7 0
      inter/interpreter.h
  4. 19 4
      paser/gwarf_yacc.y
  5. 354 347
      paser/y.tab.c

+ 21 - 0
inter/interpreter.c

@@ -97,6 +97,27 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
             }
             puts("----stop code----");
             break;
+        case chose_exp:{
+            GWARF_result tmp_result = traverse(the_statement->code.chose_exp.condition, the_var, false);
+            if(is_error(&tmp_result)){  // Name Error错误
+                return_value = tmp_result;
+                break;
+            }
+            else if(is_space(&tmp_result)){
+                return_value = tmp_result;
+                break;
+            }
+            if(to_bool(tmp_result.value)){
+                return_value = traverse(the_statement->code.chose_exp.true_do, the_var, false);
+            }
+            else{
+                return_value = traverse(the_statement->code.chose_exp.false_do, the_var, false);
+            }
+            if(!(is_error(&return_value) || is_space(&return_value))){  // Name Error错误
+                return_value.value = to_object(return_value.value, the_var);
+            }
+            break;
+        }
         case call:
             return_value = call_back(the_statement, the_var);
             break;

+ 7 - 0
inter/interpreter.h

@@ -108,6 +108,7 @@ typedef struct statement{
         include_import,  // include file
         for_in_cycle,  // for i in a
         assert_e,
+        chose_exp,
     } type;  // the statement type
 
     union
@@ -156,6 +157,12 @@ typedef struct statement{
             struct statement *left_exp;  // the left exp
         } operation;
 
+        struct{
+            struct statement *condition;  // xxx if yyy else zzz -> xxx
+            struct statement *true_do;  // xxx if yyy else zzz -> yyy
+            struct statement *false_do;  // xxx if yyy else zzz -> zzz
+        } chose_exp;
+
         struct{
             struct statement *condition;  // when to while 
             struct statement *done;  // while to do

+ 19 - 4
paser/gwarf_yacc.y

@@ -27,7 +27,7 @@
 
 %type <statement_value> base_value base_var_token base_var_ element second_number first_number zero_number top_exp command third_number while_block while_exp break_exp if_block if_exp broken_exp break_token broken_token continue_token continue_exp
 %type <statement_value> continued_exp continued_token restart_exp restart_token restarted_exp restarted_token default_token for_exp for_block global_token nonlocal_token block_exp block_block call_number def_block def_exp return_exp return_token
-%type <statement_value> eq_number class_block class_exp slice_arguments_token try_block try_exp try_token raise_exp import_exp include_exp bool_number bit_number bit_move do_while_block lambda_exp
+%type <statement_value> eq_number class_block class_exp slice_arguments_token try_block try_exp try_token raise_exp import_exp include_exp bool_number bit_number bit_move do_while_block lambda_exp chose_exp
 
 %type <parameter_list> formal_parameter arguments slice_arguments
 
@@ -279,8 +279,8 @@ eq_number
     ;
 
 lambda_exp
-    : bit_number
-    | LB RB LAMBDA bit_number
+    : chose_exp
+    | LB RB LAMBDA chose_exp
     {
         statement *lambda_tmp =  make_statement();
         lambda_tmp->type = lambda_func;
@@ -290,7 +290,7 @@ lambda_exp
 
         $$ = lambda_tmp;
     }
-    | LB formal_parameter RB LAMBDA bit_number
+    | LB formal_parameter RB LAMBDA chose_exp
     {
         statement *lambda_tmp =  make_statement();
         lambda_tmp->type = lambda_func;
@@ -302,6 +302,21 @@ lambda_exp
     }
     ;
 
+chose_exp
+    : bit_number
+    | chose_exp IF chose_exp ELSE chose_exp
+    {
+        statement *chose_tmp =  make_statement();
+        chose_tmp->type = chose_exp;
+
+        chose_tmp->code.chose_exp.condition = $3;
+        chose_tmp->code.chose_exp.true_do = $1;
+        chose_tmp->code.chose_exp.false_do = $5;
+
+        $$ = chose_tmp;
+    }
+    ;
+
 bit_number
     : bit_move
     | bit_number BITAND bit_move

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 354 - 347
paser/y.tab.c


برخی فایل ها در این مقایسه diff نمایش داده نمی شوند زیرا تعداد فایل ها بسیار زیاد است