arrayLiteral.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { Literal } from './literal';
  2. export class ArrayLiteral extends Literal {
  3. constructor(type, value) {
  4. super(type);
  5. this.value = value;
  6. }
  7. get subtype () {
  8. let element = this.value[0];
  9. if (element instanceof ArrayLiteral) {
  10. return element.value[0].type;
  11. } else {
  12. return element.type;
  13. }
  14. }
  15. get lines () {
  16. return this.value.length;
  17. }
  18. get columns () {
  19. let element = this.value[0];
  20. if (!(element instanceof ArrayLiteral)){
  21. return null;
  22. } else {
  23. return element.value.length;
  24. }
  25. }
  26. get isVector () {
  27. return this.columns === null;
  28. }
  29. get isValid () {
  30. return this.validateSize() && this.validateType();
  31. }
  32. validateType () {
  33. let valid = true;
  34. // const subtype = this.subtype;
  35. // if(this.columns !== null) {
  36. // const len = this.lines;
  37. // const len2 = this.columns;
  38. // for (let i = len - 1; i >= 0 && valid; --i) {
  39. // for (let j = len2 - 1; j >= 0 && valid; --j) {
  40. // if(!this.value[i].value[j].type.isCompatilbe(subtype)) {
  41. // valid = false;
  42. // break;
  43. // }
  44. // }
  45. // }
  46. // } else {
  47. // const len = this.lines;
  48. // for (var i = len - 1; i >= 0 && valid; i--) {
  49. // if(!this.value[i].type.isCompatilbe(subtype)) {
  50. // valid = false;
  51. // break;
  52. // }
  53. // }
  54. // }
  55. // Cannot validate type since no information on variables literals are available
  56. return valid;
  57. }
  58. validateSize () {
  59. let valid = true;
  60. // if(this.columns !== null) {
  61. // const equalityTest = this.value.map(v => v.value.length)
  62. // .reduce((old, next) => {
  63. // console.log(next);
  64. // if (old == null) {
  65. // return next;
  66. // } else if (old === next) {
  67. // return old
  68. // } else {
  69. // return -1;
  70. // }
  71. // }, null);
  72. // valid = equalityTest !== -1;
  73. // }
  74. // check is now made inside IVProgParser.parseVectorList(...)
  75. return valid;
  76. }
  77. toString () {
  78. const strList = this.value.map(arrayLiteral => arrayLiteral.toString());
  79. return "{" + strList.join(',') + "}";
  80. }
  81. }