windjammer 0.48.0

A simple language inspired by Go, Ruby, and Elixir that transpiles to Rust - 80% of Rust's power with 20% of the complexity
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
# Error Mapping System Design

## Problem Statement

**Current Issue**: Users write Windjammer code but receive Rust compiler errors pointing to generated Rust code.

**Example**:
```windjammer
// user.wj:10
fn greet(name: string) {
    println!("Hello, ${name}!")
}
```

**Current Error**:
```
error[E0308]: mismatched types
 --> output/user.rs:3
  |
3 | fn greet(name: &String) {
  |                ^^^^^^^ expected `&str`, found `&String`
```

**Problem**: User doesn't know what `output/user.rs:3` means in terms of their Windjammer code!

---

## Solution: Three-Part System

### Part 1: Source Maps

**Concept**: Map every line in generated Rust back to source Windjammer line.

**Implementation**:
```rust
// In codegen.rs
struct SourceMap {
    mappings: Vec<Mapping>,
}

struct Mapping {
    rust_file: String,
    rust_line: usize,
    wj_file: String,
    wj_line: usize,
    wj_column: usize,
}
```

**Generation**:
```rust
impl CodeGenerator {
    fn generate_function(&mut self, func: &Function) {
        // Track source location
        self.add_mapping(
            rust_line: self.current_rust_line,
            wj_file: func.source_file,
            wj_line: func.source_line,
        );
        
        // Generate code...
    }
}
```

**Output**: `output/source_map.json`
```json
{
  "mappings": [
    {
      "rust_file": "output/user.rs",
      "rust_line": 3,
      "wj_file": "user.wj",
      "wj_line": 10,
      "wj_column": 1
    }
  ]
}
```

---

### Part 2: Error Interception & Translation

**Concept**: Intercept Rust compiler output and translate it to Windjammer locations.

**Architecture**:
```
Windjammer Build Process:
1. Transpile .wj → .rs (generate source map)
2. Run `cargo build --message-format=json`
3. Intercept JSON error messages
4. Translate using source map
5. Display Windjammer-friendly errors
```

**Implementation**:
```rust
// src/error_translator.rs
pub struct ErrorTranslator {
    source_map: SourceMap,
}

impl ErrorTranslator {
    pub fn translate(&self, rust_error: RustError) -> WindjammerError {
        // Look up Windjammer location
        let wj_location = self.source_map.lookup(
            rust_error.file,
            rust_error.line
        )?;
        
        // Translate error message
        let wj_message = self.translate_message(rust_error.message);
        
        WindjammerError {
            file: wj_location.file,
            line: wj_location.line,
            column: wj_location.column,
            message: wj_message,
            severity: rust_error.severity,
        }
    }
    
    fn translate_message(&self, rust_msg: &str) -> String {
        // Replace Rust terminology with Windjammer terminology
        rust_msg
            .replace("&String", "borrowed string")
            .replace("&str", "string slice")
            .replace("&i64", "borrowed int")
            // ... more translations
    }
}
```

---

### Part 3: User-Friendly Error Display

**Concept**: Show errors in Windjammer context with helpful suggestions.

**Example Translation**:

**Rust Error**:
```
error[E0308]: mismatched types
 --> output/user.rs:3
  |
3 | fn greet(name: &String) {
  |                ^^^^^^^ expected `&str`, found `&String`
```

**Windjammer Error**:
```
error: type mismatch
  --> user.wj:10:13
   |
10 | fn greet(name: string) {
   |             ^^^^^^ expected string slice, found borrowed string
   |
help: Windjammer inferred this as a borrowed parameter.
      The function is trying to use it as a string slice.
      
suggestion: Consider using string slice operations:
   |
10 | fn greet(name: string) {
11 |     println!("Hello, {}!", name.as_str())
   |
```

---

## Error Message Translation Dictionary

### Rust → Windjammer Type Names

| Rust Type | Windjammer Display |
|-----------|-------------------|
| `&String` | `borrowed string` or `&string` |
| `&str` | `string slice` |
| `&i64` | `borrowed int` or `&int` |
| `&mut T` | `mutable borrow of T` |
| `Vec<T>` | `Vec<T>` (same) |
| `Option<T>` | `Option<T>` (same) |
| `Result<T, E>` | `Result<T, E>` (same) |

### Common Error Patterns

**Pattern 1: Type Mismatch**
```
Rust: expected `&i64`, found integer
Windjammer: expected borrowed int, found int value
Suggestion: The compiler inferred this parameter needs borrowing. Pass &value instead.
```

**Pattern 2: Lifetime Issues**
```
Rust: borrowed value does not live long enough
Windjammer: value doesn't live long enough
Explanation: This value is borrowed but goes out of scope before the borrow ends.
Suggestion: Consider cloning the value or restructuring ownership.
```

**Pattern 3: Mutability Issues**
```
Rust: cannot borrow as mutable
Windjammer: cannot modify this value
Explanation: The compiler inferred this as immutable. Use 'let mut' to make it mutable.
```

---

## Implementation Plan

### Phase 1: Basic Source Mapping
- [ ] Add source location tracking to AST nodes
- [ ] Generate line mappings during code generation
- [ ] Write source map to JSON file
- [ ] Create lookup utility

**Files to Modify**:
- `src/parser.rs` - Add source locations to AST
- `src/codegen.rs` - Track line mappings
- Create `src/source_map.rs`

### Phase 2: Error Interception
- [ ] Capture `cargo build` JSON output
- [ ] Parse Rust compiler messages
- [ ] Look up source locations
- [ ] Basic error translation

**Files to Create**:
- `src/error_translator.rs`
- Update `src/main.rs` to use error translator

### Phase 3: Error Translation
- [ ] Build Rust→Windjammer terminology dictionary
- [ ] Translate common error patterns
- [ ] Add helpful suggestions
- [ ] Context-aware messages

### Phase 4: Enhanced Display
- [ ] Colorized output
- [ ] Code snippets with highlighting
- [ ] Multi-line context
- [ ] Inline suggestions

---

## Example Implementation

### 1. Add Source Locations to AST

```rust
// src/parser.rs
#[derive(Debug, Clone)]
pub struct SourceLocation {
    pub file: String,
    pub line: usize,
    pub column: usize,
}

#[derive(Debug, Clone)]
pub struct FunctionDecl {
    pub name: String,
    pub parameters: Vec<Parameter>,
    pub return_type: Option<Type>,
    pub body: Vec<Statement>,
    pub location: SourceLocation,  // Add this
}
```

### 2. Track During Code Generation

```rust
// src/codegen.rs
impl CodeGenerator {
    fn generate_function(&mut self, func: &FunctionDecl) {
        let rust_line = self.current_line;
        
        // Record mapping
        self.source_map.add_mapping(Mapping {
            rust_file: self.output_file.clone(),
            rust_line,
            wj_file: func.location.file.clone(),
            wj_line: func.location.line,
            wj_column: func.location.column,
        });
        
        // Generate code...
        let output = format!("fn {}(", func.name);
        self.emit_line(&output);
    }
    
    fn emit_line(&mut self, line: &str) {
        self.output.push_str(line);
        self.output.push('\n');
        self.current_line += 1;
    }
}
```

### 3. Intercept and Translate Errors

```rust
// src/main.rs
fn build_and_report_errors(files: Vec<String>) -> Result<(), Error> {
    // 1. Transpile
    let (rust_files, source_map) = transpile(files)?;
    
    // 2. Run cargo with JSON output
    let output = Command::new("cargo")
        .args(&["build", "--message-format=json"])
        .current_dir("output")
        .output()?;
    
    // 3. Parse and translate errors
    let translator = ErrorTranslator::new(source_map);
    let rust_errors = parse_cargo_output(&output.stdout)?;
    
    for rust_error in rust_errors {
        let wj_error = translator.translate(rust_error)?;
        display_error(&wj_error);
    }
    
    Ok(())
}
```

---

## Error Display Format

### Standard Format
```
error: <short description>
  --> <file>:<line>:<column>
   |
<line_num> | <code line>
           | <highlight>
           |
help: <suggestion>
```

### Example
```
error: type mismatch in function call
  --> main.wj:15:21
   |
15 |     let result = double(5)
   |                         ^ expected &int, found int
   |
help: function 'double' expects a borrowed int
      
suggestion: pass a reference:
   |
15 |     let result = double(&5)
   |                         +
```

---

## Benefits

**For Users**:
- ✅ Errors point to their actual code
- ✅ Understandable terminology
- ✅ Helpful suggestions
- ✅ Learn Windjammer, not Rust internals

**For Debugging**:
- ✅ Source maps enable debugging tools
- ✅ Stack traces map correctly
- ✅ IDE integration possible

**For Adoption**:
- ✅ Better developer experience
- ✅ Lower learning curve
- ✅ More professional tooling

---

## Challenges

**Challenge 1: Mapping Accuracy**
- Generated code may not be 1:1 with source
- Macros and transformations complicate mapping

**Solution**: Track at statement level, not just line level

**Challenge 2: Error Message Quality**
- Rust errors are complex
- Hard to translate all cases

**Solution**: Start with common errors, iteratively improve

**Challenge 3: Performance**
- Adding mapping overhead
- Error translation takes time

**Solution**: Only generate maps in debug mode, cache translations

---

## Future Enhancements

### Phase 5: IDE Integration
- [ ] LSP error reporting
- [ ] Inline error display
- [ ] Quick fixes

### Phase 6: Advanced Features
- [ ] Rust panic traces → Windjammer stack traces
- [ ] Debugger integration (LLDB/GDB)
- [ ] Performance profiler integration

---

## Success Metrics

**Must Have (v0.2)**:
- ✅ Basic source mapping working
- ✅ Errors point to Windjammer files
- ✅ Common error patterns translated

**Should Have (v0.3)**:
- ✅ Helpful suggestions
- ✅ Context-aware messages
- ✅ Colorized output

**Nice to Have (v1.0)**:
- ✅ IDE integration
- ✅ Stack trace mapping
- ✅ Debugger integration

---

*Status: Design Complete*  
*Priority: P0 (Critical for usability)*  
*Estimated Effort: 1-2 weeks*  
*Next Step: Implement Phase 1 (source mapping)*