relStrExpr.h

Go to the documentation of this file.
00001 /*
00002  * CrocoPat is a tool for relational programming.
00003  * This file is part of CrocoPat. 
00004  *
00005  * Copyright (C) 2002-2008  Dirk Beyer
00006  *
00007  * CrocoPat is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * CrocoPat is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with CrocoPat; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  * Please find the GNU Lesser General Public License in file
00022  * License_LGPL.txt or at http://www.gnu.org/licenses/lgpl.txt
00023  *
00024  * Author:
00025  * Dirk Beyer (firstname.lastname@sfu.ca)
00026  * Simon Fraser University
00027  *
00028  * With contributions of: Andreas Noack, Michael Vogel
00029  */
00030 
00031 #ifndef _relStrExpr_h
00032 #define _relStrExpr_h
00033 
00034 #include "relString.h"
00035 #include "relNumExpr.h"
00036 
00037 #include <string>
00038 #include <sstream>
00039 
00041 extern map<string, relDataType*> gVariables;
00043 extern char**  gArgv;
00044 extern int     gArgc;
00045 
00047 extern string double2string(double pNum);
00048 
00050 class relStrExpr : public relObject
00051 {
00052 public:
00053   virtual relString
00054   interpret(bddSymTab* pSymTab) = 0;
00055 };
00056 
00057 
00059 class relStrExprVar : public relStrExpr
00060 {
00061 private:
00062   string*         mVar;
00063 
00064 public:
00065   relStrExprVar(string* pVar)
00066     : mVar(pVar)
00067   {}
00068 
00069   ~relStrExprVar()
00070   {
00071     delete mVar;
00072   }
00073 
00074   virtual relString
00075   interpret(bddSymTab* pSymTab)
00076   {
00077     // Fetch result.
00078     map<string, relDataType*>::const_iterator lVarIt = gVariables.find(*mVar);
00079     assert(lVarIt != gVariables.end());  // Must be declared.
00080     assert(lVarIt->second != NULL);
00081     relString* lResult = dynamic_cast<relString*>(lVarIt->second);
00082     assert(lResult != NULL);             // Must be a STRING variable.
00083 
00084     return *lResult;
00085   }
00086 };
00087 
00089 class relStrExprConst : public relStrExpr
00090 {
00091 private:
00092   string*       mVal;
00093 
00094 public:
00095   relStrExprConst(string* pVal)
00096     : mVal(pVal)
00097   {}
00098 
00099   ~relStrExprConst()
00100   {
00101     delete mVal;
00102   }
00103 
00104   virtual relString
00105   interpret(bddSymTab* pSymTab)
00106   {
00107     return relString(*mVal);
00108   }
00109 };
00110 
00112 class relStrExprBinOp : public relStrExpr
00113 {
00114 public:
00115   typedef enum {CONCAT} relStrOP;
00116 
00117 private:
00118   relStrExpr*   mExpr1;
00119   relStrOP      mOp;
00120   relStrExpr*   mExpr2;
00121 
00122 public:
00123   relStrExprBinOp( relStrExpr* pExpr1, 
00124                                    relStrOP pOp, 
00125                                relStrExpr* pExpr2)
00126     : mExpr1(pExpr1),
00127       mOp(pOp),
00128       mExpr2(pExpr2)
00129   {}
00130 
00131   ~relStrExprBinOp()
00132   {
00133     delete mExpr1;
00134     delete mExpr2;
00135   }
00136 
00137   virtual relString
00138   interpret(bddSymTab* pSymTab)
00139   {
00140     relString lExpr1 = mExpr1->interpret(pSymTab);
00141     relString lExpr2 = mExpr2->interpret(pSymTab);
00142     relString result("");
00143 
00144     if (mOp == CONCAT) result.setValue(lExpr1.getValue() + lExpr2.getValue());
00145     else { 
00146       cerr << "Internal error: Unknown operator in relStrExprBinOp::interpret." << endl;
00147       abort(); 
00148     }
00149     return result;
00150   }
00151 };
00152 
00154 class relStrExprElem : public relStrExpr
00155 {
00156 private:
00157   relExpression* mExpr;
00158 
00159 public:
00160   relStrExprElem(relExpression* pExpr) 
00161     : mExpr(pExpr)
00162   {}
00163 
00164   ~relStrExprElem();
00165 
00166   virtual relString
00167   interpret(bddSymTab* pSymTab);
00168 };
00169 
00171 class relStrExprNum : public relStrExpr
00172 {
00173 private:
00174   relNumExpr*   mExpr;
00175 
00176 public:
00177   relStrExprNum(relNumExpr* pExpr) 
00178     : mExpr(pExpr)
00179   {}
00180 
00181   ~relStrExprNum()
00182   {
00183     delete mExpr;
00184   }
00185 
00186   virtual relString
00187   interpret(bddSymTab* pSymTab)
00188   {
00189     return relString(double2string(mExpr->interpret(pSymTab).getValue()));
00190   }
00191 };
00192 
00194 class relStrCmdArg : public relStrExpr
00195 {
00196 private:
00197   relNumExpr*   mExpr;
00198 
00199 public:
00200   relStrCmdArg(relNumExpr* pExpr) 
00201     : mExpr(pExpr)
00202   {}
00203 
00204   ~relStrCmdArg()
00205   {
00206     delete mExpr;
00207   }
00208 
00209   virtual relString
00210   interpret(bddSymTab* pSymTab)
00211   {
00212     int lPos = (int) mExpr->interpret(pSymTab).getValue();
00213     if (lPos >= 0  &&  lPos < gArgc) {
00214       return relString(string(gArgv[lPos]));
00215     } else {
00216       cerr << "Error: Missing command line argument '$" << lPos << "'."
00217            << endl;
00218       exit(EXIT_FAILURE);
00219     }
00220   }
00221 };
00222 
00223 #endif
00224 

Generated on Fri Jun 6 22:21:09 2008 for CrocoPat by  doxygen 1.5.1