storeObjectArray.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { StoreObject } from './storeObject';
  2. import { ArrayType } from '../../typeSystem/array_type';
  3. import { ArrayStoreValue } from './value/array_store_value';
  4. import { Location } from "../../memory/location";
  5. import { IStoreValue } from './value/istore_value';
  6. export class StoreObjectArray extends StoreObject {
  7. static get WRONG_LINE_NUMBER () {
  8. return 1;
  9. }
  10. static get WRONG_TYPE () {
  11. return 2;
  12. }
  13. static get WRONG_COLUMN_NUMBER () {
  14. return 3;
  15. }
  16. constructor (type: ArrayType, public lines:number, public columns:number, private addresses:number[], readOnly = false) {
  17. super(type, -1, readOnly);
  18. }
  19. isCompatible (another: IStoreValue): boolean {
  20. if(another instanceof ArrayStoreValue) {
  21. if((this.isVector && another.isVector) ||
  22. (!this.isVector && !another.isVector)) {
  23. return super.isCompatible(another);
  24. }
  25. }
  26. return false;
  27. }
  28. get isVector () {
  29. return (this.type as ArrayType).dimensions === 1;
  30. }
  31. /**@override
  32. * Returns the list of values stored by this array.
  33. * All returned values are compatible with @prop{StoreObject.type}
  34. */
  35. get value () {
  36. const values = [];
  37. for(let i = 0; i < this.addresses.length; i += 1) {
  38. const address = Location.find(this.addresses[i]);
  39. if (address != null) {
  40. values.push(address.value);
  41. } else {
  42. throw new Error("!!!Critical Error: variable "+this.id+" does not have a valid address. Loc-Address "+ this.locAddress);
  43. }
  44. }
  45. return values;
  46. }
  47. destroy () {
  48. let bool = true;
  49. for(let i = 0; i < this.addresses.length; i += 1) {
  50. bool = bool && Location.deallocate(this.addresses[i]);
  51. }
  52. return bool;
  53. }
  54. get locAddress (): number {
  55. throw new Error("!!!Internal Error: Cannot access locAddress property of StoreObjectArray");
  56. }
  57. getAt (line: number, column?: number): any {
  58. if(this.isVector) {
  59. if(column != null) {
  60. throw new Error(this.id + " is not a matrix!");
  61. }
  62. column = line;
  63. line = 0;
  64. } else if (column == null) {
  65. // this is a whole line...
  66. const values = [];
  67. for(let col = 0; col < this.columns; col += 1) {
  68. const index = this.getIndexOf(line, col);
  69. const address = Location.find(this.addresses[index])!;
  70. values.push(address.value);
  71. }
  72. return values;
  73. }
  74. const index = this.getIndexOf(line, column);
  75. const address = Location.find(this.addresses[index])!;
  76. return address.value;
  77. }
  78. setAt (value: any, line:number, column?: number): void {
  79. if(!(this.type as ArrayType).canAccept(value.type)) {
  80. throw new Error("!!!Internal Error: Attempting to insert an invalid value inside array "+this.id);
  81. }
  82. if(this.isVector) {
  83. if(column != null) {
  84. throw new Error(this.id + " is not a matrix!");
  85. }
  86. column = line;
  87. line = 0;
  88. } else if (column == null) {
  89. throw new Error("!!!Internal Error: Attempting to insert a line into matrix "+ this.id );
  90. }
  91. const pos = this.getIndexOf(line, column);
  92. Location.updateAddress(this.addresses[pos], value);
  93. }
  94. private getIndexOf (line: number, column: number): number {
  95. return line * this.lines + column;
  96. }
  97. getLocAddressOf (line: number, column?: number): number | number[] {
  98. if(this.isVector) {
  99. if(column != null) {
  100. throw new Error(this.id + " is not a matrix!");
  101. }
  102. column = line;
  103. line = 0;
  104. } else if (column == null) {
  105. // this is a whole line...
  106. const values: number[] = [];
  107. for(let col = 0; col < this.columns; col += 1) {
  108. const index = this.getIndexOf(line, col);
  109. values.push(this.addresses[index]);
  110. }
  111. return values;
  112. }
  113. const index = this.getIndexOf(line, column);
  114. return this.addresses[index];
  115. }
  116. }