stage4/generate_c/generate_c_st.cc
changeset 387 db368e53133c
parent 382 ac6dfec701c9
child 392 9b88b8b6bccd
equal deleted inserted replaced
386:606443ffd589 387:db368e53133c
  1205   s4o.print("for(");
  1205   s4o.print("for(");
  1206   symbol->control_variable->accept(*this);
  1206   symbol->control_variable->accept(*this);
  1207   s4o.print(" = ");
  1207   s4o.print(" = ");
  1208   symbol->beg_expression->accept(*this);
  1208   symbol->beg_expression->accept(*this);
  1209   s4o.print("; ");
  1209   s4o.print("; ");
  1210   symbol->control_variable->accept(*this);
  1210   if (symbol->by_expression == NULL) {
  1211   s4o.print(" != ");
  1211     /* increment by 1 */
  1212   symbol->end_expression->accept(*this);
  1212     symbol->control_variable->accept(*this);
  1213   s4o.print("; ");
  1213     s4o.print(" <= ");
  1214   symbol->control_variable->accept(*this);
  1214     symbol->end_expression->accept(*this);
  1215   if (symbol->by_expression != NULL) {
  1215     s4o.print("; ");
  1216     s4o.print(" += ");
  1216     symbol->control_variable->accept(*this);
       
  1217     s4o.print("++");
       
  1218   } else {
       
  1219     /* increment by user defined value  */
       
  1220     /* The user defined increment value may be negative, in which case
       
  1221      * the expression to determine whether we have reached the end of the loop
       
  1222      * changes from a '<=' to a '>='.
       
  1223      * Since the increment value may change during runtime (remember, it is
       
  1224      * an expression, so may contain variables), choosing which test
       
  1225      * to use has to be done at runtime.
       
  1226      */
       
  1227     s4o.print("((");
  1217     symbol->by_expression->accept(*this);
  1228     symbol->by_expression->accept(*this);
  1218   } else {
  1229     s4o.print(") > 0)? (");
  1219     s4o.print("++");
  1230     symbol->control_variable->accept(*this);
  1220   }
  1231     s4o.print(" <= (");
  1221   s4o.print(") {\n");
  1232     symbol->end_expression->accept(*this);
       
  1233     s4o.print(")) : (");
       
  1234     symbol->control_variable->accept(*this);
       
  1235     s4o.print(" >= (");
       
  1236     symbol->end_expression->accept(*this);
       
  1237     s4o.print(")); ");
       
  1238     symbol->control_variable->accept(*this);
       
  1239     s4o.print(" += (");
       
  1240     symbol->by_expression->accept(*this);
       
  1241     s4o.print(")");
       
  1242   }
       
  1243   s4o.print(")");
       
  1244   
       
  1245   s4o.print(" {\n");
  1222   s4o.indent_right();
  1246   s4o.indent_right();
  1223   symbol->statement_list->accept(*this);
  1247   symbol->statement_list->accept(*this);
  1224   s4o.indent_left();
  1248   s4o.indent_left();
  1225   s4o.print(s4o.indent_spaces); s4o.print("}");
  1249   s4o.print(s4o.indent_spaces); s4o.print("}");
  1226   return NULL;
  1250   return NULL;
  1227 }
  1251 }
       
  1252 
  1228 void *visit(while_statement_c *symbol) {
  1253 void *visit(while_statement_c *symbol) {
  1229   s4o.print("while (");
  1254   s4o.print("while (");
  1230   symbol->expression->accept(*this);
  1255   symbol->expression->accept(*this);
  1231   s4o.print(") {\n");
  1256   s4o.print(") {\n");
  1232   s4o.indent_right();
  1257   s4o.indent_right();
  1233   symbol->statement_list->accept(*this);
  1258   symbol->statement_list->accept(*this);
  1234   s4o.indent_left();
  1259   s4o.indent_left();
  1235   s4o.print(s4o.indent_spaces); s4o.print("}");
  1260   s4o.print(s4o.indent_spaces); s4o.print("}");
  1236   return NULL;
  1261   return NULL;
  1237 }
  1262 }
       
  1263 
  1238 void *visit(repeat_statement_c *symbol) {
  1264 void *visit(repeat_statement_c *symbol) {
  1239   s4o.print("do {\n");
  1265   s4o.print("do {\n");
  1240   s4o.indent_right();
  1266   s4o.indent_right();
  1241   symbol->statement_list->accept(*this);
  1267   symbol->statement_list->accept(*this);
  1242   s4o.indent_left();
  1268   s4o.indent_left();
  1243   s4o.print(s4o.indent_spaces); s4o.print("} while(");
  1269   s4o.print(s4o.indent_spaces); s4o.print("} while(");
  1244   symbol->expression->accept(*this);
  1270   symbol->expression->accept(*this);
  1245   s4o.print(")");
  1271   s4o.print(")");
  1246   return NULL;
  1272   return NULL;
  1247 }
  1273 }
       
  1274 
  1248 void *visit(exit_statement_c *symbol) {
  1275 void *visit(exit_statement_c *symbol) {
  1249   s4o.print("break");
  1276   s4o.print("break");
  1250   return NULL;
  1277   return NULL;
  1251 }
  1278 }
  1252 
  1279