lambdust 0.1.0

A Rust implementation of R7RS Scheme interpreter for embedding in applications
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
# Lambdust Bridge API Documentation

## Overview

The Lambdust Bridge API provides a seamless interface for integrating the Lambdust Scheme interpreter with external Rust applications. This allows applications to:

- Expose Rust functions to Scheme code
- Register external objects for manipulation from Scheme
- Convert between Rust types and Scheme values
- Execute Scheme code with access to application data and functions

## Core Components

### LambdustBridge

The main entry point for integrating Lambdust with your application.

```rust
use lambdust::LambdustBridge;

let mut bridge = LambdustBridge::new();
```

### Key Methods

#### Function Registration

```rust
// Register a simple function
bridge.register_function("add-one", Some(1), |args| {
    let n = i64::from_scheme(&args[0])?;
    (n + 1).to_scheme()
});

// Register a variadic function
bridge.register_function("sum", None, |args| {
    let mut total = 0i64;
    for arg in args {
        total += i64::from_scheme(arg)?;
    }
    total.to_scheme()
});
```

#### Object Registration

```rust
// Register an external object
let my_object = MyStruct::new();
let object_id = bridge.register_object(my_object, "MyStruct");
```

#### Variable Definition

```rust
// Define Scheme variables
bridge.define("pi", Value::from(3.14159));
bridge.define("app-name", Value::from("My Application"));
```

#### Code Evaluation

```rust
// Evaluate Scheme expressions
let result = bridge.eval("(+ 1 2 3)")?;
let result = bridge.eval("(call-external \"my-function\" 42)")?;

// Load and evaluate Scheme files
let result = bridge.load_file("script.scm")?;
```

## Type Conversion

### ToScheme and FromScheme Traits

These traits handle conversion between Rust types and Scheme values.

#### Built-in Implementations

```rust
// Rust to Scheme
let scheme_int = 42i64.to_scheme()?;        // -> Value::Number(Integer(42))
let scheme_float = 3.14f64.to_scheme()?;    // -> Value::Number(Real(3.14))
let scheme_bool = true.to_scheme()?;        // -> Value::Boolean(true)
let scheme_string = "hello".to_scheme()?;   // -> Value::String("hello")

// Scheme to Rust
let rust_int = i64::from_scheme(&scheme_value)?;
let rust_float = f64::from_scheme(&scheme_value)?;
let rust_bool = bool::from_scheme(&scheme_value)?;
let rust_string = String::from_scheme(&scheme_value)?;
```

#### Custom Type Conversion

```rust
#[derive(Debug, Clone)]
struct Point {
    x: f64,
    y: f64,
}

impl ToScheme for Point {
    fn to_scheme(&self) -> Result<Value> {
        // Convert to a list: (x y)
        Ok(Value::from_vector(vec![
            self.x.to_scheme()?,
            self.y.to_scheme()?,
        ]))
    }
}

impl FromScheme for Point {
    fn from_scheme(value: &Value) -> Result<Self> {
        if let Some(vec) = value.to_vector() {
            if vec.len() == 2 {
                return Ok(Point {
                    x: f64::from_scheme(&vec[0])?,
                    y: f64::from_scheme(&vec[1])?,
                });
            }
        }
        Err(LambdustError::TypeError("Expected point as (x y)".to_string()))
    }
}
```

## Scheme Interface

### Built-in Bridge Functions

From Scheme code, you can access external functionality through these functions:

#### call-external

Call registered external functions:

```scheme
;; Call a function with arguments
(call-external "function-name" arg1 arg2 ...)

;; Examples
(call-external "square" 5)                    ; -> 25
(call-external "string-upper" "hello")        ; -> "HELLO"
(call-external "make-user" "Alice" 30 "alice@example.com")
```

#### get-property

Access properties of external objects:

```scheme
;; Get object property
(get-property object-id "property-name")

;; Example
(get-property user-id "name")                 ; -> "Alice"
```

#### set-property!

Modify properties of external objects:

```scheme
;; Set object property
(set-property! object-id "property-name" new-value)

;; Example
(set-property! user-id "age" 31)
```

## Application Integration Protocol

### 1. Basic Setup

```rust
use lambdust::{LambdustBridge, Value, Result};

fn main() -> Result<()> {
    let mut bridge = LambdustBridge::new();
    
    // Register your functions and objects here
    setup_bridge(&mut bridge);
    
    // Execute Scheme code
    let result = bridge.eval("(main-script)")?;
    
    Ok(())
}

fn setup_bridge(bridge: &mut LambdustBridge) {
    // Register functions
    bridge.register_function("app-log", Some(1), |args| {
        let message = String::from_scheme(&args[0])?;
        println!("App Log: {}", message);
        Ok(Value::Undefined)
    });
    
    // Define constants
    bridge.define("app-version", Value::from("1.0.0"));
}
```

### 2. Error Handling

```rust
// External functions should return Result<Value>
bridge.register_function("divide", Some(2), |args| {
    let a = f64::from_scheme(&args[0])?;
    let b = f64::from_scheme(&args[1])?;
    
    if b == 0.0 {
        return Err(LambdustError::DivisionByZero);
    }
    
    (a / b).to_scheme()
});
```

### 3. State Management

```rust
use std::sync::{Arc, Mutex};

#[derive(Debug)]
struct AppState {
    counter: i32,
    users: Vec<String>,
}

fn setup_stateful_bridge(bridge: &mut LambdustBridge) {
    let state = Arc::new(Mutex::new(AppState {
        counter: 0,
        users: Vec::new(),
    }));
    
    // Increment counter
    let state_clone = state.clone();
    bridge.register_function("increment", Some(0), move |_args| {
        let mut state = state_clone.lock().unwrap();
        state.counter += 1;
        state.counter.to_scheme()
    });
    
    // Add user
    let state_clone = state.clone();
    bridge.register_function("add-user", Some(1), move |args| {
        let name = String::from_scheme(&args[0])?;
        let mut state = state_clone.lock().unwrap();
        state.users.push(name);
        (state.users.len() as i64).to_scheme()
    });
}
```

### 4. Complex Object Interaction

```rust
#[derive(Debug)]
struct Database {
    connections: HashMap<String, String>,
}

impl Database {
    fn connect(&mut self, name: &str, url: &str) -> Result<()> {
        self.connections.insert(name.to_string(), url.to_string());
        Ok(())
    }
    
    fn query(&self, conn_name: &str, sql: &str) -> Result<Vec<String>> {
        // Simulate database query
        Ok(vec![format!("Result for: {}", sql)])
    }
}

// Register database operations
fn setup_database_bridge(bridge: &mut LambdustBridge, db: Arc<Mutex<Database>>) {
    let db_clone = db.clone();
    bridge.register_function("db-connect", Some(2), move |args| {
        let name = String::from_scheme(&args[0])?;
        let url = String::from_scheme(&args[1])?;
        
        let mut db = db_clone.lock().unwrap();
        db.connect(&name, &url)?;
        
        Ok(Value::Boolean(true))
    });
    
    let db_clone = db.clone();
    bridge.register_function("db-query", Some(2), move |args| {
        let conn_name = String::from_scheme(&args[0])?;
        let sql = String::from_scheme(&args[1])?;
        
        let db = db_clone.lock().unwrap();
        let results = db.query(&conn_name, &sql)?;
        
        // Convert results to Scheme list
        let scheme_results: Result<Vec<Value>> = results
            .into_iter()
            .map(|s| s.to_scheme())
            .collect();
            
        Ok(Value::from_vector(scheme_results?))
    });
}
```

## Example Scheme Scripts

### Basic Usage

```scheme
;; Define helper functions
(define (square x) (* x x))
(define (cube x) (* x x x))

;; Use external functions
(define result (call-external "factorial" 5))
(call-external "app-log" (string-append "Factorial result: " (number->string result)))

;; Work with application state
(call-external "increment")
(call-external "add-user" "Alice")
(call-external "add-user" "Bob")
```

### Database Operations

```scheme
;; Connect to database
(call-external "db-connect" "main" "sqlite:///app.db")

;; Define query helper
(define (query-users)
  (call-external "db-query" "main" "SELECT * FROM users"))

;; Process results
(define users (query-users))
(define user-count (length users))
(call-external "app-log" 
  (string-append "Found " (number->string user-count) " users"))
```

### Configuration and Control

```scheme
;; Application configuration
(define config 
  '((debug-mode #t)
    (max-connections 100)
    (timeout 30)))

;; Apply configuration
(define (apply-config cfg)
  (cond 
    ((null? cfg) #t)
    (else 
      (let ((setting (car cfg)))
        (call-external "set-config" 
          (symbol->string (car setting))
          (cadr setting))
        (apply-config (cdr cfg))))))

(apply-config config)
```

## Best Practices

### 1. Error Handling
- Always return `Result<Value>` from external functions
- Use appropriate `LambdustError` types for different error conditions
- Provide meaningful error messages

### 2. Type Safety
- Implement `ToScheme` and `FromScheme` for custom types
- Validate argument types in external functions
- Use type predicates in Scheme code when needed

### 3. Performance
- Minimize data copying between Rust and Scheme
- Use object IDs for large or complex objects
- Cache frequently accessed data

### 4. Memory Management
- External objects are reference-counted automatically
- Be careful with circular references
- Clean up resources in object destructors

### 5. Threading
- The bridge is not thread-safe by default
- Use `Arc<Mutex<>>` for shared state
- Consider using channels for async operations

## Security Considerations

- Validate all inputs from Scheme code
- Limit access to sensitive functions
- Sandbox Scheme execution if needed
- Be careful with file system and network access

## Examples

See the `examples/` directory for complete working examples:
- `bridge_example.rs` - Basic bridge usage
- `database_example.rs` - Database integration
- `gui_example.rs` - GUI application integration
- `plugin_system.rs` - Plugin architecture example