Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

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 cerr:
00064   void EError(const Text& message) { Error(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, Expr tblock,
00208           VestaSource *mRoot, SrcLoc *tloc)
00209     : ExprC(ModelEK, tloc), files(tfiles), imports(timports),
00210       block(tblock), modelRoot(mRoot) { /*SKIP*/ };
00211   ExprList files, imports;
00212   Expr block;
00213   VestaSource *modelRoot;
00214   void PrintD(std::ostream *os);
00215   Val Eval(const Context& c);
00216   bool ImportLocalModel();
00217 };
00218 
00219 class FileEC: public ExprC {      // Name from `files' or `import'
00220 public:
00221   FileEC(Name tname, const Text& tpath, VestaSource *mRoot,
00222          bool timport, SrcLoc *tloc)
00223     : ExprC(FileEK, tloc), name(tname), localPath(tpath),
00224       modelRoot(mRoot), import(timport) { /*SKIP*/ };
00225   Name name;
00226   Atom localPath;
00227   VestaSource *modelRoot;
00228   bool import;
00229   void PrintD(std::ostream *os);
00230   Val Eval(const Context& c);
00231 };
00232 
00233 class PrimitiveEC: public ExprC { // Language primitive function
00234 public:
00235   PrimitiveEC(const Text& tname, PrimExec texec, SrcLoc *tloc)
00236     : ExprC(PrimitiveEK, tloc), name(tname), exec(texec) { /*SKIP*/ };
00237   Atom name;
00238   PrimExec exec;
00239   void PrintD(std::ostream *os) { *os << name; };
00240   Val Eval(const Context& c)
00241     { return NEW_CONSTR(PrimitiveVC, (this->name, this->exec)); };
00242 };
00243 
00244 class PairEC: public ExprC {      // Two expression (used internally)
00245 public:
00246   PairEC(Expr tfirst, Expr tsecond, SrcLoc *tloc);
00247   Expr first, second;
00248   void PrintD(std::ostream *os);
00249   Val Eval(const Context& c) { return NEW(ErrorVC); };
00250 };
00251 
00252 class SelectEC: public ExprC {    // Field selection from binding
00253 public:
00254   SelectEC(Expr tbinding, Expr tfield, bool tbang, SrcLoc *tloc);
00255   Expr binding, field;
00256   bool bang;
00257   void PrintD(std::ostream *os);
00258   Val Eval(const Context& c);
00259 };
00260 
00261 class FuncEC: public ExprC {      // A function declaration
00262 public:
00263   FuncEC(bool tnoCache, const Text& tname, ArgList targs, Expr tbody,
00264          SrcLoc *tloc);
00265   Atom name;
00266   ArgList args;
00267   Expr body;
00268   bool noCache;
00269   void PrintD(std::ostream *os);
00270   Val Eval(const Context& c);
00271   FP::Tag FingerPrint();
00272 private:
00273   bool tagged;
00274   FP::Tag tag;
00275 };
00276 
00277 class BlockEC: public ExprC {     // Block, with `value' or `return'
00278 public:
00279   BlockEC(StmtListEC *tassocs, Expr tbody, bool treturn, SrcLoc *tloc);
00280   StmtListEC *assocs;
00281   Expr body;
00282   bool isReturn;
00283   void PrintD(std::ostream *os);
00284   Val Eval(const Context& c);
00285 };
00286 
00287 class IterateEC: public ExprC {   // Iteration statement
00288 public:
00289   IterateEC(Expr tcontrol, Expr te, StmtListEC *tbody, SrcLoc *tloc);
00290   Expr control, e;
00291   StmtListEC *body;
00292   void PrintD(std::ostream *os);
00293   Val Eval(const Context& c) { return NEW(ErrorVC); };
00294 };
00295 
00296 class ApplyEC: public ExprC {     // Application of function, primitive, model
00297 public:
00298   ApplyEC(Expr tfunc, ArgList targs, SrcLoc *loc);
00299   Expr func;
00300   ArgList args;
00301   void PrintD(std::ostream *os);
00302   Val Eval(const Context& c);
00303 };
00304 
00305 class TypedEC: public ExprC {     // Typed value
00306 public:
00307   TypedEC(Expr tval, Expr ttyp, SrcLoc *tloc)
00308     : ExprC(TypedEK, tloc), val(tval), typ(ttyp)
00309       { freeVars = tval->freeVars; };
00310   Expr val, typ;
00311   void PrintD(std::ostream *os);
00312   Val Eval(const Context& c) { return val->Eval(c); };
00313 };
00314 
00315 class BaseTEC: public ExprC {     // Base type
00316 public:
00317   BaseTEC(const Text& tbase, SrcLoc *tloc)
00318     : ExprC(BaseTEK, tloc), base(tbase) { /*SKIP*/ };
00319   Atom base;
00320   void PrintD(std::ostream *os) { *os << base; };
00321   Val Eval(const Context& c) { return NEW(ErrorVC); };
00322 };
00323 
00324 class ListTEC: public ExprC {     // List type
00325 public:
00326   ListTEC(Expr ttype, SrcLoc *tloc)
00327     : ExprC(ListTEK, tloc), type(ttype) { /*SKIP*/ };
00328   Expr type;
00329   void PrintD(std::ostream *os);
00330   Val Eval(const Context& c) { return NEW(ErrorVC); };
00331 };
00332 
00333 class BindingTEC: public ExprC {  // Binding type
00334 public:
00335   BindingTEC(Expr tfields, SrcLoc *tloc)
00336     : ExprC(BindingTEK, tloc), fields(tfields) { /*SKIP*/ };
00337   Expr fields;
00338   void PrintD(std::ostream *os);
00339   Val Eval(const Context& c) { return NEW(ErrorVC); };
00340 };
00341 
00342 class FuncTEC: public ExprC {     // Function type
00343 public:
00344   FuncTEC(Expr tfields, SrcLoc *tloc)
00345     : ExprC(FuncTEK, tloc), fields(tfields) { /*SKIP*/ };
00346   Expr fields;
00347   void PrintD(std::ostream *os);
00348   Val Eval(const Context& c) { return NEW(ErrorVC); };
00349 };
00350 
00351 class ErrorEC: public ExprC {     // Error expression
00352 public:
00353   ErrorEC(SrcLoc *tloc, bool runTime = true);
00354   void PrintD(std::ostream *os) { *os << "ERR"; };
00355   Val Eval(const Context& c) { return NEW_CONSTR(ErrorVC, (true)); };
00356 };
00357 
00358 // End of subclasses of ExprC.
00359 
00360 // Fingerprint an expression:
00361 FP::Tag FingerPrint(Expr expr);
00362 
00363 // Text list operations:
00364 Vars AddVars(const Vars& fv1, const Vars& fv2);
00365 Vars Remove(const Vars& body, const Text& id);
00366 Vars Remove(const Vars& body, const Vars& bound);
00367 
00368 Vals EvalArgs(ArgList args, const Context& c);
00369 Context ProcessModelHead(ModelEC*);
00370 
00371 #endif // Expr_H

Generated on Mon May 8 00:48:38 2006 for Vesta by  doxygen 1.4.2