# All files / src/generator map-syntax.js

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

    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
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x

    23x
    23x
    23x
    23x

    23x

    23x
    2496x
    2496x

    2495x

     // @flow
    import generateFunctionCall from './function-call';
    import generateIndirectFunctionCall from './indirect-function-call';
    import generateBinaryExpression from './binary-expression';
    import generateTernary from './ternary-expression';
    import generateIf from './if-then-else';
    import generateFunctionPointer from './function-pointer';
    import generateReturn from './return-statement';
    import generateDeclaration from './declaration';
    import generateArraySubscript from './array-subscript';
    import generateAssignment from './assignment';
    import generateMemoryAssignment from './memory-assignment';
    import generateLoop from './loop';
    import generateSequence from './sequence';
    import generateTypecast from './typecast';
    import generateBreak from './break';
    import generateNoop from './noop';
    import generateBlock from './block';
    import generateElse from './else';
    import generateSelect from './select';
    import generateNative from './native';

    import Syntax from '../Syntax';
    import { getInScope, getConstOpcode } from './utils';
    import curry from 'curry';
    import invariant from 'invariant';
    import type { MapSyntaxType, GeneratorType } from './flow/types';

    export const syntaxMap: { [string]: GeneratorType } = {
      [Syntax.FunctionCall]: generateFunctionCall,
      [Syntax.IndirectFunctionCall]: generateIndirectFunctionCall,
      // Unary
      [Syntax.Constant]: getConstOpcode,
      [Syntax.BinaryExpression]: generateBinaryExpression,
      [Syntax.TernaryExpression]: generateTernary,
      [Syntax.IfThenElse]: generateIf,
      [Syntax.Else]: generateElse,
      [Syntax.Select]: generateSelect,
      [Syntax.Block]: generateBlock,
      [Syntax.Identifier]: getInScope,
      [Syntax.FunctionIdentifier]: getInScope,
      [Syntax.FunctionPointer]: generateFunctionPointer,
      [Syntax.ReturnStatement]: generateReturn,
      // Binary
      [Syntax.Declaration]: generateDeclaration,
      [Syntax.ArraySubscript]: generateArraySubscript,
      [Syntax.Assignment]: generateAssignment,
      // Memory
      [Syntax.MemoryAssignment]: generateMemoryAssignment,
      // Loops
      [Syntax.Loop]: generateLoop,
      [Syntax.Break]: generateBreak,
      // Comma separated lists
      [Syntax.Sequence]: generateSequence,
      // Typecast
      [Syntax.TypeCast]: generateTypecast,
      [Syntax.Noop]: generateNoop,
      [Syntax.NativeMethod]: generateNative,
    };

    const mapSyntax: MapSyntaxType = curry((parent, operand) => {
      const mapping = syntaxMap[operand.Type];
      invariant(
        mapping,
        `Unexpected Syntax Token. ${operand.Type} "${operand.value}"`
      );

      return mapping(operand, parent);
    });

    export default mapSyntax;