All files / src/shared/lib/engineFields engineFieldToCType.ts

81.82% Statements 27/33
53.85% Branches 7/13
80% Functions 4/5
78.57% Lines 22/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  8x   8x 4x     8x 20x 5x   15x 5x   10x     8x 20x 5x   15x 5x   10x 5x   5x     8x       16x 16x 16x     8x                        
import type { EngineFieldCType } from "store/features/engine/engineState";
import { assertUnreachable } from "shared/lib/helpers/assert";
 
export const is16BitCType = (cType: EngineFieldCType): boolean => {
  return cType === "WORD" || cType === "UWORD";
};
 
export const minForCType = (cType: EngineFieldCType): number => {
  if (cType === "WORD") {
    return -32768;
  }
  if (cType === "BYTE") {
    return -128;
  }
  return 0;
};
 
export const maxForCType = (cType: EngineFieldCType): number => {
  if (cType === "UWORD") {
    return 65535;
  }
  if (cType === "WORD") {
    return 32767;
  }
  if (cType === "BYTE") {
    return 127;
  }
  return 255;
};
 
export const clampToCType = (
  value: number,
  cType: EngineFieldCType
): number => {
  const min = minForCType(cType);
  const max = maxForCType(cType);
  return Math.max(min, Math.min(max, value));
};
 
export const gbvmSetConstForCType = (
  cType: Exclude<EngineFieldCType, "define">
): string => {
  Iif (cType === "WORD" || cType === "UWORD") {
    return "VM_SET_CONST_INT16";
  }
  Iif (cType === "BYTE" || cType === "UBYTE") {
    return "VM_SET_CONST_INT8";
  }
  assertUnreachable(cType);
  return "UNREACHABLE";
};