arrayLiteral.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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[0].length;
  24. }
  25. }
  26. get isVector () {
  27. return this.columns === null;
  28. }
  29. get isValid () {
  30. return true;//this.validateType() && this.validateSize();
  31. }
  32. validateType () {
  33. // let valid = true;
  34. // if(this.columns !== null) {
  35. // const len = this.columns;
  36. // const len2 = this.lines;
  37. // for (let i = len - 1; i >= 0; i--) {
  38. // for (let j = len2 - 1; j >= 0; j--) {
  39. // if(this.value[i].value[j].type !== this.subtype) {
  40. // valid = false;
  41. // break;
  42. // }
  43. // }
  44. // }
  45. // } else {
  46. // const len = this.lines;
  47. // for (var i = len - 1; i >= 0; i--) {
  48. // if(this.value[i].type !== this.subtype) {
  49. // valid = false;
  50. // break;
  51. // }
  52. // }
  53. // }
  54. return true;//valid;
  55. }
  56. validateSize () {
  57. let valid = true;
  58. if(this.columns !== null) {
  59. const equalityTest = data.value.map(v => v.length)
  60. .reduce((old, next) => {
  61. if (old === null) {
  62. return next;
  63. } else if (old === next) {
  64. return old
  65. } else {
  66. return -1;
  67. }
  68. }, null);
  69. valid = equalityTest !== -1;
  70. }
  71. return valid;
  72. }
  73. toString () {
  74. const strList = this.value.map(arrayLiteral => arrayLiteral.toString());
  75. return "{" + strList.join(',') + "}";
  76. }
  77. }