# All files / src/generator loop.js

100% Statements 17/17
100% Branches 0/0
100% Functions 1/1
100% Lines 17/17

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    22x
    22x
    22x

    22x
    4x
    4x

    4x

    4x
    4x
    4x

    4x
    4x

    4x

    4x

    4x
    4x

    4x

     // @flow
    import mapSyntax from "./map-syntax";
    import mergeBlock from "./merge-block";
    import opcode from "../emitter/opcode";
    import type { GeneratorType } from "./flow/types";

    const generateLoop: GeneratorType = (node, parent) => {
      const block = [];
      const mapper = mapSyntax(parent);

      // First param in a for loop is assignment expression or Noop if it's a while loop
      const [initializer, condition, ...body] = node.params;

      block.push.apply(block, [initializer].map(mapper).reduce(mergeBlock, []));
      block.push({ kind: opcode.Block, params: [0x40] });
      block.push({ kind: opcode.Loop, params: [0x40] });

      block.push.apply(block, [condition].map(mapper).reduce(mergeBlock, []));
      block.push({ kind: opcode.BrIf, params: [1] });

      block.push.apply(block, body.map(mapper).reduce(mergeBlock, []));

      block.push({ kind: opcode.Br, params: [0] });

      block.push({ kind: opcode.End, params: [] });
      block.push({ kind: opcode.End, params: [] });

      return block;
    };

    export default generateLoop;