mathhook-macros 0.2.0

Procedural macros for MathHook - expr!, symbol!, symbols!, function!
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//! Code generation macros for MathHook language bindings and expression construction
//!
//! This crate provides procedural macros to generate Python (PyO3) and Node.js (NAPI)
//! bindings from a single function definition, eliminating code duplication while
//! maintaining zero-cost abstractions.
//!
//! Also provides procedural macros for mathematical expression and symbol construction.
//!
//! # Supported Function Arities
//!
//! - **Unary**: sin(x), cos(x), exp(x)
//! - **Binary**: pow(x, y), atan2(y, x)
//! - **Variadic**: add(args...), mul(args...)
//! - **Constants**: pi(), e()

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Ident};

mod expr;
mod function;
mod symbol;

/// Procedural macro for creating mathematical expressions with full syntax support
///
/// # Supported Operators
///
/// - **Basic arithmetic**: `+`, `-`, `*`, `/`
/// - **Unary negation**: `-x`
/// - **Power operations**: Both `**` and `.pow()` syntax
/// - **Comparison operators**: `==`, `<`, `>`, `<=`, `>=`
///
/// # Supported Literals
///
/// - **Integers**: `42`, `-5`, `0`
/// - **Floats**: `3.14`, `-2.5`, `0.0`
/// - **Identifiers/symbols**: `x`, `theta`, `alpha_1`
/// - **Mathematical constants**: `pi`, `e`, `i`
///
/// # Supported Constructs
///
/// - **Function calls**: `sin(x)`, `log(x, y)`, `f(a, b, c)`
/// - **Parenthesized expressions**: `(2*x + 3)`, `((x+y))`
/// - **Method calls**: `x.pow(2)`, `x.abs()`, `x.sqrt()`, `x.simplify()`
///
/// # Power Operator Syntax
///
/// The macro supports **two syntaxes** for exponentiation:
///
/// ## Method Syntax: `.pow()`
///
/// The `.pow()` method works everywhere without restrictions:
///
/// ```rust,ignore
/// expr!(x.pow(2))              // x squared
/// expr!(x.pow(y))              // x to the y
/// expr!((x + 1).pow(3))        // (x+1) cubed
/// expr!(2 * x.pow(3) + 5)      // Complex expression
/// ```
///
/// **Advantages:**
/// - Mirrors Rust's standard library API
/// - Works seamlessly in any context
/// - Clear, unambiguous precedence
/// - No parentheses required
///
/// ## Infix Syntax: `**`
///
/// The `**` operator provides mathematical notation:
///
/// ```rust,ignore
/// expr!(x ** 2)                // x squared
/// expr!(x ** y)                // x to the y
/// expr!((x + 1) ** 3)          // (x+1) cubed
/// ```
///
/// **IMPORTANT - When to use parentheses with `**`:**
///
/// Due to token-level preprocessing, complex expressions with `**` should use
/// parentheses for clarity when mixing with other operators:
///
/// ```rust,ignore
/// // ✅ RECOMMENDED (clear precedence):
/// expr!(2 * (x ** 3) + 5)      // Parentheses make precedence explicit
/// expr!((x ** 2) + (y ** 2))   // Clear grouping
///
/// // ⚠️  WORKS BUT LESS CLEAR:
/// expr!(x ** 2 + y ** 2)       // Relies on implicit precedence
/// ```
///
/// **Right-associativity:**
///
/// Power operations are right-associative (like standard mathematical notation):
///
/// ```rust,ignore
/// expr!(x ** 2 ** 3)           // Parsed as: x ** (2 ** 3) = x ** 8
/// expr!(2 ** 3 ** 2)           // Parsed as: 2 ** (3 ** 2) = 2 ** 9 = 512
/// ```
///
/// **Which syntax to use?**
///
/// - Use `.pow()` for guaranteed clarity in complex expressions
/// - Use `**` when writing simple mathematical formulas that match notation
/// - When in doubt, add parentheses: `expr!(2 * (x ** 3) + 5)`
///
/// # Examples
///
/// ```rust,ignore
/// use mathhook_macros::expr;
/// use mathhook_core::{Expression, symbol};
///
/// let x = symbol!(x);
/// let y = symbol!(y);
///
/// // Basic operations
/// let sum = expr!(x + 2);
/// let product = expr!(2 * x);
///
/// // Power operations (both syntaxes work)
/// let power1 = expr!(x ** 2);
/// let power2 = expr!(x.pow(2));
///
/// // Complex expressions (use parentheses with ** for clarity)
/// let quadratic = expr!((x ** 2) + 2*x + 1);
/// let nested = expr!((x + 1) * (x - 1));
/// let functions = expr!(sin(x ** 2));
///
/// // Comparison operators
/// let equation = expr!(x ** 2 == 4);
/// let inequality = expr!(x + 1 > y);
///
/// // Method calls
/// let abs_val = expr!(x.abs());
/// let sqrt_expr = expr!(x.sqrt());
/// let simplified = expr!((x + x).simplify());
/// ```
///
/// # Precedence Rules
///
/// Operator precedence from highest to lowest:
///
/// 1. **Method calls** (highest): `.pow()`, `.abs()`, `.sqrt()`, `.simplify()`
/// 2. **Power** (right-associative): `**`
/// 3. **Unary negation**: `-x`
/// 4. **Multiplication/division**: `*`, `/`
/// 5. **Addition/subtraction**: `+`, `-`
/// 6. **Comparison operators** (lowest): `==`, `<`, `>`, `<=`, `>=`
///
/// Use parentheses to override precedence:
///
/// ```rust,ignore
/// expr!(2 * x + 3)             // Parsed as: (2*x) + 3
/// expr!((2 + 3) * x)           // Parentheses override: 5*x
/// expr!(2 * (x ** 3))          // Recommended for clarity with **
/// expr!((x + 1) ** 2)          // Power of sum
/// ```
#[proc_macro]
pub fn expr(input: TokenStream) -> TokenStream {
    expr::expr_impl(input)
}

/// Procedural macro for creating symbols with optional type specification
///
/// # Syntax
///
/// ```rust,ignore
/// symbol!(x)                  // Scalar (default, commutative)
/// symbol!(A; matrix)          // Matrix (noncommutative)
/// symbol!(p; operator)        // Operator (noncommutative)
/// symbol!(i; quaternion)      // Quaternion (noncommutative)
/// symbol!("name")             // String literal for symbol name
/// ```
///
/// # Symbol Types
///
/// - **scalar**: Commutative symbols (default) - variables like x, y, z
/// - **matrix**: Noncommutative matrix symbols - A*B ≠ B*A
/// - **operator**: Noncommutative operator symbols - for quantum mechanics [x,p] ≠ 0
/// - **quaternion**: Noncommutative quaternion symbols - i*j = k, j*i = -k
///
/// # Examples
///
/// ```rust,ignore
/// use mathhook_macros::symbol;
///
/// // Scalar symbols (commutative)
/// let x = symbol!(x);
/// let theta = symbol!(theta);
///
/// // Matrix symbols (noncommutative)
/// let A = symbol!(A; matrix);
/// let B = symbol!(B; matrix);
///
/// // Operator symbols (noncommutative)
/// let p = symbol!(p; operator);
/// let x_op = symbol!(x; operator);
///
/// // Quaternion symbols (noncommutative)
/// let i = symbol!(i; quaternion);
/// let j = symbol!(j; quaternion);
/// ```
#[proc_macro]
pub fn symbol(input: TokenStream) -> TokenStream {
    symbol::symbol_impl(input)
}

/// Procedural macro for creating multiple symbols at once
///
/// # Syntax
///
/// ```rust,ignore
/// symbols![x, y, z]              // All scalars (default)
/// symbols![A, B, C => matrix]    // All matrices
/// symbols![p, x, H => operator]  // All operators
/// symbols![i, j, k => quaternion] // All quaternions
/// ```
///
/// # Returns
///
/// Returns `Vec<Symbol>` containing all created symbols.
///
/// # Examples
///
/// ```rust,ignore
/// use mathhook_macros::symbols;
///
/// // Scalar symbols (default, commutative)
/// let syms = symbols![x, y, z];
/// assert_eq!(syms.len(), 3);
///
/// // Matrix symbols (noncommutative)
/// let mats = symbols![A, B, C => matrix];
/// assert_eq!(mats.len(), 3);
///
/// // Operator symbols (noncommutative)
/// let ops = symbols![p, x, H => operator];
/// assert_eq!(ops.len(), 3);
///
/// // Quaternion symbols (noncommutative)
/// let quats = symbols![i, j, k => quaternion];
/// assert_eq!(quats.len(), 3);
/// ```
#[proc_macro]
pub fn symbols(input: TokenStream) -> TokenStream {
    symbol::symbols_impl(input)
}

/// Procedural macro for creating function expressions
///
/// # Syntax
///
/// ```rust,ignore
/// function!(sin)              // Zero args
/// function!(sin, x)           // One arg (x is an Expression)
/// function!(log, x, y)        // Two args
/// function!(f, a, b, c)       // N args
/// ```
///
/// # Examples
///
/// ```rust,ignore
/// use mathhook_macros::function;
/// use mathhook_core::expr;
///
/// // Zero-argument function
/// let gamma_call = function!(gamma);
///
/// // Single argument
/// let x = expr!(x);
/// let sin_x = function!(sin, x);
///
/// // Multiple arguments
/// let log_xy = function!(log, x, expr!(2));
/// ```
#[proc_macro]
pub fn function(input: TokenStream) -> TokenStream {
    function::function_impl(input)
}

/// Generate Python (PyO3) binding for a unary mathematical function
///
/// # Arguments
///
/// * `name` - The function name (e.g., `sin`)
///
/// # Examples
///
/// ```rust,ignore
/// generate_python_binding!(sin);
/// ```
///
/// Generates:
///
/// ```rust,ignore
/// #[pyfunction]
/// pub fn sin(x: &Bound<'_, PyAny>) -> PyResult<PyExpression> {
///     let expr = sympify_python(x)?;
///     Ok(PyExpression {
///         inner: Expression::function("sin", vec![expr]),
///     })
/// }
/// ```
#[proc_macro]
pub fn generate_python_binding(input: TokenStream) -> TokenStream {
    let name = parse_macro_input!(input as Ident);
    let name_str = name.to_string();

    let expanded = quote! {
        #[::pyo3::pyfunction]
        pub fn #name(x: &pyo3::Bound<'_, pyo3::PyAny>) -> pyo3::PyResult<crate::PyExpression> {
            use mathhook_core::Expression;
            let expr = crate::helpers::sympify_python(x)?;
            Ok(crate::PyExpression {
                inner: Expression::function(#name_str, vec![expr]),
            })
        }
    };

    TokenStream::from(expanded)
}

/// Generate Python (PyO3) binding for a binary mathematical function
///
/// # Arguments
///
/// * `name` - The function name (e.g., `pow`)
///
/// # Examples
///
/// ```rust,ignore
/// generate_python_binary_binding!(pow);
/// ```
///
/// Generates:
///
/// ```rust,ignore
/// #[pyfunction]
/// pub fn pow(x: &Bound<'_, PyAny>, y: &Bound<'_, PyAny>) -> PyResult<PyExpression> {
///     let expr1 = sympify_python(x)?;
///     let expr2 = sympify_python(y)?;
///     Ok(PyExpression {
///         inner: Expression::function("pow", vec![expr1, expr2]),
///     })
/// }
/// ```
#[proc_macro]
pub fn generate_python_binary_binding(input: TokenStream) -> TokenStream {
    let name = parse_macro_input!(input as Ident);
    let name_str = name.to_string();

    let expanded = quote! {
        #[::pyo3::pyfunction]
        pub fn #name(
            x: &pyo3::Bound<'_, pyo3::PyAny>,
            y: &pyo3::Bound<'_, pyo3::PyAny>,
        ) -> pyo3::PyResult<crate::PyExpression> {
            use mathhook_core::Expression;
            let expr1 = crate::helpers::sympify_python(x)?;
            let expr2 = crate::helpers::sympify_python(y)?;
            Ok(crate::PyExpression {
                inner: Expression::function(#name_str, vec![expr1, expr2]),
            })
        }
    };

    TokenStream::from(expanded)
}

/// Generate Python (PyO3) binding for a variadic mathematical function
///
/// # Arguments
///
/// * `name` - The function name (e.g., `add`)
///
/// # Examples
///
/// ```rust,ignore
/// generate_python_variadic_binding!(add);
/// ```
///
/// Generates:
///
/// ```rust,ignore
/// #[pyfunction]
/// #[pyo3(signature = (*args))]
/// pub fn add(args: &Bound<'_, PyTuple>) -> PyResult<PyExpression> {
///     let mut exprs = Vec::new();
///     for arg in args {
///         exprs.push(sympify_python(&arg)?);
///     }
///     Ok(PyExpression {
///         inner: Expression::function("add", exprs),
///     })
/// }
/// ```
#[proc_macro]
pub fn generate_python_variadic_binding(input: TokenStream) -> TokenStream {
    let name = parse_macro_input!(input as Ident);
    let name_str = name.to_string();

    let expanded = quote! {
        #[::pyo3::pyfunction]
        #[pyo3(signature = (*args))]
        pub fn #name(args: &pyo3::Bound<'_, pyo3::types::PyTuple>) -> pyo3::PyResult<crate::PyExpression> {
            use mathhook_core::Expression;
            let mut exprs = Vec::new();
            for arg in args {
                exprs.push(crate::helpers::sympify_python(&arg)?);
            }
            Ok(crate::PyExpression {
                inner: Expression::function(#name_str, exprs),
            })
        }
    };

    TokenStream::from(expanded)
}

/// Generate Python (PyO3) binding for a zero-argument constant function
///
/// # Arguments
///
/// * `name` - The constant name (e.g., `pi`)
///
/// # Examples
///
/// ```rust,ignore
/// generate_python_constant_binding!(pi);
/// ```
///
/// Generates:
///
/// ```rust,ignore
/// #[pyfunction]
/// pub fn pi() -> PyExpression {
///     PyExpression {
///         inner: Expression::pi(),
///     }
/// }
/// ```
#[proc_macro]
pub fn generate_python_constant_binding(input: TokenStream) -> TokenStream {
    let name = parse_macro_input!(input as Ident);

    let expanded = quote! {
        #[::pyo3::pyfunction]
        pub fn #name() -> crate::PyExpression {
            use mathhook_core::Expression;
            crate::PyExpression {
                inner: Expression::#name(),
            }
        }
    };

    TokenStream::from(expanded)
}

/// Generate Node.js (NAPI) binding for a unary mathematical function
///
/// # Arguments
///
/// * `name` - The function name (e.g., `sin`)
///
/// # Examples
///
/// ```rust,ignore
/// generate_nodejs_binding!(sin);
/// ```
///
/// Generates:
///
/// ```rust,ignore
/// #[napi]
/// pub fn sin(x: ExpressionOrNumber) -> JsExpression {
///     JsExpression {
///         inner: Expression::function("sin", vec![x.0]),
///     }
/// }
/// ```
///
/// This accepts both Expression objects and numbers: `sin(x)` or `sin(1.5)`
#[proc_macro]
pub fn generate_nodejs_binding(input: TokenStream) -> TokenStream {
    let name = parse_macro_input!(input as Ident);
    let name_str = name.to_string();

    let expanded = quote! {
        #[napi_derive::napi]
        pub fn #name(x: crate::functions::ExpressionOrNumber) -> crate::JsExpression {
            use mathhook_core::Expression;

            crate::JsExpression {
                inner: Expression::function(#name_str, vec![x.0]),
            }
        }
    };

    TokenStream::from(expanded)
}

/// Generate Node.js (NAPI) binding for a binary mathematical function
///
/// # Arguments
///
/// * `name` - The function name (e.g., `pow`)
///
/// # Examples
///
/// ```rust,ignore
/// generate_nodejs_binary_binding!(pow);
/// ```
///
/// Generates:
///
/// ```rust,ignore
/// #[napi]
/// pub fn pow(x: ExpressionOrNumber, y: ExpressionOrNumber) -> JsExpression {
///     JsExpression {
///         inner: Expression::function("pow", vec![x.0, y.0]),
///     }
/// }
/// ```
///
/// This accepts both Expression objects and numbers: `pow(x, 2)` or `pow(2, 3)`
#[proc_macro]
pub fn generate_nodejs_binary_binding(input: TokenStream) -> TokenStream {
    let name = parse_macro_input!(input as Ident);
    let name_str = name.to_string();

    let expanded = quote! {
        #[napi_derive::napi]
        pub fn #name(
            x: crate::functions::ExpressionOrNumber,
            y: crate::functions::ExpressionOrNumber,
        ) -> crate::JsExpression {
            use mathhook_core::Expression;

            crate::JsExpression {
                inner: Expression::function(#name_str, vec![x.0, y.0]),
            }
        }
    };

    TokenStream::from(expanded)
}

/// Generate Node.js (NAPI) binding for a variadic mathematical function
///
/// # Arguments
///
/// * `name` - The function name (e.g., `add`)
///
/// # Examples
///
/// ```rust,ignore
/// generate_nodejs_variadic_binding!(add);
/// ```
///
/// Generates:
///
/// ```rust,ignore
/// #[napi]
/// pub fn add(args: Vec<napi::Either<&JsExpression, f64>>) -> JsExpression {
///     let exprs: Vec<Expression> = args
///         .into_iter()
///         .map(|x| match x { /* ... */ })
///         .collect();
///     JsExpression {
///         inner: Expression::function("add", exprs),
///     }
/// }
/// ```
#[proc_macro]
pub fn generate_nodejs_variadic_binding(input: TokenStream) -> TokenStream {
    let name = parse_macro_input!(input as Ident);
    let name_str = name.to_string();

    let expanded = quote! {
        #[napi_derive::napi]
        pub fn #name(args: Vec<napi::bindgen_prelude::Either<&crate::JsExpression, f64>>) -> crate::JsExpression {
            use mathhook_core::Expression;

            let exprs: Vec<Expression> = args
                .into_iter()
                .map(|x| match x {
                    napi::bindgen_prelude::Either::A(e) => e.inner.clone(),
                    napi::bindgen_prelude::Either::B(num) => {
                        if num.fract() == 0.0 && num.is_finite() {
                            Expression::integer(num as i64)
                        } else {
                            Expression::float(num)
                        }
                    }
                })
                .collect();

            crate::JsExpression {
                inner: Expression::function(#name_str, exprs),
            }
        }
    };

    TokenStream::from(expanded)
}

/// Generate Node.js (NAPI) binding for a zero-argument constant function
///
/// # Arguments
///
/// * `name` - The constant name (e.g., `pi`)
///
/// # Examples
///
/// ```rust,ignore
/// generate_nodejs_constant_binding!(pi);
/// ```
///
/// Generates:
///
/// ```rust,ignore
/// #[napi]
/// pub fn pi() -> JsExpression {
///     JsExpression {
///         inner: Expression::pi(),
///     }
/// }
/// ```
#[proc_macro]
pub fn generate_nodejs_constant_binding(input: TokenStream) -> TokenStream {
    let name = parse_macro_input!(input as Ident);

    let expanded = quote! {
        #[napi_derive::napi]
        pub fn #name() -> crate::JsExpression {
            use mathhook_core::Expression;
            crate::JsExpression {
                inner: Expression::#name(),
            }
        }
    };

    TokenStream::from(expanded)
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_macros_module_compiles() {
        println!("All macro definitions compiled successfully");
    }
}