store.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 './value/array_store_value';
  8. import { Location } from '../../memory/location';
  9. import { StoreObjectArray } from './storeObjectArray';
  10. import { IStoreValue } from './value/istore_value';
  11. import { StoreValue } from './value/store_value';
  12. import { StoreValueAddress } from './value/store_value_address';
  13. import { StoreValueRef } from './value/store_value_ref';
  14. import { ArrayStoreValueRef } from './value/array_store_value_ref';
  15. import { StoreObjectArrayRef } from './store_object_array_ref';
  16. export class Store {
  17. static canImplicitTypeCast (castType: IType, sourceType: IType) {
  18. if (castType.isCompatible(Types.INTEGER) || castType.isCompatible(Types.REAL)) {
  19. if (sourceType.isCompatible(Types.INTEGER) || sourceType.isCompatible(Types.REAL)) {
  20. return true;
  21. }
  22. }
  23. return false;
  24. }
  25. static doImplicitCasting (castType: IType, stoObj: IStoreValue) {
  26. if(!Store.canImplicitTypeCast(castType, stoObj.type)) {
  27. throw new Error("!!!Critical error: attempted to type cast invalid types");
  28. }
  29. if(castType.isCompatible(Types.INTEGER)) {
  30. return new StoreValue(Types.INTEGER, stoObj.get().trunc());
  31. } else {
  32. return new StoreValue(Types.REAL, stoObj.get());
  33. }
  34. }
  35. private store: Map<String, StoreObject>;
  36. public nextStore?: Store
  37. public mode: Symbol;
  38. constructor(public name: String) {
  39. this.store = new Map<String, StoreObject>();
  40. this.mode = Modes.RUN;
  41. }
  42. extendStore (nextStore: Store) {
  43. this.nextStore = nextStore;
  44. }
  45. applyStore (id: String): IStoreValue {
  46. if (!this.store.has(id)) {
  47. if (this.nextStore != null) {
  48. return this.nextStore.applyStore(id);
  49. } else {
  50. throw new Error(`Variable ${id} not found.`);
  51. }
  52. }
  53. const val = this.store.get(id)!;
  54. let result = null
  55. if (val.type instanceof ArrayType) {
  56. const array = val as StoreObjectArray;
  57. const array_type = array.type as ArrayType;
  58. let l = 0, c = 0;
  59. const values = array.value.map( v => {
  60. if(array.isVector) {
  61. return new StoreValueAddress(array_type.innerType, v, l++, undefined, array.id, array.readOnly);
  62. } else {
  63. if(c >= array.columns) {
  64. c = 0;
  65. l += 1;
  66. }
  67. return new StoreValueAddress(array_type.innerType, v, l, c++, array.id, array.readOnly);
  68. }
  69. });
  70. result = new ArrayStoreValue(array_type, values, array.lines, array.columns, val.id, val.readOnly);
  71. } else {
  72. result = new StoreValue(val.type, val.value, val.id, val.readOnly);
  73. }
  74. return result;
  75. }
  76. updateStore (id: String, stoValue: IStoreValue): Store {
  77. if (!this.store.has(id)) {
  78. if (this.nextStore != null) {
  79. this.nextStore.updateStore(id, stoValue);
  80. return this;
  81. } else {
  82. // TODO: better error message
  83. throw new Error(`Variable ${id} not found.`);
  84. }
  85. } else {
  86. const oldObj = this.store.get(id)!;
  87. if (oldObj.readOnly) {
  88. // TODO: better error message
  89. throw new Error("Cannot change value of a read only variable: " + id);
  90. }
  91. if (oldObj.type instanceof ArrayType) {
  92. // oldObj.updateRef(stoValue);
  93. return this;
  94. } else if (oldObj.isCompatible(stoValue)) {
  95. // TODO check for array....
  96. // const loc_address = Location.allocate(stoValue.get());
  97. // const newObj = new StoreObject(stoValue.type, loc_address, stoValue.isConst);
  98. // newObj.setID(id);
  99. // this.store.get(id)!.destroy();
  100. // this.store.set(id, newObj);
  101. const loc_address = oldObj.locAddress;
  102. Location.updateAddress(loc_address, stoValue.get());
  103. return this;
  104. } else {
  105. const oldType = oldObj.type;
  106. const stoType = stoValue.type;
  107. // TODO: better error message
  108. throw new Error(`${oldType} is not compatible with type ${stoType} given`);
  109. }
  110. }
  111. }
  112. //In case of future use of ref, it needs to have a special function to update the storeRefObject
  113. // and no the StoreObject refferenced by it
  114. // updateStoreRef(id, stoObjAddress) {...}
  115. insertStore (id: String, stoValue: IStoreValue) {
  116. if (this.store.has(id)) {
  117. // TODO: better error message
  118. throw new Error(`${id} is already defined`);
  119. }
  120. // TODO check for array....
  121. let newObj:StoreObject;
  122. if(stoValue instanceof StoreValueRef) {
  123. newObj = new StoreObjectRef(stoValue);
  124. } else if (stoValue instanceof ArrayStoreValueRef) {
  125. newObj = new StoreObjectArrayRef(stoValue);
  126. } else if (stoValue instanceof ArrayStoreValue) {
  127. const columns = stoValue.isVector() ? 0 : stoValue.columns!;
  128. const addresses: number[] = [];
  129. const all_values = stoValue.get();
  130. if(all_values.length > 0) {
  131. for(let i = 0; i < stoValue.get().length; i += 1) {
  132. const val = all_values[i].get();
  133. addresses.push(Location.allocate(val));
  134. }
  135. } else {
  136. let total = stoValue.lines;
  137. total = stoValue.isVector() ? total : total * columns;
  138. for(let i = 0; i < total; i += 1) {
  139. addresses.push(Location.allocate(null));
  140. }
  141. }
  142. newObj = new StoreObjectArray(stoValue.type as ArrayType, stoValue.lines, columns, addresses, stoValue.isConst);
  143. } else {
  144. const loc_address = Location.allocate(stoValue.get());
  145. newObj = new StoreObject(stoValue.type, loc_address, stoValue.isConst);
  146. }
  147. newObj.setID(id);
  148. this.store.set(id, newObj);
  149. return this;
  150. }
  151. /**
  152. * Helper function similar to applyStore. But it returns the actual object in the store be it ref or not
  153. * applyStore will return the refferenced object if the object in the store is a ref
  154. */
  155. getStoreObject (id: String): StoreObject {
  156. if (!this.store.has(id)) {
  157. if (this.nextStore != null) {
  158. return this.nextStore.getStoreObject(id);
  159. } else {
  160. throw new Error(`Variable ${id} not found.`);
  161. }
  162. }
  163. return this.store.get(id)!;
  164. }
  165. destroy (): void {
  166. this.store.forEach(sto => sto.destroy(), this);
  167. }
  168. }