value.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /* ScummVM Tools
  2. *
  3. * ScummVM Tools is the legal property of its developers, whose
  4. * names are too numerous to list here. Please refer to the
  5. * COPYRIGHT file distributed with this source distribution.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. #ifndef VALUE_H
  22. #define VALUE_H
  23. #include <deque>
  24. #include <exception>
  25. #include <ostream>
  26. #include <string>
  27. #include <boost/intrusive_ptr.hpp>
  28. #include "common/scummsys.h"
  29. #include "RefCounted.h"
  30. #include "stack.h"
  31. #include "wrongtype.h"
  32. class Value;
  33. const int kNoPrecedence = 0; ///< Precedence value for individual values with no operations.
  34. const int kUnaryOpPrecedence = 1; ///< Precedence value for a unary operation. (!, -, ~, etc.)
  35. const int kMultOpPrecedence = 2; ///< Precedence value for multiplication, division, modulus (*, /, %)
  36. const int kAddOpPrecedence = 3; ///< Precedence value for addition and subtraction (+, -)
  37. const int kShiftOpPrecedence = 4; ///< precedence value for bit shifting (<<, >>)
  38. const int kRelationOpPrecedence = 5; ///< Precedence value for relative comparison (<, <=, >=, >)
  39. const int kEqualityOpPrecedence = 6; ///< Precedence value for equality comparisons (==, !=)
  40. const int kBitwiseAndPrecedence = 7; ///< Precedence value for bitwise AND (&)
  41. const int kBitwiseXorPrecedence = 8; ///< Precedence value for bitwise XOR (^)
  42. const int kBitwiseOrPrecedence = 9; ///< Precedence value for bitwise OR (|)
  43. const int kLogicalAndPrecedence = 10; ///< Precedence value for logical AND (&&)
  44. const int kLogicalOrPrecedence = 11; ///< Precedence value for logical OR (||)
  45. /**
  46. * Pointer to a Value.
  47. */
  48. typedef boost::intrusive_ptr<Value> ValuePtr;
  49. /**
  50. * Type representing a list of values, e.g. for indexes used to access an array.
  51. */
  52. typedef std::deque<ValuePtr> ValueList;
  53. /**
  54. * Type representing a stack.
  55. */
  56. typedef Stack<ValuePtr> ValueStack;
  57. /**
  58. * Class representing a value (stack entry, parameter, etc.)
  59. */
  60. class Value : public RefCounted {
  61. public:
  62. virtual ~Value() { }
  63. /**
  64. * Return whether or not the Value is an integer.
  65. *
  66. * @return True if the Value is an integer, otherwise false.
  67. */
  68. virtual bool isInteger();
  69. /**
  70. * Return whether or not the Value is an address.
  71. *
  72. * @return True if the Value is an address, otherwise false.
  73. */
  74. virtual bool isAddress();
  75. /**
  76. * Returns whether or not any stored integer value is signed.
  77. *
  78. * @return True if the integer value is signed, false if it is not.
  79. * @throws WrongTypeException if the value is not an integer.
  80. */
  81. virtual bool isSignedValue();
  82. /**
  83. * Retrieves a signed integer representing the value, if possible.
  84. *
  85. * @return A signed integer representing the value, if possible.
  86. * @throws WrongTypeException if the value is not an integer.
  87. */
  88. virtual int32 getSigned();
  89. /**
  90. * Retrieves an unsigned integer representing the value, if possible.
  91. *
  92. * @return An unsigned integer representing the value, if possible.
  93. * @throws WrongTypeException if the value is not an integer.
  94. */
  95. virtual uint32 getUnsigned();
  96. /**
  97. * Print the value to an std::ostream.
  98. *
  99. * @param output The std::ostream to write to.
  100. * @return The std::ostream used for output.
  101. */
  102. virtual std::ostream &print(std::ostream &output) const = 0;
  103. /**
  104. * Retrieves the string representation of the value.
  105. *
  106. * @return The string representation of the value.
  107. */
  108. virtual std::string getString() const;
  109. /**
  110. * Duplicates a value.
  111. *
  112. * @param output The std::ostream to output any necessary assignment to.
  113. * @return A Value corresponding to a duplicate of this entry.
  114. */
  115. virtual ValuePtr dup(std::ostream &output);
  116. /**
  117. * Negates a value.
  118. *
  119. * @return The current Value, only negated.
  120. * @throws WrongTypeException if negation is not possible.
  121. */
  122. virtual ValuePtr negate();
  123. /**
  124. * Operator precedence for this value.
  125. * Lower values bind stronger, i.e. they are resolved earlier.
  126. * In other words, if an operand has a higher precedence value than the
  127. * operator, parentheses are not required for that operand.
  128. */
  129. virtual int precedence() const;
  130. /**
  131. * Output a value to an std::ostream.
  132. *
  133. * @param output The std::ostream to output to.
  134. * @param value Reference counted pointer to the value to output.
  135. * @return The std::ostream used for output.
  136. */
  137. friend std::ostream &operator<<(std::ostream &output, Value *value) {
  138. return value->print(output);
  139. }
  140. };
  141. /**
  142. * Value containing an integer.
  143. */
  144. class IntValue : public Value {
  145. protected:
  146. const int32 _val; ///< The value of the integer.
  147. const bool _isSigned; ///< True if the value is signed, false if it's not.
  148. public:
  149. IntValue(const IntValue&) = delete;
  150. IntValue& operator = (const IntValue&) = delete;
  151. /**
  152. * Constructor for IntValue.
  153. *
  154. * @param val The integer value to be contained.
  155. * @param isSigned Whether or not the value is signed. This will affect output.
  156. */
  157. IntValue(int32 val, bool isSigned) : _val(val), _isSigned(isSigned) { }
  158. /**
  159. * Constructor for IntValue.
  160. *
  161. * @param val The integer value to be contained.
  162. * @param isSigned Whether or not the value is signed. This will affect output.
  163. */
  164. IntValue(uint32 val, bool isSigned) : _val(val), _isSigned(isSigned) { }
  165. bool isInteger();
  166. bool isSignedValue();
  167. int32 getSigned();
  168. uint32 getUnsigned();
  169. ValuePtr dup(std::ostream &output);
  170. virtual std::ostream &print(std::ostream &output) const;
  171. };
  172. /**
  173. * Value containing an absolute address.
  174. */
  175. class AddressValue : public IntValue {
  176. public:
  177. AddressValue(const AddressValue&) = delete;
  178. AddressValue& operator = (const AddressValue&) = delete;
  179. /**
  180. * Constructor for AddressValue.
  181. *
  182. * @param addr The absolute address represented by the value.
  183. */
  184. AddressValue(uint32 addr) : IntValue(addr, false) { }
  185. bool isAddress();
  186. int32 getSigned();
  187. ValuePtr dup(std::ostream &output);
  188. virtual std::ostream &print(std::ostream &output) const;
  189. };
  190. /**
  191. * Value containing a signed, relative address. When asking for unsigned integer value, exact address is returned; when printing or getting signed value, relative address is used.
  192. */
  193. class RelAddressValue : public IntValue {
  194. protected:
  195. const uint32 _baseaddr; ///< The base address for the offset.
  196. public:
  197. RelAddressValue(const RelAddressValue&) = delete;
  198. RelAddressValue& operator = (const RelAddressValue&) = delete;
  199. /**
  200. * Constructor for AddressValue.
  201. *
  202. * @param baseaddr The base address for the offset.
  203. * @param offset The relative offset to the base address.
  204. */
  205. RelAddressValue(uint32 baseaddr, int32 offset) : IntValue(offset, true), _baseaddr(baseaddr) { };
  206. bool isAddress();
  207. uint32 getUnsigned();
  208. ValuePtr dup(std::ostream &output);
  209. virtual std::ostream &print(std::ostream &output) const;
  210. };
  211. /**
  212. * Duplicated value.
  213. */
  214. class DupValue : public Value {
  215. protected:
  216. const int _idx; ///< Index to distinguish multiple duplicated entries.
  217. public:
  218. DupValue(const DupValue&) = delete;
  219. DupValue& operator = (const DupValue&) = delete;
  220. /**
  221. * Constructor for DupEntry.
  222. *
  223. * @param idx Index to distinguish multiple duplicated entries.
  224. */
  225. DupValue(int idx) : _idx(idx) { }
  226. ValuePtr dup(std::ostream &output);
  227. virtual std::ostream &print(std::ostream &output) const;
  228. };
  229. /**
  230. * String value.
  231. */
  232. class StringValue : public Value {
  233. protected:
  234. const std::string _str; ///< The string value.
  235. public:
  236. StringValue(const StringValue&) = delete;
  237. StringValue& operator = (const StringValue&) = delete;
  238. /**
  239. * Constructor for StringValue.
  240. *
  241. * @param str The string value.
  242. */
  243. StringValue(std::string str) : _str(str) { }
  244. virtual std::ostream &print(std::ostream &output) const;
  245. };
  246. class UnqotedStringValue : public StringValue
  247. {
  248. public:
  249. UnqotedStringValue(std::string str) : StringValue(str) { }
  250. virtual std::ostream &print(std::ostream &output) const override;
  251. };
  252. /**
  253. * Value representing a variable.
  254. */
  255. class VarValue : public Value {
  256. protected:
  257. std::string _varName; ///< The variable name.
  258. public:
  259. /**
  260. * Constructor for VarValue.
  261. *
  262. * @param varName The variable name.
  263. */
  264. VarValue(std::string varName) : _varName(varName) { }
  265. virtual std::ostream &print(std::ostream &output) const;
  266. };
  267. /**
  268. * Value representing array access.
  269. */
  270. class ArrayValue : public VarValue {
  271. protected:
  272. const ValueList _idxs; ///< std::deque of values representing the indexes used (left-to-right).
  273. public:
  274. ArrayValue(const ArrayValue&) = delete;
  275. ArrayValue& operator = (const ArrayValue&) = delete;
  276. /**
  277. * Constructor for ArrayValue.
  278. *
  279. * @param arrayName The name of the array.
  280. * @param idxs std::deque of stack entries representing the indexes used (left-to-right).
  281. */
  282. ArrayValue(std::string arrayName, ValueList idxs) : VarValue(arrayName), _idxs(idxs) { }
  283. virtual std::ostream &print(std::ostream &output) const;
  284. };
  285. /**
  286. * Value representing the result of a binary operation.
  287. */
  288. class BinaryOpValue : public Value {
  289. protected:
  290. const ValuePtr _lhs; ///< Value representing the left side of the operator.
  291. const ValuePtr _rhs; ///< Value representing the right side of the operator.
  292. const std::string _op; ///< The operator for this value.
  293. public:
  294. BinaryOpValue(const BinaryOpValue&) = delete;
  295. BinaryOpValue& operator = (const BinaryOpValue&) = delete;
  296. /**
  297. * Constructor for BinaryOpValue.
  298. *
  299. * @param lhs Value representing the left side of the operator.
  300. * @param rhs Value representing the right side of the operator.
  301. * @param op The operator for this value.
  302. */
  303. BinaryOpValue(ValuePtr lhs, ValuePtr rhs, std::string op) : _lhs(lhs), _rhs(rhs), _op(op) { }
  304. virtual std::ostream &print(std::ostream &output) const;
  305. virtual ValuePtr negate();
  306. virtual int precedence() const;
  307. };
  308. /**
  309. * Value representing the result of a unary operation.
  310. * Used as base class for prefix and postfix variants.
  311. */
  312. class UnaryOpValue : public Value {
  313. protected:
  314. const ValuePtr _operand; ///< Value representing the operand of the operation.
  315. const std::string _op; ///< The operator for this value.
  316. const bool _isPostfix; ///< Whether or not the operator should be postfixed to the operand.
  317. public:
  318. UnaryOpValue(const UnaryOpValue&) = delete;
  319. UnaryOpValue& operator = (const UnaryOpValue&) = delete;
  320. /**
  321. * Constructor for UnaryOpValue.
  322. *
  323. * @param operand Value representing the operand of the operation.
  324. * @param op The operator for this value.
  325. * @param isPostfix Whether or not the operator should be postfixed to the operand.
  326. */
  327. UnaryOpValue(ValuePtr operand, std::string op, bool isPostfix) :
  328. _operand(operand), _op(op), _isPostfix(isPostfix) { }
  329. virtual std::ostream &print(std::ostream &output) const;
  330. virtual int precedence() const;
  331. };
  332. /**
  333. * Negated value.
  334. */
  335. class NegatedValue : public UnaryOpValue {
  336. public:
  337. NegatedValue(const NegatedValue&) = delete;
  338. NegatedValue& operator = (const NegatedValue&) = delete;
  339. /**
  340. * Constructor for NegatedValue.
  341. *
  342. * @param val The value to negate.
  343. */
  344. NegatedValue(ValuePtr val) : UnaryOpValue(val, "!", false) { }
  345. virtual ValuePtr negate();
  346. };
  347. /**
  348. * Value representing a function call.
  349. */
  350. class CallValue : public Value {
  351. protected:
  352. const std::string _funcName; ///< The name of the function.
  353. const ValueList _args; ///< std::deque of values representing the arguments used (stored left-to-right).
  354. public:
  355. CallValue(const CallValue&) = delete;
  356. CallValue& operator = (const CallValue&) = delete;
  357. /**
  358. * Constructor for CallValue.
  359. *
  360. * @param funcName The name of the function.
  361. * @param args std::deque of values representing the arguments used.
  362. */
  363. CallValue(std::string funcName, ValueList args) : _funcName(funcName), _args(args) { }
  364. virtual std::ostream &print(std::ostream &output) const;
  365. };
  366. #endif