| 1 | #include "Parser.hpp" |
| 2 | |
| 3 | #include <iostream> |
| 4 | #include <iomanip> |
| 5 | |
| 6 | #include <objc/runtime.h> |
| 7 | #include <sstream> |
| 8 | |
| 9 | _finline CYFlags operator ~(CYFlags rhs) { |
| 10 | return static_cast<CYFlags>(~static_cast<unsigned>(rhs)); |
| 11 | } |
| 12 | |
| 13 | _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) { |
| 14 | return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs)); |
| 15 | } |
| 16 | |
| 17 | _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) { |
| 18 | return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs)); |
| 19 | } |
| 20 | |
| 21 | _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) { |
| 22 | return lhs = lhs | rhs; |
| 23 | } |
| 24 | |
| 25 | _finline CYFlags CYLeft(CYFlags flags) { |
| 26 | return flags & ~CYNoTrailer; |
| 27 | } |
| 28 | |
| 29 | _finline CYFlags CYCenter(CYFlags flags) { |
| 30 | return flags & CYNoIn; |
| 31 | } |
| 32 | |
| 33 | _finline CYFlags CYRight(CYFlags flags) { |
| 34 | return flags & (CYNoIn | CYNoTrailer); |
| 35 | } |
| 36 | |
| 37 | bool CYFalse::Value() const { |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | bool CYTrue::Value() const { |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | #define CYPA 16 |
| 46 | |
| 47 | void CYAddressOf::Output(CYOutput &out, CYFlags flags) const { |
| 48 | rhs_->Output(out, 1, CYLeft(flags)); |
| 49 | out << ".$cya()"; |
| 50 | } |
| 51 | |
| 52 | void CYArgument::Output(CYOutput &out) const { |
| 53 | if (name_ != NULL) { |
| 54 | out << *name_; |
| 55 | if (value_ != NULL) |
| 56 | out << ":"; |
| 57 | } |
| 58 | if (value_ != NULL) |
| 59 | value_->Output(out, CYPA, CYNoFlags); |
| 60 | if (next_ != NULL) { |
| 61 | if (next_->name_ == NULL) |
| 62 | out << ','; |
| 63 | else |
| 64 | out << ' '; |
| 65 | next_->Output(out); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void CYArray::Output(CYOutput &out, CYFlags flags) const { |
| 70 | out << '['; |
| 71 | if (elements_ != NULL) |
| 72 | elements_->Output(out); |
| 73 | out << ']'; |
| 74 | } |
| 75 | |
| 76 | void CYArrayComprehension::Output(CYOutput &out, CYFlags flags) const { |
| 77 | // XXX: I don't necc. need the ()s |
| 78 | out << "(function($cyv"; |
| 79 | for (CYComprehension *comprehension(comprehensions_); comprehension != NULL; comprehension = comprehension->next_) |
| 80 | if (const char *name = comprehension->Name()) |
| 81 | out << ',' << name; |
| 82 | out << "){"; |
| 83 | out << "$cyv=[];"; |
| 84 | comprehensions_->Output(out); |
| 85 | out << "$cyv.push("; |
| 86 | expression_->Output(out, CYPA, CYNoFlags); |
| 87 | out << ");"; |
| 88 | for (CYComprehension *comprehension(comprehensions_); comprehension != NULL; comprehension = comprehension->next_) |
| 89 | comprehension->End_(out); |
| 90 | out << "return $cyv;"; |
| 91 | out << "}())"; |
| 92 | } |
| 93 | |
| 94 | void CYAssignment::Output(CYOutput &out, CYFlags flags) const { |
| 95 | lhs_->Output(out, Precedence() - 1, CYLeft(flags)); |
| 96 | out << Operator(); |
| 97 | rhs_->Output(out, Precedence(), CYRight(flags)); |
| 98 | } |
| 99 | |
| 100 | void CYBlock::Output(CYOutput &out) const { |
| 101 | for (CYSource *statement(statements_); statement != NULL; statement = statement->next_) |
| 102 | statement->Output(out); |
| 103 | } |
| 104 | |
| 105 | void CYBoolean::Output(CYOutput &out, CYFlags flags) const { |
| 106 | if ((flags & CYNoLeader) != 0) |
| 107 | out << ' '; |
| 108 | out << (Value() ? "true" : "false"); |
| 109 | if ((flags & CYNoTrailer) != 0) |
| 110 | out << ' '; |
| 111 | } |
| 112 | |
| 113 | void CYBreak::Output(CYOutput &out) const { |
| 114 | out << "break"; |
| 115 | if (label_ != NULL) |
| 116 | out << ' ' << *label_; |
| 117 | out << ';'; |
| 118 | } |
| 119 | |
| 120 | void CYCall::Output(CYOutput &out, CYFlags flags) const { |
| 121 | function_->Output(out, Precedence(), CYLeft(flags)); |
| 122 | out << '('; |
| 123 | if (arguments_ != NULL) |
| 124 | arguments_->Output(out); |
| 125 | out << ')'; |
| 126 | } |
| 127 | |
| 128 | void CYCatch::Output(CYOutput &out) const { |
| 129 | out << "catch(" << *name_ << ')'; |
| 130 | code_->Output(out, true); |
| 131 | } |
| 132 | |
| 133 | void CYCategory::Output(CYOutput &out) const { |
| 134 | out << "(function($cys,$cyp,$cyc,$cyn,$cyt){"; |
| 135 | out << "$cyp=object_getClass($cys);"; |
| 136 | out << "$cyc=$cys;"; |
| 137 | if (messages_ != NULL) |
| 138 | messages_->Output(out, true); |
| 139 | out << "})("; |
| 140 | name_->ClassName(out, true); |
| 141 | out << ");"; |
| 142 | } |
| 143 | |
| 144 | void CYClass::Output(CYOutput &out) const { |
| 145 | Output(out, CYNoBF); |
| 146 | out << ";"; |
| 147 | } |
| 148 | |
| 149 | void CYClass::Output(CYOutput &out, CYFlags flags) const { |
| 150 | // XXX: I don't necc. need the ()s |
| 151 | out << "(function($cys,$cyp,$cyc,$cyn,$cyt,$cym){"; |
| 152 | out << "$cyp=object_getClass($cys);"; |
| 153 | out << "$cyc=objc_allocateClassPair($cys,"; |
| 154 | if (name_ != NULL) |
| 155 | name_->ClassName(out, false); |
| 156 | else |
| 157 | out << "$cyq(\"CY$\")"; |
| 158 | out << ",0);"; |
| 159 | out << "$cym=object_getClass($cyc);"; |
| 160 | if (fields_ != NULL) |
| 161 | fields_->Output(out); |
| 162 | if (messages_ != NULL) |
| 163 | messages_->Output(out, false); |
| 164 | out << "objc_registerClassPair($cyc);"; |
| 165 | out << "return $cyc;"; |
| 166 | out << "}("; |
| 167 | if (super_ != NULL) |
| 168 | super_->Output(out, CYPA, CYNoFlags); |
| 169 | else |
| 170 | out << "null"; |
| 171 | out << "))"; |
| 172 | } |
| 173 | |
| 174 | void CYCompound::Output(CYOutput &out, CYFlags flags) const { |
| 175 | if (CYExpression *expression = expressions_) |
| 176 | if (CYExpression *next = expression->next_) { |
| 177 | expression->Output(out, CYLeft(flags)); |
| 178 | CYFlags center(CYCenter(flags)); |
| 179 | while (next != NULL) { |
| 180 | expression = next; |
| 181 | out << ','; |
| 182 | next = expression->next_; |
| 183 | CYFlags right(next != NULL ? center : CYRight(flags)); |
| 184 | expression->Output(out, right); |
| 185 | } |
| 186 | } else |
| 187 | expression->Output(out, flags); |
| 188 | } |
| 189 | |
| 190 | void CYComprehension::Output(CYOutput &out) const { |
| 191 | Begin_(out); |
| 192 | if (next_ != NULL) |
| 193 | next_->Output(out); |
| 194 | } |
| 195 | |
| 196 | void CYCondition::Output(CYOutput &out, CYFlags flags) const { |
| 197 | test_->Output(out, Precedence() - 1, CYLeft(flags)); |
| 198 | out << '?'; |
| 199 | if (true_ != NULL) |
| 200 | true_->Output(out, CYPA, CYNoFlags); |
| 201 | out << ':'; |
| 202 | false_->Output(out, CYPA, CYRight(flags)); |
| 203 | } |
| 204 | |
| 205 | void CYContinue::Output(CYOutput &out) const { |
| 206 | out << "continue"; |
| 207 | if (label_ != NULL) |
| 208 | out << ' ' << *label_; |
| 209 | out << ';'; |
| 210 | } |
| 211 | |
| 212 | void CYClause::Output(CYOutput &out) const { |
| 213 | if (case_ != NULL) { |
| 214 | out << "case"; |
| 215 | case_->Output(out, CYNoLeader); |
| 216 | } else |
| 217 | out << "default"; |
| 218 | out << ':'; |
| 219 | if (code_ != NULL) |
| 220 | code_->Output(out, false); |
| 221 | if (next_ != NULL) |
| 222 | out << *next_; |
| 223 | } |
| 224 | |
| 225 | const char *CYDeclaration::ForEachIn() const { |
| 226 | return identifier_->Value(); |
| 227 | } |
| 228 | |
| 229 | void CYDeclaration::ForIn(CYOutput &out, CYFlags flags) const { |
| 230 | if ((flags & CYNoLeader) != 0) |
| 231 | out << ' '; |
| 232 | out << "var "; |
| 233 | Output(out, CYRight(flags)); |
| 234 | } |
| 235 | |
| 236 | void CYDeclaration::ForEachIn(CYOutput &out) const { |
| 237 | out << *identifier_; |
| 238 | } |
| 239 | |
| 240 | void CYDeclaration::Output(CYOutput &out, CYFlags flags) const { |
| 241 | out << *identifier_; |
| 242 | if (initialiser_ != NULL) { |
| 243 | out << '='; |
| 244 | initialiser_->Output(out, CYPA, CYRight(flags)); |
| 245 | } else if ((flags & CYNoTrailer) != 0) |
| 246 | out << ' '; |
| 247 | } |
| 248 | |
| 249 | void CYDeclarations::For(CYOutput &out) const { |
| 250 | out << "var "; |
| 251 | Output(out, CYNoIn); |
| 252 | } |
| 253 | |
| 254 | void CYDeclarations::Output(CYOutput &out, CYFlags flags) const { |
| 255 | const CYDeclarations *declaration(this); |
| 256 | output: |
| 257 | CYDeclarations *next(declaration->next_); |
| 258 | CYFlags right(next == NULL ? CYRight(flags) : CYCenter(flags)); |
| 259 | declaration->declaration_->Output(out, right); |
| 260 | |
| 261 | if (next != NULL) { |
| 262 | out << ','; |
| 263 | declaration = next; |
| 264 | goto output; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | void CYDirectMember::Output(CYOutput &out, CYFlags flags) const { |
| 269 | object_->Output(out, Precedence(), CYLeft(flags)); |
| 270 | if (const char *word = property_->Word()) |
| 271 | out << '.' << word; |
| 272 | else { |
| 273 | out << '['; |
| 274 | property_->Output(out, CYNoFlags); |
| 275 | out << ']'; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | void CYDoWhile::Output(CYOutput &out) const { |
| 280 | // XXX: extra space character! |
| 281 | out << "do "; |
| 282 | code_->Output(out, false); |
| 283 | out << "while("; |
| 284 | test_->Output(out, CYNoFlags); |
| 285 | out << ')'; |
| 286 | } |
| 287 | |
| 288 | void CYElement::Output(CYOutput &out) const { |
| 289 | if (value_ != NULL) |
| 290 | value_->Output(out, CYPA, CYNoFlags); |
| 291 | if (next_ != NULL || value_ == NULL) |
| 292 | out << ','; |
| 293 | if (next_ != NULL) |
| 294 | next_->Output(out); |
| 295 | } |
| 296 | |
| 297 | void CYEmpty::Output(CYOutput &out) const { |
| 298 | out << ';'; |
| 299 | } |
| 300 | |
| 301 | void CYEmpty::Output(CYOutput &out, bool block) const { |
| 302 | if (next_ != NULL) |
| 303 | CYSource::Output(out, block); |
| 304 | else |
| 305 | out << "{}"; |
| 306 | } |
| 307 | |
| 308 | void CYExpress::Output(CYOutput &out) const { |
| 309 | expression_->Output(out, CYNoBF); |
| 310 | out << ';'; |
| 311 | } |
| 312 | |
| 313 | void CYExpression::ClassName(CYOutput &out, bool object) const { |
| 314 | Output(out, CYPA, CYNoFlags); |
| 315 | } |
| 316 | |
| 317 | const char *CYExpression::ForEachIn() const { |
| 318 | return NULL; |
| 319 | } |
| 320 | |
| 321 | void CYExpression::For(CYOutput &out) const { |
| 322 | Output(out, CYNoIn); |
| 323 | } |
| 324 | |
| 325 | void CYExpression::ForEachIn(CYOutput &out) const { |
| 326 | // XXX: this should handle LeftHandSideExpression |
| 327 | Output(out, CYPA, CYNoFlags); |
| 328 | } |
| 329 | |
| 330 | void CYExpression::ForIn(CYOutput &out, CYFlags flags) const { |
| 331 | // XXX: this should handle LeftHandSideExpression |
| 332 | Output(out, flags); |
| 333 | } |
| 334 | |
| 335 | void CYExpression::Output(CYOutput &out, unsigned precedence, CYFlags flags) const { |
| 336 | if (precedence < Precedence()) { |
| 337 | out << '('; |
| 338 | Output(out, CYNoFlags); |
| 339 | out << ')'; |
| 340 | } else |
| 341 | Output(out, flags); |
| 342 | } |
| 343 | |
| 344 | void CYField::Output(CYOutput &out) const { |
| 345 | // XXX: implement! |
| 346 | } |
| 347 | |
| 348 | void CYFor::Output(CYOutput &out) const { |
| 349 | out << "for("; |
| 350 | if (initialiser_ != NULL) |
| 351 | initialiser_->For(out); |
| 352 | out << ';'; |
| 353 | if (test_ != NULL) |
| 354 | test_->Output(out, CYNoFlags); |
| 355 | out << ';'; |
| 356 | if (increment_ != NULL) |
| 357 | increment_->Output(out, CYNoFlags); |
| 358 | out << ')'; |
| 359 | code_->Output(out, false); |
| 360 | } |
| 361 | |
| 362 | void CYForEachIn::Output(CYOutput &out) const { |
| 363 | out << "with({$cys:0,$cyt:0}){"; |
| 364 | |
| 365 | out << "$cys="; |
| 366 | set_->Output(out, CYPA, CYNoFlags); |
| 367 | out << ";"; |
| 368 | |
| 369 | out << "for($cyt in $cys){"; |
| 370 | |
| 371 | initialiser_->ForEachIn(out); |
| 372 | out << "=$cys[$cyt];"; |
| 373 | |
| 374 | code_->Show(out); |
| 375 | |
| 376 | out << '}'; |
| 377 | |
| 378 | out << '}'; |
| 379 | } |
| 380 | |
| 381 | void CYForEachInComprehension::Begin_(CYOutput &out) const { |
| 382 | out << "(function($cys){"; |
| 383 | out << "$cys="; |
| 384 | set_->Output(out, CYPA, CYNoFlags); |
| 385 | out << ";"; |
| 386 | |
| 387 | out << "for(" << *name_ << " in $cys){"; |
| 388 | out << *name_ << "=$cys[" << *name_ << "];"; |
| 389 | } |
| 390 | |
| 391 | void CYForEachInComprehension::End_(CYOutput &out) const { |
| 392 | out << "}}());"; |
| 393 | } |
| 394 | |
| 395 | void CYForIn::Output(CYOutput &out) const { |
| 396 | out << "for("; |
| 397 | initialiser_->ForIn(out, CYNoIn | CYNoTrailer); |
| 398 | out << "in"; |
| 399 | set_->Output(out, CYNoLeader); |
| 400 | out << ')'; |
| 401 | code_->Output(out, false); |
| 402 | } |
| 403 | |
| 404 | void CYForInComprehension::Begin_(CYOutput &out) const { |
| 405 | out << "for(" << *name_ << " in"; |
| 406 | set_->Output(out, CYNoLeader); |
| 407 | out << ')'; |
| 408 | } |
| 409 | |
| 410 | void CYFunction::Output(CYOutput &out) const { |
| 411 | CYLambda::Output(out, CYNoFlags); |
| 412 | } |
| 413 | |
| 414 | void CYFunctionParameter::Output(CYOutput &out) const { |
| 415 | out << *name_; |
| 416 | if (next_ != NULL) { |
| 417 | out << ','; |
| 418 | out << *next_; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | void CYIf::Output(CYOutput &out) const { |
| 423 | out << "if("; |
| 424 | test_->Output(out, CYNoFlags); |
| 425 | out << ')'; |
| 426 | true_->Output(out, true); |
| 427 | if (false_ != NULL) { |
| 428 | out << "else "; |
| 429 | false_->Output(out, false); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | void CYIfComprehension::Begin_(CYOutput &out) const { |
| 434 | out << "if("; |
| 435 | test_->Output(out, CYNoFlags); |
| 436 | out << ')'; |
| 437 | } |
| 438 | |
| 439 | void CYIndirect::Output(CYOutput &out, CYFlags flags) const { |
| 440 | rhs_->Output(out, 1, CYLeft(flags)); |
| 441 | out << ".$cyi"; |
| 442 | } |
| 443 | |
| 444 | void CYIndirectMember::Output(CYOutput &out, CYFlags flags) const { |
| 445 | object_->Output(out, Precedence(), CYLeft(flags)); |
| 446 | out << ".$cyi"; |
| 447 | if (const char *word = property_->Word()) |
| 448 | out << '.' << word; |
| 449 | else { |
| 450 | out << '['; |
| 451 | property_->Output(out, CYNoFlags); |
| 452 | out << ']'; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | void CYInfix::Output(CYOutput &out, CYFlags flags) const { |
| 457 | const char *name(Operator()); |
| 458 | bool protect((flags & CYNoIn) != 0 && strcmp(name, "in")); |
| 459 | if (protect) |
| 460 | out << '('; |
| 461 | bool alphabetic(Alphabetic()); |
| 462 | CYFlags left(protect ? CYNoFlags : CYLeft(flags)); |
| 463 | if (alphabetic) |
| 464 | left |= CYNoTrailer; |
| 465 | lhs_->Output(out, Precedence(), left); |
| 466 | out << name; |
| 467 | CYFlags right(protect ? CYNoFlags : CYRight(flags)); |
| 468 | if (alphabetic) |
| 469 | right |= CYNoLeader; |
| 470 | if (strcmp(name, "-") == 0) |
| 471 | right |= CYNoHyphen; |
| 472 | rhs_->Output(out, Precedence() - 1, right); |
| 473 | if (protect) |
| 474 | out << ')'; |
| 475 | } |
| 476 | |
| 477 | void CYLambda::Output(CYOutput &out, CYFlags flags) const { |
| 478 | bool protect((flags & CYNoFunction) != 0); |
| 479 | if (protect) |
| 480 | out << '('; |
| 481 | else if ((flags & CYNoLeader) != 0) |
| 482 | out << ' '; |
| 483 | out << "function"; |
| 484 | if (name_ != NULL) |
| 485 | out << ' ' << *name_; |
| 486 | out << '('; |
| 487 | if (parameters_ != NULL) |
| 488 | out << *parameters_; |
| 489 | out << "){"; |
| 490 | if (body_ != NULL) |
| 491 | body_->Show(out); |
| 492 | out << '}'; |
| 493 | if (protect) |
| 494 | out << ')'; |
| 495 | } |
| 496 | |
| 497 | void CYLet::Output(CYOutput &out) const { |
| 498 | out << "let("; |
| 499 | declarations_->Output(out, CYNoFlags); |
| 500 | out << "){"; |
| 501 | if (statements_ != NULL) |
| 502 | statements_->Show(out); |
| 503 | out << "}"; |
| 504 | } |
| 505 | |
| 506 | void CYMessage::Output(CYOutput &out, bool replace) const { |
| 507 | if (next_ != NULL) |
| 508 | next_->Output(out, replace); |
| 509 | out << "$cyn=new Selector(\""; |
| 510 | for (CYMessageParameter *parameter(parameter_); parameter != NULL; parameter = parameter->next_) |
| 511 | if (parameter->tag_ != NULL) { |
| 512 | out << *parameter->tag_; |
| 513 | if (parameter->name_ != NULL) |
| 514 | out << ':'; |
| 515 | } |
| 516 | out << "\");"; |
| 517 | out << "$cyt=$cyn.type($cy" << (instance_ ? 's' : 'p') << ");"; |
| 518 | out << "class_" << (replace ? "replace" : "add") << "Method($cy" << (instance_ ? 'c' : 'm') << ",$cyn,"; |
| 519 | out << "new Functor(function(self,_cmd"; |
| 520 | for (CYMessageParameter *parameter(parameter_); parameter != NULL; parameter = parameter->next_) |
| 521 | if (parameter->name_ != NULL) |
| 522 | out << ',' << *parameter->name_; |
| 523 | out << "){return function(){"; |
| 524 | if (body_ != NULL) |
| 525 | body_->Show(out); |
| 526 | out << "}.call(self);},$cyt),$cyt);"; |
| 527 | } |
| 528 | |
| 529 | void CYNew::Output(CYOutput &out, CYFlags flags) const { |
| 530 | if ((flags & CYNoLeader) != 0) |
| 531 | out << ' '; |
| 532 | out << "new"; |
| 533 | constructor_->Output(out, Precedence(), CYCenter(flags) | CYNoLeader); |
| 534 | out << '('; |
| 535 | if (arguments_ != NULL) |
| 536 | arguments_->Output(out); |
| 537 | out << ')'; |
| 538 | } |
| 539 | |
| 540 | void CYNull::Output(CYOutput &out, CYFlags flags) const { |
| 541 | if ((flags & CYNoLeader) != 0) |
| 542 | out << ' '; |
| 543 | CYWord::Output(out); |
| 544 | if ((flags & CYNoTrailer) != 0) |
| 545 | out << ' '; |
| 546 | } |
| 547 | |
| 548 | void CYNumber::Output(CYOutput &out, CYFlags flags) const { |
| 549 | double value(Value()); |
| 550 | if ((flags & CYNoLeader) != 0 || value < 0 && (flags & CYNoHyphen) != 0) |
| 551 | out << ' '; |
| 552 | // XXX: decide on correct precision |
| 553 | out.out_ << std::setprecision(9) << value; |
| 554 | if ((flags & CYNoTrailer) != 0) |
| 555 | out << ' '; |
| 556 | } |
| 557 | |
| 558 | void CYNumber::PropertyName(CYOutput &out) const { |
| 559 | Output(out); |
| 560 | } |
| 561 | |
| 562 | void CYObject::Output(CYOutput &out, CYFlags flags) const { |
| 563 | bool protect((flags & CYNoBrace) != 0); |
| 564 | if (protect) |
| 565 | out << '('; |
| 566 | out << '{'; |
| 567 | if (property_ != NULL) |
| 568 | property_->Output(out); |
| 569 | out << '}'; |
| 570 | if (protect) |
| 571 | out << ')'; |
| 572 | } |
| 573 | |
| 574 | void CYPostfix::Output(CYOutput &out, CYFlags flags) const { |
| 575 | lhs_->Output(out, Precedence(), CYLeft(flags)); |
| 576 | out << Operator(); |
| 577 | } |
| 578 | |
| 579 | void CYPrefix::Output(CYOutput &out, CYFlags flags) const { |
| 580 | const char *name(Operator()); |
| 581 | bool alphabetic(Alphabetic()); |
| 582 | if (alphabetic && (flags & CYNoLeader) != 0 || name[0] == '-' && (flags & CYNoHyphen) != 0) |
| 583 | out << ' '; |
| 584 | out << name; |
| 585 | CYFlags right(CYRight(flags)); |
| 586 | if (alphabetic) |
| 587 | right |= CYNoLeader; |
| 588 | rhs_->Output(out, Precedence(), right); |
| 589 | } |
| 590 | |
| 591 | void CYProperty::Output(CYOutput &out) const { |
| 592 | name_->PropertyName(out); |
| 593 | out << ':'; |
| 594 | value_->Output(out, CYPA, CYNoFlags); |
| 595 | if (next_ != NULL) { |
| 596 | out << ','; |
| 597 | next_->Output(out); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | void CYRegEx::Output(CYOutput &out, CYFlags flags) const { |
| 602 | out << Value(); |
| 603 | if ((flags & CYNoTrailer) != 0) |
| 604 | out << ' '; |
| 605 | } |
| 606 | |
| 607 | void CYReturn::Output(CYOutput &out) const { |
| 608 | out << "return"; |
| 609 | if (value_ != NULL) |
| 610 | value_->Output(out, CYNoLeader); |
| 611 | out << ';'; |
| 612 | } |
| 613 | |
| 614 | void CYSelector::Output(CYOutput &out, CYFlags flags) const { |
| 615 | if ((flags & CYNoLeader) != 0) |
| 616 | out << ' '; |
| 617 | out << "new Selector(\""; |
| 618 | if (name_ != NULL) |
| 619 | name_->Output(out); |
| 620 | out << "\")"; |
| 621 | } |
| 622 | |
| 623 | void CYSelectorPart::Output(CYOutput &out) const { |
| 624 | if (name_ != NULL) |
| 625 | out << *name_; |
| 626 | if (value_) |
| 627 | out << ':'; |
| 628 | if (next_ != NULL) |
| 629 | next_->Output(out); |
| 630 | } |
| 631 | |
| 632 | void CYSend::Output(CYOutput &out, CYFlags flags) const { |
| 633 | if ((flags & CYNoLeader) != 0) |
| 634 | out << ' '; |
| 635 | out << "objc_msgSend("; |
| 636 | self_->Output(out, CYPA, CYNoFlags); |
| 637 | out << ","; |
| 638 | std::ostringstream name; |
| 639 | for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_) |
| 640 | if (argument->name_ != NULL) { |
| 641 | name << *argument->name_; |
| 642 | if (argument->value_ != NULL) |
| 643 | name << ':'; |
| 644 | } |
| 645 | out.out_ << reinterpret_cast<void *>(sel_registerName(name.str().c_str())); |
| 646 | for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_) |
| 647 | if (argument->value_ != NULL) { |
| 648 | out << ","; |
| 649 | argument->value_->Output(out, CYPA, CYNoFlags); |
| 650 | } |
| 651 | out << ')'; |
| 652 | } |
| 653 | |
| 654 | void CYSource::Show(CYOutput &out) const { |
| 655 | for (const CYSource *next(this); next != NULL; next = next->next_) |
| 656 | next->Output_(out); |
| 657 | } |
| 658 | |
| 659 | void CYSource::Output(CYOutput &out, bool block) const { |
| 660 | if (!block && !IsBlock()) |
| 661 | Output(out); |
| 662 | else { |
| 663 | out << '{'; |
| 664 | Show(out); |
| 665 | out << '}'; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | void CYSource::Output_(CYOutput &out) const { |
| 670 | Output(out); |
| 671 | } |
| 672 | |
| 673 | void CYStatement::Output_(CYOutput &out) const { |
| 674 | for (CYLabel *label(labels_); label != NULL; label = label->next_) |
| 675 | out << *label->name_ << ':'; |
| 676 | Output(out); |
| 677 | } |
| 678 | |
| 679 | void CYString::Output(CYOutput &out, CYFlags flags) const { |
| 680 | unsigned quot(0), apos(0); |
| 681 | for (const char *value(value_), *end(value_ + size_); value != end; ++value) |
| 682 | if (*value == '"') |
| 683 | ++quot; |
| 684 | else if (*value == '\'') |
| 685 | ++apos; |
| 686 | |
| 687 | bool single(quot > apos); |
| 688 | |
| 689 | out << (single ? '\'' : '"'); |
| 690 | for (const char *value(value_), *end(value_ + size_); value != end; ++value) |
| 691 | switch (*value) { |
| 692 | case '\\': out << "\\\\"; break; |
| 693 | case '\b': out << "\\b"; break; |
| 694 | case '\f': out << "\\f"; break; |
| 695 | case '\n': out << "\\n"; break; |
| 696 | case '\r': out << "\\r"; break; |
| 697 | case '\t': out << "\\t"; break; |
| 698 | case '\v': out << "\\v"; break; |
| 699 | |
| 700 | case '"': |
| 701 | if (!single) |
| 702 | out << "\\\""; |
| 703 | else goto simple; |
| 704 | break; |
| 705 | |
| 706 | case '\'': |
| 707 | if (single) |
| 708 | out << "\\'"; |
| 709 | else goto simple; |
| 710 | break; |
| 711 | |
| 712 | default: |
| 713 | if (*value < 0x20 || *value >= 0x7f) |
| 714 | out.out_ << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value); |
| 715 | else simple: |
| 716 | out << *value; |
| 717 | } |
| 718 | out << (single ? '\'' : '"'); |
| 719 | } |
| 720 | |
| 721 | void CYString::PropertyName(CYOutput &out) const { |
| 722 | if (const char *word = Word()) |
| 723 | out << word; |
| 724 | else |
| 725 | Output(out); |
| 726 | } |
| 727 | |
| 728 | void CYSwitch::Output(CYOutput &out) const { |
| 729 | out << "switch("; |
| 730 | value_->Output(out, CYNoFlags); |
| 731 | out << "){"; |
| 732 | if (clauses_ != NULL) |
| 733 | out << *clauses_; |
| 734 | out << '}'; |
| 735 | } |
| 736 | |
| 737 | void CYThis::Output(CYOutput &out, CYFlags flags) const { |
| 738 | if ((flags & CYNoLeader) != 0) |
| 739 | out << ' '; |
| 740 | CYWord::Output(out); |
| 741 | if ((flags & CYNoTrailer) != 0) |
| 742 | out << ' '; |
| 743 | } |
| 744 | |
| 745 | void CYThrow::Output(CYOutput &out) const { |
| 746 | out << "throw"; |
| 747 | if (value_ != NULL) |
| 748 | value_->Output(out, CYNoLeader); |
| 749 | out << ';'; |
| 750 | } |
| 751 | |
| 752 | void CYTry::Output(CYOutput &out) const { |
| 753 | out << "try"; |
| 754 | try_->Output(out, true); |
| 755 | if (catch_ != NULL) |
| 756 | catch_->Output(out); |
| 757 | if (finally_ != NULL) { |
| 758 | out << "finally"; |
| 759 | finally_->Output(out, true); |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | void CYVar::Output(CYOutput &out) const { |
| 764 | out << "var "; |
| 765 | declarations_->Output(out, CYNoFlags); |
| 766 | out << ';'; |
| 767 | } |
| 768 | |
| 769 | void CYVariable::Output(CYOutput &out, CYFlags flags) const { |
| 770 | if ((flags & CYNoLeader) != 0) |
| 771 | out << ' '; |
| 772 | out << *name_; |
| 773 | if ((flags & CYNoTrailer) != 0) |
| 774 | out << ' '; |
| 775 | } |
| 776 | |
| 777 | void CYWhile::Output(CYOutput &out) const { |
| 778 | out << "while("; |
| 779 | test_->Output(out, CYNoFlags); |
| 780 | out << ')'; |
| 781 | code_->Output(out, false); |
| 782 | } |
| 783 | |
| 784 | void CYWith::Output(CYOutput &out) const { |
| 785 | out << "with("; |
| 786 | scope_->Output(out, CYNoFlags); |
| 787 | out << ')'; |
| 788 | code_->Output(out, false); |
| 789 | } |
| 790 | |
| 791 | void CYWord::ClassName(CYOutput &out, bool object) const { |
| 792 | if (object) |
| 793 | out << "objc_getClass("; |
| 794 | out << '"' << Value() << '"'; |
| 795 | if (object) |
| 796 | out << ')'; |
| 797 | } |
| 798 | |
| 799 | void CYWord::Output(CYOutput &out) const { |
| 800 | out << Value(); |
| 801 | } |
| 802 | |
| 803 | void CYWord::PropertyName(CYOutput &out) const { |
| 804 | Output(out); |
| 805 | } |