array_store_value.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { IStoreValue } from "./istore_value";
  2. import { ArrayType } from "../../../typeSystem/array_type";
  3. import { IType } from "../../../typeSystem/itype";
  4. import { Type } from "../../../typeSystem/type";
  5. import { StoreValueAddress } from "./store_value_address";
  6. export class ArrayStoreValue implements IStoreValue {
  7. public type: IType;
  8. public id?: String;
  9. public isConst: boolean;
  10. public lines: number;
  11. public columns?: number;
  12. public values: StoreValueAddress[];
  13. constructor(type: ArrayType, values: StoreValueAddress[], lines: number, columns?: number, id?: String, isConst = false) {
  14. this.type = type;
  15. this.id = id;
  16. this.isConst = isConst
  17. this.values = values;
  18. this.lines = lines;
  19. this.columns = columns;
  20. }
  21. get (): StoreValueAddress[] {
  22. return this.values;
  23. }
  24. /**
  25. * @deprecated
  26. * Potential not necessary since array access is occuring directly in the StoreObject
  27. * @param line
  28. * @param column
  29. */
  30. getAt (line: number, column?: number): IStoreValue {
  31. if(this.isVector) {
  32. if(column != null) {
  33. throw new Error(this.id + " is not a matrix!");
  34. }
  35. column = line;
  36. line = 0;
  37. } else if (column == null) {
  38. const values: StoreValueAddress[] = [];
  39. for(let col = 0; col < this.columns!; col += 1) {
  40. const index = line * this.lines + col;
  41. values.push(this.values[index]);
  42. }
  43. const array_type = this.type as ArrayType
  44. const vector_type = new ArrayType(array_type.innerType as Type, array_type.dimensions - 1);
  45. return new ArrayStoreValue(vector_type, values, this.columns!, undefined, this.id, this.isConst);
  46. }
  47. const index = line * this.lines + column;
  48. console.log("Get at: ",index);
  49. return this.values[index];
  50. }
  51. /**
  52. * @deprecated
  53. * Potential not necessary since array access is occuring directly in the StoreObject
  54. * @param line
  55. * @param column
  56. */
  57. setAt (value: IStoreValue, line:number, column?: number): void {
  58. if(!(this.type as ArrayType).canAccept(value.type)) {
  59. throw new Error("!!!Internal Error: Attempting to insert an invalid value inside array "+this.id);
  60. }
  61. if(this.isVector) {
  62. if(column != null) {
  63. throw new Error(this.id + " is not a matrix!");
  64. }
  65. column = line;
  66. line = 0;
  67. } else if (column == null) {
  68. throw new Error("!!!Internal Error: Attempting to insert line into matrix "+ this.id );
  69. }
  70. const pos = line * this.lines + column;
  71. // TODO - fix this casting
  72. this.values[pos] = value as StoreValueAddress;
  73. }
  74. inStore () {
  75. return this.id != null;
  76. }
  77. isVector (): boolean {
  78. return (this.type as ArrayType).dimensions == 1;
  79. }
  80. }