Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

ptree-core.h

Go to the documentation of this file.
00001 /*
00002   Copyright (C) 1997-2001 Shigeru Chiba, Tokyo Institute of Technology.
00003 
00004   Permission to use, copy, distribute and modify this software and   
00005   its documentation for any purpose is hereby granted without fee,        
00006   provided that the above copyright notice appear in all copies and that 
00007   both that copyright notice and this permission notice appear in 
00008   supporting documentation.
00009 
00010   Shigeru Chiba makes no representations about the suitability of this 
00011   software for any purpose.  It is provided "as is" without express or
00012   implied warranty.
00013 */
00014 
00015 #ifndef _ptree_core_h
00016 #define _ptree_core_h
00017 
00018 #include "types.h"
00019 
00020 #define NIL     ((Ptree*)0)
00021 
00022 #include <iostream>
00023 
00024 class ProgramString;
00025 class Walker;
00026 class TypeInfo;
00027 
00028 #ifdef __opencxx
00029 metaclass QuoteClass Ptree;           // get qMake() available
00030 #endif
00031 
00032 class OCXXMOP Ptree : public LightObject {
00033 public:
00034     virtual bool IsLeaf() = nil;
00035     bool Eq(char);
00036     bool Eq(char*);
00037     bool Eq(const char*, int);
00038     bool Eq(Ptree* p) { return Eq(this, p); }
00039 
00040     void Display();
00041     void Display2(ostream&);
00042     virtual void Print(ostream&, int, int) = nil;
00043     int Write(ostream&);
00044     virtual int Write(ostream&, int) = nil;
00045     void PrintIndent(ostream&, int);
00046 
00047     char* ToString();
00048     virtual void WritePS(ProgramString&) = nil;
00049 
00050     char* GetPosition() { return data.leaf.position; }
00051     int GetLength() { return data.leaf.length; }
00052 
00053     Ptree* Car() { return data.nonleaf.child; }
00054     Ptree* Cdr() { return data.nonleaf.next; }
00055     Ptree* Cadr() { return Cdr()->Car(); }
00056     Ptree* Cddr() { return Cdr()->Cdr(); }
00057     Ptree* Ca_ar();
00058     void SetCar(Ptree* p) { data.nonleaf.child = p; }
00059     void SetCdr(Ptree* p) { data.nonleaf.next = p; }
00060 
00061     char* LeftMost();
00062     char* RightMost();
00063 
00064     virtual int What();
00065     bool IsA(int);
00066     bool IsA(int, int);
00067     bool IsA(int, int, int);
00068 
00069     virtual Ptree* Translate(Walker*);
00070     virtual void Typeof(Walker*, TypeInfo&);
00071 
00072     virtual char* GetEncodedType();
00073     virtual char* GetEncodedName();
00074 
00075     Ptree* Last() { return Last(this); }
00076     Ptree* First() { return First(this); }
00077     Ptree* Rest() { return Rest(this); }
00078     Ptree* Second() { return Second(this); }
00079     Ptree* Third() { return Third(this); }
00080     Ptree* Nth(int n) { return Nth(this, n); }
00081     int Length() { return Length(this); }
00082     Ptree* ListTail(int n) { return ListTail(this, n); }
00083 
00084 // static members
00085 
00086     static bool Eq(Ptree*, char);
00087     static bool Eq(Ptree*, char*);
00088     static bool Eq(Ptree*, char*, int);
00089     static bool Eq(Ptree*, Ptree*);
00090     static bool Equiv(Ptree*, Ptree*);
00091     static bool Equal(Ptree*, Ptree*);
00092     static Ptree* Last(Ptree*);
00093     static Ptree* First(Ptree*);
00094     static Ptree* Rest(Ptree*);
00095     static Ptree* Second(Ptree*);
00096     static Ptree* Third(Ptree*);
00097     static Ptree* Nth(Ptree*, int);
00098     static int Length(Ptree*);
00099     static Ptree* ListTail(Ptree*, int);
00100 
00101     static Ptree* Cons(Ptree*, Ptree*);
00102     static Ptree* List();
00103     static Ptree* List(Ptree*);
00104     static Ptree* List(Ptree*, Ptree*);
00105     static Ptree* List(Ptree*, Ptree*, Ptree*);
00106     static Ptree* List(Ptree*, Ptree*, Ptree*, Ptree*);
00107     static Ptree* List(Ptree*, Ptree*, Ptree*, Ptree*, Ptree*);
00108     static Ptree* List(Ptree*, Ptree*, Ptree*, Ptree*, Ptree*, Ptree*);
00109     static Ptree* List(Ptree*, Ptree*, Ptree*, Ptree*, Ptree*, Ptree*,
00110                        Ptree*);
00111     static Ptree* List(Ptree*, Ptree*, Ptree*, Ptree*, Ptree*, Ptree*,
00112                        Ptree*, Ptree*);
00113     static Ptree* CopyList(Ptree*);
00114     static Ptree* Append(Ptree*, Ptree*);
00115     static Ptree* ReplaceAll(Ptree*, Ptree*, Ptree*);
00116     static Ptree* Subst(Ptree*, Ptree*, Ptree*);
00117     static Ptree* Subst(Ptree*, Ptree*, Ptree*, Ptree*, Ptree*);
00118     static Ptree* Subst(Ptree*, Ptree*, Ptree*, Ptree*,
00119                         Ptree*, Ptree*, Ptree*);
00120     static Ptree* ShallowSubst(Ptree*, Ptree*, Ptree*);
00121     static Ptree* ShallowSubst(Ptree*, Ptree*, Ptree*, Ptree*, Ptree*);
00122     static Ptree* ShallowSubst(Ptree*, Ptree*, Ptree*, Ptree*,
00123                                Ptree*, Ptree*, Ptree*);
00124     static Ptree* ShallowSubst(Ptree*, Ptree*, Ptree*, Ptree*,
00125                                Ptree*, Ptree*, Ptree*, Ptree*, Ptree*);
00126     static Ptree* SubstSublist(Ptree*, Ptree*, Ptree*);
00127 
00128     /* they cause side-effect */
00129     static Ptree* Snoc(Ptree*, Ptree*);
00130     static Ptree* Nconc(Ptree*, Ptree*);
00131     static Ptree* Nconc(Ptree*, Ptree*, Ptree*);
00132 
00133 // in pattern.cc
00134 public:
00135     static bool Match(Ptree*, char*, ...);
00136     static Ptree* Make(const char* pat, ...);
00137     static Ptree* MakeStatement(const char* pat, ...);
00138     static Ptree* GenSym();
00139 
00140     static Ptree* qMake(char*);
00141     static Ptree* qMakeStatement(char*);
00142 
00143     bool Reify(unsigned int&);
00144     bool Reify(char*&);
00145 
00146     static char* IntegerToString(sint, int&);   // not documented
00147 
00148 private:
00149     static char* MatchPat(Ptree*, char*);
00150     static char* MatchList(Ptree*, char*);
00151     static char* MatchWord(Ptree*, char*);
00152 
00153 public:
00154     // if this is TRUE, Print() shows an encoded type and name.
00155     static bool show_encoded;
00156 
00157 protected:
00158     union {
00159         struct {
00160             Ptree* child;
00161             Ptree* next;
00162         } nonleaf;
00163         struct {
00164             char* position;
00165             int  length;
00166         } leaf;
00167     }data;
00168 
00169     friend class NonLeaf;
00170 };
00171 
00172 inline ostream& operator << (ostream& s, Ptree* p)
00173 {
00174     p->Write(s);
00175     return s;
00176 }
00177 
00178 // in ptree.cc
00179 
00180 class OCXXMOP PtreeIter : public LightObject {
00181 public:
00182     PtreeIter(Ptree* p) { ptree = p; }
00183     Ptree* operator () () { return Pop(); }
00184     Ptree* Pop();
00185     bool Next(Ptree*&);
00186     void Reset(Ptree* p) { ptree = p; }
00187 
00188     Ptree* operator * () { return This(); }
00189     Ptree* operator ++ () { Pop(); return This(); }
00190     Ptree* operator ++ (int) { return Pop(); }  // postfix
00191     Ptree* This();
00192     bool Empty() { return bool(ptree == nil); }
00193 
00194 private:
00195     Ptree* ptree;
00196 };
00197 
00198 class OCXXMOP PtreeArray : public LightObject {
00199 public:
00200     PtreeArray(int = 8);
00201     uint Number() { return num; }
00202     Ptree*& operator [] (uint index) { return Ref(index); }
00203     Ptree*& Ref(uint index);
00204     void Append(Ptree*);
00205     void Clear() { num = 0; }
00206     Ptree* All();
00207 
00208 private:
00209     uint num, size;
00210     Ptree** array;
00211     Ptree* default_buf[8];
00212 };
00213 
00214 // PtreeHead is used to implement Ptree::qMake()
00215 // The implementation is in pattern.cc
00216 
00217 class OCXXMOP PtreeHead : public LightObject {
00218 public:
00219     PtreeHead() { ptree = nil; }
00220     operator Ptree* () { return ptree; }
00221     PtreeHead& operator + (Ptree*);
00222     PtreeHead& operator + (const char*);
00223     PtreeHead& operator + (char*);
00224     PtreeHead& operator + (char);
00225     PtreeHead& operator + (int);
00226 
00227 private:
00228     Ptree* Append(Ptree*, Ptree*);
00229     Ptree* Append(Ptree*, char*, int);
00230 
00231 private:
00232     Ptree* ptree;
00233 };
00234 
00235 // error messages
00236 
00237 void OCXXMOP MopErrorMessage(char* method_name, char* msg);
00238 void OCXXMOP MopErrorMessage2(char* msg1, char* msg2);
00239 void OCXXMOP MopWarningMessage(char* method_name, char* msg);
00240 void OCXXMOP MopWarningMessage2(char* msg1, char* msg2);
00241 void OCXXMOP MopMoreWarningMessage(char* msg1, char* msg2 = nil);
00242 
00243 #endif  /* _ptree_core_h */

Generated on Mon Feb 10 17:32:54 2003 for VFiasco Semantics Compiler by doxygen1.2.15