store.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. console.log("applyStore", values);
  71. result = new ArrayStoreValue(array_type, values, array.lines, array.columns, val.id, val.readOnly);
  72. } else {
  73. result = new StoreValue(val.type, val.value, val.id, val.readOnly);
  74. }
  75. return result;
  76. }
  77. updateStore (id: string, stoValue: IStoreValue): Store {
  78. if (!this.store.has(id)) {
  79. if (this.nextStore != null) {
  80. this.nextStore.updateStore(id, stoValue);
  81. return this;
  82. } else {
  83. // TODO: better error message
  84. throw new Error(`Variable ${id} not found.`);
  85. }
  86. } else {
  87. const oldObj = this.store.get(id)!;
  88. if (oldObj.readOnly) {
  89. // TODO: better error message
  90. throw new Error("Cannot change value of a read only variable: " + id);
  91. }
  92. if (oldObj instanceof StoreObjectArray) {
  93. const array_value = stoValue as ArrayStoreValue;
  94. if(oldObj.isCompatible(array_value)) {
  95. if(oldObj.isVector) {
  96. array_value.get().forEach((val, index) => {
  97. console.log(val);
  98. oldObj.setAt(val, index, undefined);
  99. });
  100. } else {
  101. let line = 0;
  102. let column = 0;
  103. array_value.get().forEach((val) => {
  104. oldObj.setAt(val, line, column);
  105. column += 1;
  106. if(column >= oldObj.columns) {
  107. line += 1;
  108. column = 0;
  109. }
  110. });
  111. }
  112. return this;
  113. }
  114. } else if (oldObj.isCompatible(stoValue)) {
  115. const loc_address = oldObj.locAddress;
  116. Location.updateAddress(loc_address, stoValue.get());
  117. return this;
  118. }
  119. const oldType = oldObj.type;
  120. const stoType = stoValue.type;
  121. // TODO: better error message
  122. throw new Error(`${oldType.value} is not compatible with type ${stoType.value} given`);
  123. }
  124. }
  125. /**
  126. * Method used to update regions of an array (vector or matrix). The should only be used when update an specific
  127. * possition since it will only update the required addresses.
  128. * @param {string} id the variable id to be updated
  129. * @param {IStoreValue} sto_value the value to be used in the update process
  130. * @param {number} line the line address of the vector/matrix
  131. * @param {number} column the matrix column, which can be undefined
  132. */
  133. updateStoreArray (id:string, sto_value: IStoreValue, line:number, column?:number): Store {
  134. if (!this.store.has(id)) {
  135. if (this.nextStore != null) {
  136. this.nextStore.updateStoreArray(id, sto_value, line, column);
  137. return this;
  138. } else {
  139. // TODO: better error message
  140. throw new Error(`Variable ${id} not found.`);
  141. }
  142. } else {
  143. const oldObj = this.store.get(id)!;
  144. if (oldObj.readOnly) {
  145. // TODO: better error message
  146. throw new Error("Cannot change value of a read only variable: " + id);
  147. }
  148. if (oldObj instanceof StoreObjectArray) {
  149. if(sto_value instanceof ArrayStoreValue) {
  150. // this must be a vector or matrix line update
  151. const actual_values = sto_value.get();
  152. if(oldObj.isVector && sto_value.isVector()) {
  153. for(let i = 0;i < sto_value.lines; i += 1) {
  154. const val = actual_values[i]
  155. oldObj.setAt(val, i, undefined);
  156. }
  157. } else if(!oldObj.isVector && column == null && sto_value.isVector()) {
  158. for(let i = 0;i < oldObj.columns; i += 1) {
  159. const val = actual_values[i]
  160. oldObj.setAt(val, line, i);
  161. }
  162. } else {
  163. // TODO: better error message
  164. throw new Error(`Attempting to assign an invalid value to array ${id}`);
  165. }
  166. } else {
  167. if(!oldObj.isVector && column == null) {
  168. // TODO: better error message
  169. throw new Error(`Attempting to assign an invalid value to array ${id}`);
  170. }
  171. oldObj.setAt(sto_value as StoreValue, line, column);
  172. }
  173. } else {
  174. throw new Error("Cannot update a non-array variable using updateStoreArray");
  175. }
  176. // const oldType = oldObj.type;
  177. // const stoType = sto_value.type;
  178. // // TODO: better error message
  179. // throw new Error(`${oldType.value} is not compatible with type ${stoType.value} given`);
  180. return this;
  181. }
  182. }
  183. //In case of future use of ref, it needs to have a special function to update the storeRefObject
  184. // and no the StoreObject refferenced by it
  185. // updateStoreRef(id, stoObjAddress) {...}
  186. insertStore (id: String, stoValue: IStoreValue) {
  187. if (this.store.has(id)) {
  188. // TODO: better error message
  189. throw new Error(`${id} is already defined`);
  190. }
  191. // TODO check for array....
  192. let newObj:StoreObject;
  193. if(stoValue instanceof StoreValueRef) {
  194. newObj = new StoreObjectRef(stoValue);
  195. } else if (stoValue instanceof ArrayStoreValueRef) {
  196. newObj = new StoreObjectArrayRef(stoValue,0,0,false);
  197. } else if (stoValue instanceof ArrayStoreValue) {
  198. const columns = stoValue.isVector() ? 0 : stoValue.columns!;
  199. const line_address = Location.allocate(stoValue.lines);
  200. const column_address = Location.allocate(columns);
  201. const values_address = Location.allocate([]);
  202. const addresses: number[] = [];
  203. const all_values = stoValue.get();
  204. if(all_values.length > 0) {
  205. for(let i = 0; i < stoValue.get().length; i += 1) {
  206. const val = all_values[i].get();
  207. addresses.push(Location.allocate(val));
  208. }
  209. } else {
  210. let total = stoValue.lines;
  211. total = stoValue.isVector() ? total : total * columns;
  212. for(let i = 0; i < total; i += 1) {
  213. addresses.push(Location.allocate(null));
  214. }
  215. }
  216. Location.updateAddress(values_address, addresses);
  217. newObj = new StoreObjectArray(stoValue.type as ArrayType, line_address, column_address, values_address, stoValue.isConst);
  218. } else {
  219. const loc_address = Location.allocate(stoValue.get());
  220. newObj = new StoreObject(stoValue.type, loc_address, stoValue.isConst);
  221. }
  222. newObj.setID(id);
  223. this.store.set(id, newObj);
  224. return this;
  225. }
  226. /**
  227. * Helper function similar to applyStore. But it returns the actual object in the store be it ref or not
  228. * applyStore will return the refferenced object if the object in the store is a ref
  229. */
  230. getStoreObject (id: String): StoreObject {
  231. if (!this.store.has(id)) {
  232. if (this.nextStore != null) {
  233. return this.nextStore.getStoreObject(id);
  234. } else {
  235. throw new Error(`Variable ${id} not found.`);
  236. }
  237. }
  238. return this.store.get(id)!;
  239. }
  240. destroy (): void {
  241. this.store.forEach(sto => sto.destroy(), this);
  242. }
  243. }