storeObjectArrayAddress.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { StoreObject } from './storeObject';
  2. import { StoreObjectArray } from './storeObjectArray';
  3. import { CompoundType } from '../../typeSystem/compoundType';
  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. const refColumn = refLine.value[this.column];
  22. return refColumn;
  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 instanceof CompoundType)) {
  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. }