// src/lisp/primitives/comparison.ts
// Comparison Primitives: >, <, =, >=, <=

import type { IPrimitiveDefinition } from './mccarthy';

/**
 * GREATER THAN
 * (> 5 3) → true
 */
export const gt: IPrimitiveDefinition = {
  name: '>',
  handler: async (args, evaluate) => {
    const a = await evaluate(args[0]) as number;
    const b = await evaluate(args[1]) as number;
    return a > b;
  },
};

/**
 * LESS THAN
 * (< 3 5) → true
 */
export const lt: IPrimitiveDefinition = {
  name: '<',
  handler: async (args, evaluate) => {
    const a = await evaluate(args[0]) as number;
    const b = await evaluate(args[1]) as number;
    return a < b;
  },
};

/**
 * EQUAL (numeric/value equality)
 * (= 3 3) → true
 */
export const equal: IPrimitiveDefinition = {
  name: '=',
  handler: async (args, evaluate) => {
    const a = await evaluate(args[0]);
    const b = await evaluate(args[1]);
    return a === b;
  },
};

/**
 * GREATER THAN OR EQUAL
 * (>= 5 5) → true
 */
export const gte: IPrimitiveDefinition = {
  name: '>=',
  handler: async (args, evaluate) => {
    const a = await evaluate(args[0]) as number;
    const b = await evaluate(args[1]) as number;
    return a >= b;
  },
};

/**
 * LESS THAN OR EQUAL
 * (<= 3 5) → true
 */
export const lte: IPrimitiveDefinition = {
  name: '<=',
  handler: async (args, evaluate) => {
    const a = await evaluate(args[0]) as number;
    const b = await evaluate(args[1]) as number;
    return a <= b;
  },
};

/**
 * All comparison primitives
 */
export const comparisonPrimitives: IPrimitiveDefinition[] = [
  gt,
  lt,
  equal,
  gte,
  lte,
];
