# All files / src/core index.js

100% Statements 61/61
92% Branches 23/25
100% Functions 21/21
100% Lines 58/58

    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
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167

    23x
    23x
    23x

    23x

    23x

    311x

    622x
    234x

    388x

    311x
    622x

    55x

    567x

    311x

    87x

    174x
    245x
    245x
    245x
    245x
    245x

    245x
    219x

    26x

    245x

    245x

    87x

    87x
    12x

    24x

    87x
    299x

    598x

    87x
    53x

    53x
    1x

    104x
    52x
    52x
    52x

    52x
    50x

    2x

    87x
    571x

    571x
    571x

    571x
    149x

    571x
    513x

    58x

    87x
    49x
    98x
    49x
    49x

     /**
     * Core language plugin
     *
     * The parsers in here very closly mirror the underlying WebAssembly structure
     * and are used as the core language for every feature built on top.
     */
    import Syntax from '../Syntax';
    import { extendNode } from '../utils/extend-node';
    import {
      TYPE_CAST,
      TYPE_CONST,
      GLOBAL_INDEX,
      LOCAL_INDEX,
    } from '../semantics/metadata';
    import { typeWeight } from '../types';

    const balanceTypesInMathExpression = expression => {
      // find the heaviest type in the expression
      const type = expression.params.reduce((acc, { type: childType }) => {
        // The way we do that is by scanning the top-level nodes in our expression
        if (typeWeight(acc) < typeWeight(childType)) {
          return childType;
        }

        return acc;
      }, expression.type);

      // iterate again, this time, patching any lighter types
      const params = expression.params.map(paramNode => {
        if (
          paramNode.type != null &&
          typeWeight(paramNode.type) !== typeWeight(type)
        ) {
          return {
            ...paramNode,
            type,
            value: paramNode.value,
            Type: Syntax.TypeCast,
            meta: {
              ...paramNode.meta,
              [TYPE_CAST]: { to: type, from: paramNode.type },
            },
            params: [paramNode],
          };
        }

        return paramNode;
      });

      return {
        ...expression,
        params,
        type,
      };
    };

    // Core plugin
    export default function Core() {
      return {
        semantics() {
          // Parse declaration node
          const declaration = next => ([node, context]) => {
            const scope = context.locals || context.globals;
            const indexMeta = (() => {
              let index = Object.keys(scope).indexOf(node.value);
              Eif (index === -1) {
                index = Object.keys(scope).length;
              }
              if (scope === context.locals) {
                return { [LOCAL_INDEX]: index };
              }

              return { [GLOBAL_INDEX]: index };
            })();

            scope[node.value] = extendNode(
              {
                params: node.params.map(extendNode({ type: node.type })),
                meta: {
                  ...node.meta,
                  ...indexMeta,
                  [TYPE_CONST]: node.Type === Syntax.ImmutableDeclaration,
                },
                Type: Syntax.Declaration,
              },
              node
            );

            return next([scope[node.value], context]);
          };

          return {
            Declaration: declaration,
            ImmutableDeclaration: declaration,
            // CharacterLiteral: next => ([node]) => next([mapCharacterLiteral(node)]),
            Select: _ => ([node, context], transform) =>
              balanceTypesInMathExpression({
                ...node,
                params: node.params.map(child => transform([child, context])),
              }),
            BinaryExpression: _ => ([node, context], transform) =>
              balanceTypesInMathExpression({
                ...node,
                params: node.params.map(child => transform([child, context])),
              }),
            Pair: next => (args, transform) => {
              const [typeCastMaybe, context] = args;
              // Ignore everything in global scope (promotions only possible in functions
              if (!context.locals) {
                return next(args);
              }

              const params = typeCastMaybe.params.map(p => transform([p, context]));
              const [targetNode, typeNode] = params;
              const { type: from } = targetNode;
              const { value: to } = typeNode;

              if (typeNode.Type === Syntax.Type && !!from && !!to) {
                return {
                  ...typeCastMaybe,
                  type: to,
                  value: targetNode.value,
                  Type: Syntax.TypeCast,
                  meta: { ...typeCastMaybe.meta, [TYPE_CAST]: { to, from } },
                  // We need to drop the typeNode here, because it's not something we can generate
                  params: [targetNode],
                };
              }

              // If both sides of a pair don't have types then it's not a typecast,
              // more likely a string: value pair in an object for example
              return {
                ...typeCastMaybe,
                params,
              };
            },
            Identifier: next => args => {
              const [node, context] = args;

              let ref;
              Eif (context.locals) {
                ref = context.locals[node.value];
              }
              if (!ref) {
                ref = context.globals[node.value];
              }
              if (ref) {
                return {
                  ...node,
                  meta: { ...node.meta, ...ref.meta },
                  type: ref.type,
                };
              }

              return next(args);
            },
            MemoryAssignment: _ignore => (args, transform) => {
              const [inputNode, context] = args;
              const params = inputNode.params.map(p => transform([p, context]));
              const { type } = params[0];
              return { ...inputNode, params, type };
            },
          };
        },
      };
    }