storeObjectArrayAddress.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { StoreObject } from './storeObject';
  2. import { StoreObjectArray } from './storeObjectArray';
  3. import { ArrayType } from '../../typeSystem/array_type';
  4. import { ProcessorErrorFactory } from '../error/processorErrorFactory';
  5. export class StoreObjectArrayAddress extends StoreObject {
  6. constructor (refID, line, column, store) {
  7. super(null, null, false);
  8. this.refID = refID;
  9. this.store = store;
  10. this.line = line;
  11. this.column = column;
  12. }
  13. get isRef () {
  14. return false;
  15. }
  16. get inStore () {
  17. return true;
  18. }
  19. get refValue () {
  20. const refLine = this.store.applyStore(this.refID).value[this.line];
  21. if(!refLine) {
  22. if(this.getArrayObject().isVector) {
  23. throw ProcessorErrorFactory.vector_line_outbounds(this.refID, this.line, this.getArrayObject().lines);
  24. } else {
  25. throw ProcessorErrorFactory.matrix_line_outbounds(this.refID, this.line, this.getArrayObject().lines);
  26. }
  27. }
  28. if (this.column !== null) {
  29. const refColumn = refLine.value[this.column];
  30. if(!refColumn) {
  31. if(this.getArrayObject().isVector) {
  32. throw ProcessorErrorFactory.vector_not_matrix(this.refID);
  33. } else {
  34. throw ProcessorErrorFactory.matrix_column_outbounds(this.refID, this.column, this.getArrayObject().columns);
  35. }
  36. }
  37. return refColumn;
  38. }
  39. return refLine;
  40. }
  41. get value () {
  42. return this.refValue.value;
  43. }
  44. get type () {
  45. return this.refValue.type;
  46. }
  47. get lines () {
  48. if(!(this.type instanceof ArrayType)) {
  49. return null;
  50. }
  51. return this.refValue.value.length;
  52. }
  53. get columns () {
  54. switch (this.type.dimensions) {
  55. case 2:
  56. return this.refValue.value[0].value.length;
  57. default:
  58. return null;
  59. }
  60. }
  61. getArrayObject () {
  62. return this.store.applyStore(this.refID);
  63. }
  64. updateArrayObject (stoObj) {
  65. const anArray = this.getArrayObject();
  66. const newArray = Object.assign(new StoreObjectArray(null,null,null), anArray);
  67. if(!stoObj.type.isCompatible(this.type)) {
  68. throw new Error(`Invalid operation: cannot assign the value given to ${this.refID}`);
  69. } else if (this.type instanceof ArrayType && this.type.canAccept(stoObj.type)) {
  70. throw new Error(`Invalid operation: cannot assign the value given to ${this.refID}`);
  71. }
  72. if (this.column !== null) {
  73. newArray.value[this.line].value[this.column] = stoObj;
  74. return newArray;
  75. } else {
  76. newArray.value[this.line] = stoObj;
  77. return newArray;
  78. }
  79. }
  80. isCompatible (another) {
  81. if(this.type.isCompatible(another.type)) {
  82. if(another.type instanceof ArrayType) {
  83. return this.lines === another.lines && this.columns === another.columns;
  84. } else {
  85. this.refValue.isCompatible(another);
  86. }
  87. }
  88. }
  89. }