00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00034
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
00044 public:
00045
00046 SrcLoc *loc;
00047
00048
00049 ExprKind kind;
00050
00051
00052 Vars freeVars;
00053
00054
00055 virtual void PrintD(std::ostream& os) = 0;
00056
00057
00058 virtual Val Eval(const Context& c) = 0;
00059
00060
00061 void PrintExprVars(std::ostream& os);
00062
00063
00064 void EError(std::ostream& os, const Text& message) { Error(os, message, loc); };
00065
00066 protected:
00067
00068 ExprC(ExprKind tcl, SrcLoc *tloc)
00069 : loc(tloc), kind(tcl) { };
00070 };
00071
00072
00073 class ConstantEC: public ExprC {
00074 public:
00075 ConstantEC(Val lit, SrcLoc *tloc)
00076 : ExprC(ConstantEK, tloc), val(lit) { };
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 {
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 {
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 {
00101 public:
00102 ExprListEC(int size, SrcLoc *tloc)
00103 : ExprC(ExprListEK, tloc), elems(size) { };
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 {
00113 public:
00114 ArgListEC(int size, SrcLoc *tloc)
00115 : ExprC(ArgListEK, tloc), elems(size), inPKs(0) { };
00116 Exprs elems;
00117 Bit64 inPKs;
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 {
00126 public:
00127 StmtListEC(SrcLoc *tloc)
00128 : ExprC(StmtListEK, tloc) { };
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 {
00139 public:
00140 ListEC(int size, SrcLoc *tloc)
00141 : ExprC(ListEK, tloc), elems(size) { };
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 {
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 {
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 {
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 {
00177 public:
00178 BindingEC(int size, SrcLoc *tloc)
00179 : ExprC(BindingEK, tloc), assocs(size) { };
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 {
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 {
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 {
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) { };
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 {
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) { };
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 {
00235 public:
00236 PrimitiveEC(const Text& tname, PrimExec texec, SrcLoc *tloc)
00237 : ExprC(PrimitiveEK, tloc), name(tname), exec(texec) { };
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 {
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 {
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 {
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 {
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 {
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 {
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 {
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 {
00318 public:
00319 BaseTEC(const Text& tbase, SrcLoc *tloc)
00320 : ExprC(BaseTEK, tloc), base(tbase) { };
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 {
00327 public:
00328 ListTEC(Expr ttype, SrcLoc *tloc)
00329 : ExprC(ListTEK, tloc), type(ttype) { };
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 {
00336 public:
00337 BindingTEC(Expr tfields, SrcLoc *tloc)
00338 : ExprC(BindingTEK, tloc), fields(tfields) { };
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 {
00345 public:
00346 FuncTEC(Expr tfields, SrcLoc *tloc)
00347 : ExprC(FuncTEK, tloc), fields(tfields) { };
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 {
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
00361
00362
00363 FP::Tag FingerPrint(Expr expr);
00364
00365
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