cast.cc 22.9 KB
Newer Older
1 2
#include "cast.h"

3
void CCSTFile::write(FILE *f)
4
{
Sarah Spall's avatar
Sarah Spall committed
5 6 7 8 9 10
  /*
    for(std::vector<T>::iterator it = v.begin(); it != v.end(); ++it) {
   */
  for(std::vector<CCSTExDeclaration*>::iterator it = defs_.begin(); it != defs_.end(); ++it)
    {
      CCSTExDeclaration *ex_dec = *it;
11
      ex_dec->write(f);
Sarah Spall's avatar
Sarah Spall committed
12
    }
13 14
}

Sarah Spall's avatar
Sarah Spall committed
15
void CCSTFuncDef::write(FILE *f)
16
{
Sarah Spall's avatar
Sarah Spall committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30
  for(std::vector<CCSTDecSpecifier*>::iterator it = specifiers_.begin(); it != specifiers_.end(); ++it)
    {
      CCSTDecSpecifier *ds = *it;
      ds->write(f);
    }
  this->ret_->write(f);
  
  for(std::vector<CCSTDeclaration*>::iterator it = decs_.begin(); it != decs_.end(); ++it)
    {
      CCSTDeclaration *ds = *it;
      ds->write(f);
    }
  // write body
  this->body_->write(f);
31 32
}

Sarah Spall's avatar
Sarah Spall committed
33
void CCSTDeclaration::write(FILE *f)
34
{
Sarah Spall's avatar
Sarah Spall committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48
  for(std::vector<CCSTDecSpecifier*>::iterator it = specifier_.begin(); it != specifier_.end(); ++it)
    {
      CCSTDecSpecifier *ds = *it;
      ds->write(f);
    }
  if(this->decs_ != null)
    {
      for(std::vector<CCSTInitDeclarator*>::iterator it = decs_.begin(); it != decs_.end(); ++it)
	{
	  CCSTInitDeclarator *ds = *it;
	  ds->write(f);
	}
    }
  // anything else
49 50
}

Sarah Spall's avatar
Sarah Spall committed
51
void CCSTStoClassSpecifier::write(FILE *f)
52
{
Sarah Spall's avatar
Sarah Spall committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  switch (this->val_)
    {
    case auto_t:
      fprintf(f, "%s ", "auto");
      break;
    case register_t:
      fprintf(f, "%s ", "register");
      break;
    case static_t:
      fprintf(f, "%s ", "static");
      break;
    case extern_t:
      fprintf(f, "%s ", "extern");
      break;
    case typedef_t:
      fprintf(f, "%s ", "typedef");
      break;
    case default:
      fprintf(f, "%s ", "error");
      break;
    } 

}

void CCSTSimpleTypeSpecifier::write(FILE *f)
{
  switch (this->type_)
    {
    case void_t:
      fprintf(f, "%s ", "void");
      break;
    case char_t:
      fprintf(f, "%s ", "char");
      break;
    case short_t:
      fprintf(f, "%s ", "short");
      break;
    case int_t:
      fprintf(f, "%s ", "int");
      break;
    case long_t:
      fprintf(f, "%s ", "long");
      break;
    case float_t:
      fprintf(f, "%s ", "float");
      break;
    case double_t:
      fprintf(f, "%s ", "double");
      break;
    case signed_t:
      fprintf(f, "%s ", "signed");
      break;
    case unsigned_t:
      fprintf(f, "%s ", "unsigned");
      break;
    case default:
      fprintf(f, "%s ", "error");
      break;
    } 
}

void CCSTStructUnionSpecifier::write(FILE *f)
{
  switch (this->s_or_u_)
    {
    case struct_t:
      {
	//  <struct-or-union> <identifier> what about this case
	fprintf(f, "%s %s ", "struct", this->id_);
	break;
      }
    case union_t:
      {
	fprintf(f, "%s %s ", "union", this->id_);
	break;
      }
    case default:
      printf("error");
      exit(0);
      break;
    }
  if(this->struct_dec_ != null)
    {
      fprintf(f, "{\n");
      for(std::vector<CCSTStructDeclaration*>::iterator it = struct_dec_.begin(); it != struct_dec_.end(); ++it)
	{
	  CCSTStructDeclaration *ds = *it;
	  ds->write(f);
	  fprintf(f, "\n");
	}
      fprintf(f, "};\n");
    }
145 146
}

Sarah Spall's avatar
Sarah Spall committed
147
void CCSTStructDeclaration::write(FILE *f)
148
{
Sarah Spall's avatar
Sarah Spall committed
149 150 151 152 153 154 155 156 157 158 159 160
  if(this->spec_qual_ != null)
    {
      for(std::vector<CCSTSpecifierQual*>::iterator it = spec_qual_.begin(); it != spec_qual_.end(); ++it)
	{
	  CCSTSpecifierQual *ds = *it;
	  ds->write(f);
	  fprintf(f, " ");
	  
	}
    }
  this->dec_list_->write(f);
  
161 162
}

Sarah Spall's avatar
Sarah Spall committed
163
void CCSTStructDecList::write(FILE *f)
164
{
Sarah Spall's avatar
Sarah Spall committed
165 166 167 168 169 170
  for(std::vector<CCSTStructDeclarator*>::iterator it = struct_decs_.begin(); it != struct_decs_.end(); ++it)
	{
	  CCSTStructDeclarator *ds = *it;
	  ds->write(f);
	  fprintf(f, ", "); // if last do not write ,
	}
171 172
}

Sarah Spall's avatar
Sarah Spall committed
173
void CCSTStructDeclarator::write(FILE *f)
174
{
Sarah Spall's avatar
Sarah Spall committed
175 176 177 178 179 180 181 182 183
  if(this->dec_ != null)
    {
      this->dec_->write(f);
    }
  if(this->expr_ != null)
    {
      fprintf(f, " : ");
      this->expr_->write(f);
    }
184 185
}

Sarah Spall's avatar
Sarah Spall committed
186
void CCSTDeclarator::write(FILE *f)
187
{
Sarah Spall's avatar
Sarah Spall committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  if(this->pointer_ != null)
    {
      this->pointer_->write(f);
    }
  if(this->d_dec_ == null)
    {
      printf("error");
      exit(0);
    }
  this->d_dec_->write(f);
}

void CCSTPointer::write(FILE *f)
{
  fprintf(f,"*");
  for(std::vector<type_qualifier>::iterator it = type_q_.begin(); it != type_q_.end(); ++it)
	{
	  type_qualifier tq =  *it;
	  switch (tq)
	    {
	    case const_t:
	      fprintf(f, "%s ", "const");
	      break;
	    case volatile_t:
	      fprintf(f, "%s ", "volatile");
	      break;
	    case default:
	      break;
	    }
	}
  if(this->p_ != null)
    {
      this->p_->write(f);
    }
222 223
}

Sarah Spall's avatar
Sarah Spall committed
224
void CCSTDirectDecId::write(FILE *f)
225
{
Sarah Spall's avatar
Sarah Spall committed
226
  fprintf(f, " %s ", this->id_);
227 228
}

Sarah Spall's avatar
Sarah Spall committed
229
void CCSTDirectDecDec::write(FILE *f)
230
{
Sarah Spall's avatar
Sarah Spall committed
231 232 233 234 235 236 237 238
  if(this->dec_ == null)
    {
      printf("Error\n");
      exit(0);
    }
  fprintf(f, "( "); // are there actually supposed to be parens?
  this->dec_->write(f);
  fprintf(f, " )");
239 240
}

Sarah Spall's avatar
Sarah Spall committed
241
void CCSTDirectDecConstExpr::write(FILE *f)
242
{
Sarah Spall's avatar
Sarah Spall committed
243 244 245 246 247 248 249 250 251 252 253 254
  if(this->direct_dec_ == null)
    {
      printf("error\n");
      exit(0);
    }
  this->direct_dec_->write();
  fprintf(f, "[ ");
  if(this->const_expr_ != null)
    {
      this->const_expr_->write(f);
    }
  fprintf(f, "]");
255 256
}

Sarah Spall's avatar
Sarah Spall committed
257
void CCSTDirectDecParamTypeList::write()
258
{
Sarah Spall's avatar
Sarah Spall committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
  if(this->direct_dec_ == null)
    {
      printf("error\n");
      exit(0);
    }
  this->direct_dec_->write();
  fprintf(f, "( ");
  if(this->p_t_list_ == null)
    {
      printf("error\n");
      exit(0);
    }
  this->p_t_list_->write(f);
  fprintf(f, ")");
}
274

Sarah Spall's avatar
Sarah Spall committed
275
void CCSTDirectDecIdList::write()
276
{
Sarah Spall's avatar
Sarah Spall committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290
  if(this->direct_dec_ == null)
    {
      printf("error\n");
      exit(0);
    }
  fprintf(f, "( ");
  for(std::vector<char*>::iterator it = ids_.begin(); it != ids_.end(); ++it)
	{
	  char *id = *it;
	  fprintf(f, " %s", id);
	  fprintf(f, ", "); // should i be printing commas
	}
  fprintf(f, " )");
}
291

Sarah Spall's avatar
Sarah Spall committed
292
void CCSTConstExpr::write(FILE *f)
293
{
Sarah Spall's avatar
Sarah Spall committed
294 295 296 297 298 299
  if(this->cond_expr_ == null)
    {
      printf("error\n");
      exit(0);
    }
  
300 301
}

Sarah Spall's avatar
Sarah Spall committed
302
void CCSTLogicalOrExpr::write(FILE *f)
303
{
Sarah Spall's avatar
Sarah Spall committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  if(this->or_ != null)
    {
      if(this->and_ == null)
	{
	  printf("error\n");
	  exit(0);
	}
      fprintf(f, "( ");
      this->or_->write(f);
      fprintf(f, " )");
      fprintf(f, " || ");
      fprintf(f, "( ");
      this->and_->write(f);
      fprintf(f, " )");
    }
  else 
    {
      // fprintf(f, "( ");
      this->and_->write(f);
      // fprintf(f, " )");
    }
325 326
}

Sarah Spall's avatar
Sarah Spall committed
327
void CCSTLogicalAndExpr::write(FILE *f)
328
{
Sarah Spall's avatar
Sarah Spall committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
  if(this->and_ != null)
    {
      if(this->or_ == null)
	{
	  printf("error\n");
	  exit(0);
	}
      fprintf(f, "( ");
      this->and_->write(f);
      fprintf(f, " )");
      fprintf(f, " && ");
      fprintf(f, "( ");
      this->or_->write(f);
      fprintf(f, " )");
    }
  else
    {
      //  fprintf(f, "( ");
      this->or_->write(f);
      //    fprintf(f, " )");
    }
350 351
}

Sarah Spall's avatar
Sarah Spall committed
352
void CCSTInclusiveOrExpr::write(FILE *f)
353
{
Sarah Spall's avatar
Sarah Spall committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
  if(this->in_or_ != null)
    {
      if(this->xor_ == null)
	{
	  printf("error\n");
	  exit(0);
	}
      fprintf(f, "( ");
      this->in_or_->write(f);
      fprintf(f, " )");
      fprintf(f, " | ");
      fprintf(f, "( ");
      this->xor_->write(f);
      fprintf(f, " )");
    }
  else
    {
      this->xor_->write();
    }
373 374
}

Sarah Spall's avatar
Sarah Spall committed
375
void CCSTXorExpr::write(FILE *f)
376
{
Sarah Spall's avatar
Sarah Spall committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
  if(this->xor_ != null)
    {
      fprintf(f, "( ");
      this->xor_->write(f);
      fprintf(f, " )");
      fprintf(f, " ^ ");
      fprintf(f, "( ");
      this->and_->write(f);
      fprintf(f, " )");
    }
  else
    {
      // the if else not needed, because and expression inherits from xor expr
      this->and_->write(f);
    }
392 393
}

Sarah Spall's avatar
Sarah Spall committed
394
void CCSTAndExpr::write(FILE *f)
395
{
Sarah Spall's avatar
Sarah Spall committed
396 397 398 399 400 401 402 403 404 405 406 407
  if(this->and_ == null || this->eq_ == null)
    {
      fprintf("error\n");
      exit(0);
    }
  fprintf(f, "( ");
  this->and_->write(f);
  fprintf(f, " )");
  fprintf(f, " & ");
  fprintf(f, "( ");
  this->eq_->write(f);
  fprintf(f, " )");
408 409
}

Sarah Spall's avatar
Sarah Spall committed
410
void CCSTEqExpr::write(FILE *f)
411
{
Sarah Spall's avatar
Sarah Spall committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
  if(this->eq_expr_ == null || this->r_expr_ == null)
    {
      printf("error\n");
      exit(0);
    }
  if(equal_)
    {
      fprintf(f, "( ");
      this->eq_expr_->write(f);
      fprintf(f, " )");
      fprintf(f, " == ");
      fprintf(f, "( ");
      this->r_expr_->write(f);
      fprintf(f, " )");
    }
  else
    {
      fprintf(f, "( ");
      this->eq_expr_->write(f);
      fprintf(f, " )");
      fprintf(f, " != ");
      fprintf(f, "( ");
      this->r_expr_->write(f);
      fprintf(f, " )");
    }
437 438
}

Sarah Spall's avatar
Sarah Spall committed
439
void CCSTRelationalExpr::write(FILE *f)
440
{
Sarah Spall's avatar
Sarah Spall committed
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
  if(this->r_expr_ == null || this->s_expr_ == null)
    {
      printf("error\n");
      exit(0);
    }
  fprintf(f, "( ");
  this->r_expr_->write(f);
  fprintf(f, " )");
  switch (this->op_)
    {
    case lessthan_t:
      fprintf(f, " < ");  
      break;
    case greaterthan_t:
      fprintf(f, " > ");
      break;
    case lessthaneq_t:
      fprintf(f, " <= ");
      break;
    case greaterthaneq_t:
      fprintf(f, " >= ");
      break;
    case default:
      {
	printf("error\n");
	exit(0);
      }
    }
  fprintf(f, "( ");
  this->s_expr_->write(f);
  fprintf(f, " )");
472 473
}

Sarah Spall's avatar
Sarah Spall committed
474
void CCSTShiftExpr::write(FILE *f)
475
{
Sarah Spall's avatar
Sarah Spall committed
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
  if(this->s_expr_ == null || this->a_expr_ == null)
    {
      printf("error\n");
      exit(0);
    }
  fprintf(f, "( ");
  this->s_expr_->write(f);
  fprintf(f, " )");
  switch (this->shift_)
    {
    case leftshift_t:
      fprintf(f, " << ");
      break;
    case rightshift_t:
      fprintf(f, " >> ");
      break;
    case default:
      {
	printf("error\n");
	exit(0);
      } 
    }
  fprintf(f, "( ");
  this->a_expr_->write(f);
  fprintf(f, " )");
501 502
}

Sarah Spall's avatar
Sarah Spall committed
503
void CCSTAdditiveExpr::write(FILE *f)
504
{ 
Sarah Spall's avatar
Sarah Spall committed
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
  if(this->a_expr_ == null || this->m_expr_ == null)
    {
      printf("error\n");
      exit(0);
    }
  fprintf(f, "( ");
  this->a_expr_->write(f);
  fprintf(f, " )");
  switch (this->op_)
    {
    case plus_t:
      fprintf(f, " + ");
      break;
    case minus_t:
      fprintf(f, " - ");
      break;
    case default:
      {
	printf("error\n");
	exit(0);
      }
    }
  fprintf(f, "( ");
  this->m_expr_->write(f);
  fprintf(f, " )");
530 531
}

Sarah Spall's avatar
Sarah Spall committed
532
void CCSTMultExpr::write(FILE *f)
533
{
Sarah Spall's avatar
Sarah Spall committed
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
  if(this->m_expr_ == null || this->c_expr_ == null)
    {
      printf("error\n");
      exit(0);
    }
  fprintf(f, "( ");
  this->m_expr_->write(f);
  fprintf(f, " )");
  switch (this->op_)
    {
    case multiply_t:
      fprintf(f, " * ");
      break;
    case divide_t:
      fprintf(f, " / ");
      break;
    case mod_t:
      fprintf(f, " % ");
      break;
    case default:
      {
	printf("error\n");
	exit(0);
      }
    }
  fprintf(f, "( ");
  this->c_expr_->write(f);
  fprintf(f, " )");
562 563
}

Sarah Spall's avatar
Sarah Spall committed
564
void CCSTCastExpr::write(FILE *f)
565
{
Sarah Spall's avatar
Sarah Spall committed
566 567 568 569 570 571 572 573 574
  if(this->cast_type_ == null || this->cast_expr_ == null)
    {
      printf("error\n");
      exit(0);
    }
  fprintf(f, "( ");
  this->cast_type_->write(f);
  fprintf(f, " )");
  this->cast_expr_->write(f);
575 576
}

Sarah Spall's avatar
Sarah Spall committed
577
void CCSTUnaryExprCastExpr::write(FILE *f)
578
{
Sarah Spall's avatar
Sarah Spall committed
579 580 581 582 583 584 585 586 587 588
  if(this->unary_op_ == null || this->cast_expr_ == null)
    {
      printf("error\n");
      exit(0);
    }
  this->unary_op_->write(f);
  fprintf(f, " ( ");
  this->cast_expr_->write(f);
  fprintf(f, " )");
}
589

Sarah Spall's avatar
Sarah Spall committed
590
void CCSTUnaryExprOpOp::write(FILE *f)
591
{
Sarah Spall's avatar
Sarah Spall committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
  if(this->unary_expr_ == null)
    {
      printf("error\n");
      exit(0);
    }
  switch (this->op_)
    {
    case increment_t:
      fprintf(f, "++ ");
      break;
    case decrement_t:
      fprintf(f, "-- ");
      break;
    case default:
      {
	printf("error\n");
	exit(0);
      }
    }
  this->unary_expr_->write(f):
}
613

Sarah Spall's avatar
Sarah Spall committed
614
void CCSTUnaryExprSizeOf::write(FILE *f)
615
{
Sarah Spall's avatar
Sarah Spall committed
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
  if(this->unary_expr_ != null)
    {
      fprintf(f, "sizeof");
      fprintf(f, "( ");
      this->unary_expr_->write(f);
      fprintf(f, " )");
    }
  else if(this->type_name_ != null)
    {
      fprint(f, "sizeof");
      fprintf(f, "( ");
      this->type_name_->write(f);
      fprintf(f, " )");
    }
  else
    {
      printf("error\n");
      exit(0);
    }
}
636

Sarah Spall's avatar
Sarah Spall committed
637
void CCSTPostFixExprOpOp::write(FILE *f)
638
{
Sarah Spall's avatar
Sarah Spall committed
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
  if(this->post_fix_expr_ == null)
    {
      printf("error\n");
      exit(0);
    }
  this->post_fix_expr->write(f);
  fprintf(f, " ");
  switch (this->op_)
    {
    case increment_t:
      break;
    case decrement_t:
      break;
    case default:
      {
	printf("error\n");
	exit(0);
      }
    }
}
659

Sarah Spall's avatar
Sarah Spall committed
660
void CCSTPostFixExprAccess::write(FILE *f)
661
{
Sarah Spall's avatar
Sarah Spall committed
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
  if(this->post_fix_expr_ == null || this->id_ == "")
    {
      printf("error\n");
      exit(0);
    }
  this->post_fix_expr_->write(f);
  switch (this->op_)
    {
    case pointer_access_t:
      fprintf(f, "->");
      break;
    case object_access_t:
      fprintf(f, ".");
      break;
    case default:
      {
	printf("error\n");
	exit(0);
      }
    }
  fprintf(f, "%s", this->id_);
}

void CCSTPostFixExprExpr::write(FILE *f)
{
687 688 689 690 691 692 693 694 695
  if(this->post_fix_expr_ == null || this->expr_ == null)
    {
      printf("error\n");
      exit(0);
    }
  this->post_fix_expr_->write(f);
  fprintf(f, "[ ");
  this->expr_->write(f);
  fprintf(f, " ]");
Sarah Spall's avatar
Sarah Spall committed
696 697 698 699
}

void CCSTPostFixExprAssnExpr::write(FILE *f)
{
700 701 702 703 704 705
  if(this->post_fix_expr_ == null)
    {
      printf("error\n");
      exit(0);
    }
  // TODO really unsure
Sarah Spall's avatar
Sarah Spall committed
706 707 708 709
}

void CCSTPrimaryExpr::write(FILE *f)
{
710 711 712 713 714 715 716 717
  if(this->expr_ == null)
    {
      printf("error\n");
      exit(0);
    }
  fprintf(f, "( ");
  this->expr_->write(f);
  fprintf(f, " )");
Sarah Spall's avatar
Sarah Spall committed
718 719
}

720
void CCSTString::write(FILE *f)
Sarah Spall's avatar
Sarah Spall committed
721
{
722 723
  // how should this be stored exactly?
  fprintf(f, "%s", this->string_);
Sarah Spall's avatar
Sarah Spall committed
724
}
725

726
void CCSTPrimaryExprId::write(FILE *f)
Sarah Spall's avatar
Sarah Spall committed
727
{
728
  fprintf(f, "%s", this->id_);
Sarah Spall's avatar
Sarah Spall committed
729 730 731 732
}



733
void CCSTInteger::write(FILE *f)
Sarah Spall's avatar
Sarah Spall committed
734
{
735
  // todo
Sarah Spall's avatar
Sarah Spall committed
736 737
}

738
void CCSTChar::write(FILE *f)
739
{
740
  fprintf(f, "%c", this->char_);
Sarah Spall's avatar
Sarah Spall committed
741
}
742

743
void CCSTFloat::write(FILE *f)
Sarah Spall's avatar
Sarah Spall committed
744
{
745 746 747 748 749 750 751 752
  if(float_)
    {
      fprintf(f, "%f", this->f_);
    }
  else
    {
      fprintf(f, "%f", this->d_);
    }
Sarah Spall's avatar
Sarah Spall committed
753 754
}

755
void CCSTEnumConst::write(FILE *f)
756 757
{
  //TODO
758
  // unsure
Sarah Spall's avatar
Sarah Spall committed
759
}
760

761
void CCSTExpression::write(FILE *f)
762
{
763 764 765 766 767
   for(std::vector<CCSTAssignExpr*>::iterator it = assn_exprs_.begin(); it != assn_exprs_.end(); ++it)
	{
	  CCSTAssignExpr *assn = *it;
	  assn->write(f);
	}
768 769
}

Sarah Spall's avatar
Sarah Spall committed
770
void CCSTAssignExpr::write()
771
{
772 773 774 775 776 777 778 779 780 781
  if(this->unary_expr_ == null || this->assn_op_ == null || this->assn_expr_ == null)
    {
      printf("error\n");
      exit(0);
    }
  this->unary_expr_->write(f);
  fprintf(f, " ");
  this->assn_op_->write(f);
  fprintf(f, " ");
  this->assn_expr_->write(f);
Sarah Spall's avatar
Sarah Spall committed
782
}
783 784 785

void CCSTAssignOp::write()
{
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
  switch (this->op_)
    {
    case equal_t:
      fprintf(f, "=");
      break;
    case mult_eq_t:
      fprintf(f, "*=");
      break;
    case div_eq_t:
      fprintf(f, "/=");
      break;
    case mod_eq_t:
      fprintf(f, "%=");
      break;
    case plus_eq_t:
      fprintf(f, "+=");
      break;
    case minus_eq_t:
      fprintf(f, "-=");
      break;
    case lshift_eq_t:
      fprintf(f, "<<=");
      break;
    case rshift_eq_t:
      fprintf(f, ">>=");
      break;
    case and_eq_t:
      fprintf(f, "&=");
      break;
    case xor_eq_t:
      fprintf(f, "^=");
      break;
    case or_eq_t:
      fprintf(f, "|=");
      break;
    case default:
      {
	printf("error\n");
	exit(0);
      }
    }
827 828
}

829
void CCSTUnaryOp::write(FILE *f)
830
{
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
  switch (this->op_)
    {
    case bit_and_t:
      fprintf(f, "&");
      break;
    case mult_t:
      fprintf(f, "*");
      break;
    case plus_t:
      fprintf(f, "+");
      break;
    case minus_t:
      fprintf(f, "-");
      break;
    case tilde_t:
      fprintf(f, "~");
      break;
    case bang_t:
      fprintf(f, "!");
      break;
    case default:
      {
	printf("error\n");
	exit(0);
      }
    }
857 858
}

859
void CCSTTypeName::write(FILE *f)
860
{
861 862 863 864 865 866 867 868 869 870
   for(std::vector<CCSTSpecifierQual*>::iterator it = spec_quals_.begin(); it != spec_quals_.end(); ++it)
	{
	  CCSTSpecifierQual *qual = *it;
	  qual->write(f);
	  fprintf(f, " ");
	}
   if(this->abs_dec_ != null)
     {
       this->abs_dec_->write(f);
     }
871 872
}

873
void CCSTParamTypeList::write(FILE *f)
874
{
875 876 877 878 879 880 881 882
  if(this->p_list_ == null)
    {
      printf("error\n");
      exit(0);
    }
  this->p_list_->write(f);
  fprintf(f, " , ");
  fprintf(f, "...");
883 884
}

885
void CCSTParamList::write(FILE *f)
886
{
887 888 889 890 891 892
   for(std::vector<CCSTParamDeclaration*>::iterator it = p_dec_.begin(); it != p_dec_.end(); ++it)
	{
	  CCSTParamDeclaration *dec = *it;
	  dec->write(f);
	  fprintf(f, ", "); // should i be printing commas
	}
893 894
}

895
void CCSTParamDeclaration::write(FILE *f)
896
{
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
  for(std::vector<CCSTDecSpecifier*>::iterator it = ids_.begin(); it != ids_.end(); ++it)
    {
      CCSTDecSpecifier *spec = *it;
      spec->write(f);
      fprintf(f, " ");
    }
  
  if(this->dec_ == null && this->abs_dec_ == null)
    {
      // write nothing
    }
  else if(this->dec_ == null)
    {
      if(this->abs_dec_ == null)
	{
	  printf("error\n");
	  exit(0);
	}
      this->abs_dec_->write(f);
    }
  else
    {
      this->dec_->write(f);
    }
921
  
Sarah Spall's avatar
Sarah Spall committed
922
}
923

924
void CCSTAbstDeclarator::write(FILE *f)
925
{
926 927 928 929 930 931 932 933 934 935 936
  if(this->p_ == null)
    {
      printf("error\n");
      exit(0);
    }
  this->p_->write(f);
  fprintf(f, " ");
  if(this->d_abs_dec_ != null)
    {
      this->d_abs_dec->write(f);
    }
Sarah Spall's avatar
Sarah Spall committed
937
}
938

939
void CCSTDirectAbstDeclarator::write(FILE *f)
940 941 942 943 944 945 946
{
  /*
    <direct-abstract-declarator> ::=  ( <abstract-declarator> )
                               | {<direct-abstract-declarator>}? [ {<constant-expression>}? ]
                               | {<direct-abstract-declarator>}? ( {<parameter-type-list>|? )
   */
  // TODO
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
  if(this->d_abs_dec_ == null && this->const_expr_ == null && this->param_type_list_ == null)
    {
      fprintf(f, "( ");
      this->abs_dec_->write(f);
      fprintf(f, " )");
    }
  else
    {
      if(this->d_abs_dec_ == null)
	{
	  printf("error\n");
	  exit(0);
	}
      this->d_abs_dec->write(f);
      if(this->const_expr_ == null)
	{
	  
	  if(this->param_type_list_ == null)
	    {
	      printf("error\n");
	      exit(0);
	    }
	  fprintf(f, " ( ");
	  this->param_type_list_->write(f);
	  fprintf(f, " )");
      else
	{
	  fprintf(f, " [ ");
	  this->const_expr_->write(f);
	  fprintf(f, " ] ");
	}
    }
Sarah Spall's avatar
Sarah Spall committed
979
}
980

981
void CCSTEnumSpecifier::write(FILE *f)
982
{
983 984 985 986 987 988 989 990 991 992 993 994 995
  if(this->el_ == null)
    {
      fprintf(f, "enum ");
      fprintf(f, "%s", this->id_);
    }
  else
    {
      fprintf(f, "enum ");
      fprintf(f, "%s", this->id_);
      fprintf(f, "{ ");
      this->el_->write(f);
      fprintf(F, " }");
    }
996 997
}

998
void CCSTEnumeratorList::write(FILE *f)
999
{
1000 1001 1002 1003 1004 1005
  for(std::vector<CCSTEnumerator*>::iterator it = list_.begin(); it != list_.end(); ++it)
    {
      CCSTEnumerator *l = *it;
      l->write(f);
      fprintf(f, ", ");
    }
1006 1007
}

1008
void CCSTEnumerator::write(FILE *f)
1009
{
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
  if(this->ce_ == null)
    {
      fprintf(f, "%s", this->id_);
    }
  else
    {
      fprintf(f, "%s", this->id_);
      fprintf(f, " = ");
      this->ce_->write(f);
    }
1020 1021
}

1022
void CCSTTypedefName::write(FILE *f)
1023
{
1024
  fprintf(f, "%s", this->id_);
1025 1026
}

1027
void CCSTDeclaration::write(FILE *f)
1028
{
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
  //?x
  for(std::vector<CCSTDecSpecifier*>::iterator it = dec_spec_.begin(); it != dec_spec_.end(); ++it)
    {
      CCSTDecSpecifier *dec_spec = *it;
      dec_spec->write(f);
      fprintf(f, " ");
    }
  for(std::vector<CCSTInitDeclarator*>::iterator it = init_dec_.begin(); it != init_dec_.end(); ++it)
    {
      CCSTInitDeclarator *init_dec = *it;
      init_dec->write(this->out_file_);
      fprintf(f, " ");
    }
1042 1043
}

1044
void CCSTInitDeclarator::write(FILE *f)
1045
{
1046 1047 1048 1049 1050 1051 1052 1053 1054
  // does inheritence cover just declarator case?
  if(this->dec_ == null || this->init_ == null)
    {
      printf("error\n");
      exit(0);
    }
  this->dec_->write(f);
  fprintf(f, " = ");
  this->init_->write(f);
1055 1056
}

1057
void CCSTInitializer::write(FILE *f)
1058 1059 1060 1061 1062 1063 1064 1065 1066
{
  /*
    
<initializer> ::= <assignment-expression>
                | { <initializer-list> }
                | { <initializer-list> , }
   */

  // TODO
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
  if(this->assn_expr_ == null)
    {
      if(this->init_list_ == null)
	{
	  printf("error\n");
	  exit(0);
	}
      fprintf(f, "{ ");
      this->init_list_->write(f);
      fprintf(f, " }");
    }
  else
    {
      this->assn_expr_->write(f);
    }
Sarah Spall's avatar
Sarah Spall committed
1082
}
1083

1084
void CCSTInitializerList::write(FILE *f)
1085 1086
{
  // TODO
1087 1088 1089 1090 1091 1092 1093
  for(std::vector<CCSTInitializer*>::iterator it = init_list_.begin(); it != init_list_.end(); ++it)
    {
      CCSTInitializer *init = *it;
      init->write(f);
      fprintf(f, ", ");
    }
  
Sarah Spall's avatar
Sarah Spall committed
1094
}
1095

1096
void CCSTCompoundStatement::write(FILE *f)
1097
{
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
  fprintf(f, "{\n");
  for(std::vector<CCSTDeclaration*>::iterator it = declarations_.begin(); it != declarations_.end(); ++it)
    {
      CCSTDeclaration *dec = *it;
      dec->write(f);
      fprintf(f, "\n");
    }
  for(std::vector<CCSTStatement*>::iterator it = statements_.begin(); it != statements_.end(); ++it)
    {
      CCSTStatement *state = *it;
      state->write(f);
      fprintf(f, "\n");
    }
  fprintf(f, "}");
1112 1113
}

1114
void CCSTPlainLabelStatement::write(FILE *f)
1115
{
1116 1117 1118 1119
  //todo
  fprintf(f, "%s: ", this->id_);
  this->stmnt_->write(f);
  fprintf(f, ";");
1120 1121
}

1122
void CCSTCaseStatement::write(FILE *f)
1123
{
1124 1125 1126 1127 1128 1129
  fprintf(f, "case ");
  this->case_label_->write(f);
  fprintf(f, ":\n");
  fprintf(f, "{\n");
  this->body_->write(f);
  fprintf(f, "\n}");
1130 1131
}

1132
void CCSTExprStatement::write(FILE *f)
1133
{
1134 1135 1136 1137 1138 1139
  // weird why the semicolon with no expression
  if(this->expr_ != null)
    {
      this->expr_->write(f);
    }
  fprintf(f, ";");
1140 1141
}

1142
void CCSTIfStatement::write(FILE *f)
1143
{
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
  fprintf(f, "if");
  fprintf(f, "( ");
  if(this->cond_ == null)
    {
      printf("error\n");
      exit(0);
    }
  this->cond_->write(f);
  fprintf(f, " )");
  fprintf(f, "\n");
  fprintf(f, "{\n");
  this->body_->write(f);
  fprintf(f, "\n");
  fprintf(f, "}");
  fprintf(f, "\n");
1159 1160
}

1161
void CCSTIfElseStatement::write(FILE *f)
1162
{
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
  fprintf(f, "if");
  fprintf(f, "( ");
  if(this->cond_ == null)
    {
      printf("error\n");
      exit(0);
    }
  this->cond_->write(f);
  fprintf(f, " )");
  fprintf(f, "\n");
  fprintf(f, "{\n");
  this->if_body_->write(f);
  fprintf(f, "\n");
  fprintf(f, "}");
  fprintf(f, "\n");
  fprintf(f, "else");
  fprintf(f, "\n");
  fprintf(f, "{\n");
  this->else_body_->write(f);
  fprintf(f, "\n}");
1183 1184
}

1185
void CCSTSwitchStatement::write(FILE *f)
1186
{
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
  if(this->expr_ == null)
    {
      printf("error\n");
      exit(0);
    }
  fprintf(f, "switch ");
  fprintf(f, "( ");
  this->expr_->write(f);
  fprintf(f, " )");
  fprintf(f, "\n");
  fprintf(f, "{\n");
  this->body_->write(); // all cases?
  fprintf(f, "\n}");
1200 1201
}

1202
void CCSTWhileLoop::write(FILE *f)
1203
{
1204 1205
  fprintf(f, "while");
  fprintf(f, "( ");
1206
  this->cond_->write();
1207 1208 1209
  fprintf(f, " )");
  fprintf(f, "\n");
  fprintf(f, "{");
1210
  this->body_->write();
1211 1212
  fprintf(f, "\n");
  fprintf(f, "}");
1213 1214
}

1215
void CCSTDoLoop::write(FILE *f)
1216
{
1217 1218
  fprintf(f, "do\n");
  fprintf(f, "{\n");
1219
  this->body_->write();
1220 1221 1222
  fprintf(f, "\n}\n");
  fprintf(f, "while");
  fprintf(f, "( ");
1223
  this->cond_->write();
1224 1225
  fprintf(f, " )");
  fprintf(f, ";");
1226 1227
}

1228
void CCSTForLoop::write(FILE *f)
1229 1230
{
  // write for (
1231 1232 1233
  fprintf(f, "for");
  fprintf(f, "( ");
  if(this->init_ != null)
1234
    this->init_->write();
1235 1236
  fprintf(f, ";");
  if(this->cond_ != null)
1237
    this->cond_->write();
1238 1239
  fprintf(f, ";");
  if(this->up_ != null)
1240
    this->up_->write();
1241 1242
  fprintf(f, " )\n");
  fprintf(f, "{\n");
1243
  this->body_->write();
1244
  fprintf(f, "\n}");
1245 1246
}

Sarah Spall's avatar
Sarah Spall committed
1247
void CCSTGoto::write(FILE *f)
1248
{
1249 1250 1251
  fprintf(f, "goto ");
  fprintf(f, "%s", this->identifier_);
  fprintf(f, ";");
1252 1253
}

Sarah Spall's avatar
Sarah Spall committed
1254
void CCSTContinue::write(FILE *f)
1255
{
Sarah Spall's avatar
Sarah Spall committed
1256
  fprintf(f, "continue");// write continue ;
1257 1258
}

Sarah Spall's avatar
Sarah Spall committed
1259
void CCSTBreak::write(FILE *f)
1260
{
Sarah Spall's avatar
Sarah Spall committed
1261
  fprintf(f, "break");
1262 1263
}

Sarah Spall's avatar
Sarah Spall committed
1264
void CCSTReturn::write(FILE *f)
1265 1266 1267
{
  if(this->expr_ == null)
    {
Sarah Spall's avatar
Sarah Spall committed
1268
      fprintf(f, "return;");
1269 1270 1271
    }
  else
    {
1272
      fprintf(f, "return");
1273
      this->expr_->write();
1274
      fprintf(f, ";");
1275 1276
    }
}