stage4/generate_c/generate_c_st.cc
changeset 1038 036f15e4041d
parent 1026 fe580c0c1911
child 1049 4d7183013481
equal deleted inserted replaced
1037:bf39078476e4 1038:036f15e4041d
  1231 
  1231 
  1232 /********************************/
  1232 /********************************/
  1233 /* B 3.2.4 Iteration Statements */
  1233 /* B 3.2.4 Iteration Statements */
  1234 /********************************/
  1234 /********************************/
  1235 void *visit(for_statement_c *symbol) {
  1235 void *visit(for_statement_c *symbol) {
  1236   s4o.print("for(");
  1236   /* Due to the way the GET/SET_GLOBAL accessor macros access VAR_GLOBAL variables,
  1237   symbol->control_variable->accept(*this);
  1237    * these varibles cannot be used within a C for(;;) loop.
  1238   s4o.print(" = ");
  1238    * We must therefore implemnt the FOR END_FOR loop as a C while() loop
  1239   symbol->beg_expression->accept(*this);
  1239    */
  1240   s4o.print("; ");
  1240   s4o.print("/* FOR ... */\n" + s4o.indent_spaces);
       
  1241   /* For the initialization part, we create an assignment_statement_c   */
       
  1242   /* and have this visitor visit it!                                    */ 
       
  1243   assignment_statement_c ini_assignment(symbol->control_variable, symbol->beg_expression);
       
  1244   ini_assignment.accept(*this);
       
  1245   //symbol->control_variable->accept(*this);  // this does not work for VAR_GLOBAL variables
       
  1246   //s4o.print(" = ");
       
  1247   //symbol->beg_expression->accept(*this);
       
  1248   
       
  1249   /* comparison // check for end of loop */
       
  1250   s4o.print(";\n" + s4o.indent_spaces + "while( ");
  1241   if (symbol->by_expression == NULL) {
  1251   if (symbol->by_expression == NULL) {
  1242     /* increment by 1 */
  1252     /* increment by 1 */    
  1243     symbol->control_variable->accept(*this);
  1253     symbol->control_variable->accept(*this);
  1244     s4o.print(" <= ");
  1254     s4o.print(" <= ");
  1245     symbol->end_expression->accept(*this);
  1255     symbol->end_expression->accept(*this);
  1246     s4o.print("; ");
       
  1247     symbol->control_variable->accept(*this);
       
  1248     s4o.print("++");
       
  1249   } else {
  1256   } else {
  1250     /* increment by user defined value  */
  1257     /* increment by user defined value  */
  1251     /* The user defined increment value may be negative, in which case
  1258     /* The user defined increment value may be negative, in which case
  1252      * the expression to determine whether we have reached the end of the loop
  1259      * the expression to determine whether we have reached the end of the loop
  1253      * changes from a '<=' to a '>='.
  1260      * changes from a '<=' to a '>='.
  1263     symbol->end_expression->accept(*this);
  1270     symbol->end_expression->accept(*this);
  1264     s4o.print(")) : (");
  1271     s4o.print(")) : (");
  1265     symbol->control_variable->accept(*this);
  1272     symbol->control_variable->accept(*this);
  1266     s4o.print(" >= (");
  1273     s4o.print(" >= (");
  1267     symbol->end_expression->accept(*this);
  1274     symbol->end_expression->accept(*this);
  1268     s4o.print(")); ");
  1275     s4o.print(")) ");
  1269     symbol->control_variable->accept(*this);
  1276   }
  1270     s4o.print(" += (");
  1277   s4o.print(" ) {\n");
  1271     symbol->by_expression->accept(*this);
       
  1272     s4o.print(")");
       
  1273   }
       
  1274   s4o.print(")");
       
  1275   
  1278   
  1276   s4o.print(" {\n");
  1279   /* the body part */
  1277   s4o.indent_right();
  1280   s4o.indent_right();
  1278   symbol->statement_list->accept(*this);
  1281   symbol->statement_list->accept(*this);
       
  1282 
       
  1283   /* increment part */
       
  1284   s4o.print(s4o.indent_spaces + "/* BY ... (of FOR loop) */\n");
       
  1285   s4o.print(s4o.indent_spaces); 
       
  1286   if (symbol->by_expression == NULL) {
       
  1287     /* increment by 1 */    
       
  1288     /* For the increment part, we create an add_expression_c and assignment_statement_c   */
       
  1289     /* and have this visitor vist the latter!                                             */ 
       
  1290     integer_c              integer_oneval("1");
       
  1291     add_expression_c       add_expression(symbol->control_variable, &integer_oneval);
       
  1292     assignment_statement_c inc_assignment(symbol->control_variable, &add_expression);
       
  1293     integer_oneval.const_value._int64 .set(1);                    // set the stage3 anottation we need 
       
  1294     integer_oneval.const_value._uint64.set(1);                    // set the stage3 anottation we need
       
  1295     integer_oneval.datatype = symbol->control_variable->datatype; // set the stage3 anottation we need
       
  1296     add_expression.datatype = symbol->control_variable->datatype; // set the stage3 anottation we need
       
  1297     inc_assignment.accept(*this);
       
  1298     //symbol->control_variable->accept(*this);  // this does not work for VAR_GLOBAL variables
       
  1299     //s4o.print("++");
       
  1300   } else {
       
  1301     /* increment by user defined value  */
       
  1302     /* For the increment part, we create an add_expression_c and assignment_statement_c   */
       
  1303     /* and have this visitor vist the latter!                                             */ 
       
  1304     add_expression_c       add_expression(symbol->control_variable, symbol->by_expression);
       
  1305     assignment_statement_c inc_assignment(symbol->control_variable, &add_expression);
       
  1306     add_expression.datatype = symbol->control_variable->datatype; // set the stage3 anottation we need
       
  1307     inc_assignment.accept(*this);
       
  1308     //symbol->control_variable->accept(*this);  // this does not work for VAR_GLOBAL variables
       
  1309     //s4o.print(" += (");
       
  1310     //symbol->by_expression->accept(*this);
       
  1311     //s4o.print(")");
       
  1312   }  
       
  1313   
  1279   s4o.indent_left();
  1314   s4o.indent_left();
  1280   s4o.print(s4o.indent_spaces); s4o.print("}");
  1315   s4o.print(";\n" + s4o.indent_spaces + "} /* END_FOR */");
  1281   return NULL;
  1316   return NULL;
  1282 }
  1317 }
  1283 
  1318 
  1284 void *visit(while_statement_c *symbol) {
  1319 void *visit(while_statement_c *symbol) {
  1285   s4o.print("while (");
  1320   s4o.print("while (");