/vesta/vestasys.org/vesta/eval/98/src/Expr.H

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 // 
00003 // This file is part of Vesta.
00004 // 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 // 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // File: Expr.H 
00020 
00021 #ifndef Expr_H
00022 #define Expr_H
00023 
00024 #include "ValExpr.H"
00025 #include "Location.H"
00026 #include "Val.H"
00027 #include "Prim.H"
00028 #include "Err.H"
00029 #include <FP.H>
00030 #include <iostream>
00031 #include <fstream>
00032 
00033 // 'ExpC' includes all of the different classes of exprs that
00034 // a client may encounter in an expression tree, or may build.
00035 enum ExprKind {
00036   ConstantEK, IfEK, ComputedEK, ArgListEK, ExprListEK, StmtListEK,
00037   ApplyEK, ApplyOpEK, ApplyUnOpEK, AssignEK, BindEK, BindingEK,
00038   BlockEK, FileEK, FuncEK, IterateEK, ListEK, ModelEK, NameEK, 
00039   PairEK, PrimitiveEK, SelectEK, TypedEK, BaseTEK, ListTEK, 
00040   BindingTEK, FuncTEK, ErrorEK, TryEK };
00041 
00042 class ExprC {
00043   // The superclass of all expressions:
00044 public:
00045   // Where the source text for this expression starts:
00046   SrcLoc *loc;
00047 
00048   // Which subclass of ExprC `this' belongs to:
00049   ExprKind kind;
00050 
00051   // The identifiers of the (syntactically) free variables of `this':
00052   Vars freeVars;
00053 
00054   // Print `this' on `d':
00055   virtual void PrintD(std::ostream& os) = 0;
00056 
00057   // Both value and dependency of this in the context `c':
00058   virtual Val Eval(const Context& c) = 0;
00059 
00060   // Print the free variables of `this':
00061   void PrintExprVars(std::ostream& os);
00062 
00063   // Print `message' on 'os':
00064   void EError(std::ostream& os, const Text& message) { Error(os, message, loc); };
00065 
00066 protected:
00067   // Construct a <tcl>C with location `tloc':
00068   ExprC(ExprKind tcl, SrcLoc *tloc)
00069     : loc(tloc), kind(tcl) { /*SKIP*/ };
00070 };
00071 
00072 // Subclasses of different expression types:
00073 class ConstantEC: public ExprC {  // Literal or without free variables
00074 public:
00075   ConstantEC(Val lit, SrcLoc *tloc)
00076     : ExprC(ConstantEK, tloc), val(lit) { /*SKIP*/ };
00077   Val val;
00078   void PrintD(std::ostream& os) { val->PrintD(os); };
00079   Val Eval(const Context& c);
00080 };
00081 
00082 class IfEC: public ExprC {        // Conditional expression
00083 public:
00084   IfEC(Expr tcond, Expr tthen, Expr tels, SrcLoc *tloc);
00085   Expr test, then, els;
00086   void PrintD(std::ostream& os);
00087   Val Eval(const Context& c);
00088 };
00089 
00090 class ComputedEC: public ExprC {  // Expression evaluating to a name
00091 public:
00092   ComputedEC(Expr tname)
00093     : ExprC(ComputedEK, tname->loc), name(tname)
00094       { freeVars = tname->freeVars; };
00095   Expr name;
00096   void PrintD(std::ostream& os);
00097   Val Eval(const Context& c);
00098 };
00099 
00100 class ExprListEC: public ExprC {  // Used in parser for syntactic lists
00101 public:
00102   ExprListEC(int size, SrcLoc *tloc)
00103     : ExprC(ExprListEK, tloc), elems(size) { /*SKIP*/ };
00104   Exprs elems;
00105   void PrintD(std::ostream& os);
00106   Val Eval(const Context& c);
00107   void AddExpr(Expr telem);
00108   bool Empty() { return (elems.size() == 0); };
00109   int Length() { return elems.size(); };
00110 };
00111 
00112 class ArgListEC: public ExprC {  // Used for formals and actuals
00113 public:
00114   ArgListEC(int size, SrcLoc *tloc)
00115     : ExprC(ArgListEK, tloc), elems(size), inPKs(0) { /*SKIP*/ };
00116   Exprs elems;
00117   Bit64 inPKs;  // Well, there can only be 64 arguments for a function
00118   void PrintD(std::ostream& os);
00119   Val Eval(const Context& c) { return NEW(ErrorVC); };
00120   void AddExpr(Expr telem, bool inPK);
00121   bool Empty() { return (elems.size() == 0); };
00122   int Length() { return elems.size(); };
00123 };
00124 
00125 class StmtListEC: public ExprC {  // Used in parser for statement lists
00126 public:
00127   StmtListEC(SrcLoc *tloc)
00128     : ExprC(StmtListEK, tloc) { /*SKIP*/ };
00129   Exprs elems;
00130   Vars boundVars;
00131   void PrintD(std::ostream& os);
00132   Val Eval(const Context& c) { return NEW(ErrorVC); };
00133   void AddExpr(Expr telem);
00134   bool Empty() { return (elems.size() == 0); };
00135   int Length() { return elems.size(); };
00136 };
00137 
00138 class ListEC: public ExprC {      // Explicit list
00139 public:
00140   ListEC(int size, SrcLoc *tloc)
00141     : ExprC(ListEK, tloc), elems(size) { /*SKIP*/ };
00142   Exprs elems;
00143   void PrintD(std::ostream& os);
00144   Val Eval(const Context& c);
00145   void AddExpr(Expr telem);
00146 };
00147 
00148 class AssignEC: public ExprC {    // lhs = rhs (as a statement)
00149 public:
00150   AssignEC(Name tlhs, Expr trhs, bool tisFunc, SrcLoc *tloc);
00151   Name lhs;
00152   Expr rhs;
00153   bool isFunc;
00154   void PrintD(std::ostream& os);
00155   Val Eval(const Context& c) { return NEW(ErrorVC); };
00156 };
00157 
00158 class BindEC: public ExprC {      // lhs = rhs (as a binding component)
00159 public:
00160   BindEC(Expr tlhs, Expr trhs, SrcLoc *tloc);
00161   Expr lhs, rhs;
00162   void PrintD(std::ostream& os);
00163   Val Eval(const Context& c) { return NEW(ErrorVC); };
00164 };
00165 
00166 class NameEC: public ExprC {      // Identifier
00167 public:
00168   NameEC(const Text& tid, SrcLoc *tloc)
00169     : ExprC(NameEK, tloc), id(tid) { freeVars.Push(id); };
00170   Atom id;
00171   void PrintD(std::ostream& os) { os << id; };
00172   Val Eval(const Context& c);
00173   void ClearFreeVars() { freeVars.SetEmpty(); };
00174 };
00175 
00176 class BindingEC: public ExprC {   // Complete binding
00177 public:
00178   BindingEC(int size, SrcLoc *tloc)
00179     : ExprC(BindingEK, tloc), assocs(size) { /*SKIP*/ };
00180   Exprs assocs;
00181   void PrintD(std::ostream& os);
00182   Val Eval(const Context& c);
00183   void AddExpr(Expr elem);
00184 };
00185 
00186 class ApplyOpEC: public ExprC {   // Application of binary operator
00187 public:
00188   ApplyOpEC(Expr te1, const Text& top, Expr te2, SrcLoc *loc);
00189   Atom op;
00190   Expr e1, e2;
00191   void PrintD(std::ostream& os);
00192   Val Eval(const Context& c);
00193 };
00194 
00195 class ApplyUnOpEC: public ExprC { // Application of unary operator
00196 public:
00197   ApplyUnOpEC(const Text& top, Expr te, SrcLoc *tloc)
00198     : ExprC(ApplyUnOpEK, tloc), op(top), e(te) { freeVars = e->freeVars; };
00199   Atom op;
00200   Expr e;
00201   void PrintD(std::ostream& os);
00202   Val Eval(const Context& c);
00203 };
00204 
00205 class ModelEC: public ExprC {     // Parsed model
00206 public:
00207   ModelEC(ExprList tfiles, ExprList timports, bool tnoDup, Expr tblock,
00208           VestaSource *mRoot, SrcLoc *tloc)
00209     : ExprC(ModelEK, tloc), files(tfiles), imports(timports), noDup(tnoDup),
00210       block(tblock), modelRoot(mRoot) { /*SKIP*/ };
00211   ExprList files, imports;
00212   bool noDup;
00213   Expr block;
00214   VestaSource *modelRoot;
00215   void PrintD(std::ostream& os);
00216   Val Eval(const Context& c);
00217   bool ImportLocalModel();
00218 };
00219 
00220 class FileEC: public ExprC {      // Name from `files' or `import'
00221 public:
00222   FileEC(Name tname, const Text& tpath, VestaSource *mRoot,
00223          bool timport, SrcLoc *tloc)
00224     : ExprC(FileEK, tloc), name(tname), localPath(tpath),
00225       modelRoot(mRoot), import(timport) { /*SKIP*/ };
00226   Name name;
00227   Atom localPath;
00228   VestaSource *modelRoot;
00229   bool import;
00230   void PrintD(std::ostream& os);
00231   Val Eval(const Context& c);
00232 };
00233 
00234 class PrimitiveEC: public ExprC { // Language primitive function
00235 public:
00236   PrimitiveEC(const Text& tname, PrimExec texec, SrcLoc *tloc)
00237     : ExprC(PrimitiveEK, tloc), name(tname), exec(texec) { /*SKIP*/ };
00238   Atom name;
00239   PrimExec exec;
00240   void PrintD(std::ostream& os) { os << name; };
00241   Val Eval(const Context& c)
00242     { return NEW_CONSTR(PrimitiveVC, (this->name, this->exec)); };
00243 };
00244 
00245 class PairEC: public ExprC {      // Two expression (used internally)
00246 public:
00247   PairEC(Expr tfirst, Expr tsecond, SrcLoc *tloc);
00248   Expr first, second;
00249   void PrintD(std::ostream& os);
00250   Val Eval(const Context& c) { return NEW(ErrorVC); };
00251 };
00252 
00253 class SelectEC: public ExprC {    // Field selection from binding
00254 public:
00255   SelectEC(Expr tbinding, Expr tfield, bool tbang, SrcLoc *tloc);
00256   Expr binding, field;
00257   bool bang;
00258   void PrintD(std::ostream& os);
00259   Val Eval(const Context& c);
00260 };
00261 
00262 class FuncEC: public ExprC {      // A function declaration
00263 public:
00264   FuncEC(bool tnoCache, bool tNoDup, const Text& tname, ArgList targs,
00265           Expr tbody, SrcLoc *tloc);
00266   Atom name;
00267   ArgList args;
00268   Expr body;
00269   bool noCache;
00270   bool noDup;
00271   void PrintD(std::ostream& os);
00272   Val Eval(const Context& c);
00273   FP::Tag FingerPrint();
00274 private:
00275   bool tagged;
00276   FP::Tag tag;
00277 };
00278 
00279 class BlockEC: public ExprC {     // Block, with `value' or `return'
00280 public:
00281   BlockEC(StmtListEC *tassocs, Expr tbody, bool treturn, SrcLoc *tloc);
00282   StmtListEC *assocs;
00283   Expr body;
00284   bool isReturn;
00285   void PrintD(std::ostream& os);
00286   Val Eval(const Context& c);
00287 };
00288 
00289 class IterateEC: public ExprC {   // Iteration statement
00290 public:
00291   IterateEC(Expr tcontrol, Expr te, StmtListEC *tbody, SrcLoc *tloc);
00292   Expr control, e;
00293   StmtListEC *body;
00294   void PrintD(std::ostream& os);
00295   Val Eval(const Context& c) { return NEW(ErrorVC); };
00296 };
00297 
00298 class ApplyEC: public ExprC {     // Application of function, primitive, model
00299 public:
00300   ApplyEC(Expr tfunc, ArgList targs, SrcLoc *loc);
00301   Expr func;
00302   ArgList args;
00303   void PrintD(std::ostream& os);
00304   Val Eval(const Context& c);
00305 };
00306 
00307 class TypedEC: public ExprC {     // Typed value
00308 public:
00309   TypedEC(Expr tval, Expr ttyp, SrcLoc *tloc)
00310     : ExprC(TypedEK, tloc), val(tval), typ(ttyp)
00311       { freeVars = tval->freeVars; };
00312   Expr val, typ;
00313   void PrintD(std::ostream& os);
00314   Val Eval(const Context& c) { return val->Eval(c); };
00315 };
00316 
00317 class BaseTEC: public ExprC {     // Base type
00318 public:
00319   BaseTEC(const Text& tbase, SrcLoc *tloc)
00320     : ExprC(BaseTEK, tloc), base(tbase) { /*SKIP*/ };
00321   Atom base;
00322   void PrintD(std::ostream& os) { os << base; };
00323   Val Eval(const Context& c) { return NEW(ErrorVC); };
00324 };
00325 
00326 class ListTEC: public ExprC {     // List type
00327 public:
00328   ListTEC(Expr ttype, SrcLoc *tloc)
00329     : ExprC(ListTEK, tloc), type(ttype) { /*SKIP*/ };
00330   Expr type;
00331   void PrintD(std::ostream& os);
00332   Val Eval(const Context& c) { return NEW(ErrorVC); };
00333 };
00334 
00335 class BindingTEC: public ExprC {  // Binding type
00336 public:
00337   BindingTEC(Expr tfields, SrcLoc *tloc)
00338     : ExprC(BindingTEK, tloc), fields(tfields) { /*SKIP*/ };
00339   Expr fields;
00340   void PrintD(std::ostream& os);
00341   Val Eval(const Context& c) { return NEW(ErrorVC); };
00342 };
00343 
00344 class FuncTEC: public ExprC {     // Function type
00345 public:
00346   FuncTEC(Expr tfields, SrcLoc *tloc)
00347     : ExprC(FuncTEK, tloc), fields(tfields) { /*SKIP*/ };
00348   Expr fields;
00349   void PrintD(std::ostream& os);
00350   Val Eval(const Context& c) { return NEW(ErrorVC); };
00351 };
00352 
00353 class ErrorEC: public ExprC {     // Error expression
00354 public:
00355   ErrorEC(SrcLoc *tloc, bool runTime = true);
00356   void PrintD(std::ostream& os) { os << "ERR"; };
00357   Val Eval(const Context& c) { return NEW_CONSTR(ErrorVC, (true)); };
00358 };
00359 
00360 // End of subclasses of ExprC.
00361 
00362 // Fingerprint an expression:
00363 FP::Tag FingerPrint(Expr expr);
00364 
00365 // Text list operations:
00366 Vars AddVars(const Vars& fv1, const Vars& fv2);
00367 Vars Remove(const Vars& body, const Text& id);
00368 Vars Remove(const Vars& body, const Vars& bound);
00369 
00370 Vals EvalArgs(ArgList args, const Context& c);
00371 Context ProcessModelHead(ModelEC*);
00372 
00373 #endif // Expr_H

Generated on Thu May 24 23:01:47 2007 for Vesta by  doxygen 1.5.1