|
| 1 | +# ZRTL Plugins |
| 2 | + |
| 3 | +ZRTL (Zyntax Runtime Library) plugins provide native functionality to Zyntax programs. Each plugin exports symbols that can be called from compiled code. |
| 4 | + |
| 5 | +## Available Plugins |
| 6 | + |
| 7 | +| Plugin | Description | Key Symbols | |
| 8 | +|--------|-------------|-------------| |
| 9 | +| **zrtl_io** | Console I/O | `$IO$print`, `$IO$println`, `$IO$read_line` | |
| 10 | +| **zrtl_fs** | File system operations | `$FS$read_file`, `$FS$write_file`, `$FS$exists` | |
| 11 | +| **zrtl_time** | Time and date | `$Time$now`, `$Time$sleep`, `$Time$format` | |
| 12 | +| **zrtl_string** | String manipulation | `$String$concat`, `$String$split`, `$String$trim` | |
| 13 | +| **zrtl_math** | Math functions | `$Math$sin`, `$Math$cos`, `$Math$sqrt`, `$Math$pow` | |
| 14 | +| **zrtl_json** | JSON parsing/generation | `$JSON$parse`, `$JSON$stringify` | |
| 15 | +| **zrtl_regex** | Regular expressions | `$Regex$match`, `$Regex$replace`, `$Regex$split` | |
| 16 | +| **zrtl_net** | TCP/UDP networking | `$Net$tcp_connect`, `$Net$tcp_listen`, `$Net$udp_bind` | |
| 17 | +| **zrtl_http** | HTTP client | `$HTTP$get`, `$HTTP$post`, `$HTTP$request` | |
| 18 | +| **zrtl_websocket** | WebSocket client/server | `$WS$connect`, `$WS$send`, `$WS$on_message` | |
| 19 | +| **zrtl_thread** | Threading and sync | `$Thread$spawn`, `$Thread$join`, `$Mutex$new` | |
| 20 | +| **zrtl_process** | Process management | `$Process$spawn`, `$Process$exec`, `$Process$exit` | |
| 21 | +| **zrtl_env** | Environment variables | `$Env$get`, `$Env$set`, `$Env$vars` | |
| 22 | +| **zrtl_crypto** | Cryptography | `$Crypto$hash_sha256`, `$Crypto$aes_encrypt` | |
| 23 | +| **zrtl_compress** | Compression | `$Compress$gzip`, `$Compress$gunzip`, `$Compress$zstd` | |
| 24 | +| **zrtl_sql** | SQLite database | `$SQL$open`, `$SQL$execute`, `$SQL$query` | |
| 25 | +| **zrtl_paint** | 2D graphics (tiny-skia) | `$Paint$canvas_create`, `$Paint$fill_circle`, `$Paint$fill_rect` | |
| 26 | +| **zrtl_window** | Windowing (SDL2) | `$Window$create`, `$Window$poll_events`, `$Window$present` | |
| 27 | +| **zrtl_sdl** | Low-level SDL2 bindings | `$SDL$init`, `$SDL$create_texture`, `$SDL$render_copy` | |
| 28 | +| **zrtl_image** | Image loading | `$Image$load`, `$Image$decode`, `$Image$to_canvas` | |
| 29 | +| **zrtl_svg** | SVG rendering | `$SVG$parse`, `$SVG$render`, `$SVG$to_canvas` | |
| 30 | +| **zrtl_xml** | XML parsing | `$XML$parse`, `$XML$query`, `$XML$serialize` | |
| 31 | + |
| 32 | +## Building Plugins |
| 33 | + |
| 34 | +### Build All Plugins |
| 35 | + |
| 36 | +```bash |
| 37 | +cd plugins |
| 38 | +./build_zrtl.sh |
| 39 | +``` |
| 40 | + |
| 41 | +This creates `.zrtl` plugin files in `target/zrtl/`. |
| 42 | + |
| 43 | +### Build Individual Plugin |
| 44 | + |
| 45 | +```bash |
| 46 | +cargo build --release -p zrtl_paint |
| 47 | +``` |
| 48 | + |
| 49 | +## Using Plugins |
| 50 | + |
| 51 | +### From CLI |
| 52 | + |
| 53 | +```bash |
| 54 | +# Load plugins via --plugins flag |
| 55 | +zyntax compile --grammar my.zyn --source code.txt --plugins zrtl_io,zrtl_paint --run |
| 56 | +``` |
| 57 | + |
| 58 | +### From Rust (Embedding SDK) |
| 59 | + |
| 60 | +```rust |
| 61 | +use zyntax_embed::ZyntaxRuntime; |
| 62 | + |
| 63 | +let mut runtime = ZyntaxRuntime::new()?; |
| 64 | + |
| 65 | +// Load plugins |
| 66 | +runtime.load_plugin("plugins/target/zrtl/zrtl_io.zrtl")?; |
| 67 | +runtime.load_plugin("plugins/target/zrtl/zrtl_paint.zrtl")?; |
| 68 | + |
| 69 | +// Now compile and run code that uses $IO$ and $Paint$ symbols |
| 70 | +``` |
| 71 | + |
| 72 | +### From Grammar (DSL) |
| 73 | + |
| 74 | +Map plugin symbols to DSL builtins: |
| 75 | + |
| 76 | +```zyn |
| 77 | +@builtin { |
| 78 | + print: "$IO$println", |
| 79 | + draw_circle: "$Paint$fill_circle", |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +## Creating Custom Plugins |
| 84 | + |
| 85 | +Use the `zrtl_plugin!` macro to define a plugin: |
| 86 | + |
| 87 | +```rust |
| 88 | +use zyntax_embed::zrtl_plugin; |
| 89 | + |
| 90 | +// Define exported functions |
| 91 | +#[no_mangle] |
| 92 | +pub extern "C" fn my_function(x: i32) -> i32 { |
| 93 | + x * 2 |
| 94 | +} |
| 95 | + |
| 96 | +// Export symbols |
| 97 | +zrtl_plugin! { |
| 98 | + name: "my_plugin", |
| 99 | + version: "1.0.0", |
| 100 | + symbols: [ |
| 101 | + ("$MyPlugin$my_function", my_function), |
| 102 | + ] |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +Build as a cdylib: |
| 107 | + |
| 108 | +```toml |
| 109 | +# Cargo.toml |
| 110 | +[lib] |
| 111 | +crate-type = ["cdylib"] |
| 112 | + |
| 113 | +[dependencies] |
| 114 | +zyntax_embed = { path = "../crates/zyntax_embed" } |
| 115 | +``` |
| 116 | + |
| 117 | +See [Chapter 14: Runtime Plugins](https://github.com/darmie/zyntax/wiki/14-Runtime-Plugins) in The Zyn Book for complete documentation. |
| 118 | + |
| 119 | +## Plugin Architecture |
| 120 | + |
| 121 | +``` |
| 122 | +┌─────────────────────────────────────────────────────────┐ |
| 123 | +│ Zyntax Runtime │ |
| 124 | +├─────────────────────────────────────────────────────────┤ |
| 125 | +│ Symbol Table │ |
| 126 | +│ ┌─────────────────┬─────────────────────────────────┐ │ |
| 127 | +│ │ $IO$println │ → fn(*const u8) │ │ |
| 128 | +│ │ $Paint$canvas │ → fn(u32, u32) -> u64 │ │ |
| 129 | +│ │ $Math$sqrt │ → fn(f64) -> f64 │ │ |
| 130 | +│ └─────────────────┴─────────────────────────────────┘ │ |
| 131 | +└─────────────────────────────────────────────────────────┘ |
| 132 | + ▲ ▲ ▲ |
| 133 | + │ │ │ |
| 134 | + ┌─────┴─────┐ ┌─────┴─────┐ ┌─────┴─────┐ |
| 135 | + │ zrtl_io │ │zrtl_paint │ │ zrtl_math │ |
| 136 | + │ .zrtl │ │ .zrtl │ │ .zrtl │ |
| 137 | + └───────────┘ └───────────┘ └───────────┘ |
| 138 | +``` |
| 139 | + |
| 140 | +Plugins are loaded dynamically at runtime. Each plugin registers its symbols in the runtime's symbol table, making them available for compiled code to call. |
| 141 | + |
| 142 | +## Symbol Naming Convention |
| 143 | + |
| 144 | +All ZRTL symbols follow the pattern: `$PluginName$function_name` |
| 145 | + |
| 146 | +- `$IO$println` - Print line from IO plugin |
| 147 | +- `$Paint$fill_circle` - Fill circle from Paint plugin |
| 148 | +- `$Math$sin` - Sine function from Math plugin |
| 149 | + |
| 150 | +This namespacing prevents collisions between plugins. |
0 commit comments