store.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  2. import { Modes } from '../modes';
  3. import { Types } from "../../typeSystem/types";
  4. import { StoreObject } from './storeObject';
  5. import { IType } from '../../typeSystem/itype';
  6. import { StoreObjectRef } from './storeObjectRef';
  7. import { ArrayType } from '../../typeSystem/array_type';
  8. import { ArrayStoreValue } from './value/array_store_value';
  9. import { Location } from '../../memory/location';
  10. import { StoreObjectArray } from './storeObjectArray';
  11. import { IStoreValue } from './value/istore_value';
  12. import { StoreValue } from './value/store_value';
  13. import { StoreValueAddress } from './value/store_value_address';
  14. import { StoreValueRef } from './value/store_value_ref';
  15. import { ArrayStoreValueRef } from './value/array_store_value_ref';
  16. import { StoreObjectArrayRef } from './store_object_array_ref';
  17. export class Store {
  18. static canImplicitTypeCast (castType: IType, sourceType: IType): boolean {
  19. if (castType.isCompatible(Types.INTEGER) || castType.isCompatible(Types.REAL)) {
  20. if (sourceType.isCompatible(Types.INTEGER) || sourceType.isCompatible(Types.REAL)) {
  21. return true;
  22. }
  23. }
  24. return false;
  25. }
  26. static doImplicitCasting (castType: IType, stoObj: IStoreValue): IStoreValue {
  27. if(!Store.canImplicitTypeCast(castType, stoObj.type)) {
  28. throw new Error("!!!Critical error: attempted to type cast invalid types");
  29. }
  30. if(castType.isCompatible(Types.INTEGER)) {
  31. return new StoreValue(Types.INTEGER, stoObj.get().trunc());
  32. } else {
  33. return new StoreValue(Types.REAL, stoObj.get());
  34. }
  35. }
  36. private store: Map<string, StoreObject>;
  37. public nextStore?: Store
  38. public mode: symbol;
  39. constructor(public name: string) {
  40. this.store = new Map<string, StoreObject>();
  41. this.mode = Modes.RUN;
  42. }
  43. extendStore (nextStore: Store): void {
  44. this.nextStore = nextStore;
  45. }
  46. applyStore (id: string): IStoreValue {
  47. if (!this.store.has(id)) {
  48. if (this.nextStore != null) {
  49. return this.nextStore.applyStore(id);
  50. } else {
  51. throw new Error(`Variable ${id} not found.`);
  52. }
  53. }
  54. const val = this.store.get(id)!;
  55. let result = null
  56. if (val.type instanceof ArrayType) {
  57. const array = val as StoreObjectArray;
  58. const array_type = array.type as ArrayType;
  59. let l = 0, c = 0;
  60. const values = array.value.map( v => {
  61. if(array.isVector) {
  62. return new StoreValueAddress(array_type.innerType, v, l++, undefined, array.id, array.readOnly);
  63. } else {
  64. if(c >= array.columns) {
  65. c = 0;
  66. l += 1;
  67. }
  68. return new StoreValueAddress(array_type.innerType, v, l, c++, array.id, array.readOnly);
  69. }
  70. });
  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. oldObj.setAt(val, index, undefined);
  98. });
  99. } else {
  100. let line = 0;
  101. let column = 0;
  102. array_value.get().forEach((val) => {
  103. oldObj.setAt(val, line, column);
  104. column += 1;
  105. if(column >= oldObj.columns) {
  106. line += 1;
  107. column = 0;
  108. }
  109. });
  110. }
  111. return this;
  112. }
  113. } else if (oldObj.isCompatible(stoValue)) {
  114. const loc_address = oldObj.locAddress;
  115. Location.updateAddress(loc_address, stoValue.get());
  116. return this;
  117. }
  118. const oldType = oldObj.type;
  119. const stoType = stoValue.type;
  120. // TODO: better error message
  121. throw new Error(`${oldType.value} is not compatible with type ${stoType.value} given`);
  122. }
  123. }
  124. /**
  125. * Method used to update regions of an array (vector or matrix). The should only be used when update an specific
  126. * possition since it will only update the required addresses.
  127. * @param {string} id the variable id to be updated
  128. * @param {IStoreValue} sto_value the value to be used in the update process
  129. * @param {number} line the line address of the vector/matrix
  130. * @param {number} column the matrix column, which can be undefined
  131. */
  132. updateStoreArray (id: string, sto_value: IStoreValue, line: number, column?: number): Store {
  133. if (!this.store.has(id)) {
  134. if (this.nextStore != null) {
  135. this.nextStore.updateStoreArray(id, sto_value, line, column);
  136. return this;
  137. } else {
  138. // TODO: better error message
  139. throw new Error(`Variable ${id} not found.`);
  140. }
  141. } else {
  142. const oldObj = this.store.get(id)!;
  143. if (oldObj.readOnly) {
  144. // TODO: better error message
  145. throw new Error("Cannot change value of a read only variable: " + id);
  146. }
  147. if (oldObj instanceof StoreObjectArray) {
  148. if(sto_value instanceof ArrayStoreValue) {
  149. // this must be a vector or matrix line update
  150. const actual_values = sto_value.get();
  151. if(oldObj.isVector && sto_value.isVector()) {
  152. for(let i = 0;i < sto_value.lines; i += 1) {
  153. const val = actual_values[i]
  154. oldObj.setAt(val, i, undefined);
  155. }
  156. } else if(!oldObj.isVector && column == null && sto_value.isVector()) {
  157. for(let i = 0;i < oldObj.columns; i += 1) {
  158. const val = actual_values[i]
  159. oldObj.setAt(val, line, i);
  160. }
  161. } else {
  162. // TODO: better error message
  163. throw new Error(`Attempting to assign an invalid value to array ${id}`);
  164. }
  165. } else {
  166. if(!oldObj.isVector && column == null) {
  167. // TODO: better error message
  168. throw new Error(`Attempting to assign an invalid value to array ${id}`);
  169. }
  170. oldObj.setAt(sto_value as StoreValue, line, column);
  171. }
  172. } else {
  173. throw new Error("Cannot update a non-array variable using updateStoreArray");
  174. }
  175. // const oldType = oldObj.type;
  176. // const stoType = sto_value.type;
  177. // // TODO: better error message
  178. // throw new Error(`${oldType.value} is not compatible with type ${stoType.value} given`);
  179. return this;
  180. }
  181. }
  182. /**
  183. * Inserts a new variable into the Store. This method should be used when declaring a new variable,
  184. * including the special return variable $.
  185. * @param id variable id
  186. * @param stoValue the value to be used as the initial value of id
  187. */
  188. insertStore (id: string, stoValue: IStoreValue): Store {
  189. if (this.store.has(id)) {
  190. // TODO: better error message
  191. throw new Error(`${id} is already defined`);
  192. }
  193. // TODO check for array....
  194. let newObj: StoreObject;
  195. if(stoValue instanceof StoreValueRef) {
  196. newObj = new StoreObjectRef(stoValue);
  197. } else if (stoValue instanceof ArrayStoreValueRef) {
  198. newObj = new StoreObjectArrayRef(stoValue, stoValue.lines, stoValue.columns);
  199. } else if (stoValue instanceof ArrayStoreValue) {
  200. const columns = stoValue.isVector() ? 0 : stoValue.columns!;
  201. const addresses: number[] = [];
  202. const all_values = stoValue.get();
  203. if(all_values.length > 0) {
  204. for(let i = 0; i < stoValue.get().length; i += 1) {
  205. const val = all_values[i].get();
  206. addresses.push(Location.allocate(val));
  207. }
  208. } else {
  209. let total = stoValue.lines;
  210. total = stoValue.isVector() ? total : total * columns;
  211. for(let i = 0; i < total; i += 1) {
  212. addresses.push(Location.allocate(null));
  213. }
  214. }
  215. newObj = new StoreObjectArray(stoValue.type as ArrayType, stoValue.lines, columns, addresses, stoValue.isConst);
  216. } else {
  217. const loc_address = Location.allocate(stoValue.get());
  218. newObj = new StoreObject(stoValue.type, loc_address, stoValue.isConst);
  219. }
  220. newObj.setID(id);
  221. this.store.set(id, newObj);
  222. return this;
  223. }
  224. /**
  225. * Helper function similar to applyStore. But it returns the actual object in the store be it ref or not
  226. * applyStore will return the refferenced object if the object in the store is a ref
  227. */
  228. getStoreObject (id: string): StoreObject {
  229. if (!this.store.has(id)) {
  230. if (this.nextStore != null) {
  231. return this.nextStore.getStoreObject(id);
  232. } else {
  233. throw new Error(`Variable ${id} not found.`);
  234. }
  235. }
  236. return this.store.get(id)!;
  237. }
  238. destroy (): void {
  239. this.store.forEach(sto => sto.destroy(), this);
  240. }
  241. isDefined (id: string): boolean {
  242. if (!this.store.has(id)) {
  243. if (this.nextStore != null) {
  244. return this.nextStore.isDefined(id);
  245. } else {
  246. return false;
  247. }
  248. }
  249. return true;
  250. }
  251. }