You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
1.8 KiB

3 years ago
3 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace SuperBASIC
  5. {
  6. enum NumberType{
  7. Ans,
  8. Number,
  9. Operand,
  10. Memory
  11. };
  12. struct BasicNumber
  13. {
  14. internal NumberType type;
  15. internal Runtime runtime;
  16. readonly private float number;
  17. readonly private int operand;
  18. [Serializable]
  19. public class BadNumber : Exception
  20. {
  21. public BadNumber() { }
  22. public BadNumber(string message) : base(message) { }
  23. public BadNumber(string message, Exception inner) : base(message, inner) { }
  24. protected BadNumber(
  25. System.Runtime.Serialization.SerializationInfo info,
  26. System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
  27. }
  28. internal BasicNumber(Runtime rt, float v)
  29. {
  30. type = NumberType.Number;
  31. number = v;
  32. operand = 0;
  33. runtime = rt;
  34. }
  35. internal BasicNumber(Runtime rt)
  36. {
  37. type = NumberType.Ans;
  38. number = 0;
  39. operand = 0;
  40. runtime = rt;
  41. }
  42. internal BasicNumber(Runtime rt, int v, NumberType reqType = NumberType.Operand)
  43. {
  44. if(reqType == NumberType.Operand)
  45. {
  46. type = NumberType.Operand;
  47. number = 0;
  48. operand = v;
  49. runtime = rt;
  50. }
  51. else
  52. {
  53. type = NumberType.Memory;
  54. number = 0;
  55. if (v > Int16.MaxValue) throw new BadNumber("Generated out of memory access");
  56. operand = v;
  57. runtime = rt;
  58. }
  59. }
  60. internal int GetOperand()
  61. {
  62. return operand;
  63. }
  64. public float GetValue()
  65. {
  66. if (type == NumberType.Number)
  67. {
  68. return number;
  69. }
  70. else if(type == NumberType.Ans)
  71. {
  72. return runtime.GetRegister();
  73. }
  74. #if MEMORY
  75. else
  76. {
  77. return Functions.Memory.MemoryGet((short)operand);
  78. }
  79. #else
  80. return 0;
  81. #endif
  82. }
  83. public static implicit operator float(BasicNumber v) => v.GetValue();
  84. public override string ToString() => $"{GetValue()}";
  85. }
  86. }