ruchy 4.1.1

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
565
566
567
568
569
570
571
572
573
574
575
576
577
# Ruchy REPL Guide

*Self-Hosting Edition - Updated for v1.5.0 Historic Achievement*

## 🎉 SELF-HOSTING REPL

**Ruchy REPL now supports self-hosting development!** You can write compiler code in Ruchy and execute it directly in the REPL.

## 🚀 Quick Start - The Golden Path

The Ruchy REPL is your interactive playground for learning and experimenting with the language. Like Elixir's IEx or Julia's REPL, it provides immediate feedback and is the best way to learn Ruchy.

### Start the REPL

```bash
cargo run -- repl
# Or if installed: ruchy repl
```

You'll see:
```
Welcome to Ruchy REPL v1.5.0 - Self-Hosting Edition
Type :help for commands, :quit to exit
Self-hosting compiler ready!

ruchy> 
```

## 📚 Essential Examples That Work Today

### 1. Basic Arithmetic

```ruchy
ruchy> 1 + 2
3

ruchy> 10 * 5
50

ruchy> 100 / 4
25

ruchy> 7 % 3
1

ruchy> 2 ** 8
256
```

### 2. Variables and Bindings

```ruchy
ruchy> let x = 10
10

ruchy> let y = 20
20

ruchy> x + y
30

ruchy> let message = "Hello"
"Hello"

ruchy> let pi = 3.14159
3.14159
```

### 3. String Operations

```ruchy
ruchy> "Hello" + " World"
"Hello World"

ruchy> let name = "Ruchy"
"Ruchy"

ruchy> let greeting = "Welcome to " + name
"Welcome to Ruchy"
```

### 4. Printing Output

```ruchy
ruchy> println("Hello, World!")
Hello, World!
()

ruchy> print("Loading")
Loading()

ruchy> println("The answer is", 42)
The answer is 42
()

ruchy> let x = 100
100
ruchy> println("x =", x)
x = 100
()
```

### 5. Boolean Logic

```ruchy
ruchy> true
true

ruchy> false
false

ruchy> true && false
false

ruchy> true || false
true

ruchy> !true
false

ruchy> 5 > 3
true

ruchy> 10 == 10
true

ruchy> "hello" == "hello"
true
```

### 6. Conditional Expressions

```ruchy
ruchy> if true { 1 } else { 2 }
1

ruchy> let age = 18
18

ruchy> if age >= 18 { "adult" } else { "minor" }
"adult"

ruchy> let score = 85
85

ruchy> if score > 90 { "A" } else { if score > 80 { "B" } else { "C" } }
"B"
```

### 7. Lists and Collections

```ruchy
ruchy> [1, 2, 3]
1

ruchy> let nums = [10, 20, 30]
10

ruchy> []
()
```

### 8. Blocks and Compound Expressions

```ruchy
ruchy> { 
    let a = 5;
    let b = 10;
    a + b
}
15

ruchy> {
    println("Computing...");
    42
}
Computing...
42
```

### 9. Pattern Matching

```ruchy
ruchy> match 5 {
    0 => "zero",
    1 => "one",
    _ => "other"
}
"other"

ruchy> let x = 2
2

ruchy> match x {
    1 | 2 | 3 => "small",
    _ => "big"
}
"small"
```

### 10. Functions (Basic Definition)

```ruchy
ruchy> fun add(a: i32, b: i32) -> i32 { a + b }
"fn add(a, b)"

ruchy> fun greet(name: String) { println("Hello", name) }
"fn greet(name)"
```

### 11. Lambda Expressions (Both Syntaxes)

```ruchy
ruchy> |x| x + 1
"|x| <body>"

ruchy> x => x + 1
"x => <body>"

ruchy> |x, y| x * y
"|x, y| <body>"

ruchy> (x, y) => x * y  
"(x, y) => <body>"
```

### 12. Self-Hosting Compiler Development

```ruchy
ruchy> struct RuchyCompiler {
    input: String
}

ruchy> impl RuchyCompiler {
    fn tokenize(&self) -> Vec<String> {
        self.input.split_whitespace().map(|s| s.to_string()).collect()
    }
    
    fn parse(&self, tokens: Vec<String>) -> String {
        tokens.join(" -> ")
    }
}

ruchy> let compiler = RuchyCompiler { 
    input: "let x = 42".to_string() 
}

ruchy> let tokens = compiler.tokenize()
ruchy> let ast = compiler.parse(tokens)
ruchy> println("Self-hosting AST: {}", ast)
Self-hosting AST: let -> x -> = -> 42
```

### 13. Range Expressions

```ruchy
ruchy> 0..10
"0..10"

ruchy> 1..5
"1..5"
```

## 🔧 REPL Commands

### Information Commands

```ruchy
:help              # Show available commands
:history           # Show command history
:bindings          # Show current variable bindings
```

### Session Management

```ruchy
:clear             # Clear all bindings and start fresh
:quit or :q        # Exit the REPL
```

### Development Commands

```ruchy
:compile           # Compile current session to Rust
:transpile         # Transpile session with --minimal flag for self-hosting
:load <file>       # Load and execute a .ruchy file
:bootstrap         # Test bootstrap compilation cycle
```

## 📋 Currently Supported Grammar

### ✅ Working Features

| Feature | Example | Output |
|---------|---------|--------|
| **Integers** | `42` | `42` |
| **Floats** | `3.14` | `3.14` |
| **Strings** | `"hello"` | `"hello"` |
| **Booleans** | `true`, `false` | `true`, `false` |
| **Arithmetic** | `1 + 2 * 3` | `7` |
| **Comparisons** | `5 > 3` | `true` |
| **Logic** | `true && false` | `false` |
| **Variables** | `let x = 10` | `10` |
| **If/Else** | `if x > 0 { "pos" } else { "neg" }` | `"pos"` |
| **Blocks** | `{ let x = 1; x + 1 }` | `2` |
| **Match** | `match x { 1 => "one", _ => "other" }` | Result varies |
| **Functions** | `fun f(x: i32) { x }` | Function stored |
| **Lambdas (Pipe)** | `\|x\| x * 2` | Lambda stored |
| **Lambdas (Arrow)** | `x => x * 2` | Lambda stored |
| **Self-Hosting** | `struct Compiler { ... }` | Self-hosting support |
| **Printing** | `println("Hi")` | Prints `Hi` |
| **String Concat** | `"a" + "b"` | `"ab"` |

### ⚠️ Partially Working

| Feature | Issue | Workaround |
|---------|-------|------------|
| **Mixed arithmetic** | `3.14 * 2` fails (type mismatch) | Use same types: `3.14 * 2.0` |
| **Function calls** | User functions not callable yet | Use built-ins like `println` |
| **For loops** | Not implemented in evaluator | Use recursion or match |
| **List operations** | Limited evaluation | Lists define but don't operate |

### 🚧 Not Yet Implemented

- Async/await expressions
- Actor system (`!` and `?` operators)
- DataFrame operations
- Method calls on objects
- Import statements
- Type annotations in let bindings
- Complex pattern matching
- List comprehensions
- Try/catch blocks

## 💡 Tips and Tricks

### 1. Multi-line Input
The REPL supports multi-line expressions. Just keep typing:

```ruchy
ruchy> if true {
    println("This is");
    println("multi-line");
    42
}
This is
multi-line
42
```

### 2. Expression Values
Everything is an expression and returns a value:

```ruchy
ruchy> let result = if 5 > 3 { "yes" } else { "no" }
"yes"

ruchy> let computation = {
    let x = 10;
    let y = 20;
    x + y
}
30
```

### 3. Debugging with Print
Use `println` liberally to understand what's happening:

```ruchy
ruchy> {
    let x = 5;
    println("x is", x);
    let y = x * 2;
    println("y is", y);
    x + y
}
x is 5
y is 10
15
```

### 4. Type Exploration
The REPL shows you the type of expressions through their values:

```ruchy
ruchy> 42
42                    # Integer

ruchy> 3.14
3.14                  # Float

ruchy> "text"
"text"                # String

ruchy> true
true                  # Boolean

ruchy> ()
()                    # Unit type

ruchy> [1, 2, 3]
1                     # List (shows first element)
```

## 🎯 Common Patterns

### Calculator Mode
```ruchy
ruchy> let tax_rate = 0.08
0.08

ruchy> let price = 100
100

ruchy> let tax = price * tax_rate
Error: Type mismatch    # Oops! Need same types

ruchy> let price = 100.0
100.0

ruchy> let tax = price * tax_rate
8.0

ruchy> let total = price + tax
108.0

ruchy> println("Total with tax:", total)
Total with tax: 108.0
()
```

### Decision Making
```ruchy
ruchy> let score = 75
75

ruchy> let grade = if score >= 90 {
    "A"
} else { if score >= 80 {
    "B"
} else { if score >= 70 {
    "C"
} else {
    "F"
}}}
"C"

ruchy> println("Your grade:", grade)
Your grade: C
()
```

### Building Up Computations
```ruchy
ruchy> let base = 100
100

ruchy> let bonus = 20
20

ruchy> let penalty = 5
5

ruchy> let final_score = base + bonus - penalty
115

ruchy> println("Final score:", final_score)
Final score: 115
()
```

## 🔍 Troubleshooting

### Common Errors and Solutions

**Type Mismatch**
```ruchy
ruchy> 3.14 * 2
Error: Type mismatch in binary operation

# Solution: Use consistent types
ruchy> 3.14 * 2.0
6.28
```

**Undefined Variable**
```ruchy
ruchy> x + 1
Error: Undefined variable: x

# Solution: Define the variable first
ruchy> let x = 10
10
ruchy> x + 1
11
```

**String + Number**
```ruchy
ruchy> "The answer is " + 42
Error: Type mismatch

# Solution: Use println for mixed types
ruchy> println("The answer is", 42)
The answer is 42
()
```

## 📖 Learning Path

1. **Start Simple**: Basic arithmetic and variables
2. **Add Logic**: Boolean expressions and if/else
3. **Use Functions**: Define and understand function syntax
4. **Pattern Match**: Learn match expressions
5. **Combine**: Build larger expressions from smaller ones

## 🚦 Quick Reference Card

```ruchy
# Numbers
42, 3.14, 2 ** 8

# Strings  
"hello", "a" + "b"

# Booleans
true, false, !true, a && b, x || y

# Variables
let x = 10
let name = "Ruchy"

# Conditionals
if condition { expr1 } else { expr2 }

# Pattern Matching
match value {
    pattern1 => result1,
    pattern2 => result2,
    _ => default
}

# Functions
fun name(param: Type) -> RetType { body }

# Lambdas
|param| expression
|x, y| x + y

# Printing
println("text", value1, value2)
print("no newline")

# Blocks
{
    statement1;
    statement2;
    final_expression
}

# Commands
:help, :quit, :history, :clear, :bindings
```

## Next Steps

Once comfortable with the REPL basics:

1. Try loading example files with `:load examples/fibonacci.ruchy`
2. Experiment with self-hosting compiler development using `:bootstrap`
3. Use `:transpile` to generate minimal Rust code for self-hosting
4. Use `:compile` to see optimized generated Rust code
5. Read the [Language Specification]./SPECIFICATION.md for advanced features
6. Check docs/execution/roadmap.yaml to see what's coming next
7. Explore examples in the [examples directory]../examples/

Remember: The REPL is your friend and now supports self-hosting development! It's the fastest way to learn Ruchy and test compiler ideas. Keep it open while coding and use it to verify your understanding.