You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
3.5 KiB

  1. const Readable = require("stream").Readable;
  2. module.exports = function buildSyms(ctx) {
  3. const rs = Readable();
  4. let it = new ComponentIt(ctx, 0, "main");
  5. let counter = 0;
  6. rs._read = function() {
  7. const actual = it.current();
  8. if (actual == null ) {
  9. rs.push(null);
  10. return;
  11. }
  12. let s=actual.offset;
  13. while (ctx.signals[s].e >= 0) s = ctx.signals[s].e;
  14. let wId = ctx.signals[s].id;
  15. if (typeof(wId) == "undefined") wId=-1;
  16. rs.push(`${actual.offset},${wId},${actual.cIdx},${actual.name}\n`);
  17. it.next();
  18. counter ++;
  19. if ((ctx.verbose)&&(counter%10000 == 0)) console.log("Symbols saved: "+counter);
  20. };
  21. return rs;
  22. };
  23. class SignalIt {
  24. constructor (ctx, offset, prefix, cIdx) {
  25. this.ctx = ctx;
  26. this.offset = offset;
  27. this.prefix = prefix;
  28. this.cur = 0;
  29. this.cIdx = cIdx;
  30. }
  31. next() {
  32. this.cur = 1;
  33. return this.current();
  34. }
  35. current() {
  36. if (this.cur == 0) {
  37. return {offset: this.offset, name: this.prefix, cIdx: this.cIdx};
  38. }
  39. }
  40. }
  41. class ArrayIt {
  42. constructor (ctx, type, sizes, offset, prefix, cIdx) {
  43. if (sizes.length == 0) {
  44. if (type == "S") {
  45. return new SignalIt(ctx, offset, prefix, cIdx);
  46. } else {
  47. return new ComponentIt(ctx, offset, prefix);
  48. }
  49. }
  50. this.ctx = ctx;
  51. this.type = type;
  52. this.sizes = sizes;
  53. this.offset = offset;
  54. this.prefix = prefix;
  55. this.cIdx = cIdx;
  56. this.subIt = null;
  57. this.cur = 0;
  58. this.subArrSize = 1;
  59. for (let i=1; i<sizes.length; i++) {
  60. this.subArrSize *= sizes[i];
  61. }
  62. this._loadSubIt();
  63. }
  64. _loadSubIt() {
  65. if (this.cur < this.sizes[0]) {
  66. this.subIt = new ArrayIt(this.ctx, this.type, this.sizes.slice(1), this.offset + this.cur*this.subArrSize, this.prefix + "[" + this.cur + "]", this.cIdx);
  67. }
  68. }
  69. next() {
  70. if (this.subIt) {
  71. const res = this.subIt.next();
  72. if (res == null) {
  73. this.subIt = null;
  74. this.cur++;
  75. this._loadSubIt();
  76. }
  77. }
  78. return this.current();
  79. }
  80. current() {
  81. if (this.subIt) {
  82. return this.subIt.current();
  83. } else {
  84. return null;
  85. }
  86. }
  87. }
  88. class ComponentIt {
  89. constructor (ctx, idxComponent, prefix) {
  90. this.ctx = ctx;
  91. this.idxComponent = idxComponent;
  92. this.prefix = prefix;
  93. this.names = Object.keys(ctx.components[idxComponent].names.o);
  94. this.subIt = null;
  95. this.cur = 0;
  96. this._loadSubIt();
  97. }
  98. _loadSubIt() {
  99. if (this.cur < this.names.length) {
  100. const entrie = this.ctx.components[this.idxComponent].names.o[this.names[this.cur]];
  101. this.subIt = new ArrayIt(this.ctx, entrie.type, entrie.sizes, entrie.offset, this.prefix + "." + this.names[this.cur], this.idxComponent);
  102. }
  103. }
  104. next() {
  105. if (this.subIt) {
  106. const res = this.subIt.next();
  107. if (res == null) {
  108. this.subIt = null;
  109. this.cur++;
  110. this._loadSubIt();
  111. }
  112. }
  113. return this.current();
  114. }
  115. current() {
  116. if (this.subIt) {
  117. return this.subIt.current();
  118. } else {
  119. return null;
  120. }
  121. }
  122. }