# All files / src/semantics/map-function map-identifier.js

95.24% Statements 20/21
91.67% Branches 11/12
100% Functions 2/2
95.24% Lines 20/21

    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
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    21x
    21x
    21x

    21x

    386x
    386x
    386x
    386x
    307x
    307x
    307x

    307x

    79x
    51x

    28x

    28x
    7x
    6x

    7x

    21x

     // @flow
    import Syntax from "../../Syntax";
    import curry from "curry";
    import { FUNCTION_INDEX, TYPE_ARRAY } from "../metadata";

    const mapIdentifier = curry(
      ({ locals, globals, functions, table, userTypes }, identifier) => {
        // Not a function call or pointer, look-up variables
        const local = locals[identifier.value];
        const global = globals[identifier.value];
        debugger;
        if (local != null) {
          const type = (() => {
            const isArray = local.meta[TYPE_ARRAY];
            return isArray || local.type;
          })();
          return {
            ...identifier,
            type,
            meta: { ...local.meta },
          };
        } else if (global != null) {
          return {
            ...identifier,
            type: globals[identifier.value].type,
            meta: { ...global.meta },
          };
        } else Iif (userTypes[identifier.value] != null) {
          return {
            ...identifier,
            type: "i32",
            Type: Syntax.UserType,
          };
        } else if (functions[identifier.value] != null) {
          if (table[identifier.value] == null) {
            table[identifier.value] = functions[identifier.value];
          }
          return {
            ...identifier,
            type: "i32",
            meta: {
              [FUNCTION_INDEX]: Object.keys(functions).indexOf(identifier.value),
            },
            value: Object.keys(table).indexOf(identifier.value),
            Type: Syntax.FunctionPointer,
          };
        }

        return identifier;
      }
    );

    export default mapIdentifier;