Skip to content

Introduction ​

Dew is a minimal expression language that compiles to multiple backends. Parse once, emit to WGSL/GLSL (GPU shaders), Cranelift (native JIT), or Lua (scripting).

Part of the rhi ecosystem.

Architecture ​

dew-core               # Syntax only: AST, parsing
    |
    +-- dew-cond       # Conditional backend helpers
    |
    +-- dew-scalar     # Scalar domain: f32/f64 math functions
    |
    +-- dew-linalg     # Linalg domain: Vec2, Vec3, Mat2, Mat3
    |
    +-- dew-complex    # Complex numbers: [re, im]
    |
    +-- dew-quaternion # Quaternions: [x, y, z, w], Vec3

All domain crates have multiple backends (feature flags): WGSL, GLSL, OpenCL, CUDA, HIP, Rust, C, TokenStream, Lua, and Cranelift.

Core = syntax only, domains = semantics. Each domain crate has its own:

  • Value types and type system
  • Function registry
  • Self-contained backends (behind feature flags)

Feature Flags ​

dew-core uses feature flags to manage complexity:

FeatureDescription
(none)Basic expressions: numbers, variables, arithmetic
condConditionals: if/then/else, comparisons (<, >=, ==), boolean logic (and, or, not)
funcFunction calls: name(args...) with extensible registry

Domain crates automatically enable func (they rely on function calls).

Quick Example ​

rust
use dew_core::Expr;
use dew_scalar::{eval, scalar_registry};

// Parse an expression
let expr = Expr::parse("sin(x) + cos(y)").unwrap();

// Evaluate with variables
let mut vars = std::collections::HashMap::new();
vars.insert("x".to_string(), 0.5_f32);
vars.insert("y".to_string(), 1.0_f32);

let registry = scalar_registry();
let result = eval(expr.ast(), &vars, &registry).unwrap();

With Conditionals ​

When dew-core is compiled with the cond feature:

rust
use dew_core::Expr;
use dew_scalar::{eval, scalar_registry};

// Conditional expression
let expr = Expr::parse("if x > 0 then sqrt(x) else 0").unwrap();

let mut vars = std::collections::HashMap::new();
vars.insert("x".to_string(), 9.0_f32);

let result = eval(expr.ast(), &vars, &scalar_registry()).unwrap();
// result = 3.0

Backends ​

Each domain crate includes multiple backends as optional features:

BackendFeatureUse case
WGSLwgslGPU shaders (WebGPU)
GLSLglslGPU shaders (OpenGL/Vulkan)
OpenCLopenclGPU compute kernels (cross-platform)
CUDAcudaGPU compute kernels (NVIDIA)
HIPhipGPU compute kernels (AMD ROCm, source-compatible with CUDA)
RustrustRust source code generation
CcC source code generation
TokenStreamtokenstreamProc-macro code generation
LualuaScripting, hot-reload (includes lua-codegen for WASM)
CraneliftcraneliftNative JIT compilation

Enable in Cargo.toml:

toml
[dependencies]
dew-scalar = { version = "0.1", features = ["wgsl", "glsl", "lua", "cranelift"] }

C Backend Limitations ​

The C backend generates code that assumes external type and function definitions:

Required type definitions (user-provided):

  • Scalars: float (standard C)
  • Vectors: vec2, vec3, vec4 (structs with .x, .y, .z, .w fields)
  • Matrices: mat2, mat3, mat4
  • Complex: complex_t (struct with .re, .im fields)
  • Quaternions: quat_t (struct with .x, .y, .z, .w fields)

Required function definitions (user-provided):

  • Vector ops: vec2_add(), vec2_dot(), vec2_scale(), vec2_normalize(), etc.
  • Matrix ops: mat2_mul(), mat2_mul_vec2(), etc.
  • Complex ops: complex_mul(), complex_conj(), complex_exp(), etc.
  • Quaternion ops: quat_mul(), quat_slerp(), quat_from_axis_angle(), etc.

What it uses from standard C:

  • <math.h>: sinf, cosf, tanf, expf, logf, sqrtf, fabsf, floorf, ceilf, fminf, fmaxf, powf, fmodf, atan2f
  • Ternary operator for conditionals: cond ? then : else
  • Float literals with f suffix: 1.0f, 3.14159f

The generated code is designed to be embedded in projects with existing math libraries (e.g., cglm, HandmadeMath, custom implementations). It provides the expression logic while you provide the type system.

Crates ​

CrateDescription
dew-coreCore AST and parsing (feature-gated conditionals and functions)
dew-condConditional backend helpers for domain crates
dew-scalarScalar math: sin, cos, exp, lerp, etc.
dew-linalgLinear algebra: Vec2-4, Mat2-4, dot, cross, etc.
dew-complexComplex numbers: exp, log, polar, conjugate, etc.
dew-quaternionQuaternions: rotation, slerp, axis-angle, etc.