Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. /*
  2. * value.h
  3. */
  4.  
  5. #ifndef VALUE_H_
  6. #define VALUE_H_
  7.  
  8. #include <string>
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. // object holds boolean, integer, or string, and remembers which it holds
  13. class Value {
  14. bool bval;
  15. int ival;
  16. string sval;
  17. enum VT { isBool, isInt, isString, isTypeError } type;
  18.  
  19. public:
  20. Value() : bval(false), ival(0), type(isTypeError) {}
  21. Value(bool bval) : bval(bval), ival(0), type(isBool) {}
  22. Value(int ival) : bval(false), ival(ival), type(isInt) {}
  23. Value(string sval) : bval(false), ival(0), sval(sval), type(isString) {}
  24.  
  25. // in the case of an error, I use the value to hold the error message
  26. Value(string sval, bool isError) : bval(false), ival(0), sval(sval), type(isTypeError) {}
  27.  
  28. bool isBoolType() const { return type == VT::isBool; }
  29. bool isIntType() const { return type == VT::isInt; }
  30. bool isStringType() const { return type == VT::isString; }
  31. bool isError() const { return type == VT::isTypeError; }
  32. bool hasMessage() const { return isError() && sval.size() > 0; }
  33.  
  34. bool isTrue() const { return isBoolType() && bval; }
  35. bool getBoolean() const {
  36. if( !isBoolType() )
  37. throw "Not boolean valued";
  38. return bval;
  39. }
  40.  
  41. int getInteger() const {
  42. if( !isIntType() )
  43. throw "Not integer valued";
  44. return ival;
  45. }
  46.  
  47. string getString() const {
  48. if( !isStringType() )
  49. throw "Not string valued";
  50. return sval;
  51. }
  52.  
  53. string getMessage() const {
  54. if( !hasMessage() )
  55. throw "No message";
  56. return sval;
  57. }
  58.  
  59.  
  60. friend ostream& operator<<(ostream& out, const Value& v) {
  61. if( v.isBoolType()) out << (v.bval ? "True" : "False");
  62. else if( v.isIntType() ) out << v.getInteger();
  63. else if( v.isStringType() ) out << v.getString();
  64. else if( v.sval.size() > 0 ) out << std::to_string(1) + ": RUNTIME ERROR " << v.getMessage();
  65. else out << "TYPE ERROR";
  66. return out;
  67. }
  68.  
  69. Value operator+(const Value& v){
  70. if(this->isIntType()&& v.isIntType())
  71. return Value(this->getInteger()+v.getInteger());
  72. else if(this->isStringType()&&v.isStringType()){
  73. return Value(this->getString() + "" + v.getString());
  74. }
  75. else
  76. {
  77. string err = "'+' operator used incorrectly";
  78. return Value(err, true);
  79. }
  80. }
  81.  
  82.  
  83. Value operator-(const Value& v){
  84. if(this->isIntType()&&v.isIntType())
  85. return Value(this->getInteger()-v.getInteger());
  86. string err = "only ints can be subtracted";
  87. return Value(err, true);
  88. }
  89. Value operator*(const Value& v)
  90. {
  91.  
  92. if(this->isIntType()&&v.isIntType()){
  93. return Value(this->getInteger() * v.getInteger());
  94.  
  95. }
  96.  
  97. else if(v.isBoolType()&&this->isIntType()&&this->getInteger()==-1)
  98. {
  99. return !(v.getBoolean());
  100. }
  101. else if(this->isIntType()&&v.isStringType())
  102. {
  103. if(this->getInteger()>-1)
  104. {
  105. string ans="";
  106. for(int i=0; i<this->getInteger(); i++)
  107. ans+=v.getString();
  108. return Value(ans);
  109. }
  110. string err = "can't multiply a negative number and a string";
  111. return Value(err, true);
  112. }
  113. else if(this->isStringType()&&v.isIntType())
  114. {
  115. if(v.getInteger()>-1)
  116. {
  117. string ans="";
  118. for(int i=0; i<v.getInteger(); i++)
  119. ans+=this->getString();
  120. return Value(ans);
  121. }
  122. string err = "can't multiply a negative number and a string";
  123. return Value(err, true);
  124. }
  125. else
  126. {
  127. string err = "'*' operator used incorrectly";
  128. return Value(err, true);
  129. }
  130. }
  131. Value operator/(const Value& v)
  132. {
  133. if(this->isIntType()&&v.isIntType())
  134. return Value(this->getInteger()/v.getInteger());
  135. else
  136. {
  137. string err = "'/' operator used incorrectly";
  138. return Value(err, true);
  139. }
  140. }
  141.  
  142. Value operator<(const Value& v){
  143. if(this->isIntType()&&v.isIntType())
  144. return Value(this->getInteger()<v.getInteger());
  145. else if(this->isStringType()&&v.isStringType())
  146. return Value(this->getString()<v.getString());
  147. else
  148. {
  149. string err = "'<' operator used incorrectly";
  150. return Value(err, true);
  151. }
  152. }
  153. Value operator<=(const Value& v)
  154. {
  155. if(this->isIntType()&&v.isIntType())
  156. return Value(this->getInteger()<=v.getInteger());
  157. else if(this->isStringType()&&v.isStringType())
  158. return Value(this->getString()<=v.getString());
  159. else
  160. {
  161. string err = "'<=' operator used incorrectly";
  162. return Value(err, true);
  163. }
  164.  
  165. }
  166. Value operator>(const Value& v)
  167. {
  168. if(this->isIntType()&&v.isIntType())
  169. return Value(this->getInteger()>v.getInteger());
  170. else if(this->isStringType()&&v.isStringType())
  171. return Value(this->getString()>v.getString());
  172. else
  173. {
  174. string err = "'>' operator used incorrectly";
  175. return Value(err, true);
  176. }
  177. }
  178. Value operator>=(const Value& v){
  179. if(this->isIntType()&&v.isIntType())
  180. return Value(this->getInteger()>=v.getInteger());
  181. else if(this->isStringType()&&v.isStringType())
  182. return Value(this->getString()>=v.getString());
  183. else
  184. {
  185. string err = "'>=' operator used incorrectly";
  186. return Value(err, true);
  187. }
  188. }
  189. Value operator==(const Value& v){
  190. if(this->isIntType()&&v.isIntType())
  191. return Value(this->getInteger()==v.getInteger());
  192. else if(this->isStringType()&&v.isStringType())
  193. return Value(this->getString()==v.getString());
  194. else if(this->isBoolType()&&v.isBoolType())
  195. return Value(this->getBoolean()==v.getBoolean());
  196. else
  197. {
  198. string err = "'==' operator used incorrectly";
  199. return Value(err, true);
  200. }
  201. }
  202. Value operator||(const Value& v){
  203. if(this->isBoolType())
  204. if(this->getBoolean()==true)
  205. return Value(true);
  206.  
  207. if(this->isBoolType()&&v.isBoolType())
  208. return Value(this->getBoolean()||v.getBoolean());
  209. string err = "'||' operator used incorrectly";
  210. return Value(err, true);
  211. }
  212. Value operator&&(const Value& v){
  213. if(this->isBoolType())
  214. if(this->getBoolean()==false)
  215. return Value(false);
  216. if(this->isBoolType()&&v.isBoolType())
  217. return Value(this->getBoolean()&&v.getBoolean());
  218. string err = "'&&' operator used incorrectly";
  219. return Value(err, true);
  220. }
  221. Value operator!=(const Value& v) {
  222. Value ans = this->operator==(v);
  223. ans.bval = !ans.bval;
  224. return ans;
  225. }
  226. };
  227.  
  228. #endif /* VALUE_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement