# All files / src/generator declaration.js

100% Statements 12/12
80% Branches 4/5
100% Functions 1/1
100% Lines 12/12

    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
    33
    34
    24x
    24x
    24x
    24x
    24x

    24x

    115x

    115x
    110x

    110x

    110x

    5x

     // @flow
    import { i32 } from "../Syntax";
    import generateExpression from "./expression";
    import { isBuiltinType } from "./utils";
    import opcode from "../emitter/opcode";
    import { LOCAL_INDEX } from "../semantics/metadata";
    import type { GeneratorType } from "./flow/types";

    const generateDeclaration: GeneratorType = (
      node,
      parent = { code: [], locals: [] }
    ) => {
      const initNode = node.params[0];

      if (initNode) {
        const metaIndex = node.meta[LOCAL_INDEX];

        const type = isBuiltinType(node.type) ? node.type : i32;

        return [
          ...generateExpression({ ...initNode, type }, parent),
          {
            kind: opcode.SetLocal,
            params: [metaIndex],
            debug: `${node.value}<${String(node.type)}>`,
          },
        ];
      }

      return [];
    };

    export default generateDeclaration;