00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <string.h>
00016 #include <iostream.h>
00017 #include "token.h"
00018 #include "ptree.h"
00019 #include "encoding.h"
00020 #include "walker.h"
00021
00022
00023
00024 Leaf::Leaf(char* ptr, int len)
00025 {
00026 data.leaf.position = ptr;
00027 data.leaf.length = len;
00028 }
00029
00030 Leaf::Leaf(Token& tk)
00031 {
00032 data.leaf.position = tk.ptr;
00033 data.leaf.length = tk.len;
00034 }
00035
00036 bool Leaf::IsLeaf()
00037 {
00038 return TRUE;
00039 }
00040
00041 void Leaf::Print(ostream& s, int, int)
00042 {
00043 char* p = data.leaf.position;
00044 int n = data.leaf.length;
00045
00046
00047
00048 if(n < 1)
00049 return;
00050 else if(n == 1 && *p == '@'){
00051 s << "\\@";
00052 return;
00053 }
00054
00055 char c = *p++;
00056 if(c == '[' || c == ']')
00057 s << '\\' << c;
00058 else
00059 s << c;
00060
00061 while(--n > 0)
00062 s << *p++;
00063 }
00064
00065 int Leaf::Write(ostream& out, int indent)
00066 {
00067 int n = 0;
00068 char* ptr = data.leaf.position;
00069 int len = data.leaf.length;
00070 while(len-- > 0){
00071 char c = *ptr++;
00072 if(c == '\n'){
00073 PrintIndent(out, indent);
00074 ++n;
00075 }
00076 else
00077 out << c;
00078 }
00079
00080 return n;
00081 }
00082
00083 void Leaf::WritePS(ProgramString& out)
00084 {
00085 char* ptr = data.leaf.position;
00086 int len = data.leaf.length;
00087 while(len-- > 0)
00088 out << *ptr++;
00089 }
00090
00091
00092
00093
00094 LeafName::LeafName(Token& t) : Leaf(t) {}
00095
00096 Ptree* LeafName::Translate(Walker* w)
00097 {
00098 return w->TranslateVariable(this);
00099 }
00100
00101 void LeafName::Typeof(Walker* w, TypeInfo& t)
00102 {
00103 w->TypeofVariable(this, t);
00104 }
00105
00106
00107
00108
00109 DupLeaf::DupLeaf(char* str, int len) : Leaf(new (GC) char[len], len)
00110 {
00111 memmove(data.leaf.position, str, len);
00112 }
00113
00114 DupLeaf::DupLeaf(char* str1, int len1, char* str2, int len2)
00115 : Leaf(new (GC) char[len1 + len2], len1 + len2)
00116 {
00117 memmove(data.leaf.position, str1, len1);
00118 memmove(&data.leaf.position[len1], str2, len2);
00119 }
00120
00121 void DupLeaf::Print(ostream& s, int, int)
00122 {
00123 int i, j;
00124 char* pos;
00125
00126 pos = data.leaf.position;
00127 j = data.leaf.length;
00128
00129 if(j == 1 && *pos == '@'){
00130 s << "\\@";
00131 return;
00132 }
00133
00134 s << '`';
00135 for(i = 0; i < j; ++i)
00136 if(pos[i] == '[' || pos[i] == ']'){
00137 s << '\\';
00138 s << pos[i];
00139 }
00140 else
00141 s << pos[i];
00142
00143 s << '`';
00144 }
00145
00146 int LeafThis::What()
00147 {
00148 return THIS;
00149 }
00150
00151 Ptree* LeafThis::Translate(Walker* w)
00152 {
00153 return w->TranslateThis(this);
00154 }
00155
00156 void LeafThis::Typeof(Walker* w, TypeInfo& t)
00157 {
00158 w->TypeofThis(this, t);
00159 }
00160
00161 #define ResearvedWordImpl(w) \
00162 int Leaf##w::What() { return w; }
00163
00164 ResearvedWordImpl(AUTO)
00165 ResearvedWordImpl(BOOLEAN)
00166 ResearvedWordImpl(CHAR)
00167 ResearvedWordImpl(CONST)
00168 ResearvedWordImpl(DOUBLE)
00169 ResearvedWordImpl(EXTERN)
00170 ResearvedWordImpl(FLOAT)
00171 ResearvedWordImpl(FRIEND)
00172 ResearvedWordImpl(INLINE)
00173 ResearvedWordImpl(INT)
00174 ResearvedWordImpl(LONG)
00175 ResearvedWordImpl(MUTABLE)
00176 ResearvedWordImpl(NAMESPACE)
00177 ResearvedWordImpl(PRIVATE)
00178 ResearvedWordImpl(PROTECTED)
00179 ResearvedWordImpl(PUBLIC)
00180 ResearvedWordImpl(REGISTER)
00181 ResearvedWordImpl(SHORT)
00182 ResearvedWordImpl(SIGNED)
00183 ResearvedWordImpl(STATIC)
00184 ResearvedWordImpl(UNSIGNED)
00185 ResearvedWordImpl(USING)
00186 ResearvedWordImpl(VIRTUAL)
00187 ResearvedWordImpl(VOID)
00188 ResearvedWordImpl(VOLATILE)
00189
00190 ResearvedWordImpl(UserKeyword2)
00191
00192 #undef ResearvedWordImpl
00193
00194
00195
00196 NonLeaf::NonLeaf(Ptree* p, Ptree* q)
00197 {
00198 data.nonleaf.child = p;
00199 data.nonleaf.next = q;
00200 }
00201
00202 bool NonLeaf::IsLeaf()
00203 {
00204 return FALSE;
00205 }
00206
00207 void NonLeaf::Print(ostream& s, int indent, int depth)
00208 {
00209 if(TooDeep(s, depth))
00210 return;
00211
00212 Ptree* rest = this;
00213 s << '[';
00214 while(rest != nil){
00215 if(rest->IsLeaf()){
00216 s << "@ ";
00217 rest->Print(s, indent, depth + 1);
00218 rest = nil;
00219 }
00220 else{
00221 Ptree* head = rest->data.nonleaf.child;
00222 if(head == nil)
00223 s << "nil";
00224 else
00225 head->Print(s, indent, depth + 1);
00226 rest = rest->data.nonleaf.next;
00227 if(rest != nil)
00228 s << ' ';
00229 }
00230 }
00231
00232 s << ']';
00233 }
00234
00235 bool NonLeaf::TooDeep(ostream& s, int depth)
00236 {
00237 if(depth >= 32){
00238 s << " ** too many nestings ** ";
00239 return TRUE;
00240 }
00241 else
00242 return FALSE;
00243 }
00244
00245 int NonLeaf::Write(ostream& out, int indent)
00246 {
00247 int n = 0;
00248 Ptree* p = this;
00249 for(;;){
00250 Ptree* head = p->Car();
00251 if(head != nil)
00252 n += head->Write(out, indent);
00253
00254 p = p->Cdr();
00255 if(p == nil)
00256 break;
00257 else if(p->IsLeaf()){
00258 MopErrorMessage("NonLeaf::Write()", "not list");
00259 break;
00260 }
00261 else
00262 out << ' ';
00263 }
00264
00265 return n;
00266 }
00267
00268 void NonLeaf::PrintWithEncodeds(ostream& s, int indent, int depth)
00269 {
00270 char* encode = GetEncodedType();
00271 if(encode != nil){
00272 s << '#';
00273 Encoding::Print(s, encode);
00274 }
00275
00276 encode = GetEncodedName();
00277 if(encode != nil){
00278 s << '@';
00279 Encoding::Print(s, encode);
00280 }
00281
00282 NonLeaf::Print(s, indent, depth);
00283 }
00284
00285 void NonLeaf::WritePS(ProgramString& out)
00286 {
00287 Ptree* p = this;
00288 for(;;){
00289 Ptree* head = p->Car();
00290 if(head != nil)
00291 head->WritePS(out);
00292
00293 p = p->Cdr();
00294 if(p == nil)
00295 break;
00296 else if(p->IsLeaf()){
00297 MopErrorMessage("NonLeaf::WritePS()", "not list");
00298 break;
00299 }
00300 else
00301 out << ' ';
00302 }
00303 }
00304
00305
00306
00307
00308 void PtreeBrace::Print(ostream& s, int indent, int depth)
00309 {
00310 if(TooDeep(s, depth))
00311 return;
00312
00313 int indent2 = indent + 1;
00314 s << "[{";
00315 Ptree* body = Ptree::Second(this);
00316 if(body == nil){
00317 PrintIndent(s, indent2);
00318 s << "nil";
00319 }
00320 else
00321 while(body != nil){
00322 PrintIndent(s, indent2);
00323 if(body->IsLeaf()){
00324 s << "@ ";
00325 body->Print(s, indent + 1, depth + 1);
00326 }
00327 else{
00328 Ptree* head = body->Car();
00329 if(head == nil)
00330 s << "nil";
00331 else
00332 head->Print(s, indent + 1, depth + 1);
00333 }
00334
00335 body = body->Cdr();
00336 }
00337
00338 PrintIndent(s, indent);
00339 s << "}]";
00340 }
00341
00342 int PtreeBrace::Write(ostream& out, int indent)
00343 {
00344 int n = 0;
00345
00346 out << '{';
00347 Ptree* p = this->Cadr();
00348 while(p != nil){
00349 if(p->IsLeaf()){
00350 MopErrorMessage("PtreeBrace::Write()", "non list");
00351 break;
00352 }
00353 else{
00354 PrintIndent(out, indent + 1);
00355 ++n;
00356 Ptree* q = p->Car();
00357 p = p->Cdr();
00358 if(q != nil)
00359 n += q->Write(out, indent + 1);
00360 }
00361 }
00362
00363 PrintIndent(out, indent);
00364 ++n;
00365 out << '}';
00366 return n;
00367 }
00368
00369 Ptree* PtreeBrace::Translate(Walker* w)
00370 {
00371 return w->TranslateBrace(this);
00372 }
00373
00374
00375
00376 Ptree* PtreeBlock::Translate(Walker* w)
00377 {
00378 return w->TranslateBlock(this);
00379 }
00380
00381
00382
00383 Ptree* PtreeClassBody::Translate(Walker* w)
00384 {
00385 return w->TranslateClassBody(this, nil, nil);
00386 }
00387
00388
00389
00390 int PtreeTypedef::What()
00391 {
00392 return ntTypedef;
00393 }
00394
00395 Ptree* PtreeTypedef::Translate(Walker* w)
00396 {
00397 return w->TranslateTypedef(this);
00398 }
00399
00400
00401
00402 int PtreeTemplateDecl::What()
00403 {
00404 return ntTemplateDecl;
00405 }
00406
00407 Ptree* PtreeTemplateDecl::Translate(Walker* w)
00408 {
00409 return w->TranslateTemplateDecl(this);
00410 }
00411
00412
00413
00414 int PtreeTemplateInstantiation::What()
00415 {
00416 return ntTemplateInstantiation;
00417 }
00418
00419 Ptree* PtreeTemplateInstantiation::Translate(Walker* w)
00420 {
00421 return w->TranslateTemplateInstantiation(this);
00422 }
00423
00424
00425
00426 int PtreeExternTemplate::What()
00427 {
00428 return ntExternTemplate;
00429 }
00430
00431 Ptree* PtreeExternTemplate::Translate(Walker* w)
00432 {
00433 return w->TranslateExternTemplate(this);
00434 }
00435
00436
00437
00438 int PtreeMetaclassDecl::What()
00439 {
00440 return ntMetaclassDecl;
00441 }
00442
00443 Ptree* PtreeMetaclassDecl::Translate(Walker* w)
00444 {
00445 return w->TranslateMetaclassDecl(this);
00446 }
00447
00448
00449
00450 int PtreeLinkageSpec::What()
00451 {
00452 return ntLinkageSpec;
00453 }
00454
00455 Ptree* PtreeLinkageSpec::Translate(Walker* w)
00456 {
00457 return w->TranslateLinkageSpec(this);
00458 }
00459
00460
00461
00462 int PtreeNamespaceSpec::What()
00463 {
00464 return ntNamespaceSpec;
00465 }
00466
00467 Ptree* PtreeNamespaceSpec::Translate(Walker* w)
00468 {
00469 return w->TranslateNamespaceSpec(this);
00470 }
00471
00472
00473
00474 PtreeUsing::PtreeUsing(Ptree* p, Ptree* q, Ptree* r, Encoding& e, Ptree* s)
00475 : NonLeaf(p, Ptree::List(q, r, s))
00476 {
00477 name = e.Get();
00478 }
00479
00480 char* PtreeUsing::GetEncodedName()
00481 {
00482 return name;
00483 }
00484
00485 int PtreeUsing::What()
00486 {
00487 return ntUsing;
00488 }
00489
00490 Ptree* PtreeUsing::Translate(Walker* w)
00491 {
00492 return w->TranslateUsing(this);
00493 }
00494
00495
00496
00497 int PtreeDeclaration::What()
00498 {
00499 return ntDeclaration;
00500 }
00501
00502 Ptree* PtreeDeclaration::Translate(Walker* w)
00503 {
00504 return w->TranslateDeclaration(this);
00505 }
00506
00507
00508
00509 PtreeDeclarator::PtreeDeclarator(Ptree* list, Encoding& t, Encoding& n,
00510 Ptree* dname)
00511 : NonLeaf(list->Car(), list->Cdr())
00512 {
00513 type = t.Get();
00514 name = n.Get();
00515 declared_name = dname;
00516 comments = nil;
00517 }
00518
00519 PtreeDeclarator::PtreeDeclarator(Encoding& t, Encoding& n, Ptree* dname)
00520 : NonLeaf(nil, nil)
00521 {
00522 type = t.Get();
00523 name = n.Get();
00524 declared_name = dname;
00525 comments = nil;
00526 }
00527
00528 PtreeDeclarator::PtreeDeclarator(Ptree* p, Ptree* q,
00529 Encoding& t, Encoding& n, Ptree* dname)
00530 : NonLeaf(p, q)
00531 {
00532 type = t.Get();
00533 name = n.Get();
00534 declared_name = dname;
00535 comments = nil;
00536 }
00537
00538 PtreeDeclarator::PtreeDeclarator(Ptree* list, Encoding& t)
00539 : NonLeaf(list->Car(), list->Cdr())
00540 {
00541 type = t.Get();
00542 name = nil;
00543 declared_name = nil;
00544 comments = nil;
00545 }
00546
00547 PtreeDeclarator::PtreeDeclarator(Encoding& t)
00548 : NonLeaf(nil, nil)
00549 {
00550 type = t.Get();
00551 name = nil;
00552 declared_name = nil;
00553 comments = nil;
00554 }
00555
00556 PtreeDeclarator::PtreeDeclarator(PtreeDeclarator* decl, Ptree* p, Ptree* q)
00557 : NonLeaf(p, q)
00558 {
00559 type = decl->type;
00560 name = decl->name;
00561 declared_name = decl->declared_name;
00562 comments = nil;
00563 }
00564
00565 int PtreeDeclarator::What()
00566 {
00567 return ntDeclarator;
00568 }
00569
00570 char* PtreeDeclarator::GetEncodedType()
00571 {
00572 return type;
00573 }
00574
00575 char* PtreeDeclarator::GetEncodedName()
00576 {
00577 return name;
00578 }
00579
00580 void PtreeDeclarator::Print(ostream& s, int i, int d)
00581 {
00582 if(show_encoded)
00583 PrintWithEncodeds(s, i, d);
00584 else
00585 NonLeaf::Print(s, i, d);
00586 }
00587
00588
00589
00590
00591 PtreeName::PtreeName(Ptree* p, Encoding& e)
00592 : NonLeaf(p->Car(), p->Cdr())
00593 {
00594 name = e.Get();
00595 }
00596
00597 int PtreeName::What()
00598 {
00599 return ntName;
00600 }
00601
00602 char* PtreeName::GetEncodedName()
00603 {
00604 return name;
00605 }
00606
00607 void PtreeName::Print(ostream& s, int i, int d)
00608 {
00609 if(show_encoded)
00610 PrintWithEncodeds(s, i, d);
00611 else
00612 NonLeaf::Print(s, i, d);
00613 }
00614
00615 Ptree* PtreeName::Translate(Walker* w)
00616 {
00617 return w->TranslateVariable(this);
00618 }
00619
00620 void PtreeName::Typeof(Walker* w, TypeInfo& t)
00621 {
00622 w->TypeofVariable(this, t);
00623 }
00624
00625
00626
00627 PtreeFstyleCastExpr::PtreeFstyleCastExpr(Encoding& e, Ptree* p, Ptree* q)
00628 : NonLeaf(p, q)
00629 {
00630 type = e.Get();
00631 }
00632
00633 PtreeFstyleCastExpr::PtreeFstyleCastExpr(char* e, Ptree* p, Ptree* q)
00634 : NonLeaf(p, q)
00635 {
00636 type = e;
00637 }
00638
00639 int PtreeFstyleCastExpr::What()
00640 {
00641 return ntFstyleCast;
00642 }
00643
00644 char* PtreeFstyleCastExpr::GetEncodedType()
00645 {
00646 return type;
00647 }
00648
00649 void PtreeFstyleCastExpr::Print(ostream& s, int i, int d)
00650 {
00651 if(show_encoded)
00652 PrintWithEncodeds(s, i, d);
00653 else
00654 NonLeaf::Print(s, i, d);
00655 }
00656
00657 Ptree* PtreeFstyleCastExpr::Translate(Walker* w)
00658 {
00659 return w->TranslateFstyleCast(this);
00660 }
00661
00662 void PtreeFstyleCastExpr::Typeof(Walker* w, TypeInfo& t)
00663 {
00664 w->TypeofFstyleCast(this, t);
00665 }
00666
00667
00668
00669 PtreeClassSpec::PtreeClassSpec(Ptree* p, Ptree* q, Ptree* c)
00670 : NonLeaf(p, q)
00671 {
00672 encoded_name = nil;
00673 comments = c;
00674 }
00675
00676 PtreeClassSpec::PtreeClassSpec(Ptree* car, Ptree* cdr,
00677 Ptree* c, char* encode) : NonLeaf(car, cdr)
00678 {
00679 encoded_name = encode;
00680 comments = c;
00681 }
00682
00683 int PtreeClassSpec::What()
00684 {
00685 return ntClassSpec;
00686 }
00687
00688 Ptree* PtreeClassSpec::Translate(Walker* w)
00689 {
00690 return w->TranslateClassSpec(this);
00691 }
00692
00693 char* PtreeClassSpec::GetEncodedName()
00694 {
00695 return encoded_name;
00696 }
00697
00698 Ptree* PtreeClassSpec::GetComments()
00699 {
00700 return comments;
00701 }
00702
00703
00704
00705 PtreeEnumSpec::PtreeEnumSpec(Ptree* head)
00706 : NonLeaf(head, nil)
00707 {
00708 encoded_name = nil;
00709 }
00710
00711 int PtreeEnumSpec::What()
00712 {
00713 return ntEnumSpec;
00714 }
00715
00716 Ptree* PtreeEnumSpec::Translate(Walker* w)
00717 {
00718 return w->TranslateEnumSpec(this);
00719 }
00720
00721 char* PtreeEnumSpec::GetEncodedName()
00722 {
00723 return encoded_name;
00724 }
00725
00726 PtreeAccessSpec::PtreeAccessSpec(Ptree* p, Ptree* q)
00727 : NonLeaf(p, q)
00728 {
00729 }
00730
00731 int PtreeAccessSpec::What()
00732 {
00733 return ntAccessSpec;
00734 }
00735
00736 Ptree* PtreeAccessSpec::Translate(Walker* w)
00737 {
00738 return w->TranslateAccessSpec(this);
00739 }
00740
00741 PtreeAccessDecl::PtreeAccessDecl(Ptree* p, Ptree* q)
00742 : NonLeaf(p, q)
00743 {
00744 }
00745
00746 int PtreeAccessDecl::What()
00747 {
00748 return ntAccessDecl;
00749 }
00750
00751 Ptree* PtreeAccessDecl::Translate(Walker* w)
00752 {
00753 return w->TranslateAccessDecl(this);
00754 }
00755
00756 PtreeUserAccessSpec::PtreeUserAccessSpec(Ptree* p, Ptree* q)
00757 : NonLeaf(p, q)
00758 {
00759 }
00760
00761 int PtreeUserAccessSpec::What()
00762 {
00763 return ntUserAccessSpec;
00764 }
00765
00766 Ptree* PtreeUserAccessSpec::Translate(Walker* w)
00767 {
00768 return w->TranslateUserAccessSpec(this);
00769 }
00770
00771 PtreeUserdefKeyword::PtreeUserdefKeyword(Ptree* p, Ptree* q)
00772 : NonLeaf(p, q)
00773 {
00774 }
00775
00776 int PtreeUserdefKeyword::What()
00777 {
00778 return ntUserdefKeyword;
00779 }
00780
00781 #define PtreeStatementImpl(s)\
00782 int Ptree##s##Statement::What() { return nt##s##Statement; }\
00783 Ptree* Ptree##s##Statement::Translate(Walker* w) {\
00784 return w->Translate##s(this); }
00785
00786 PtreeStatementImpl(If)
00787 PtreeStatementImpl(Switch)
00788 PtreeStatementImpl(While)
00789 PtreeStatementImpl(Do)
00790 PtreeStatementImpl(For)
00791 PtreeStatementImpl(Try)
00792 PtreeStatementImpl(Break)
00793 PtreeStatementImpl(Continue)
00794 PtreeStatementImpl(Return)
00795 PtreeStatementImpl(Goto)
00796 PtreeStatementImpl(Case)
00797 PtreeStatementImpl(Default)
00798 PtreeStatementImpl(Label)
00799
00800 #undef PtreeStatementImpl
00801
00802 int PtreeExprStatement::What()
00803 {
00804 return ntExprStatement;
00805 }
00806
00807 Ptree* PtreeExprStatement::Translate(Walker* w)
00808 {
00809 return w->TranslateExprStatement(this);
00810 }
00811
00812 #define PtreeExprImpl(n)\
00813 int Ptree##n##Expr::What() { return nt##n##Expr; }\
00814 Ptree* Ptree##n##Expr::Translate(Walker* w) {\
00815 return w->Translate##n(this); }\
00816 void Ptree##n##Expr::Typeof(Walker* w, TypeInfo& t) {\
00817 w->Typeof##n(this, t); }
00818
00819 PtreeExprImpl(Comma)
00820 PtreeExprImpl(Assign)
00821 PtreeExprImpl(Cond)
00822 PtreeExprImpl(Infix)
00823 PtreeExprImpl(Pm)
00824 PtreeExprImpl(Cast)
00825 PtreeExprImpl(Unary)
00826 PtreeExprImpl(Throw)
00827 PtreeExprImpl(Sizeof)
00828 PtreeExprImpl(New)
00829 PtreeExprImpl(Delete)
00830 PtreeExprImpl(Array)
00831 PtreeExprImpl(Funcall)
00832 PtreeExprImpl(Postfix)
00833 PtreeExprImpl(UserStatement)
00834 PtreeExprImpl(DotMember)
00835 PtreeExprImpl(ArrowMember)
00836 PtreeExprImpl(Paren)
00837 PtreeExprImpl(StaticUserStatement)
00838
00839 #undef PtreeExprImpl