ucsym.h

Go to the documentation of this file.
00001 
00007 /*
00008 Copyright (C) 2000 The Exult Team
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 */
00024 
00025 #ifndef INCL_UCSYM
00026 #define INCL_UCSYM
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #  include <config.h>
00030 #endif
00031 
00032 #include <string>
00033 #include <map>
00034 #include <vector>
00035 using std::vector;
00036 
00037 class Uc_array_expression;
00038 class Uc_expression;
00039 class Uc_function;
00040 
00041 /*
00042  *  For comparing names:
00043  */
00044 class String_compare
00045   {
00046 public:
00047   bool operator()(char * const &x, char * const &y) const;
00048   };
00049 
00050 /*
00051  *  A formal parameter or local symbol within a function.
00052  */
00053 class Uc_symbol
00054   {
00055 protected:
00056   std::string name;     // This will be the key.
00057 public:
00058   friend class Uc_scope;
00059   Uc_symbol(char *nm) : name(nm)
00060     {  }
00061   const char *get_name() { return name.c_str(); }
00062           // Gen. code to put result on stack.
00063   virtual int gen_value(vector<char>& out);
00064           // Gen. to assign from stack.
00065   virtual int gen_assign(vector<char>& out);
00066           // Generate function/procedure call.
00067   virtual int gen_call(vector<char>& out, Uc_function *fun, bool orig,
00068     Uc_expression *item, Uc_array_expression *parms, 
00069               bool retvalue);
00070   virtual int get_string_offset() // Get offset in text_data.
00071     { return -1; }
00072           // Return var/int expression.
00073   virtual Uc_expression *create_expression();
00074   };
00075 
00076 /*
00077  *  A variable (untyped) that can be assigned to.
00078  */
00079 class Uc_var_symbol : public Uc_symbol
00080   {
00081 protected:
00082   int offset;     // Within function.  Locals follow
00083           //   formal parameters.
00084 public:
00085   friend class Uc_scope;
00086   Uc_var_symbol(char *nm, int off) : Uc_symbol(nm), offset(off)
00087     {  }
00088   int get_offset()
00089     { return offset; }
00090           // Gen. code to put result on stack.
00091   virtual int gen_value(vector<char>& out);
00092           // Gen. to assign from stack.
00093   virtual int gen_assign(vector<char>& out);
00094           // Return var/int expression.
00095   virtual Uc_expression *create_expression();
00096   };
00097 
00098 /*
00099  *  A static (persistent) variable.
00100  */
00101 class Uc_static_var_symbol : public Uc_var_symbol
00102   {
00103 public:
00104   Uc_static_var_symbol(char *nm, int off) : Uc_var_symbol(nm, offset)
00105     {  }
00106           // Gen. code to put result on stack.
00107   virtual int gen_value(vector<char>& out);
00108           // Gen. to assign from stack.
00109   virtual int gen_assign(vector<char>& out);
00110   };
00111 
00112 /*
00113  *  A constant integer variable.
00114  */
00115 class Uc_const_int_symbol : public Uc_symbol
00116   {
00117   int value;
00118 public:
00119   Uc_const_int_symbol(char *nm, int v) : Uc_symbol(nm), value(v)
00120     {  }
00121           // Gen. code to put result on stack.
00122   virtual int gen_value(vector<char>& out);
00123           // Return var/int expression.
00124   virtual Uc_expression *create_expression();
00125   int get_value() const
00126     { return value; }
00127   };
00128 
00129 /*
00130  *  A (constant) string.  The offset is within the usecode function's
00131  *  text_data field.
00132  */
00133 class Uc_string_symbol : public Uc_symbol
00134   {
00135   int offset;     // In function's text_data.
00136 public:
00137   Uc_string_symbol(char *nm, int off) : Uc_symbol(nm), offset(off)
00138     {  }
00139           // Gen. code to put result on stack.
00140   virtual int gen_value(vector<char>& out);
00141   virtual int get_string_offset() // Get offset in text_data.
00142     { return offset; }
00143           // Return var/int expression.
00144   virtual Uc_expression *create_expression();
00145   };
00146 
00147 /*
00148  *  An intrinsic symbol:
00149  */
00150 class Uc_intrinsic_symbol : public Uc_symbol
00151   {
00152   int intrinsic_num;    // Intrinsic #.
00153   int num_parms;      // # parms. +++++Not used/set yet.
00154 public:
00155   Uc_intrinsic_symbol(char *nm, int n) : Uc_symbol(nm), intrinsic_num(n),
00156     num_parms(0)
00157     {  }
00158   int get_intrinsic_num()
00159     { return intrinsic_num; }
00160   int get_num_parms()   // ++++Not valid yet.
00161     { return num_parms; }
00162           // Generate function/procedure call.
00163   virtual int gen_call(vector<char>& out, Uc_function *fun, bool orig,
00164     Uc_expression *item, Uc_array_expression *parms, 
00165               bool retvalue);
00166   };
00167 
00168 /*
00169  *  A function-prototype symbol:
00170  */
00171 class Uc_function_symbol : public Uc_symbol
00172   {
00173   static int last_num;    // Last 'usecode_num', so we can
00174           //   assign automatically.
00175 public:
00176           // Keep track of #'s used.
00177   typedef std::map<int, Uc_symbol *> Sym_nums;
00178 private:
00179   static Sym_nums nums_used;
00180           // Note:  offset = Usecode fun. #.
00181   std::vector<char *> parms;  // Parameters.
00182   int usecode_num;    // Usecode function #.
00183 public:
00184   Uc_function_symbol(char *nm, int num, std::vector<char *>& p);
00185   const std::vector<char *>& get_parms()
00186     { return parms; }
00187   int get_usecode_num()
00188     { return usecode_num; }
00189   int get_num_parms()
00190     { return parms.size(); }
00191           // Generate function/procedure call.
00192   virtual int gen_call(vector<char>& out, Uc_function *fun, bool orig,
00193     Uc_expression *item, Uc_array_expression *parms, 
00194               bool retvalue);
00195   };
00196 
00197 /*
00198  *  A 'scope' in the symbol table:
00199  */
00200 class Uc_scope
00201   {
00202   Uc_scope *parent;   // ->parent.
00203           // For finding syms. by name.
00204   typedef std::map<char *, Uc_symbol *, String_compare> Sym_map;
00205   Sym_map symbols;
00206   std::vector<Uc_scope *> scopes; // Scopes within.
00207 public:
00208   Uc_scope(Uc_scope *p) : parent(p)
00209     {  }
00210   ~Uc_scope();
00211   Uc_scope *get_parent()
00212     { return parent; }
00213   Uc_symbol *search(const char *nm) // Look in this scope.
00214     {
00215     char *nm1 = (char *) nm;
00216     Sym_map::const_iterator it = symbols.find(nm1);
00217     if (it == symbols.end())
00218       return 0;
00219     else
00220       return (*it).second;
00221     }
00222           // Search upwards through scopes.
00223   Uc_symbol *search_up(char *nm);
00224   void add(Uc_symbol *sym)  // Add (does NOT check for dups.)
00225     {
00226     const char *nm = sym->name.c_str();
00227     char *nm1 = (char *)nm; // ???Can't figure this out!
00228     symbols[nm1] = sym; 
00229     }
00230   Uc_scope *add_scope()   // Create new scope.
00231     {
00232     Uc_scope *newscope = new Uc_scope(this);
00233     scopes.push_back(newscope);
00234     return newscope;
00235     }
00236           // Add a function decl.
00237   int add_function_symbol(Uc_function_symbol *fun);
00238   };
00239 
00240 #endif
00241 
00242 

Generated on Mon Jul 9 14:42:51 2007 for ExultEngine by  doxygen 1.5.1