Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

code-path-segment.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * @fileoverview A class of the code path segment.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const debug = require("./debug-helpers");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. /**
  14. * Checks whether or not a given segment is reachable.
  15. * @param {CodePathSegment} segment A segment to check.
  16. * @returns {boolean} `true` if the segment is reachable.
  17. */
  18. function isReachable(segment) {
  19. return segment.reachable;
  20. }
  21. //------------------------------------------------------------------------------
  22. // Public Interface
  23. //------------------------------------------------------------------------------
  24. /**
  25. * A code path segment.
  26. */
  27. class CodePathSegment {
  28. // eslint-disable-next-line jsdoc/require-description
  29. /**
  30. * @param {string} id An identifier.
  31. * @param {CodePathSegment[]} allPrevSegments An array of the previous segments.
  32. * This array includes unreachable segments.
  33. * @param {boolean} reachable A flag which shows this is reachable.
  34. */
  35. constructor(id, allPrevSegments, reachable) {
  36. /**
  37. * The identifier of this code path.
  38. * Rules use it to store additional information of each rule.
  39. * @type {string}
  40. */
  41. this.id = id;
  42. /**
  43. * An array of the next segments.
  44. * @type {CodePathSegment[]}
  45. */
  46. this.nextSegments = [];
  47. /**
  48. * An array of the previous segments.
  49. * @type {CodePathSegment[]}
  50. */
  51. this.prevSegments = allPrevSegments.filter(isReachable);
  52. /**
  53. * An array of the next segments.
  54. * This array includes unreachable segments.
  55. * @type {CodePathSegment[]}
  56. */
  57. this.allNextSegments = [];
  58. /**
  59. * An array of the previous segments.
  60. * This array includes unreachable segments.
  61. * @type {CodePathSegment[]}
  62. */
  63. this.allPrevSegments = allPrevSegments;
  64. /**
  65. * A flag which shows this is reachable.
  66. * @type {boolean}
  67. */
  68. this.reachable = reachable;
  69. // Internal data.
  70. Object.defineProperty(this, "internal", {
  71. value: {
  72. used: false,
  73. loopedPrevSegments: []
  74. }
  75. });
  76. /* istanbul ignore if */
  77. if (debug.enabled) {
  78. this.internal.nodes = [];
  79. }
  80. }
  81. /**
  82. * Checks a given previous segment is coming from the end of a loop.
  83. * @param {CodePathSegment} segment A previous segment to check.
  84. * @returns {boolean} `true` if the segment is coming from the end of a loop.
  85. */
  86. isLoopedPrevSegment(segment) {
  87. return this.internal.loopedPrevSegments.indexOf(segment) !== -1;
  88. }
  89. /**
  90. * Creates the root segment.
  91. * @param {string} id An identifier.
  92. * @returns {CodePathSegment} The created segment.
  93. */
  94. static newRoot(id) {
  95. return new CodePathSegment(id, [], true);
  96. }
  97. /**
  98. * Creates a segment that follows given segments.
  99. * @param {string} id An identifier.
  100. * @param {CodePathSegment[]} allPrevSegments An array of the previous segments.
  101. * @returns {CodePathSegment} The created segment.
  102. */
  103. static newNext(id, allPrevSegments) {
  104. return new CodePathSegment(
  105. id,
  106. CodePathSegment.flattenUnusedSegments(allPrevSegments),
  107. allPrevSegments.some(isReachable)
  108. );
  109. }
  110. /**
  111. * Creates an unreachable segment that follows given segments.
  112. * @param {string} id An identifier.
  113. * @param {CodePathSegment[]} allPrevSegments An array of the previous segments.
  114. * @returns {CodePathSegment} The created segment.
  115. */
  116. static newUnreachable(id, allPrevSegments) {
  117. const segment = new CodePathSegment(id, CodePathSegment.flattenUnusedSegments(allPrevSegments), false);
  118. /*
  119. * In `if (a) return a; foo();` case, the unreachable segment preceded by
  120. * the return statement is not used but must not be remove.
  121. */
  122. CodePathSegment.markUsed(segment);
  123. return segment;
  124. }
  125. /**
  126. * Creates a segment that follows given segments.
  127. * This factory method does not connect with `allPrevSegments`.
  128. * But this inherits `reachable` flag.
  129. * @param {string} id An identifier.
  130. * @param {CodePathSegment[]} allPrevSegments An array of the previous segments.
  131. * @returns {CodePathSegment} The created segment.
  132. */
  133. static newDisconnected(id, allPrevSegments) {
  134. return new CodePathSegment(id, [], allPrevSegments.some(isReachable));
  135. }
  136. /**
  137. * Makes a given segment being used.
  138. *
  139. * And this function registers the segment into the previous segments as a next.
  140. * @param {CodePathSegment} segment A segment to mark.
  141. * @returns {void}
  142. */
  143. static markUsed(segment) {
  144. if (segment.internal.used) {
  145. return;
  146. }
  147. segment.internal.used = true;
  148. let i;
  149. if (segment.reachable) {
  150. for (i = 0; i < segment.allPrevSegments.length; ++i) {
  151. const prevSegment = segment.allPrevSegments[i];
  152. prevSegment.allNextSegments.push(segment);
  153. prevSegment.nextSegments.push(segment);
  154. }
  155. } else {
  156. for (i = 0; i < segment.allPrevSegments.length; ++i) {
  157. segment.allPrevSegments[i].allNextSegments.push(segment);
  158. }
  159. }
  160. }
  161. /**
  162. * Marks a previous segment as looped.
  163. * @param {CodePathSegment} segment A segment.
  164. * @param {CodePathSegment} prevSegment A previous segment to mark.
  165. * @returns {void}
  166. */
  167. static markPrevSegmentAsLooped(segment, prevSegment) {
  168. segment.internal.loopedPrevSegments.push(prevSegment);
  169. }
  170. /**
  171. * Replaces unused segments with the previous segments of each unused segment.
  172. * @param {CodePathSegment[]} segments An array of segments to replace.
  173. * @returns {CodePathSegment[]} The replaced array.
  174. */
  175. static flattenUnusedSegments(segments) {
  176. const done = Object.create(null);
  177. const retv = [];
  178. for (let i = 0; i < segments.length; ++i) {
  179. const segment = segments[i];
  180. // Ignores duplicated.
  181. if (done[segment.id]) {
  182. continue;
  183. }
  184. // Use previous segments if unused.
  185. if (!segment.internal.used) {
  186. for (let j = 0; j < segment.allPrevSegments.length; ++j) {
  187. const prevSegment = segment.allPrevSegments[j];
  188. if (!done[prevSegment.id]) {
  189. done[prevSegment.id] = true;
  190. retv.push(prevSegment);
  191. }
  192. }
  193. } else {
  194. done[segment.id] = true;
  195. retv.push(segment);
  196. }
  197. }
  198. return retv;
  199. }
  200. }
  201. module.exports = CodePathSegment;