storeObjectArrayAddress.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { StoreObject } from './storeObject';
  2. import { StoreObjectArray } from './storeObjectArray';
  3. import { Types } from '../../typeSystem/types';
  4. import { CompoundType } from '../../typeSystem/compoundType';
  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 (this.column !== null) {
  22. return refLine.value[this.column];
  23. }
  24. return refLine;
  25. }
  26. get value () {
  27. return this.refValue.value;
  28. }
  29. get type () {
  30. return this.refValue.type;
  31. }
  32. get lines () {
  33. if(this.type !== Types.ARRAY) {
  34. return null;
  35. }
  36. return this.refValue.value.length;
  37. }
  38. get columns () {
  39. switch (this.type.dimensions) {
  40. case 2:
  41. return this.refValue.value[0].value.length;
  42. default:
  43. return null;
  44. }
  45. }
  46. getArrayObject () {
  47. return this.store.applyStore(this.refID);
  48. }
  49. updateArrayObject (stoObj) {
  50. const anArray = this.getArrayObject();
  51. const newArray = Object.assign(new StoreObjectArray(null,null,null), anArray);
  52. if(!stoObj.type.isCompatible(this.type)) {
  53. throw new Error(`Invalid operation: cannot assign the value given to ${this.refID}`);
  54. } else if (this.type instanceof CompoundType && this.type.canAccept(stoObj.type)) {
  55. throw new Error(`Invalid operation: cannot assign the value given to ${this.refID}`);
  56. }
  57. if (this.column !== null) {
  58. newArray.value[this.line].value[this.column] = stoObj;
  59. return newArray;
  60. } else {
  61. newArray.value[this.line] = stoObj;
  62. return newArray;
  63. }
  64. }
  65. isCompatible (another) {
  66. if(this.type.isCompatible(another.type)) {
  67. if(another.type instanceof CompoundType) {
  68. return this.lines === another.lines && this.columns === another.columns;
  69. } else {
  70. this.refValue.isCompatible(another);
  71. }
  72. }
  73. }
  74. }