storeObjectArrayAddress.js 2.3 KB

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