Acme-ExtUtils-XSOne-Test-Calculator

 view release on metacpan or  search on metacpan

Calculator.xs  view on Meta::CPAN

 * Call from import() like:
 *   static const char *basic_exports[] = {"add", "subtract", ...};
 *   do_import(aTHX_ "...::Basic", basic_exports, 10, items, ax);
 */
static void do_import(pTHX_ const char *pkg, const char **exports, int export_count, I32 items, I32 ax) {
    const char *caller;
    int i, j;

    /* Get caller's package name */
    caller = CopSTASHPV(PL_curcop);
    if (!caller || !*caller) {
        caller = "main";
    }

    /* Process import list (skip first arg which is the package name) */
    for (i = 1; i < items; i++) {
        SV *arg = ST(i);
        const char *name;
        STRLEN name_len;
        int found = 0;

        name = SvPV(arg, name_len);

        /* Find the export */
        for (j = 0; j < export_count; j++) {
            if (strcmp(name, exports[j]) == 0) {
                export_sub(aTHX_ pkg, name, caller);
                found = 1;
                break;
            }
        }

        if (!found) {
            croak("\"%s\" is not exported by the %s module", name, pkg);
        }
    }
}
/* C code from: Calculator/Basic.xs */
#line 1 "lib/Acme/ExtUtils/XSOne/Test/Calculator/Basic.xs"
/*
 * Acme::ExtUtils::XSOne::Test::Calculator::Basic - Basic arithmetic operations
 */


/* C helper functions for Basic package */
static double basic_safe_divide(double a, double b, int *error) {
    if (b == 0.0) {
        *error = 1;
        return 0.0;
    }
    *error = 0;
    return a / b;
}

static double basic_clamp(double value, double min_val, double max_val) {
    if (value < min_val) return min_val;
    if (value > max_val) return max_val;
    return value;
}

static double basic_percent(double value, double percent) {
    return value * percent / 100.0;
}
/* C code from: Calculator/Memory.xs */
#line 1 "lib/Acme/ExtUtils/XSOne/Test/Calculator/Memory.xs"
/*
 * Acme::ExtUtils::XSOne::Test::Calculator::Memory - Memory and history functions
 *
 * This module accesses the shared state defined in _header.xs
 */

/* Memory package helpers - need access to memory_slots */
static int mem_is_valid_slot(int slot) {
    return (slot >= 0 && slot < MAX_MEMORY_SLOTS);
}

static int mem_get_used_slots(void) {
    int count = 0;
    for (int i = 0; i < MAX_MEMORY_SLOTS; i++) {
        if (memory_slots[i] != 0.0) count++;
    }
    return count;
}

static double mem_sum_all(void) {
    double sum = 0.0;
    for (int i = 0; i < MAX_MEMORY_SLOTS; i++) {
        sum += memory_slots[i];
    }
    return sum;
}

static void mem_add_to_slot(int slot, double value) {
    if (mem_is_valid_slot(slot)) {
        memory_slots[slot] += value;
    }
}
/* C code from: Calculator/Scientific.xs */
#line 1 "lib/Acme/ExtUtils/XSOne/Test/Calculator/Scientific.xs"
/*
 * Acme::ExtUtils::XSOne::Test::Calculator::Scientific - Scientific/advanced operations
 */


/* C helper functions for Scientific package */
static double sci_safe_log(double a, int *error) {
    if (a <= 0.0) {
        *error = 1;
        return 0.0;
    }
    *error = 0;
    return log(a);
}

static double sci_safe_sqrt(double a, int *error) {
    if (a < 0.0) {
        *error = 1;
        return 0.0;
    }
    *error = 0;
    return sqrt(a);
}

Calculator.xs  view on Meta::CPAN

    add_to_history('/', a, b, RETVAL);
OUTPUT:
    RETVAL

double
modulo(a, b)
    double a
    double b
CODE:
    if (b == 0.0) {
        croak("Modulo by zero");
    }
    RETVAL = fmod(a, b);
    add_to_history('%', a, b, RETVAL);
OUTPUT:
    RETVAL

double
negate(a)
    double a
CODE:
    RETVAL = -a;
    add_to_history('n', a, 0, RETVAL);
OUTPUT:
    RETVAL

double
absolute(a)
    double a
CODE:
    RETVAL = fabs(a);
    add_to_history('a', a, 0, RETVAL);
OUTPUT:
    RETVAL

double
safe_divide(a, b)
    double a
    double b
CODE:
    int error;
    RETVAL = basic_safe_divide(a, b, &error);
    if (error) {
        RETVAL = 0.0;  /* Return 0 instead of croak */
    }
    add_to_history('/', a, b, RETVAL);
OUTPUT:
    RETVAL

double
clamp(value, min_val, max_val)
    double value
    double min_val
    double max_val
CODE:
    RETVAL = basic_clamp(value, min_val, max_val);
OUTPUT:
    RETVAL

double
percent(value, pct)
    double value
    double pct
CODE:
    RETVAL = basic_percent(value, pct);
    add_to_history('%', value, pct, RETVAL);
OUTPUT:
    RETVAL

void
import(...)
CODE:
{
    static const char *basic_exports[] = {
        "add", "subtract", "multiply", "divide", "modulo",
        "negate", "absolute", "safe_divide", "clamp", "percent"
    };
    do_import(aTHX_ "Acme::ExtUtils::XSOne::Test::Calculator::Basic",
              basic_exports, 10, items, ax);
}


MODULE = Acme::ExtUtils::XSOne::Test::Calculator    PACKAGE = Acme::ExtUtils::XSOne::Test::Calculator::Memory

PROTOTYPES: DISABLE

int
store(slot, value)
    int slot
    double value
CODE:
    RETVAL = store_memory(slot, value);
    if (!RETVAL) {
        warn("Invalid memory slot %d (valid: 0-%d)", slot, MAX_MEMORY_SLOTS - 1);
    }
OUTPUT:
    RETVAL

double
recall(slot)
    int slot
CODE:
    if (slot < 0 || slot >= MAX_MEMORY_SLOTS) {
        warn("Invalid memory slot %d (valid: 0-%d)", slot, MAX_MEMORY_SLOTS - 1);
        RETVAL = 0.0;
    } else {
        RETVAL = recall_memory(slot);
    }
OUTPUT:
    RETVAL

void
clear()
CODE:
    clear_all_memory();

double
ans()
CODE:
    RETVAL = get_last_result();
OUTPUT:
    RETVAL

int
history_count()
CODE:
    RETVAL = history_count;
OUTPUT:
    RETVAL

void
get_history_entry(index)
    int index
PPCODE:
    if (index < 0 || index >= history_count) {
        croak("Invalid history index %d (valid: 0-%d)", index, history_count - 1);



( run in 0.514 second using v1.01-cache-2.11-cpan-39bf76dae61 )