All files / transpiler/output/codegen/helpers BooleanHelper.ts

100% Statements 11/11
100% Branches 10/10
100% Functions 3/3
100% Lines 7/7

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                                5x 4x 3x             5x               4x 3x 2x          
/**
 * BooleanHelper
 *
 * Helper class for boolean conversion logic in code generation.
 * Extracts testable pure functions from CodeGenerator.
 */
 
class BooleanHelper {
  /**
   * Convert boolean literal to integer literal for C code generation.
   * C89 doesn't have native bool type, so we convert to 0/1.
   *
   * @param expr - Expression string (may be "true", "false", or any expression)
   * @returns "1" for "true", "0" for "false", or wrapped ternary otherwise
   */
  static foldBooleanToInt(expr: string): string {
    if (expr === "true") return "1";
    if (expr === "false") return "0";
    return `(${expr} ? 1 : 0)`;
  }
 
  /**
   * Check if an expression is a boolean literal.
   */
  static isBooleanLiteral(expr: string): boolean {
    return expr === "true" || expr === "false";
  }
 
  /**
   * Convert a boolean literal to its integer equivalent.
   * Returns null if not a boolean literal.
   */
  static booleanLiteralToInt(expr: string): string | null {
    if (expr === "true") return "1";
    if (expr === "false") return "0";
    return null;
  }
}
 
export default BooleanHelper;