store.ts 9.1 KB

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