Skip to content

API Reference ​

Complete reference for all Dew operators and functions.

Operator Precedence ​

Lower precedence numbers bind tighter (evaluate first).

PrecedenceOperatorsAssociativityDescription
4 (highest)^RightPower
3- (unary), ~RightNegation, bitwise NOT
2*, /, %LeftMultiplication, division, modulo
1+, -LeftAddition, subtraction
0<<, >>LeftBit shift
-1&LeftBitwise AND
-2|LeftBitwise OR
-3<, <=, >, >=, ==, !=LeftComparison
-4notRightLogical NOT
-5andLeftLogical AND (short-circuit)
-6orLeftLogical OR (short-circuit)

Examples:

x + y * z       # → x + (y * z)
2 ^ 3 ^ 2       # → 2 ^ (3 ^ 2) = 512  (right-associative)
a < b and b < c # → (a < b) and (b < c)
-x ^ 2          # → -(x ^ 2)  (unary has lower precedence than power)

Scalar Functions (dew-scalar) ​

Constants ​

FunctionReturnDescription
pi()Scalarπ ≈ 3.14159265359
e()ScalarEuler's number ≈ 2.71828182846
tau()Scalarτ = 2π ≈ 6.28318530718

Trigonometry ​

FunctionArgumentsReturnDomainRangeDescription
sin(x)ScalarScalarℝ[-1, 1]Sine
cos(x)ScalarScalarℝ[-1, 1]Cosine
tan(x)ScalarScalarℝ \ ℝTangent
asin(x)ScalarScalar[-1, 1][-π/2, π/2]Arc sine
acos(x)ScalarScalar[-1, 1][0, π]Arc cosine
atan(x)ScalarScalarℝ(-π/2, π/2)Arc tangent
atan2(y, x)2× ScalarScalarℝ²(-π, π]Two-argument arc tangent
sinh(x)ScalarScalarℝℝHyperbolic sine
cosh(x)ScalarScalarℝ[1, ∞)Hyperbolic cosine
tanh(x)ScalarScalarℝ(-1, 1)Hyperbolic tangent

Exponential & Logarithmic ​

FunctionArgumentsReturnDomainRangeDescription
exp(x)ScalarScalarℝ(0, ∞)e^x
exp2(x)ScalarScalarℝ(0, ∞)2^x
ln(x)ScalarScalar(0, ∞)ℝNatural logarithm
log(x)ScalarScalar(0, ∞)ℝNatural logarithm (alias)
log2(x)ScalarScalar(0, ∞)ℝBase-2 logarithm
log10(x)ScalarScalar(0, ∞)ℝBase-10 logarithm
pow(x, y)2× ScalarScalarx > 0 or y ∈ ℤℝx^y
sqrt(x)ScalarScalar[0, ∞)[0, ∞)Square root
inversesqrt(x)ScalarScalar(0, ∞)(0, ∞)1 / sqrt(x)

Common Math ​

FunctionArgumentsReturnDescription
abs(x)ScalarScalarAbsolute value |x|
sign(x)ScalarScalarSign: -1 (x < 0), 0 (x = 0), 1 (x > 0)
floor(x)ScalarScalarRound down to nearest integer
ceil(x)ScalarScalarRound up to nearest integer
round(x)ScalarScalarRound to nearest integer
trunc(x)ScalarScalarTruncate toward zero
fract(x)ScalarScalarFractional part: x - floor(x)
min(a, b)2× ScalarScalarMinimum of a and b
max(a, b)2× ScalarScalarMaximum of a and b
clamp(x, lo, hi)3× ScalarScalarConstrain x to [lo, hi]
saturate(x)ScalarScalarClamp to [0, 1]

Interpolation ​

FunctionArgumentsReturnDescription
lerp(a, b, t)3× ScalarScalarLinear interpolation: a + (b - a) * t
mix(a, b, t)3× ScalarScalarAlias for lerp (GLSL naming)
step(edge, x)2× ScalarScalar0 if x < edge, else 1
smoothstep(e0, e1, x)3× ScalarScalarSmooth Hermite interpolation [0, 1]
inverse_lerp(a, b, v)3× ScalarScalarInverse lerp: (v - a) / (b - a)
remap(x, in_lo, in_hi, out_lo, out_hi)5× ScalarScalarRemap x from [in_lo, in_hi] to [out_lo, out_hi]

Integer Operations ​

Available with scalar_registry_int() for i32/i64 types:

OperatorDescription
%Modulo
&Bitwise AND
|Bitwise OR
<<Left shift
>>Right shift
~xBitwise NOT

Vector Functions (dew-linalg) ​

Constructors ​

FunctionArgumentsReturnDescription
vec2(x, y)2× ScalarVec2Construct 2D vector
vec3(x, y, z)3× ScalarVec3Construct 3D vector
vec4(x, y, z, w)4× ScalarVec4Construct 4D vector

Component Extraction ​

FunctionArgumentReturnDescription
x(v)Vec2/3/4ScalarExtract x component (index 0)
y(v)Vec2/3/4ScalarExtract y component (index 1)
z(v)Vec3/4ScalarExtract z component (index 2)
w(v)Vec4ScalarExtract w component (index 3)

Vector Operations ​

FunctionArgumentsReturnDescription
dot(a, b)2× VecNScalarDot product: Σ(aᵢ × bᵢ)
cross(a, b)2× Vec3Vec3Cross product (3D only)
length(v)VecNScalarVector magnitude: sqrt(dot(v, v))
distance(a, b)2× VecNScalarDistance: length(b - a)
normalize(v)VecNVecNUnit vector: v / length(v)
reflect(i, n)2× VecNVecNReflect i across normal n
hadamard(a, b)2× VecNVecNElement-wise multiply: [a₀×b₀, a₁×b₁, ...]
lerp(a, b, t)2× VecN, ScalarVecNLinear interpolation
mix(a, b, t)2× VecN, ScalarVecNAlias for lerp

Vectorized Math ​

These functions apply element-wise to vectors:

FunctionArgumentReturnDescription
sin(v)VecNVecNApply sin to each component
cos(v)VecNVecNApply cos to each component
abs(v)VecNVecNApply abs to each component
floor(v)VecNVecNApply floor to each component
fract(v)VecNVecNApply fract to each component
sqrt(v)VecNVecNApply sqrt to each component
min(a, b)2× VecNVecNComponent-wise minimum
max(a, b)2× VecNVecNComponent-wise maximum
clamp(v, lo, hi)3× VecNVecNComponent-wise clamp
step(edge, v)Scalar/VecN, VecNVecNComponent-wise step
smoothstep(e0, e1, v)Scalar, Scalar, VecNVecNComponent-wise smoothstep

Rotation (2D) ​

FunctionArgumentsReturnDescription
rotate2d(v, angle)Vec2, ScalarVec2Rotate 2D vector by angle (radians)

Rotation (3D) ​

FunctionArgumentsReturnDescription
rotate_x(v, angle)Vec3, ScalarVec3Rotate around X axis
rotate_y(v, angle)Vec3, ScalarVec3Rotate around Y axis
rotate_z(v, angle)Vec3, ScalarVec3Rotate around Z axis
rotate3d(v, axis, angle)Vec3, Vec3, ScalarVec3Rotate around arbitrary axis (Rodrigues' formula)

Matrix Constructors ​

FunctionArgumentsReturnDescription
mat2(...)4× ScalarMat22×2 matrix (column-major)
mat3(...)9× ScalarMat33×3 matrix (column-major)
mat4(...)16× ScalarMat44×4 matrix (column-major)

Matrix Operations ​

ExpressionTypesReturnDescription
m * vMatN × VecNVecNMatrix-vector multiply (column vector)
v * mVecN × MatNVecNVector-matrix multiply (row vector)
m1 * m2MatN × MatNMatNMatrix multiplication
m * sMatN × ScalarMatNScale matrix

Complex Functions (dew-complex) ​

Component Access ​

FunctionArgumentReturnDescription
re(z)ComplexScalarReal part
im(z)ComplexScalarImaginary part

Properties ​

FunctionArgumentReturnDescription
abs(z)ComplexScalarMagnitude |z| = √(re² + im²)
arg(z)ComplexScalarPhase angle: atan2(im, re)
norm(z)ComplexScalarSquared magnitude: re² + im²
conj(z)ComplexComplexConjugate: re - i×im

Exponential & Logarithmic ​

FunctionArgumentReturnDescription
exp(z)ComplexComplexe^z = e^re × (cos(im) + i×sin(im))
log(z)ComplexComplexln(z) = ln|z| + i×arg(z)
sqrt(z)ComplexComplexPrincipal square root
pow(z, n)Complex, ScalarComplexz^n

Construction ​

FunctionArgumentsReturnDescription
polar(r, theta)2× ScalarComplexFrom polar: r × e^(i×theta)

Operators ​

ExpressionTypesReturnDescription
z1 + z2Complex + ComplexComplexAddition
z1 - z2Complex - ComplexComplexSubtraction
z1 * z2Complex × ComplexComplexMultiplication
z1 / z2Complex / ComplexComplexDivision
z ^ nComplex ^ ScalarComplexPower
-z-ComplexComplexNegation
s * zScalar × ComplexComplexScale

Quaternion Functions (dew-quaternion) ​

Quaternions use [x, y, z, w] order (scalar-last convention).

Properties ​

FunctionArgumentReturnDescription
length(q)QuaternionScalarMagnitude: √(x² + y² + z² + w²)
normalize(q)QuaternionQuaternionUnit quaternion (for rotations)
dot(q1, q2)2× QuaternionScalarDot product
conj(q)QuaternionQuaternionConjugate: [-x, -y, -z, w]
inverse(q)QuaternionQuaternionMultiplicative inverse: conj(q) / norm(q)

Construction ​

FunctionArgumentsReturnDescription
axis_angle(axis, angle)Vec3, ScalarQuaternionFrom axis-angle representation

Rotation ​

ExpressionTypesReturnDescription
q * vQuaternion × Vec3Vec3Rotate vector by quaternion
q1 * q2Quaternion × QuaternionQuaternionCombine rotations (q1 then q2)

Interpolation ​

FunctionArgumentsReturnDescription
slerp(q1, q2, t)2× Quaternion, ScalarQuaternionSpherical linear interpolation
lerp(q1, q2, t)2× Quaternion, ScalarQuaternionLinear interpolation

Vector Operations ​

FunctionArgumentsReturnDescription
cross(a, b)2× Vec3Vec3Cross product
length(v)Vec3ScalarVector magnitude
normalize(v)Vec3Vec3Unit vector

Conditional Expressions (feature = "cond") ​

Comparison Operators ​

OperatorDescriptionResult
<Less than0 or 1
<=Less than or equal0 or 1
>Greater than0 or 1
>=Greater than or equal0 or 1
==Equal0 or 1
!=Not equal0 or 1

Boolean results are represented as scalars: 0.0 = false, 1.0 = true.

Logical Operators ​

OperatorDescriptionEvaluation
andLogical ANDShort-circuit: if left is 0, returns 0 without evaluating right
orLogical ORShort-circuit: if left is 1, returns 1 without evaluating right
notLogical NOTUnary: returns 1 if operand is 0, else 0

If-Then-Else ​

if condition then value_if_true else value_if_false

Examples:

if x > 0 then sqrt(x) else 0
if a < b and b < c then 1 else 0
if x == 0 then y else x / y

Type Compatibility ​

Scalar Operations ​

LeftOperatorRightResult
Scalar+, -, *, /, ^ScalarScalar

Vector-Scalar Operations ​

LeftOperatorRightResult
VecN+, -VecNVecN
VecN*, /ScalarVecN
Scalar*VecNVecN

Matrix Operations ​

LeftOperatorRightResult
MatN*VecNVecN (column vector)
VecN*MatNVecN (row vector)
MatN*MatNMatN
MatN*, /ScalarMatN

Complex Operations ​

LeftOperatorRightResult
Complex+, -, *, /ComplexComplex
Complex*, /ScalarComplex
Scalar*ComplexComplex
Complex^ScalarComplex

Quaternion Operations ​

LeftOperatorRightResult
Quaternion+, -QuaternionQuaternion
Quaternion*QuaternionQuaternion (Hamilton product)
Quaternion*Vec3Vec3 (rotation)
Quaternion*, /ScalarQuaternion
Scalar*QuaternionQuaternion

Let Bindings ​

Syntax:

let name = value; body

Scope: The bound variable is only visible in the body expression.

Chaining:

let a = x * 2; let b = a + 1; b * b

Shadowing: Inner bindings hide outer ones with the same name.

Backend Support:

  • Lua/Cranelift: Native support
  • WGSL/GLSL: Requires LetInlining pass (included in standard_passes())

Error Handling ​

Parse Errors ​

rust
Expr::parse("x + + y")  // Error: unexpected token at position 4

Evaluation Errors ​

rust
eval(ast, &vars, &registry)
// UnknownVariable("x") - variable not in vars map
// UnknownFunction("foo") - function not registered
// TypeMismatch - incompatible operand types
// DivideByZero - division by zero (if checked)

Type Errors ​

Domain crates enforce type safety during evaluation and codegen:

rust
// Error: can't add Vec2 and Vec3
emit_wgsl("vec2(1, 2) + vec3(1, 2, 3)", &var_types)