Skip to main content

wick_cond/
lib.rs

1//! Backend helpers for conditionals, comparisons, and boolean logic.
2//!
3//! This crate provides code generation utilities that domain crates use to emit
4//! conditional expressions to various backends (WGSL, Lua, Cranelift). Rather than
5//! each domain crate reimplementing backend-specific conditional logic, they can
6//! delegate to these shared helpers.
7//!
8//! # Architecture
9//!
10//! ```text
11//! dew-core (AST: Compare, And, Or, If)
12//!     │
13//!     ▼
14//! dew-cond (backend helpers)
15//!     │
16//!     ├── wgsl::emit_compare, emit_if, ...
17//!     ├── glsl::emit_compare, emit_if, ...
18//!     ├── rust::emit_compare, emit_if, ...
19//!     ├── lua::emit_compare, emit_if, ...
20//!     └── cranelift::emit_compare, emit_if, ...
21//!     │
22//!     ▼
23//! domain crates (dew-scalar, dew-linalg, dew-complex, dew-quaternion)
24//!     use dew-cond helpers in their backend modules
25//! ```
26//!
27//! # Features
28//!
29//! | Feature       | Description                           |
30//! |---------------|---------------------------------------|
31//! | `wgsl`        | WGSL code generation helpers          |
32//! | `lua-codegen` | Lua code generation (pure Rust)       |
33//! | `lua`         | Alias for lua-codegen                 |
34//! | `cranelift`   | Cranelift JIT compilation helpers     |
35//!
36//! # Backend Modules
37//!
38//! Each backend module provides the same set of functions with backend-specific implementations:
39//!
40//! | Function         | Description                                         |
41//! |------------------|-----------------------------------------------------|
42//! | `emit_compare`   | Comparison operators (`<`, `<=`, `>`, `>=`, `==`, `!=`) |
43//! | `emit_and`       | Logical AND                                         |
44//! | `emit_or`        | Logical OR                                          |
45//! | `emit_not`       | Logical NOT                                         |
46//! | `emit_if`        | Conditional expression (if/then/else)               |
47//! | `scalar_to_bool` | Convert numeric value to boolean                    |
48//! | `bool_to_scalar` | Convert boolean to numeric (1.0/0.0)                |
49//!
50//! # Examples
51//!
52//! ## WGSL Backend
53//!
54//! ```ignore
55//! use wick_cond::{wgsl, CompareOp};
56//!
57//! // Comparison
58//! let code = wgsl::emit_compare(CompareOp::Lt, "a", "b");
59//! assert_eq!(code, "(a < b)");
60//!
61//! // Conditional (uses WGSL's select function)
62//! let code = wgsl::emit_if("cond", "then_val", "else_val");
63//! assert_eq!(code, "select(else_val, then_val, cond)");
64//!
65//! // Boolean logic
66//! let code = wgsl::emit_and("x > 0.0", "y > 0.0");
67//! assert_eq!(code, "(x > 0.0 && y > 0.0)");
68//! ```
69//!
70//! ## Lua Backend
71//!
72//! ```ignore
73//! use wick_cond::{lua, CompareOp};
74//!
75//! // Comparison (note: Lua uses ~= for not-equal)
76//! let code = lua::emit_compare(CompareOp::Ne, "a", "b");
77//! assert_eq!(code, "(a ~= b)");
78//!
79//! // Conditional (uses Lua's and/or idiom)
80//! let code = lua::emit_if("cond", "then_val", "else_val");
81//! assert_eq!(code, "(cond and then_val or else_val)");
82//!
83//! // Boolean logic
84//! let code = lua::emit_not("flag");
85//! assert_eq!(code, "(not flag)");
86//! ```
87//!
88//! # Boolean Representation
89//!
90//! Dew expressions use scalars for boolean values (0.0 = false, non-zero = true).
91//! The `scalar_to_bool` and `bool_to_scalar` functions handle conversions between
92//! the numeric representation and backend-native booleans.
93//!
94//! ```ignore
95//! use wick_cond::wgsl;
96//!
97//! // Convert scalar to boolean for conditions
98//! let bool_expr = wgsl::scalar_to_bool("x");
99//! assert_eq!(bool_expr, "(x != 0.0)");
100//!
101//! // Convert boolean back to scalar
102//! let scalar_expr = wgsl::bool_to_scalar("flag");
103//! assert_eq!(scalar_expr, "select(0.0, 1.0, flag)");
104//! ```
105
106pub use wick_core::CompareOp;
107
108#[cfg(feature = "wgsl")]
109pub mod wgsl;
110
111#[cfg(feature = "glsl")]
112pub mod glsl;
113
114#[cfg(feature = "rust")]
115pub mod rust;
116
117#[cfg(feature = "c")]
118pub mod c;
119
120#[cfg(feature = "opencl")]
121pub mod opencl;
122
123#[cfg(feature = "cuda")]
124pub mod cuda;
125
126#[cfg(feature = "hip")]
127pub mod hip;
128
129#[cfg(feature = "tokenstream")]
130pub mod tokenstream;
131
132#[cfg(any(feature = "lua", feature = "lua-codegen"))]
133pub mod lua;
134
135#[cfg(feature = "cranelift")]
136pub mod cranelift;