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(const Text& message) { Error(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, Expr tblock,
00208 VestaSource *mRoot, SrcLoc *tloc)
00209 : ExprC(ModelEK, tloc), files(tfiles), imports(timports),
00210 block(tblock), modelRoot(mRoot) { };
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 {
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) { };
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 {
00234 public:
00235 PrimitiveEC(const Text& tname, PrimExec texec, SrcLoc *tloc)
00236 : ExprC(PrimitiveEK, tloc), name(tname), exec(texec) { };
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 {
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 {
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 {
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 {
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 {
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 {
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 {
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 {
00316 public:
00317 BaseTEC(const Text& tbase, SrcLoc *tloc)
00318 : ExprC(BaseTEK, tloc), base(tbase) { };
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 {
00325 public:
00326 ListTEC(Expr ttype, SrcLoc *tloc)
00327 : ExprC(ListTEK, tloc), type(ttype) { };
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 {
00334 public:
00335 BindingTEC(Expr tfields, SrcLoc *tloc)
00336 : ExprC(BindingTEK, tloc), fields(tfields) { };
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 {
00343 public:
00344 FuncTEC(Expr tfields, SrcLoc *tloc)
00345 : ExprC(FuncTEK, tloc), fields(tfields) { };
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 {
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
00359
00360
00361 FP::Tag FingerPrint(Expr expr);
00362
00363
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