Skip to content

Commit 5a1ba37

Browse files
committed
feat: Add zrtl_math plugin and fix runtime plugin docs
- Add zrtl_math plugin with 50+ math functions: - Trigonometry (sin, cos, tan, asin, acos, atan, atan2, sinh, cosh, tanh) - Exponentials & logarithms (exp, exp2, log, log2, log10, pow, sqrt, cbrt) - Rounding (floor, ceil, round, trunc, fract, abs) - Min/max/clamp for floats and integers - Random number generation (xorshift64) - Mathematical constants (pi, e, tau, sqrt2, ln2, ln10) - Special functions (lerp, to_radians, to_degrees, is_nan, is_finite) - Fix book chapter 14 (Runtime Plugins): - Replace incorrect zrtl_io::symbols() example with correct load_plugin() API - Add step-by-step JIT runtime usage example - Add custom symbols example with with_symbols() - Document zrtl_math plugin functions - Fix markdown code block language specifiers
1 parent 8f8969a commit 5a1ba37

4 files changed

Lines changed: 711 additions & 16 deletions

File tree

book/14-runtime-plugins.md

Lines changed: 115 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The Zyntax Runtime Library (ZRTL) provides native functionality to languages bui
44

55
## Architecture
66

7-
```
7+
```text
88
┌─────────────────────────────────────────────────────────┐
99
│ Your Language │
1010
│ (Haxe, Zig, Custom DSL) │
@@ -161,6 +161,51 @@ Environment variables and process information.
161161
| `$Env$os` | `() -> StringPtr` | Get OS name |
162162
| `$Env$arch` | `() -> StringPtr` | Get CPU architecture |
163163

164+
### zrtl_math - Mathematics
165+
166+
Mathematical functions for numerical computation.
167+
168+
| Symbol | Signature | Description |
169+
|--------|-----------|-------------|
170+
| `$Math$sin` | `(f64) -> f64` | Sine (radians) |
171+
| `$Math$cos` | `(f64) -> f64` | Cosine (radians) |
172+
| `$Math$tan` | `(f64) -> f64` | Tangent (radians) |
173+
| `$Math$asin` | `(f64) -> f64` | Arcsine |
174+
| `$Math$acos` | `(f64) -> f64` | Arccosine |
175+
| `$Math$atan` | `(f64) -> f64` | Arctangent |
176+
| `$Math$atan2` | `(f64, f64) -> f64` | Two-argument arctangent |
177+
| `$Math$sinh` | `(f64) -> f64` | Hyperbolic sine |
178+
| `$Math$cosh` | `(f64) -> f64` | Hyperbolic cosine |
179+
| `$Math$tanh` | `(f64) -> f64` | Hyperbolic tangent |
180+
| `$Math$exp` | `(f64) -> f64` | e^x |
181+
| `$Math$exp2` | `(f64) -> f64` | 2^x |
182+
| `$Math$log` | `(f64) -> f64` | Natural logarithm |
183+
| `$Math$log2` | `(f64) -> f64` | Base-2 logarithm |
184+
| `$Math$log10` | `(f64) -> f64` | Base-10 logarithm |
185+
| `$Math$pow` | `(f64, f64) -> f64` | Power (x^y) |
186+
| `$Math$sqrt` | `(f64) -> f64` | Square root |
187+
| `$Math$cbrt` | `(f64) -> f64` | Cube root |
188+
| `$Math$hypot` | `(f64, f64) -> f64` | Hypotenuse |
189+
| `$Math$floor` | `(f64) -> f64` | Round down |
190+
| `$Math$ceil` | `(f64) -> f64` | Round up |
191+
| `$Math$round` | `(f64) -> f64` | Round to nearest |
192+
| `$Math$trunc` | `(f64) -> f64` | Truncate toward zero |
193+
| `$Math$abs` | `(f64) -> f64` | Absolute value (float) |
194+
| `$Math$abs_i64` | `(i64) -> i64` | Absolute value (int) |
195+
| `$Math$min` | `(f64, f64) -> f64` | Minimum |
196+
| `$Math$max` | `(f64, f64) -> f64` | Maximum |
197+
| `$Math$clamp` | `(f64, f64, f64) -> f64` | Clamp to range |
198+
| `$Math$random` | `() -> f64` | Random [0, 1) |
199+
| `$Math$random_range` | `(f64, f64) -> f64` | Random in range |
200+
| `$Math$random_int` | `(i64, i64) -> i64` | Random integer |
201+
| `$Math$seed` | `(u64) -> void` | Seed RNG |
202+
| `$Math$pi` | `() -> f64` | Pi constant |
203+
| `$Math$e` | `() -> f64` | Euler's number |
204+
| `$Math$tau` | `() -> f64` | Tau (2π) |
205+
| `$Math$lerp` | `(f64, f64, f64) -> f64` | Linear interpolation |
206+
| `$Math$to_radians` | `(f64) -> f64` | Degrees to radians |
207+
| `$Math$to_degrees` | `(f64) -> f64` | Radians to degrees |
208+
164209
## Building Plugins
165210

166211
### Dynamic Libraries (.zrtl)
@@ -171,13 +216,15 @@ cd plugins
171216
```
172217

173218
Output in `plugins/target/zrtl/`:
174-
```
219+
220+
```text
175221
zrtl_io.zrtl
176222
zrtl_fs.zrtl
177223
zrtl_time.zrtl
178224
zrtl_thread.zrtl
179225
zrtl_net.zrtl
180226
zrtl_env.zrtl
227+
zrtl_math.zrtl
181228
plugins.json
182229
```
183230

@@ -195,7 +242,8 @@ cd plugins
195242
```
196243

197244
Output in `plugins/target/static/<target>/`:
198-
```
245+
246+
```text
199247
libzrtl_io.a
200248
libzrtl_fs.a
201249
...
@@ -227,7 +275,7 @@ Map your language's standard library to ZRTL symbols:
227275

228276
### In Your Language Source
229277

230-
```
278+
```zig
231279
// Your custom language
232280
fn main() {
233281
println("Hello from ZRTL!");
@@ -239,23 +287,74 @@ fn main() {
239287
}
240288
```
241289

242-
### Loading at Runtime
290+
### Loading at Runtime (JIT)
291+
292+
ZRTL plugins are loaded at runtime using the `ZyntaxRuntime` API:
293+
294+
```rust
295+
use zyntax_embed::{ZyntaxRuntime, LanguageGrammar};
296+
297+
fn main() -> Result<(), Box<dyn std::error::Error>> {
298+
// Step 1: Create a new runtime
299+
let mut runtime = ZyntaxRuntime::new()?;
300+
301+
// Step 2: Load ZRTL plugins (dynamic libraries)
302+
// Option A: Load individual plugins
303+
runtime.load_plugin("plugins/target/zrtl/zrtl_io.zrtl")?;
304+
runtime.load_plugin("plugins/target/zrtl/zrtl_fs.zrtl")?;
305+
306+
// Option B: Load all plugins from a directory
307+
let count = runtime.load_plugins_from_directory("plugins/target/zrtl")?;
308+
println!("Loaded {} plugins", count);
309+
310+
// Step 3: Load your language grammar
311+
let grammar = LanguageGrammar::compile_zyn(include_str!("my_lang.zyn"))?;
312+
runtime.register_grammar("mylang", grammar.clone())?;
313+
runtime.map_extension("mylang", "ml")?; // .ml files use this grammar
314+
315+
// Step 4: Compile source code that uses plugin functions
316+
let source = r#"
317+
fn main() {
318+
// These calls resolve to $IO$println and $FS$read_file
319+
println("Hello from ZRTL!");
320+
let contents = readFile("data.txt");
321+
println(contents);
322+
}
323+
"#;
324+
325+
runtime.compile_source(&grammar, source)?;
326+
327+
// Step 5: Call the compiled function
328+
runtime.call::<()>("main", &[])?;
329+
330+
Ok(())
331+
}
332+
```
333+
334+
### Loading Plugins with Custom Symbols
335+
336+
For advanced use cases, you can also register external symbols directly:
243337

244338
```rust
245339
use zyntax_embed::ZyntaxRuntime;
246340

247-
// Create runtime with ZRTL symbols
248-
let symbols = zrtl_io::symbols(); // Get plugin symbols
249-
let mut runtime = ZyntaxRuntime::with_symbols(&symbols)?;
341+
// Define a native function
342+
extern "C" fn my_custom_print(msg: i64) {
343+
println!("Custom: {}", msg);
344+
}
345+
346+
fn main() -> Result<(), Box<dyn std::error::Error>> {
347+
// Create runtime with custom symbols
348+
let symbols: &[(&str, *const u8)] = &[
349+
("$Custom$print", my_custom_print as *const u8),
350+
];
351+
let mut runtime = ZyntaxRuntime::with_symbols(symbols)?;
250352

251-
// Compile and run code that uses plugin functions
252-
runtime.compile_source(r#"
253-
fn main() {
254-
println("Hello from ZRTL!");
255-
}
256-
"#)?;
353+
// Now code can call $Custom$print
354+
// ...
257355

258-
runtime.call::<()>("main", &[])?;
356+
Ok(())
357+
}
259358
```
260359

261360
### Static Linking (AOT)
@@ -371,7 +470,7 @@ The build scripts will automatically discover and build it.
371470

372471
ZRTL uses a hierarchical naming scheme:
373472

374-
```
473+
```text
375474
$Module$function_name
376475
```
377476

plugins/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ members = [
77
"zrtl_net",
88
"zrtl_time",
99
"zrtl_env",
10+
"zrtl_math",
1011
]
1112

1213
[workspace.dependencies]

plugins/zrtl_math/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "zrtl_math"
3+
version = "0.1.0"
4+
edition = "2021"
5+
description = "ZRTL Math plugin - trigonometry, exponentials, rounding, random"
6+
license = "Apache-2.0"
7+
8+
[lib]
9+
crate-type = ["cdylib", "staticlib", "rlib"]
10+
11+
[dependencies]
12+
zrtl = { path = "../../sdk/zrtl" }

0 commit comments

Comments
 (0)