ucexpr.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_UCEXPR
00026 #define INCL_UCEXPR 1
00027 
00028 #include <vector>
00029 #include <string>
00030 #include "ucloc.h"
00031 
00032 using std::vector;
00033 
00034 class Uc_symbol;
00035 class Uc_var_symbol;
00036 class Uc_function;
00037 
00038 /*
00039  *  Base class for expressions.
00040  */
00041 class Uc_expression : public Uc_location
00042   {
00043 public:
00044           // Use current location.
00045   Uc_expression() : Uc_location()
00046     {  }
00047   virtual ~Uc_expression() {  }
00048           // Gen. code to put result on stack.
00049   virtual void gen_value(vector<char>& out) = 0;
00050           // Gen code to push value(s).
00051   virtual int gen_values(vector<char>& out);
00052           // Gen. code to jmp if this is false.
00053   virtual int gen_jmp_if_false(vector<char>& out, int offset);
00054           // Gen. to assign from stack.
00055   virtual void gen_assign(vector<char>& out);
00056   virtual int get_string_offset() // Get offset in text_data.
00057     { return -1; }
00058           // Get/create var == this.
00059   virtual Uc_var_symbol *need_var(vector<char>& out, Uc_function *fun);
00060           // Evaluate constant.
00061   virtual bool eval_const(int& val);
00062   };
00063 
00064 /*
00065  *  A variable.
00066  */
00067 class Uc_var_expression : public Uc_expression
00068   {
00069   Uc_var_symbol *var;
00070 public:
00071           // Use current location.
00072   Uc_var_expression(Uc_var_symbol *v) : var(v)
00073     {  }
00074           // Gen. code to put result on stack.
00075   virtual void gen_value(vector<char>& out);
00076           // Gen. to assign from stack.
00077   virtual void gen_assign(vector<char>& out);
00078   virtual int get_string_offset();// Get offset in text_data.
00079   virtual Uc_var_symbol *need_var(vector<char>& , Uc_function *)
00080     { return var; }
00081   };
00082 
00083 /*
00084  *  An array element.
00085  */
00086 class Uc_arrayelem_expression : public Uc_expression
00087   {
00088   Uc_var_symbol *array;
00089   Uc_expression *index;
00090 public:
00091   Uc_arrayelem_expression(Uc_var_symbol *a, Uc_expression *i)
00092     : array(a), index(i)
00093     {  }
00094   ~Uc_arrayelem_expression()
00095     { delete index; }
00096           // Gen. code to put result on stack.
00097   virtual void gen_value(vector<char>& out);
00098           // Gen. to assign from stack.
00099   virtual void gen_assign(vector<char>& out);
00100   };
00101 
00102 /*
00103  *  Global flag.
00104  */
00105 class Uc_flag_expression : public Uc_expression
00106   {
00107   int index;
00108 public:
00109   Uc_flag_expression(int i)
00110     : index(i)
00111     {  }
00112           // Gen. code to put result on stack.
00113   virtual void gen_value(vector<char>& out);
00114           // Gen. to assign from stack.
00115   virtual void gen_assign(vector<char>& out);
00116   };
00117 
00118 /*
00119  *  Binary expressions.
00120  */
00121 class Uc_binary_expression : public Uc_expression
00122   {
00123   int opcode;     // Should be the UC_<opcode>
00124   Uc_expression *left, *right;  // Operands to add, sub, etc.
00125 public:
00126   Uc_binary_expression(int o, Uc_expression *l, Uc_expression *r)
00127     : opcode(o), left(l), right(r)
00128     {  }
00129           // Gen. code to put result on stack.
00130   virtual void gen_value(vector<char>& out);
00131           // Evaluate constant.
00132   virtual bool eval_const(int& val);
00133   };
00134 
00135 /*
00136  *  Unary expressions.
00137  */
00138 class Uc_unary_expression : public Uc_expression
00139   {
00140   int opcode;     // Should be the UC_<opcode>
00141   Uc_expression *operand;
00142 public:
00143   Uc_unary_expression(int o, Uc_expression *r)
00144     : opcode(o), operand(r)
00145     {  }
00146           // Gen. code to put result on stack.
00147   virtual void gen_value(vector<char>& out);
00148   };
00149 
00150 /*
00151  *  Compare user conversation response to a given string (or list of
00152  *  strings.
00153  */
00154 class Uc_response_expression : public Uc_expression
00155   {
00156   Uc_expression *operand;
00157 public:
00158   Uc_response_expression(Uc_expression *r)
00159     : operand(r)
00160     {  }
00161           // Gen. code to put result on stack.
00162   virtual void gen_value(vector<char>& out);
00163           // Gen. code to jmp if this is false.
00164   virtual int gen_jmp_if_false(vector<char>& out, int offset);
00165   };
00166 
00167 /*
00168  *  Integer value.
00169  */
00170 class Uc_int_expression : public Uc_expression
00171   {
00172   int value;
00173 public:
00174   Uc_int_expression(int v) : value(v)
00175     {  }
00176           // Gen. code to put result on stack.
00177   virtual void gen_value(vector<char>& out);
00178           // Evaluate constant.
00179   virtual bool eval_const(int& val);
00180   };
00181 
00182 /*
00183  *  Boolean value.
00184  */
00185 class Uc_bool_expression : public Uc_expression
00186   {
00187   bool tf;
00188 public:
00189   Uc_bool_expression(bool t) : tf(t)
00190     {  }
00191           // Gen. code to put result on stack.
00192   virtual void gen_value(vector<char>& out);
00193   };
00194 
00195 /*
00196  *  Eventid (a special int variable passed to each function):
00197  */
00198 class Uc_event_expression : public Uc_expression
00199   {
00200 public:
00201   Uc_event_expression() {  }
00202           // Gen. code to put result on stack.
00203   virtual void gen_value(vector<char>& out);
00204           // Gen. to assign from stack.
00205   virtual void gen_assign(vector<char>& out);
00206   };
00207 
00208 /*
00209  *  Item (a special ptr. variable passed to each function):
00210  */
00211 class Uc_item_expression : public Uc_expression
00212   {
00213 public:
00214   Uc_item_expression() {  }
00215           // Gen. code to put result on stack.
00216   virtual void gen_value(vector<char>& out);
00217   };
00218 
00219 /*
00220  *  String value.
00221  */
00222 class Uc_string_expression : public Uc_expression
00223   {
00224   int offset;     // Offset in function's data area.
00225 public:
00226   Uc_string_expression(int o) : offset(o)
00227     {  }
00228           // Gen. code to put result on stack.
00229   virtual void gen_value(vector<char>& out);
00230   virtual int get_string_offset() // Get offset in text_data.
00231     { return offset; }
00232   };
00233 
00234 /*
00235  *  String value given by a prefix (i.e. "Jo"* for "Job").
00236  */
00237 class Uc_string_prefix_expression : public Uc_expression
00238   {
00239   Uc_function *fun;   // Needed to look up prefix.
00240   std::string prefix;   // What to look up.
00241   int offset;     // Offset in function's data area.
00242           //   This is -1 if not found yet.
00243 public:
00244   Uc_string_prefix_expression(Uc_function *f, char *pre) 
00245     : fun(f), prefix(pre), offset(-1)
00246     {  }
00247           // Gen. code to put result on stack.
00248   virtual void gen_value(vector<char>& out);
00249   virtual int get_string_offset();// Get offset in text_data.
00250   };
00251 
00252 /*
00253  *  A concatenation, which generates an array:
00254  */
00255 class Uc_array_expression : public Uc_expression
00256   {
00257     std::vector<Uc_expression*> exprs;
00258 public:
00259   Uc_array_expression() {  }
00260   Uc_array_expression(Uc_expression *e0)
00261     { add(e0); }    // Create with 1st expression.
00262   Uc_array_expression(Uc_expression *e0, Uc_expression *e1)
00263     { add(e0); add(e1); }
00264   ~Uc_array_expression();
00265   void add(Uc_expression *e)  // Append an expression.
00266     { exprs.push_back(e); }
00267   void clear()      // Remove, but DON'T delete, elems.
00268     { exprs.clear(); }
00269   void concat(Uc_expression *e);  // Concat e's elements onto this.
00270   const std::vector<Uc_expression*>& get_exprs()
00271     { return exprs; }
00272           // Gen. code to put result on stack.
00273   virtual void gen_value(vector<char>& out);
00274           // Gen code to push value(s).
00275   virtual int gen_values(vector<char>& out);
00276   };
00277 
00278 /*
00279  *  A function or intrinsic call.
00280  */
00281 class Uc_call_expression : public Uc_expression
00282   {
00283   Uc_symbol *sym;     // Function or intrinsic.
00284   bool original;      // Call original function instead of
00285           //   the one from 'patch'.
00286   Uc_expression *itemref;   // Non-null for CALLE.
00287   Uc_array_expression *parms;
00288   Uc_function *function;    // May need function this is in.
00289   bool return_value;    // True for a function (to return
00290           //   its value).
00291 public:
00292   Uc_call_expression(Uc_symbol *s, Uc_array_expression *prms,
00293           Uc_function *fun, bool orig = false)
00294     : sym(s), itemref(0), parms(prms), 
00295       function(fun), original(orig), return_value(true)
00296     {  }
00297   ~Uc_call_expression()
00298     { delete parms; delete itemref; }
00299   void set_itemref(Uc_expression *iexpr)
00300     { itemref = iexpr; }
00301   void set_no_return()
00302     { return_value = false; }
00303           // Gen. code to put result on stack.
00304   virtual void gen_value(vector<char>& out);
00305   };
00306 
00307 /*
00308  *  Write a 2-byte value to the end/position of a character stream.
00309  */
00310 
00311 inline void Write2(vector<char>& out, unsigned short val)
00312   {
00313   out.push_back((char) (val&0xff));
00314   out.push_back((char) ((val>>8)&0xff));
00315   }
00316 inline void Write2(vector<char>& out, int pos, unsigned short val)
00317   {
00318   out[pos] = (char) (val&0xff);
00319   out[pos + 1] = (char) ((val>>8)&0xff);
00320   }
00321 
00322 #endif

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