-
Notifications
You must be signed in to change notification settings - Fork 0
/
Variable.cs
552 lines (474 loc) · 17.2 KB
/
Variable.cs
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
using Monkeyspeak.Extensions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Linq;
using System.Collections;
namespace Monkeyspeak
{
[Serializable]
public class VariableIsConstantException : Exception
{
public VariableIsConstantException()
{
}
public VariableIsConstantException(string message)
: base(message)
{
}
public VariableIsConstantException(string message, Exception inner)
: base(message, inner)
{
}
protected VariableIsConstantException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
: base(info, context) { }
}
public class VariableEqualityComparer : IEqualityComparer<IVariable>
{
public bool Equals(IVariable x, IVariable y)
{
return x.Equals(y);
}
public int GetHashCode(IVariable obj)
{
return obj.GetHashCode();
}
}
public interface IVariable : IEquatable<IVariable>
{
string Name { get; }
object Value { get; set; }
bool IsConstant { get; }
}
[Serializable]
[CLSCompliant(false)]
public class Variable : IVariable
{
public bool Equals(IVariable other)
{
return Equals(value, other.Value) && string.Equals(Name, other.Name);
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// </returns>
public override int GetHashCode()
{
unchecked
{
return Name.GetHashCode();
}
}
public static readonly IVariable NoValue = new Variable("%none", null, true);
public bool IsConstant
{
get;
private set;
}
private object value;
public object Value
{
get { return value; }
set
{
// removed Value = as it interfered with page.setVariable - Gerolkae
if (!CheckType(value)) throw new TypeNotSupportedException(value.GetType().Name +
" is not a supported type. Expecting string, double or variable.");
if (value != null && IsConstant)
throw new VariableIsConstantException($"Attempt to assign a value to constant '{Name}'");
if (value is IVariable)
this.value = (value as IVariable).Value;
else
this.value = value;
}
}
public string Name { get; internal set; }
internal Variable(string name, bool constant = false)
{
IsConstant = constant;
Name = name;
}
internal Variable(string name, object value, bool constant = false)
{
IsConstant = constant;
Name = name;
this.value = value;
}
private bool CheckType(object _value)
{
if (_value == null) return true;
return _value is string ||
_value is double ||
_value is IVariable;
}
/// <summary>
/// Returns a const identifier if the variable is constant followed by name,
/// <para>otherwise just the name is returned.</para>
/// </summary>
/// <returns></returns>
public override string ToString()
{
return ((IsConstant) ? "const " : "") + $"{Name} = {value ?? "null"}";
}
/// <summary>
///
/// </summary>
/// <param name="asConstant">Clone as Constant</param>
/// <returns></returns>
public Variable Clone(bool asConstant = false)
{
return new Variable(Name, value, asConstant);
}
/// <summary>
/// Implements the operator ==.
/// </summary>
/// <param name="varA">The variable a.</param>
/// <param name="varB">The variable b.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator ==(Variable varA, Variable varB)
{
return varA.Value == varB.Value;
}
/// <summary>
/// Implements the operator !=.
/// </summary>
/// <param name="varA">The variable a.</param>
/// <param name="varB">The variable b.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator !=(Variable varA, Variable varB)
{
return varA.Value != varB.Value;
}
/// <summary>
/// Implements the operator +.
/// </summary>
/// <param name="varA">The variable a.</param>
/// <param name="num">The number.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Variable operator +(Variable varA, double num)
{
varA.Value = varA.Value.AsDouble() + num;
return varA;
}
/// <summary>
/// Implements the operator -.
/// </summary>
/// <param name="varA">The variable a.</param>
/// <param name="num">The number.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Variable operator -(Variable varA, double num)
{
varA.Value = varA.Value.AsDouble() - num;
return varA;
}
/// <summary>
/// Implements the operator *.
/// </summary>
/// <param name="varA">The variable a.</param>
/// <param name="num">The number.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Variable operator *(Variable varA, double num)
{
varA.Value = varA.Value.AsDouble() * num;
return varA;
}
/// <summary>
/// Implements the operator /.
/// </summary>
/// <param name="varA">The variable a.</param>
/// <param name="num">The number.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Variable operator /(Variable varA, double num)
{
varA.Value = varA.Value.AsDouble() / num;
return varA;
}
/// <summary>
/// Implements the operator +.
/// </summary>
/// <param name="varA">The variable a.</param>
/// <param name="str">The string.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Variable operator +(Variable varA, string str)
{
varA.Value = varA.Value.AsDouble() + str;
return varA;
}
/// <summary>
/// Performs an implicit conversion from <see cref="Variable"/> to <see cref="System.String"/>.
/// </summary>
/// <param name="var">The variable.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator string(Variable var)
{
return var.Value.AsString();
}
/// <summary>
/// Performs an implicit conversion from <see cref="Variable"/> to <see cref="System.Double"/>.
/// </summary>
/// <param name="var">The variable.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator double(Variable var)
{
return var.Value.AsDouble();
}
/// <summary>
/// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
return obj != null && obj is Variable && Equals((Variable)obj);
}
}
[Serializable]
[CLSCompliant(false)]
public sealed class VariableTable : IVariable, IDictionary<string, object>
{
public static VariableTable Empty = new VariableTable("null", true, 0);
private static int limit = 100;
public static int Limit { get => limit; set => limit = value; }
public string Name { get; private set; }
/// <summary>
/// Gets the index of the current element.
/// </summary>
/// <value>
/// The index of the current element.
/// </value>
public int CurrentElementIndex { get; private set; }
/// <summary>
/// Gets or sets the active string based indexer.
/// </summary>
/// <value>
/// The active indexer.
/// </value>
public string ActiveIndexer { get; set; }
internal IDictionary<string, object> values;
public object Value
{
get { return string.IsNullOrEmpty(ActiveIndexer) ? values.Values.FirstOrDefault() : this[ActiveIndexer]; }
set
{
if (!CheckType(value)) throw new TypeNotSupportedException(value.GetType().Name +
" is not a supported type. Expecting string, double or variable.");
if (value != null && IsConstant)
throw new VariableIsConstantException($"Attempt to assign a value to constant '{Name}'");
if (string.IsNullOrWhiteSpace(ActiveIndexer))
ActiveIndexer = "value";
if (value is IVariable)
this[ActiveIndexer] = (value as IVariable).Value;
else
this[ActiveIndexer] = value;
}
}
public object this[string key]
{
get
{
if (values.TryGetValue(key, out object value))
return value;
return null;
}
set
{
if (values.Count + 1 > Limit) return;
if (!CheckType(value)) throw new TypeNotSupportedException(value.GetType().Name +
" is not a supported type. Expecting string, double or variable.");
if (value != null && IsConstant)
throw new VariableIsConstantException($"Attempt to assign a value to constant '{Name}'");
if (value is IVariable)
values[key] = (value as IVariable).Value;
else
values[key] = value;
}
}
public KeyValuePair<string, object> this[int index]
{
get
{
return At(index);
}
}
public int Count { get => values.Count; }
public bool IsConstant { get; private set; }
public ICollection<string> Keys => values.Keys;
public ICollection<object> Values => values.Values;
bool ICollection<KeyValuePair<string, object>>.IsReadOnly => ((IDictionary<string, object>)values).IsReadOnly;
object IDictionary<string, object>.this[string key] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public VariableTable(string name, bool isConstant = false, int limit = 100)
{
Name = name;
values = new Dictionary<string, object>(limit);
IsConstant = isConstant;
Limit = limit;
}
public VariableTable(string name, IDictionary<string, object> dictionary, bool isConstant = false)
{
Name = name;
values = dictionary;
IsConstant = isConstant;
Limit = limit;
}
public void Add(string key, object value)
{
if (values.Count + 1 > Limit) return;
if (!CheckType(value)) throw new TypeNotSupportedException(value.GetType().Name +
" is not a supported type. Expecting string, double or variable.");
if (value != null && IsConstant)
throw new VariableIsConstantException($"Attempt to assign a value to constant '{Name}'");
if (value is IVariable)
values[key] = (value as IVariable).Value;
else
values[key] = value;
}
public void Add(object value)
{
if (values.Count + 1 > Limit) return;
if (!CheckType(value)) throw new TypeNotSupportedException(value.GetType().Name +
" is not a supported type. Expecting string, double or variable.");
if (value != null && IsConstant)
throw new VariableIsConstantException($"Attempt to assign a value to constant '{Name}'");
var key = (values.Count + 1).ToString();
if (value is IVariable)
values[key] = (value as IVariable).Value;
else
values[key] = value;
}
public bool Contains(string index)
{
return values.ContainsKey(index);
}
private KeyValuePair<string, object> At(int index)
{
return values.ElementAtOrDefault(index);
}
public bool Next(out object value)
{
if (CurrentElementIndex > Count - 1)
{
CurrentElementIndex = 0;
ActiveIndexer = null;
value = null;
return false;
}
var pair = this[CurrentElementIndex];
CurrentElementIndex++;
ActiveIndexer = pair.Key;
value = pair.Value;
return true;
}
private bool CheckType(object _value)
{
if (_value == null) return true;
return _value is string ||
_value is double ||
_value is IVariable;
}
public bool Equals(IVariable other)
{
return Equals(values, other.Value) && string.Equals(Name, other.Name);
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// </returns>
public override int GetHashCode()
{
unchecked
{
return Name.GetHashCode();
}
}
/// <summary>
/// Returns a const identifier if the variable is constant followed by name,
/// <para>otherwise just the name is returned.</para>
/// </summary>
/// <returns></returns>
public override string ToString()
{
string replace = (values != null ? values.ToString(',') : "null");
return ((IsConstant) ? "const " : "") + $"{Name} = {replace}";
}
public void ResetIndex()
{
CurrentElementIndex = 0;
}
/// <summary>
/// Clears all values in this table.
/// </summary>
public void Clear()
{
values.Clear();
}
public bool ContainsKey(string key)
{
return values.ContainsKey(key);
}
public bool Remove(string key)
{
return values.Remove(key);
}
public bool TryGetValue(string key, out object value)
{
return values.TryGetValue(key, out value);
}
public void Add(KeyValuePair<string, object> item)
{
Add(item.Key, item.Value);
}
public bool Contains(KeyValuePair<string, object> item)
{
return values.Contains(item);
}
public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
{
((IDictionary<string, object>)values).CopyTo(array, arrayIndex);
}
public bool Remove(KeyValuePair<string, object> item)
{
return values.Remove(item.Key);
}
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
return values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return values.GetEnumerator();
}
public static VariableTable From(string name, IDictionary<string, object> dict)
{
var table = new VariableTable(name);
foreach (var kv in dict) table.Add(kv);
return table;
}
}
}