Parcourir la source

initial assignement if statement

Andrea Gussoni il y a 8 ans
Parent
commit
220d809c58
2 fichiers modifiés avec 38 ajouts et 1 suppressions
  1. 1 0
      acse/Acse.lex
  2. 37 1
      acse/Acse.y

+ 1 - 0
acse/Acse.lex

@@ -93,6 +93,7 @@ ID       [a-zA-Z_][a-zA-Z0-9_]*
 "else"            { return ELSE; }
 "for"             { return FOR; }
 "if"              { return IF; }
+"iif"             { return IIF; }
 "int"             { yylval.intval = INTEGER_TYPE; return TYPE; }
 "while"           { return WHILE; }
 "return"          { return RETURN; }

+ 37 - 1
acse/Acse.y

@@ -91,7 +91,7 @@ t_io_infos *file_infos;    /* input and output files used by the compiler */
 
 %}
 
-%expect 1
+%expect 2
 
 /*=========================================================================
                           SEMANTIC RECORDS
@@ -125,6 +125,7 @@ t_io_infos *file_infos;    /* input and output files used by the compiler */
 %token <label> DO
 %token <while_stmt> WHILE
 %token <label> IF
+%token <label> IIF
 %token <label> ELSE
 %token <intval> TYPE
 %token <svalue> IDENTIFIER
@@ -134,6 +135,7 @@ t_io_infos *file_infos;    /* input and output files used by the compiler */
 %type <decl> declaration
 %type <list> declaration_list
 %type <label> if_stmt
+%type <label> init_if_stmt
 
 /*=========================================================================
                           OPERATOR PRECEDENCES
@@ -247,6 +249,7 @@ statement   : assign_statement SEMI      { /* does nothing */ }
 ;
 
 control_statement : if_statement         { /* does nothing */ }
+            | init_if_statement          { /* does nothing */ }
             | while_statement            { /* does nothing */ }
             | do_while_statement SEMI    { /* does nothing */ }
             | return_statement SEMI      { /* does nothing */ }
@@ -303,6 +306,22 @@ assign_statement : IDENTIFIER LSQUARE exp RSQUARE ASSIGN exp
                free($1);
             }
 ;
+
+init_if_statement   :   init_if_stmt
+                    { 
+                        assignLabel(program, $1);
+                    }
+                    | init_if_stmt ELSE
+                    {
+                        $2 = newLabel(program);
+                        gen_bt_instruction(program, $2, 0);
+                        assignLabel(program, $1);
+                    }
+                    code_block
+                    {
+                        assignLabel(program, $2);
+                    }
+;
             
 if_statement   : if_stmt
                {
@@ -348,6 +367,23 @@ if_stmt  :  IF
                code_block { $$ = $1; }
 ;
 
+init_if_stmt    :   IIF
+                    {
+                        $1 = newLabel(program);
+                    }
+                    LPAR assign_statement SEMI exp RPAR
+                    {
+                    if ($6.expression_type == IMMEDIATE)
+                        gen_load_immediate(program, $6.value);
+                    else
+                        gen_andb_instruction(program, $6.value, $6.value,
+                            $6.value, CG_DIRECT_ALL);
+                    gen_beq_instruction(program, $1, 0);
+                    }
+                    code_block
+                    { $$ = $1; }
+;
+
 while_statement  : WHILE
                   {
                      /* initialize the value of the non-terminal */