ruchy 4.2.0

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
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
# Ruchy Language Features - Complete Reference

## Table of Contents
- [Language Features]#language-features
- [CLI Commands]#cli-commands
- [WASM Support]#wasm-support
- [Notebook System]#notebook-system
- [Testing Framework]#testing-framework
- [Quality Engineering]#quality-engineering
- [Standard Library]#standard-library

## Language Features

### Core Syntax
```ruchy
// Variables and Constants
let x = 42
let mut y = 10
const PI = 3.14159

// Functions
fun add(a, b) {
    return a + b
}

// Fat arrow functions
let double = x => x * 2
let sum = (a, b) => a + b

// Async functions
async fun fetch_data(url) {
    let response = await http.get(url)
    return response.data
}
```

### Type System
```ruchy
// Type annotations
let x: Int = 42
let name: String = "Ruchy"
let numbers: Vec<Int> = [1, 2, 3]
let maybe: Option<String> = Some("value")

// Generic functions
fun identity<T>(x: T) -> T {
    return x
}

// Type aliases
type Point = { x: Float, y: Float }
type Result<T> = Option<T>
```

### Control Flow
```ruchy
// If expressions
let result = if x > 10 { "big" } else { "small" }

// Pattern matching
match value {
    Some(x) => println(f"Got {x}"),
    None => println("Nothing"),
    _ => println("Unknown")
}

// Pattern guards
match x {
    n if n > 0 => "positive",
    n if n < 0 => "negative",
    _ => "zero"
}

// Loops
for i in 0..10 {
    println(i)
}

while condition {
    // body
}

loop {
    if done { break }
}
```

### Error Handling
```ruchy
// Try-catch-finally
try {
    risky_operation()
} catch (e) {
    println(f"Error: {e}")
} finally {
    cleanup()
}

// Result type
fun divide(a, b) -> Result<Float> {
    if b == 0 {
        return Err("Division by zero")
    }
    return Ok(a / b)
}

// Panic for unrecoverable errors
panic!("This should never happen")
```

### Data Structures
```ruchy
// Arrays
let arr = [1, 2, 3, 4, 5]
let first = arr[0]
let slice = arr[1..3]

// Tuples
let point = (10, 20)
let (x, y) = point  // Destructuring

// Objects/Records
let person = {
    name: "Alice",
    age: 30,
    greet: fun() { println(f"Hello, I'm {this.name}") }
}

// DataFrames (with dataframe feature)
let df = df![
    "name" => ["Alice", "Bob"],
    "age" => [30, 25]
]
```

### Functional Programming
```ruchy
// Higher-order functions
let doubled = [1, 2, 3].map(x => x * 2)
let evens = [1, 2, 3, 4].filter(x => x % 2 == 0)
let sum = [1, 2, 3].reduce(0, (acc, x) => acc + x)

// Pipeline operator
let result = data
    |> filter(x => x > 0)
    |> map(x => x * 2)
    |> reduce(0, +)

// Partial application
let add5 = add(5, _)
let result = add5(10)  // 15

// Function composition
let process = compose(validate, transform, save)
```

### String Interpolation
```ruchy
let name = "World"
let greeting = f"Hello, {name}!"
let calculation = f"2 + 2 = {2 + 2}"

// Format specifiers
let pi = 3.14159
let formatted = f"Pi: {pi:.2}"  // "Pi: 3.14"
```

### Module System
```ruchy
// Importing modules
import std.io
import math.{sin, cos, PI}
from collections import HashMap

// Exporting from modules
export fun public_function() { }
export let constant = 42
export { helper1, helper2 }

// Module aliases
import very.long.module.path as short
```

### Macros and Metaprogramming
```ruchy
// DataFrame macro
let data = df![
    "column1" => [1, 2, 3],
    "column2" => ["a", "b", "c"]
]

// Custom macros (coming soon)
macro_rules! assert_eq {
    ($left, $right) => {
        if $left != $right {
            panic!(f"Assertion failed: {$left} != {$right}")
        }
    }
}
```

## CLI Commands

### Core Commands
```bash
# REPL - Interactive shell
ruchy repl
  --verbose    # Enable verbose output
  --quiet      # Suppress output

# Run - Execute scripts
ruchy run script.ruchy
  --verbose    # Show execution details

# Format - Code formatting
ruchy fmt path/
  --check      # Check without modifying
```

### WebAssembly Commands
```bash
# Compile to WASM
ruchy wasm compile input.ruchy
  -o, --output <file>    # Output file (default: input.wasm)
  --optimize             # Enable optimizations
  --validate             # Validate output (default: true)

# Validate WASM module
ruchy wasm validate module.wasm

# Run WASM module (coming soon)
ruchy wasm run module.wasm [args...]
```

### Notebook Commands
```bash
# Start notebook server
ruchy notebook serve
  -p, --port <port>      # Port number (default: 8888)
  --host <host>          # Host address (default: 127.0.0.1)

# Test notebooks
ruchy notebook test notebook.ipynb
  --coverage             # Generate coverage report
  --format <fmt>         # Output format: text, json, html

# Convert notebooks
ruchy notebook convert input.ipynb output.html
  --format <fmt>         # Output format: html, markdown, script
```

### Testing Commands
```bash
# Run tests
ruchy test run path/
  --coverage             # Generate coverage report
  --parallel             # Run tests in parallel
  --filter <pattern>     # Filter tests by name

# Generate test report
ruchy test report
  --format <fmt>         # Format: json, html, junit
  -o, --output <file>    # Output file
```

## WASM Support

### Compilation Features
- **Deterministic compilation**: Same source always produces identical bytecode
- **Validation**: Built-in wasmparser validation
- **Optimization**: Optional optimization passes
- **Security**: Sandboxed execution environment

### Supported WASM Features
- Functions with parameters and return values
- Local variables
- Basic arithmetic operations
- Control flow (if/else, loops)
- Memory operations
- Module exports

### Example WASM Compilation
```ruchy
// input.ruchy
fun fibonacci(n) {
    if n <= 1 {
        return n
    }
    return fibonacci(n - 1) + fibonacci(n - 2)
}

fun main() {
    return fibonacci(10)
}
```

```bash
# Compile to WASM
ruchy wasm compile input.ruchy -o fib.wasm --optimize

# Validate the module
ruchy wasm validate fib.wasm
# Output: ✓ WASM module is valid
```

## Notebook System

### Features
- **Jupyter-compatible**: Works with .ipynb files
- **Web interface**: Professional notebook UI
- **Cell execution**: Run cells individually or all at once
- **State management**: Persistent state between cells
- **Testing framework**: Comprehensive notebook testing

### Notebook Testing
```bash
# Test a notebook
ruchy notebook test my_notebook.ipynb --coverage

# Output:
# ✓ Cell 1: Passed
# ✓ Cell 2: Passed  
# ✓ Cell 3: Passed
# Coverage: 85%
```

### Testing Framework Features
- **Golden file testing**: Compare against expected outputs
- **Property-based testing**: Random input generation
- **Differential testing**: Compare implementations
- **Mutation testing**: Code mutation detection
- **Performance testing**: Regression detection

## Testing Framework

### Test Types
1. **Unit Tests**: Function-level testing
2. **Integration Tests**: Component interaction
3. **Property Tests**: Invariant verification
4. **Fuzz Tests**: Random input testing
5. **Acceptance Tests**: End-to-end validation

### Property Testing
```rust
// Example property test
proptest! {
    #[test]
    fn prop_compilation_deterministic(x in 0i32..100) {
        let source = format!("fun main() {{ return {} }}", x);
        let result1 = compile(&source);
        let result2 = compile(&source);
        prop_assert_eq!(result1, result2);
    }
}
```

### Fuzz Testing
```bash
# Run fuzz tests
cargo fuzz run wasm_comprehensive
cargo fuzz run wasm_security  
cargo fuzz run wasm_stress
```

## Quality Engineering

### PMAT Integration
- **TDG Score**: Technical Debt Grading (must be A- or higher)
- **Complexity Limits**: All functions <10 cyclomatic complexity
- **SATD Detection**: Zero tolerance for technical debt comments
- **Documentation Coverage**: >70% for public APIs

### Quality Gates
```bash
# Check quality before commit
pmat tdg . --min-grade A-
pmat analyze complexity --max-cyclomatic 10
pmat analyze satd --fail-on-violation
```

### Continuous Monitoring
```bash
# Start real-time dashboard
pmat tdg dashboard --port 8080 --open
```

## Standard Library

### Core Modules
- **std.io**: Input/output operations
- **std.fs**: File system operations
- **std.net**: Networking
- **std.math**: Mathematical functions
- **std.collections**: Data structures
- **std.string**: String utilities
- **std.regex**: Regular expressions
- **std.json**: JSON parsing/serialization

### Math Functions
```ruchy
import math

let x = math.sqrt(16)      // 4
let y = math.pow(2, 8)      // 256
let z = math.sin(math.PI)   // ~0
let a = math.abs(-42)       // 42
let b = math.floor(3.7)     // 3
let c = math.ceil(3.2)      // 4
let d = math.round(3.5)     // 4
let e = math.min(5, 3)      // 3
let f = math.max(5, 3)      // 5
```

### String Methods
```ruchy
let s = "Hello, World!"

s.len()                    // 13
s.to_upper()              // "HELLO, WORLD!"
s.to_lower()              // "hello, world!"
s.trim()                  // Remove whitespace
s.split(",")              // ["Hello", " World!"]
s.replace("World", "Ruchy") // "Hello, Ruchy!"
s.contains("World")       // true
s.starts_with("Hello")    // true
s.ends_with("!")          // true
s[0..5]                   // "Hello" (slicing)
```

### Collection Methods
```ruchy
let arr = [1, 2, 3, 4, 5]

arr.len()                 // 5
arr.push(6)              // [1, 2, 3, 4, 5, 6]
arr.pop()                // 5, arr = [1, 2, 3, 4]
arr.first()              // 1
arr.last()               // 5
arr.reverse()            // [5, 4, 3, 2, 1]
arr.sort()               // [1, 2, 3, 4, 5]
arr.contains(3)          // true
arr.index_of(3)          // 2
arr.slice(1, 3)          // [2, 3]
```

### Type Conversions
```ruchy
// String conversions
let s = 42.to_string()           // "42"
let n = "42".parse_int()         // 42
let f = "3.14".parse_float()     // 3.14

// Numeric conversions
let i = 3.14.to_int()            // 3
let f = 42.to_float()            // 42.0

// Boolean conversions
let b = 1.to_bool()              // true
let b = 0.to_bool()              // false
```

## Configuration

### Project Configuration (ruchy.toml)
```toml
[project]
name = "my-project"
version = "0.1.0"
authors = ["Your Name"]

[dependencies]
std = "1.0"
dataframe = { version = "0.5", optional = true }

[features]
default = ["batteries-included"]
batteries-included = ["dataframe", "notebook", "wasm-compile"]
minimal = []

[quality]
max-complexity = 10
min-coverage = 80
tdg-min-grade = "A-"
```

### Environment Variables
```bash
RUCHY_PATH=/usr/local/lib/ruchy    # Module search path
RUCHY_HOME=~/.ruchy                # Configuration directory
RUCHY_DEBUG=1                       # Enable debug output
RUCHY_COLORS=1                      # Enable colored output
```

## Platform Support

### Operating Systems
- Linux (x86_64, aarch64)
- macOS (x86_64, arm64)
- Windows (x86_64)
- FreeBSD (x86_64)

### Architectures
- x86_64
- aarch64/arm64
- armv7
- wasm32

### Minimum Requirements
- Rust 1.75+
- 2GB RAM
- 100MB disk space

## Performance

### Benchmarks
- **Parser**: ~1M lines/second
- **Type checking**: ~500K lines/second
- **Code generation**: ~300K lines/second
- **WASM compilation**: ~100K lines/second

### Optimization Levels
- **Debug**: No optimizations, full debug info
- **Release**: Standard optimizations
- **Release with LTO**: Link-time optimization
- **Release with PGO**: Profile-guided optimization

## Integration

### Editor Support
- **VS Code**: Official extension available
- **Vim/Neovim**: LSP support
- **Emacs**: LSP support
- **IntelliJ**: Plugin in development

### Build Systems
- **Cargo**: Native integration
- **Make**: Makefile templates
- **CMake**: CMake modules
- **Bazel**: Build rules

### CI/CD
- **GitHub Actions**: Workflows provided
- **GitLab CI**: Pipeline templates
- **Jenkins**: Jenkinsfile examples
- **CircleCI**: Config examples

## Resources

### Documentation
- [API Documentation]https://docs.rs/ruchy
- [Language Specification]SPECIFICATION.md
- [Tutorial]https://ruchy-lang.org/tutorial

### Community
- [GitHub Repository]https://github.com/paiml/ruchy
- [GitHub Issues]https://github.com/paiml/ruchy/issues

### Learning Resources
- [Examples Directory]https://github.com/paiml/ruchy/tree/main/examples
- [Language Completeness Documentation]../docs/lang-completeness-book/

---

*Last updated: September 2025 - Version 3.0.3*