Value.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System;
  2. namespace Yarn
  3. {
  4. // A value from inside Yarn.
  5. public class Value : IComparable, IComparable<Value> {
  6. public static readonly Value NULL = new Value();
  7. public enum Type {
  8. Number, // a constant number
  9. String, // a string
  10. Bool, // a boolean value
  11. Variable, // the name of a variable; will be expanded at runtime
  12. Object, // We can't tell what it is, but the client code wants us to store it.
  13. Null, // the null value
  14. }
  15. public Value.Type type { get; internal set; }
  16. private Object backingValue;
  17. public float AsNumber {
  18. get {
  19. switch (type) {
  20. case Type.Number:
  21. return (backingValue as float?).Value;
  22. case Type.String:
  23. try {
  24. return float.Parse (backingValue.ToString());
  25. } catch (FormatException) {
  26. return 0.0f;
  27. }
  28. case Type.Bool:
  29. return (backingValue as bool?).Value ? 1.0f : 0.0f;
  30. case Type.Null:
  31. case Type.Object:
  32. return 0.0f;
  33. default:
  34. throw new InvalidOperationException ("Cannot cast to number from " + type.ToString());
  35. }
  36. }
  37. }
  38. public bool AsBool {
  39. get {
  40. switch (type) {
  41. case Type.Number:
  42. var v = (backingValue as float?).Value;
  43. return !float.IsNaN(v) && v != 0.0f;
  44. case Type.String:
  45. return !String.IsNullOrEmpty(backingValue.ToString()); // What??
  46. case Type.Bool:
  47. return (backingValue as bool?).Value;
  48. case Type.Null:
  49. return false;
  50. case Type.Object:
  51. return true;
  52. default:
  53. throw new InvalidOperationException ("Cannot cast to bool from " + type.ToString());
  54. }
  55. }
  56. }
  57. public string AsString {
  58. get {
  59. if (backingValue == null)
  60. return "null";
  61. return backingValue.ToString();
  62. }
  63. }
  64. public Object AsObject
  65. {
  66. get
  67. {
  68. return backingValue;
  69. }
  70. }
  71. // Create a null value
  72. public Value () : this(null) { }
  73. // Create a value with a C# object
  74. public Value(object value)
  75. {
  76. // Copy an existing value
  77. if (typeof(Value).IsInstanceOfType(value))
  78. {
  79. var otherValue = value as Value;
  80. type = otherValue.type;
  81. backingValue = otherValue.backingValue;
  82. }
  83. else
  84. {
  85. backingValue = value;
  86. if (value == null)
  87. type = Type.Null;
  88. else if (value.GetType() == typeof(string))
  89. type = Type.String;
  90. else if (value.GetType() == typeof(int) ||
  91. value.GetType() == typeof(float) ||
  92. value.GetType() == typeof(double))
  93. {
  94. type = Type.Number;
  95. backingValue = System.Convert.ToSingle(value);
  96. }
  97. else if (value.GetType() == typeof(bool))
  98. type = Type.Bool;
  99. else
  100. type = Type.Object;
  101. }
  102. }
  103. public virtual int CompareTo(object obj) {
  104. if (obj == null) return 1;
  105. // soft, fast coercion
  106. var other = obj as Value;
  107. // not a value
  108. if( other == null ) throw new ArgumentException("Object is not a Value");
  109. // it is a value!
  110. return this.CompareTo(other);
  111. }
  112. public virtual int CompareTo(Value other) {
  113. if (other == null) {
  114. return 1;
  115. }
  116. if (other.type == this.type)
  117. {
  118. switch (this.type)
  119. {
  120. case Type.Null:
  121. return 0;
  122. case Type.String:
  123. return this.AsString.CompareTo(other.AsString);
  124. case Type.Number:
  125. return this.AsNumber.CompareTo(other.AsNumber);
  126. case Type.Bool:
  127. return this.AsBool.CompareTo(other.AsBool);
  128. default:
  129. break; // Let the string test do it.
  130. }
  131. }
  132. // try to do a string test at that point!
  133. return this.AsString.CompareTo(other.AsString);
  134. }
  135. public override bool Equals (object obj)
  136. {
  137. if (obj == null || GetType() != obj.GetType()) {
  138. return false;
  139. }
  140. var other = (Value)obj;
  141. switch (this.type) {
  142. case Type.Number:
  143. return this.AsNumber == other.AsNumber;
  144. case Type.String:
  145. return this.AsString == other.AsString;
  146. case Type.Bool:
  147. return this.AsBool == other.AsBool;
  148. case Type.Null:
  149. return other.type == Type.Null || other.AsNumber == 0 || other.AsBool == false; // Why does null = 0 or false?
  150. case Type.Object:
  151. return Object.ReferenceEquals(this.backingValue, other.backingValue);
  152. default:
  153. throw new ArgumentOutOfRangeException ();
  154. }
  155. }
  156. // override object.GetHashCode
  157. public override int GetHashCode()
  158. {
  159. // TODO: yeah hay maybe fix this
  160. // - Just use 'this' as the hash.
  161. if(backingValue != null ) {
  162. return backingValue.GetHashCode();
  163. }
  164. return 0;
  165. }
  166. public override string ToString ()
  167. {
  168. return string.Format ("[Value: type={0}, AsNumber={1}, AsBool={2}, AsString={3}]", type, AsNumber, AsBool, AsString);
  169. }
  170. public static Value operator+ (Value a, Value b) {
  171. // catches:
  172. // undefined + string
  173. // number + string
  174. // string + string
  175. // bool + string
  176. // null + string
  177. if (a.type == Type.String || b.type == Type.String ) {
  178. // we're headed for string town!
  179. return new Value( a.AsString + b.AsString );
  180. }
  181. // catches:
  182. // number + number
  183. // bool (=> 0 or 1) + number
  184. // null (=> 0) + number
  185. // bool (=> 0 or 1) + bool (=> 0 or 1)
  186. // null (=> 0) + null (=> 0)
  187. if ((a.type == Type.Number || b.type == Type.Number) ||
  188. (a.type == Type.Bool && b.type == Type.Bool) ||
  189. (a.type == Type.Null && b.type == Type.Null)
  190. ) {
  191. return new Value( a.AsNumber + b.AsNumber );
  192. }
  193. throw new System.ArgumentException(
  194. string.Format("Cannot add types {0} and {1}.", a.type, b.type )
  195. );
  196. }
  197. public static Value operator- (Value a, Value b) {
  198. if (a.type == Type.Number && (b.type == Type.Number || b.type == Type.Null) ||
  199. b.type == Type.Number && (a.type == Type.Number || a.type == Type.Null)
  200. ) {
  201. return new Value( a.AsNumber - b.AsNumber );
  202. }
  203. throw new System.ArgumentException(
  204. string.Format("Cannot subtract types {0} and {1}.", a.type, b.type )
  205. );
  206. }
  207. public static Value operator* (Value a, Value b) {
  208. if (a.type == Type.Number && (b.type == Type.Number || b.type == Type.Null) ||
  209. b.type == Type.Number && (a.type == Type.Number || a.type == Type.Null)
  210. ) {
  211. return new Value( a.AsNumber * b.AsNumber );
  212. }
  213. throw new System.ArgumentException(
  214. string.Format("Cannot multiply types {0} and {1}.", a.type, b.type )
  215. );
  216. }
  217. public static Value operator/ (Value a, Value b) {
  218. if (a.type == Type.Number && (b.type == Type.Number || b.type == Type.Null) ||
  219. b.type == Type.Number && (a.type == Type.Number || a.type == Type.Null)
  220. ) {
  221. return new Value( a.AsNumber / b.AsNumber );
  222. }
  223. throw new System.ArgumentException(
  224. string.Format("Cannot divide types {0} and {1}.", a.type, b.type )
  225. );
  226. }
  227. public static Value operator %(Value a, Value b) {
  228. if (a.type == Type.Number && (b.type == Type.Number || b.type == Type.Null) ||
  229. b.type == Type.Number && (a.type == Type.Number || a.type == Type.Null)) {
  230. return new Value (a.AsNumber % b.AsNumber);
  231. }
  232. throw new System.ArgumentException(
  233. string.Format("Cannot modulo types {0} and {1}.", a.type, b.type )
  234. );
  235. }
  236. public static Value operator- (Value a) {
  237. if( a.type == Type.Number ) {
  238. return new Value( -a.AsNumber );
  239. }
  240. if (a.type == Type.Null &&
  241. a.type == Type.String &&
  242. (a.AsString == null || a.AsString.Trim() == "")
  243. ) {
  244. return new Value( -0 );
  245. }
  246. return new Value( float.NaN );
  247. }
  248. // Define the is greater than operator.
  249. public static bool operator > (Value operand1, Value operand2)
  250. {
  251. return operand1.CompareTo(operand2) == 1;
  252. }
  253. // Define the is less than operator.
  254. public static bool operator < (Value operand1, Value operand2)
  255. {
  256. return operand1.CompareTo(operand2) == -1;
  257. }
  258. // Define the is greater than or equal to operator.
  259. public static bool operator >= (Value operand1, Value operand2)
  260. {
  261. return operand1.CompareTo(operand2) >= 0;
  262. }
  263. // Define the is less than or equal to operator.
  264. public static bool operator <= (Value operand1, Value operand2)
  265. {
  266. return operand1.CompareTo(operand2) <= 0;
  267. }
  268. }
  269. }