x-engine-c v0.2.13
The C engine for x-lang
Loading...
Searching...
No Matches
arith.c File Reference
#include "x-prim.h"
#include "x-type.h"
#include "x-type/int.h"

Functions

static x_obj_tx_prim_arith_binop (x_obj_t *p_base, x_obj_t *p_args, x_char_t *p_op, x_char_t *p_err, int use_ops)
 
static x_obj_tx_prim_sum (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_sub (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_prod (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_div (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_mod (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_bitnot (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_bitand (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_bitor (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_bitxor (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_shl (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_shr (x_obj_t *p_base, x_obj_t *p_args)
 
x_obj_tx_prim_arith_register (x_obj_t *p_base, x_obj_t *p_args)
 

Function Documentation

◆ x_prim_arith_binop()

static x_obj_t * x_prim_arith_binop ( x_obj_t p_base,
x_obj_t p_args,
x_char_t p_op,
x_char_t p_err,
int  use_ops 
)
static

Shared body for the binary integer ops: evaluate both operands, optionally dispatch through the type-ops registry, guard nil, compute.

use_ops selects the x_type_op_try dispatch. The tower ops (+ * / %) pass 1 so a typed operand (float, bigint, ...) reaches its type's handler; the bitwise family passes 0 – #52 ruled bitwise has no tower semantics (there is no float &; lib/x/core/arithmetic.x records the ruling), so the dispatch hook is deliberately not offered there.

Nil operands raise instead of reading x_intval(NULL) – the same nil-safety convention x_prim_eq already follows (#52 ruled: an x-level wrapper test measured +9% on every method dispatch, so the check lives here, where it is two pointer tests). Runs AFTER op_try, so typed operands never reach it. The raw prims are the only guard on the bare-core and (Base make) child paths, where the lib wrappers are absent (#239).

The switch keys on the operator's first byte: all nine ops are unique there (<</>> via </>).

Parameters
p_basex_obj_t* – Base (execution context)
p_argsx_obj_t* – Unevaluated args, evaluated via x_eargs
p_opx_char_t* – Operator spelling, as registered
p_errx_char_t* – Raise message for a nil operand
use_opsint – Non-zero to try x_type_op_try first
Returns
x_obj_t* – New integer object, or the handler's result

◆ x_prim_arith_register()

x_obj_t * x_prim_arith_register ( x_obj_t p_base,
x_obj_t p_args 
)

Register arithmetic primitives into the environment.

Binds: +, -, *, /, %, ~, &, |, ^, <<, >>

Parameters
p_basex_obj_t* – Base (execution context)
p_argsx_obj_t* – Unused
Returns
x_obj_t* – p_base

◆ x_prim_bitand()

static x_obj_t * x_prim_bitand ( x_obj_t p_base,
x_obj_t p_args 
)
static

Bitwise AND. x-lang: (& a b)

See also
x_prim_arith_binop

◆ x_prim_bitnot()

static x_obj_t * x_prim_bitnot ( x_obj_t p_base,
x_obj_t p_args 
)
static

Bitwise NOT. x-lang: (~ n)

Unary: no binary op dispatch; the nil guard mirrors x_prim_sub's unary branch (#239 – the raw prim is the only guard on bare-core and (Base make) child paths).

◆ x_prim_bitor()

static x_obj_t * x_prim_bitor ( x_obj_t p_base,
x_obj_t p_args 
)
static

Bitwise OR. x-lang: (| a b)

See also
x_prim_arith_binop

◆ x_prim_bitxor()

static x_obj_t * x_prim_bitxor ( x_obj_t p_base,
x_obj_t p_args 
)
static

Bitwise XOR. x-lang: (^ a b)

See also
x_prim_arith_binop

◆ x_prim_div()

static x_obj_t * x_prim_div ( x_obj_t p_base,
x_obj_t p_args 
)
static

Binary integer division (truncates toward zero). x-lang: (/ a b)

See also
x_prim_arith_binop

◆ x_prim_mod()

static x_obj_t * x_prim_mod ( x_obj_t p_base,
x_obj_t p_args 
)
static

Binary integer modulo. x-lang: (% a b)

See also
x_prim_arith_binop

◆ x_prim_prod()

static x_obj_t * x_prim_prod ( x_obj_t p_base,
x_obj_t p_args 
)
static

Binary integer multiplication. x-lang: (* a b)

See also
x_prim_arith_binop

◆ x_prim_shl()

static x_obj_t * x_prim_shl ( x_obj_t p_base,
x_obj_t p_args 
)
static

Left shift. x-lang: (<< a b)

See also
x_prim_arith_binop

◆ x_prim_shr()

static x_obj_t * x_prim_shr ( x_obj_t p_base,
x_obj_t p_args 
)
static

Right shift. x-lang: (>> a b)

See also
x_prim_arith_binop

◆ x_prim_sub()

static x_obj_t * x_prim_sub ( x_obj_t p_base,
x_obj_t p_args 
)
static

Integer subtraction or negation. x-lang: (- a b) or (- a)

With one argument, returns the negation. With two, returns the difference. The second argument is evaluated lazily (only if present).

Parameters
p_basex_obj_t* – Base (execution context)
p_argsx_obj_t* – Unevaluated args (1 or 2)
Returns
x_obj_t* – New integer object

◆ x_prim_sum()

static x_obj_t * x_prim_sum ( x_obj_t p_base,
x_obj_t p_args 
)
static

Binary integer addition, with typed-operand dispatch. x-lang: (+ a b)

See also
x_prim_arith_binop