store.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { Modes } from '../modes';
  2. import { Types } from "../../typeSystem/types";
  3. import { StoreObject } from './storeObject';
  4. import { IType } from '../../typeSystem/itype';
  5. import { StoreObjectRef } from './storeObjectRef';
  6. import { ArrayType } from '../../typeSystem/array_type';
  7. import { ArrayStoreValue } from './array_store_value';
  8. import { IStoreValue } from './istore_value';
  9. import { StoreValue } from './store_value';
  10. import { Location } from '../../memory/location';
  11. import { StoreValueRef } from './store_value_ref';
  12. export class Store {
  13. static canImplicitTypeCast (castType: IType, sourceType: IType) {
  14. if (castType.isCompatible(Types.INTEGER) || castType.isCompatible(Types.REAL)) {
  15. if (sourceType.isCompatible(Types.INTEGER) || sourceType.isCompatible(Types.REAL)) {
  16. return true;
  17. }
  18. }
  19. return false;
  20. }
  21. static doImplicitCasting (castType: IType, stoObj: IStoreValue) {
  22. if(!Store.canImplicitTypeCast(castType, stoObj.type)) {
  23. throw new Error("!!!Critical error: attempted to type cast invalid types");
  24. }
  25. if(castType.isCompatible(Types.INTEGER)) {
  26. return new StoreValue(Types.INTEGER, stoObj.get().trunc());
  27. } else {
  28. return new StoreValue(Types.REAL, stoObj.get());
  29. }
  30. }
  31. private store: Map<String, StoreObject>;
  32. public nextStore?: Store
  33. public mode: Symbol;
  34. constructor(public name: String) {
  35. this.store = new Map<String, StoreObject>();
  36. this.mode = Modes.RUN;
  37. }
  38. extendStore (nextStore: Store) {
  39. this.nextStore = nextStore;
  40. }
  41. applyStore (id: String): IStoreValue {
  42. if (!this.store.has(id)) {
  43. if (this.nextStore != null) {
  44. return this.nextStore.applyStore(id);
  45. } else {
  46. throw new Error(`Variable ${id} not found.`);
  47. }
  48. }
  49. const val = this.store.get(id)!;
  50. let result = null
  51. if (val.type instanceof ArrayType) {
  52. result = new ArrayStoreValue(val.type, val.value, 0, 0, val.id, val.readOnly);
  53. } else {
  54. result = new StoreValue(val.type, val.value, val.id, val.readOnly);
  55. }
  56. return result;
  57. }
  58. updateStore (id: String, stoValue: IStoreValue): Store {
  59. if (!this.store.has(id)) {
  60. if (this.nextStore != null) {
  61. this.nextStore.updateStore(id, stoValue);
  62. return this;
  63. } else {
  64. // TODO: better error message
  65. throw new Error(`Variable ${id} not found.`);
  66. }
  67. } else {
  68. const oldObj = this.store.get(id)!;
  69. if (oldObj.readOnly) {
  70. // TODO: better error message
  71. throw new Error("Cannot change value of a read only variable: " + id);
  72. }
  73. if (oldObj.type instanceof ArrayType) {
  74. // oldObj.updateRef(stoValue);
  75. return this;
  76. } else if (oldObj.isCompatible(stoValue)) {
  77. // TODO check for array....
  78. // const loc_address = Location.allocate(stoValue.get());
  79. // const newObj = new StoreObject(stoValue.type, loc_address, stoValue.isConst);
  80. // newObj.setID(id);
  81. // this.store.get(id)!.destroy();
  82. // this.store.set(id, newObj);
  83. const loc_address = oldObj.locAddress;
  84. Location.updateAddress(loc_address, stoValue.get());
  85. return this;
  86. } else {
  87. const oldType = oldObj.type;
  88. const stoType = stoValue.type;
  89. // TODO: better error message
  90. throw new Error(`${oldType} is not compatible with type ${stoType} given`);
  91. }
  92. }
  93. }
  94. //In case of future use of ref, it needs to have a special function to update the storeRefObject
  95. // and no the StoreObject refferenced by it
  96. // updateStoreRef(id, stoObjAddress) {...}
  97. insertStore (id: String, stoValue: IStoreValue) {
  98. if (this.store.has(id)) {
  99. // TODO: better error message
  100. throw new Error(`${id} is already defined`);
  101. }
  102. // TODO check for array....
  103. let newObj:StoreObject;
  104. if(stoValue instanceof StoreValueRef) {
  105. // TODO check for array....
  106. newObj = new StoreObjectRef(stoValue, stoValue.getRefAddress());
  107. } else {
  108. const loc_address = Location.allocate(stoValue.get());
  109. newObj = new StoreObject(stoValue.type, loc_address, stoValue.isConst);
  110. }
  111. newObj.setID(id);
  112. this.store.set(id, newObj);
  113. return this;
  114. }
  115. /**
  116. * Helper function similar to applyStore. But it returns the actual object in the store be it ref or not
  117. * applyStore will return the refferenced object if the object in the store is a ref
  118. */
  119. getStoreObject (id: String): StoreObject {
  120. if (!this.store.has(id)) {
  121. if (this.nextStore != null) {
  122. return this.nextStore.getStoreObject(id);
  123. } else {
  124. throw new Error(`Variable ${id} not found.`);
  125. }
  126. }
  127. return this.store.get(id)!;
  128. }
  129. destroy (): void {
  130. this.store.forEach(sto => sto.destroy(), this);
  131. }
  132. }