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