00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef UTAP_EXPRESSION_HH
00023 #define UTAP_EXPRESSION_HH
00024
00025 #include <vector>
00026 #include <set>
00027 #include <map>
00028
00029 #include "utap/common.h"
00030 #include "utap/symbols.h"
00031
00032 namespace UTAP
00033 {
00067 class expression_t
00068 {
00069 private:
00070 expression_t(Constants::kind_t, const position_t &);
00071 public:
00073 expression_t();
00074
00076 expression_t(const expression_t &);
00077
00079 ~expression_t();
00080
00082 expression_t clone() const;
00083
00085 Constants::kind_t getKind() const;
00086
00088 uint32_t getSize() const;
00089
00091 const position_t &getPosition() const;
00092
00094 type_t getType() const;
00095
00097 void setType(type_t);
00098
00101 int32_t getValue() const;
00102
00104 int32_t getIndex() const;
00105
00107 bool empty() const;
00108
00110 Constants::synchronisation_t getSync() const;
00111
00113 std::string toString(bool old = false) const;
00114
00116 expression_t &operator[](uint32_t);
00117
00119 const expression_t operator[](uint32_t) const;
00120
00122 expression_t &get(uint32_t);
00123
00125 const expression_t &get(uint32_t) const;
00126
00128 expression_t &operator=(const expression_t &);
00129
00131 bool equal(const expression_t &) const;
00132
00135 symbol_t getSymbol();
00136
00139 const symbol_t getSymbol() const;
00140
00143 bool isReferenceTo(const std::set<symbol_t> &) const;
00144
00147 bool changesVariable(const std::set<symbol_t> &) const;
00148
00151 bool dependsOn(const std::set<symbol_t> &) const;
00152
00153 void collectPossibleWrites(std::set<symbol_t> &) const;
00154 void collectPossibleReads(std::set<symbol_t> &) const;
00155
00158 bool operator < (const expression_t) const;
00159
00162 bool operator == (const expression_t) const;
00163
00164 static int getPrecedence(Constants::kind_t);
00165
00167 static expression_t createConstant(const position_t &, int32_t);
00168
00170 static expression_t createIdentifier(const position_t &, symbol_t);
00171
00173 static expression_t createUnary(const position_t &, Constants::kind_t,
00174 expression_t, type_t = type_t::UNKNOWN);
00176 static expression_t createBinary(const position_t &, Constants::kind_t,
00177 expression_t, expression_t,
00178 type_t = type_t::UNKNOWN);
00179
00181 static expression_t createTernary(const position_t &,
00182 Constants::kind_t, expression_t,
00183 expression_t, expression_t,
00184 type_t = type_t::UNKNOWN);
00185
00187 static expression_t createNary(const position_t &, Constants::kind_t,
00188 const std::vector<expression_t> &,
00189 type_t = type_t::UNKNOWN);
00190
00192 static expression_t createDot(const position_t &, expression_t,
00193 int32_t = -1, type_t = type_t::UNKNOWN);
00194
00196 static expression_t createSync(const position_t &, expression_t,
00197 Constants::synchronisation_t);
00198
00200 static expression_t createDeadlock(const position_t &);
00201
00202 private:
00203 struct expression_data;
00204 expression_data *data;
00205 int getPrecedence() const;
00206 void toString(bool, char *&str, char *&end, int &size) const;
00207 };
00208
00209 class InterpreterException : public std::exception
00210 {
00211 public:
00212 const char *what() const throw() { return "InterpreterException"; }
00213 };
00214
00215 class Interpreter
00216 {
00217 private:
00218 std::map<symbol_t, expression_t> valuation;
00219 int32_t evaluateBinary(int32_t left, Constants::kind_t, int32_t right) const;
00220 public:
00221 Interpreter();
00222 Interpreter(const std::map<symbol_t, expression_t> &);
00223
00224 void addValuation(const std::map<symbol_t, expression_t> &);
00225
00226 const std::map<symbol_t, expression_t> &getValuation() const;
00227
00228 int32_t evaluate(const expression_t) const
00229 throw (InterpreterException);
00230 void evaluate(const expression_t, std::vector<int32_t> &) const
00231 throw (InterpreterException);
00232 range_t evaluate(
00233 std::pair<expression_t, expression_t>) const
00234 throw (InterpreterException);
00235
00236 size_t sizeOfType(type_t) const;
00237
00238 };
00239
00240 }
00241
00242 std::ostream &operator<< (std::ostream &o, const UTAP::expression_t &e);
00243
00244 #endif
00245
00246
00247