VariableDictionary.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Collections.Generic;
  3. namespace NTERA.Interpreter
  4. {
  5. public class VariableDictionary : Dictionary<string, Dictionary<int, Value>>
  6. {
  7. public VariableDictionary() : base(StringComparer.InvariantCultureIgnoreCase)
  8. {
  9. InitDefaults();
  10. }
  11. public new Value this[string key]
  12. {
  13. get => this[key, 0];
  14. set => this[key, 0] = value;
  15. }
  16. public Value this[string key, int arrayIndex]
  17. {
  18. get => base[key][arrayIndex];
  19. set
  20. {
  21. if (base.TryGetValue(key, out var dict))
  22. dict[arrayIndex] = value;
  23. else
  24. Add(key, arrayIndex, value);
  25. }
  26. }
  27. public void Add(string key, Value value)
  28. {
  29. Add(key, 0, value);
  30. }
  31. public void Add(string key, int arrayIndex, Value value)
  32. {
  33. base.Add(key, new Dictionary<int, Value>
  34. {
  35. [arrayIndex] = value
  36. });
  37. }
  38. private void InitDefaults()
  39. {
  40. Add("LOCAL", 0);
  41. Add("LOCALS", "");
  42. Add("RESULT", 0);
  43. Add("RESULTS", "");
  44. }
  45. }
  46. }