00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef Val_H
00022 #define Val_H
00023
00024 #include "ValExpr.H"
00025 #include "Location.H"
00026 #include "Dep.H"
00027 #include "Prim.H"
00028 #include "Err.H"
00029 #include <Generics.H>
00030 #include <CacheC.H>
00031 #include <PrefixTbl.H>
00032 #include <VestaSource.H>
00033 #include <iostream>
00034 #include <fstream>
00035
00036
00037 extern Val valTrue, valFalse, valZero, valErr, valUnbnd;
00038 extern Val valTBinding, valTBool, valTClosure, valTErr;
00039 extern Val valTInt, valTList, valTText, valTModel;
00040 extern Context conEmpty, conInitial;
00041 extern Assoc nullAssoc;
00042
00043
00044 enum ValueKind {
00045 BooleanVK, IntegerVK, ListVK, BindingVK, PrimitiveVK,
00046 TextVK, ClosureVK, ModelVK, ErrorVK, FpVK, UnbndVK };
00047
00049 class ValC {
00050
00051
00052 public:
00053 ValueKind vKind;
00054
00055
00056 bool tagged;
00057
00058
00059 bool cacheit;
00060
00061
00062
00063 int dpsVersion;
00064
00065
00066 FP::Tag tag;
00067
00068
00069
00070 DPaths* dps;
00071
00072
00073 DepPath* path;
00074
00075
00076 virtual void PrintD(std::ostream *os, bool verbose = false, int indent = 0) = 0;
00077
00078
00079 virtual Val Copy(bool more = false) = 0;
00080
00081
00082 virtual FP::Tag FingerPrint() = 0;
00083
00084
00085 void VError(const Text& message) { Error(message); };
00086
00087
00088 Val MergeDPS(DPaths* ps);
00089
00090
00091 Val Merge(Val val);
00092
00093
00094 Val MergeAndTypeDPS(Val val);
00095
00096
00097 Val MergeAndLenDPS(Val val);
00098
00099
00100 Val MergeLenDPS(Val val);
00101
00102
00103 Val AddToDPS(DepPath* dp, Val v, PathKind pk = DummyPK);
00104
00105
00106 Val AddExtendToDPS(DepPath* dp, Val v, PathKind pk, const Text& id);
00107
00108
00109 Val Extend(Val v, const Text& id, PathKind pk = NormPK, bool add = true);
00110
00111
00112 int SizeOfDPS() { return (dps == NULL) ? 0 : dps->Size(); };
00113
00114
00115 protected:
00116 ValC(ValueKind vk)
00117 : vKind(vk), tagged(false), cacheit(true), path(NULL),
00118 dps(NULL), dpsVersion(0) { };
00119
00120
00121 ValC(const ValC& val)
00122 : vKind(val.vKind), tagged(val.tagged), tag(val.tag), cacheit(val.cacheit),
00123 path(NULL), dps(NULL), dpsVersion(0) { };
00124
00125 };
00126
00127
00128 class BooleanVC: public ValC {
00129 public:
00130 BooleanVC(bool bb)
00131 : ValC(BooleanVK), b(bb) { };
00132 BooleanVC(const BooleanVC& val)
00133 : ValC(val), b(val.b) { };
00134 void PrintD(std::ostream *os, bool verbose = false, int indent = 0)
00135 { *os << ((b) ? "TRUE" : "FALSE"); };
00136 FP::Tag FingerPrint();
00137 bool b;
00138 Val Copy(bool more) { return NEW_CONSTR(BooleanVC, (*this)); };
00139 };
00140
00141 class IntegerVC: public ValC {
00142 public:
00143 IntegerVC(Basics::int32 i): ValC(IntegerVK), num(i) { };
00144 IntegerVC(const IntegerVC& val): ValC(val), num(val.num) { };
00145 void PrintD(std::ostream *os, bool verbose = false, int indent = 0)
00146 { *os << num; };
00147 FP::Tag FingerPrint();
00148 Basics::int32 num;
00149 Val Copy(bool more) { return NEW_CONSTR(IntegerVC, (*this)); };
00150 };
00151
00152 class ListVC: public ValC {
00153 public:
00154 ListVC(const Vals& telems)
00155 : ValC(ListVK), elems(telems), lenDps(NULL) { };
00156 ListVC(const ListVC& val)
00157 : ValC(val), elems(val.elems), lenDps(NULL) { };
00158 void PrintD(std::ostream *os, bool verbose = false, int indent = 0);
00159 FP::Tag FingerPrint();
00160 Vals elems;
00161 DPaths* lenDps;
00162 IntegerVC* Length();
00163 Val GetElem(int index);
00164 Val GetElemNoDpnd(int index);
00165 Val AddToLenDPS(const DepPath& dp, Val val);
00166 Val MergeToLenDPS(DPaths *ps);
00167 Val Copy(bool more);
00168 };
00169
00170 class BindingVC: public ValC {
00171 public:
00172 BindingVC(): ValC(BindingVK), lenDps(NULL) { };
00173 BindingVC(const Context& telems)
00174 : ValC(BindingVK), lenDps(NULL) { elems = telems; };
00175 BindingVC(Binding b1, Binding b2, bool rec);
00176 BindingVC(const BindingVC& val)
00177 : ValC(val), elems(val.elems), lenDps(NULL) { };
00178 Context elems;
00179 DPaths* lenDps;
00180 void PrintD(std::ostream *os, bool verbose = false, int indent = 0);
00181 FP::Tag FingerPrint();
00182 IntegerVC* Length();
00183 Val Names();
00184 bool Null() { return this->elems.Null(); };
00185 Val Defined(const Text& id);
00186 Val Lookup(const Text& id);
00187 Val GetElem(const Text& name, int& index);
00188 Val GetElem(int index, Text& name);
00189 bool DefinedNoDpnd(const Text& id);
00190 Val LookupNoDpnd(const Text& id);
00191 Val GetElemNoDpnd(const Text& name, int& index);
00192 Val GetElemNoDpnd(int index, Text& name);
00193 Binding AddBindingAssoc(const Text& name, Val v);
00194 Binding RemoveBindingAssoc(const Text& id);
00195 Val AddToLenDPS(const DepPath& dp, Val val);
00196 Val MergeToLenDPS(DPaths *ps);
00197 Val Copy(bool more);
00198 private:
00199 Context SimpleOverlay(Binding b);
00200 Context RecursiveOverlay(Binding b);
00201 };
00202
00203 class PrimitiveVC: public ValC {
00204 public:
00205 PrimitiveVC(const Text& tname, PrimExec texec)
00206 : ValC(PrimitiveVK), name(tname), exec(texec) { };
00207 PrimitiveVC(const PrimitiveVC& val)
00208 : ValC(val), name(val.name), exec(val.exec) { };
00209 void PrintD(std::ostream *os, bool verbose = false, int indent = 0)
00210 { *os << name; };
00211 FP::Tag FingerPrint();
00212 Text name;
00213 PrimExec exec;
00214 Val Copy(bool more) { return NEW_CONSTR(PrimitiveVC, (*this)); };
00215 };
00216
00217 class TextVC: public ValC {
00218 public:
00219 class TextC {
00220 public:
00221 TextC(const Text& ttext)
00222 : txt(ttext), shortId(NullShortId), hasTxt(true), hasName(false)
00223 { };
00224 TextC(const Text& tname, const Text& ttext);
00225 TextC(const Text& tname, std::fstream *iFile, VestaSource *vSource);
00226 TextC(const Text& tname, const ShortId sid)
00227 : name(tname), shortId(sid), hasTxt(false), hasName(true)
00228 { };
00229 Text txt;
00230 Text name;
00231 ShortId shortId;
00232 bool hasTxt, hasName;
00233 };
00234 TextVC(const Text& ttext): ValC(TextVK) { content = NEW_CONSTR(TextC, (ttext)); };
00235
00236 TextVC(const Text& tname, const Text& ttext, char c, const FP::Tag& fp);
00237
00238 TextVC(const Text& tname, std::fstream *iFile, VestaSource *vSource);
00239
00240 TextVC(const Text& tname, const ShortId sid, int fp_content);
00241
00242 TextVC(const Text& tname, const ShortId sid, const FP::Tag& fp)
00243 : ValC(TextVK)
00244 { content = NEW_CONSTR(TextC, (tname, sid)); tag = fp; tagged = true; };
00245
00246 TextVC(const TextVC& val)
00247 : ValC(val), content(val.content) { };
00248 TextC* content;
00249 void PrintD(std::ostream *os, bool verbose = false, int indent = 0);
00250 FP::Tag FingerPrint();
00251 Text NDS();
00252 Text TName();
00253 ShortId Sid();
00254 bool HasTxt() { return content->hasTxt; };
00255 bool HasSid() { return (content->shortId != NullShortId); };
00256 int Length();
00257 Val Copy(bool more) { return NEW_CONSTR(TextVC, (*this)); };
00258 private:
00259 std::istream* Content(bool& closeIt);
00260 };
00261
00262 class ModelVC: public ValC {
00263 public:
00264 class ModelC {
00265 public:
00266 ModelC(const Text& tlPath)
00267 : name(tlPath), sid(NullShortId), mRoot(NULL), parsed(false)
00268 { };
00269 ModelC(const Text& tname, ShortId tsid, VestaSource *root,
00270 Expr mod, const Context& cc)
00271 : name(tname), sid(tsid), mRoot(root), model(mod), c(cc),
00272 parsed(true) { };
00273 ModelC(const Text& tname, ShortId tsid, VestaSource *root)
00274 : name(tname), sid(tsid), mRoot(root), parsed(false)
00275 { };
00276 Text name;
00277 VestaSource *mRoot;
00278 Expr model;
00279 Context c;
00280 ShortId sid;
00281 bool parsed;
00282 };
00283 ModelVC(const Text& tname, ShortId tsid, VestaSource *root,
00284 Expr mod, const Context& cc, VestaSource *vSource);
00285 ModelVC(const Text& tlPath, VestaSource *root, SrcLoc *loc);
00286 ModelVC(const Text& tname, ShortId tsid, VestaSource *root,
00287 const FP::Tag& ttag, const FP::Tag& tlidTag)
00288 : ValC(ModelVK)
00289 { content = NEW_CONSTR(ModelC, (tname, tsid, root));
00290 tag = ttag; tagged = true; lidTag = tlidTag; };
00291 ModelVC(const ModelVC& val)
00292 : ValC(val), content(val.content), lidTag(val.lidTag)
00293 { };
00294 ModelC* content;
00295 void PrintD(std::ostream *os, bool verbose = false, int indent = 0);
00296 FP::Tag lidTag;
00297 FP::Tag FingerPrint() { return this->lidTag; };
00298 FP::Tag FingerPrintFile() { return this->tag; };
00299 ShortId Sid() { return this->content->sid; };
00300 Val Force();
00301 Val Copy(bool more) { return NEW_CONSTR(ModelVC, (*this)); };
00302 };
00303
00304 class ClosureVC: public ValC {
00305 public:
00306 ClosureVC(FuncEC *tfunc, const Context& c, bool fresh);
00307 ClosureVC(const ClosureVC& val)
00308 : ValC(val), func(val.func), con(val.con),
00309 exprTag(val.exprTag), exprTagged(val.exprTagged) { };
00310 FuncEC *func;
00311 Context con;
00312 bool exprTagged;
00313 FP::Tag exprTag;
00314 void PrintD(std::ostream *os, bool verbose = false, int indent = 0);
00315 FP::Tag FingerPrint();
00316 FP::Tag FingerPrintExpr();
00317 Val Copy(bool more) { return NEW_CONSTR(ClosureVC, (*this)); };
00318 };
00319
00320 class ErrorVC: public ValC {
00321 public:
00322 ErrorVC();
00323 ErrorVC(bool cacheable)
00324 : ValC(ErrorVK) { this->cacheit = cacheable; };
00325 ErrorVC(const ErrorVC& val): ValC(val) { };
00326 void PrintD(std::ostream *os, bool verbose = false, int indent = 0)
00327 { *os << "ERR"; };
00328 FP::Tag FingerPrint();
00329 Val Copy(bool more) { return NEW_CONSTR(ErrorVC, (*this)); };
00330 };
00331
00332 class FpVC: public ValC {
00333
00334
00335 public:
00336 FpVC(const FP::Tag& ttag)
00337 : ValC(FpVK) { tag = ttag; tagged = true; };
00338 void PrintD(std::ostream *os, bool verbose = false, int indent = 0)
00339 { *os << "<Value tag>"; };
00340 FP::Tag FingerPrint() { return tag; }
00341 Val Copy(bool more)
00342 { outputMu.lock();
00343 VError("Copy a FpVC?");
00344 outputMu.unlock();
00345 return NEW_CONSTR(FpVC, (tag)); };
00346 };
00347
00348 class UnbndVC: public ValC {
00349
00350 public:
00351 UnbndVC() : ValC(UnbndVK) { };
00352 void PrintD(std::ostream *os, bool verbose = false, int indent = 0)
00353 { *os << "<unbound name>"; };
00354 FP::Tag FingerPrint();
00355 Val Copy(bool more) { return NEW(UnbndVC); };
00356 };
00357
00358 class AssocVC {
00359
00360 public:
00361 AssocVC(const Text& tname, Val tval)
00362 : name(tname), val(tval) { };
00363 void PrintD(std::ostream *os, bool verbose = false, int indent = 0);
00364 FP::Tag FingerPrint();
00365 Atom name;
00366 Val val;
00367 };
00368
00369
00370 bool IsValTrue(Val v);
00371 bool IsValFalse(Val v);
00372 bool IsEmptyBinding(Val v);
00373 bool IsEmptyList(Val v);
00374
00375
00376 Val ValType(Val v);
00377
00378
00379 Val LetDpnd(Val v, const Context& c);
00380 Val FuncDpnd(Val bodyv, Val fv, const Context& c);
00381 Val ModelDpnd(Val bodyv, Val fv, const Context& c);
00382 DPaths* ClosureDpnd(ClosureVC *cl);
00383 void ModelCutOff(Val v);
00384 void ValueDpnd(Val v, DPaths *ps);
00385
00386
00387 Text PrintForm(Val v);
00388
00389
00390 void PrintContext(std::ostream *os, const Context& c, bool verbose = false,
00391 int indent = 0);
00392
00393
00394 Assoc FindInContext(const Text& id, const Context& c);
00395
00396
00397 Val LookupInContext(const Text& id, const Context& c);
00398
00399
00400 Val LookupPath(const FV2::T& path, const Context& c);
00401 Val LookupPath(DepPath *dp, const Context& c);
00402 Val LookupPath(Basics::uint16 idx, PathKind pkind, const PrefixTbl& tbl,
00403 const Context& c, Val *vals);
00404
00405
00406 void AppendDToContext(const Text& name, Expr elem, const Context& cElem,
00407 Context& cc);
00408
00409
00410 Context RestrictContext(const Context& con, Vars fv);
00411
00412
00413
00414 Context Snip(const Context& orig, const Text& remove, bool& found);
00415
00416
00417 Context Prune(const Context& orig, const Context& remove);
00418
00419
00420 void PushToContext(const Text& name, Expr elem, const Context& cElem,
00421 Context& in);
00422
00423
00424 Context AddStmtAssocs(StmtListEC *assocs, const Context& c);
00425
00426
00427 void ValInit();
00428
00429 #endif // Val_H