Эх сурвалжийг харах

implemented conditional expression

Andrea Gussoni 8 жил өмнө
parent
commit
dca048ff2d

+ 22 - 0
acse/Acse.y

@@ -141,6 +141,7 @@ t_io_infos *file_infos;    /* input and output files used by the compiler */
 
 %left COMMA
 %left ASSIGN
+%left IF ELSE
 %left OROR
 %left ANDAND
 %left OR_OP
@@ -567,6 +568,27 @@ exp: NUMBER      { $$ = create_expression ($1, IMMEDIATE); }
                      }
 ;
 
+   | exp IF exp ELSE exp
+     {
+       if ( $3.expression_type == IMMEDIATE)
+        $$ = $3.value ? $1 : $5;
+       else {
+        t_axe_expression zero = create_expression(0, IMMEDIATE);
+        t_axe_expression cmp = 
+            handle_binary_comparison(program, $3, zero, _EQ_);
+        t_axe_expression one = create_expression(1, IMMEDIATE);
+        t_axe_expression mask = handle_bin_numeric_op(program, cmp, one, SUB);
+        int r = getNewRegister(program);
+        gen_notb_instruction(program, r, mask.value);
+        t_axe_expression notmask = create_expression(r, REGISTER);
+        $$ = handle_bin_numeric_op(program,
+                handle_bin_numeric_op(program, $1, mask, ANDB),
+                handle_bin_numeric_op(program, $5, notmask, ANDB),
+                ORB);
+       }
+     }   
+;
+
 %%
 /*=========================================================================
                                   MAIN

+ 11 - 0
tests/conditional-expression/conditional.src

@@ -0,0 +1,11 @@
+int a;
+int b;
+int c;
+
+read(a);
+read(b);
+read(c);
+
+write (a if b > 0 else c);
+
+write(a if b else c if b if c else 50 else -100);