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
#![cfg(any(
    not(any(
        feature = "parser_tests",
        feature = "analyzer_tests",
        feature = "codegen_tests",
        feature = "interpreter_tests",
        feature = "conformance_tests",
        feature = "integration_tests",
    )),
    feature = "conformance_tests",
))]

//! Backend Integration Conformance Tests
//!
//! Verifies that Windjammer programs produce identical output across all backends:
//! - Rust (compiled via wj build → rustc)
//! - Go (compiled via wj build → go run)
//! - JavaScript (compiled via wj build → node)
//! - Interpreter (Windjammerscript tree-walking)
//!
//! WGSL is excluded: shader-only target, no main()/println.
//!
//! Test cases live in test_cases/*.wj and cover:
//! - basic: variables, functions, structs, enums, control flow
//! - ownership: copy semantics, mutation, parameter inference
//! - patterns: match, match guards, if let
//! - traits: trait definitions, implementations, generics
//! - strings: literals, concatenation, interpolation
//! - collections: Vec push, len, indexing, iteration

use std::fs;
use std::path::Path;
use std::process::Command;
use tempfile::TempDir;

/// Result of compiling and running a .wj file on a specific backend
struct BackendResult {
    #[allow(dead_code)]
    backend: String,
    stdout: String,
    success: bool,
    error: String,
}

fn compile_and_run_rust(source: &str, test_name: &str) -> BackendResult {
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join(format!("{}.wj", test_name));
    fs::write(&test_file, source).unwrap();

    let output_dir = temp_dir.path().join("build");
    fs::create_dir_all(&output_dir).unwrap();

    let wj = Command::new(env!("CARGO_BIN_EXE_wj"))
        .arg("build")
        .arg("--target")
        .arg("rust")
        .arg("--no-cargo")
        .arg(&test_file)
        .current_dir(temp_dir.path())
        .output()
        .expect("Failed to execute wj");

    if !wj.status.success() {
        return BackendResult {
            backend: "rust".into(),
            stdout: String::new(),
            success: false,
            error: format!(
                "wj compilation failed: {}",
                String::from_utf8_lossy(&wj.stderr)
            ),
        };
    }

    let expected_rs = output_dir.join(format!("{}.rs", test_name));
    let rs_file = if expected_rs.exists() {
        expected_rs
    } else {
        fs::read_dir(&output_dir)
            .ok()
            .and_then(|dir| {
                dir.filter_map(|e| e.ok())
                    .find(|e| e.path().extension().is_some_and(|ext| ext == "rs"))
                    .map(|e| e.path())
            })
            .unwrap_or_else(|| {
                let entries: Vec<String> = fs::read_dir(&output_dir)
                    .map(|d| {
                        d.filter_map(|e| e.ok())
                            .map(|e| e.file_name().to_string_lossy().to_string())
                            .collect()
                    })
                    .unwrap_or_default();
                panic!("No .rs file in {:?}. Files: {:?}", output_dir, entries);
            })
    };

    let bin_path = temp_dir.path().join("bin");
    let rustc = Command::new("rustc")
        .arg(&rs_file)
        .arg("-o")
        .arg(&bin_path)
        .arg("--edition")
        .arg("2021")
        .output()
        .expect("Failed to execute rustc");

    if !rustc.status.success() {
        return BackendResult {
            backend: "rust".into(),
            stdout: String::new(),
            success: false,
            error: format!("rustc failed: {}", String::from_utf8_lossy(&rustc.stderr)),
        };
    }

    let run = Command::new(&bin_path)
        .output()
        .expect("Failed to run binary");

    BackendResult {
        backend: "rust".into(),
        stdout: String::from_utf8(run.stdout).unwrap_or_default(),
        success: run.status.success(),
        error: String::from_utf8(run.stderr).unwrap_or_default(),
    }
}

fn go_available() -> bool {
    Command::new("go")
        .arg("version")
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

fn compile_and_run_go(source: &str, test_name: &str) -> Option<BackendResult> {
    if !go_available() {
        return None;
    }

    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join(format!("{}.wj", test_name));
    fs::write(&test_file, source).unwrap();

    let output_dir = temp_dir.path().join("build");
    fs::create_dir_all(&output_dir).unwrap();

    let wj = Command::new(env!("CARGO_BIN_EXE_wj"))
        .arg("build")
        .arg("--target")
        .arg("go")
        .arg("--no-cargo")
        .arg(&test_file)
        .current_dir(temp_dir.path())
        .output()
        .expect("Failed to execute wj");

    if !wj.status.success() {
        return Some(BackendResult {
            backend: "go".into(),
            stdout: String::new(),
            success: false,
            error: format!(
                "wj compilation failed: {}",
                String::from_utf8_lossy(&wj.stderr)
            ),
        });
    }

    let go_file = output_dir.join("main.go");
    if !go_file.exists() {
        let entries: Vec<String> = fs::read_dir(&output_dir)
            .map(|d| {
                d.filter_map(|e| e.ok())
                    .map(|e| e.file_name().to_string_lossy().to_string())
                    .collect()
            })
            .unwrap_or_default();
        return Some(BackendResult {
            backend: "go".into(),
            stdout: String::new(),
            success: false,
            error: format!("No main.go found. Files: {:?}", entries),
        });
    }

    let run = Command::new("go")
        .arg("run")
        .arg(&go_file)
        .current_dir(&output_dir)
        .output()
        .expect("Failed to execute go run");

    Some(BackendResult {
        backend: "go".into(),
        stdout: String::from_utf8(run.stdout).unwrap_or_default(),
        success: run.status.success(),
        error: String::from_utf8(run.stderr).unwrap_or_default(),
    })
}

fn compile_and_run_js(source: &str, test_name: &str) -> BackendResult {
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join(format!("{}.wj", test_name));
    fs::write(&test_file, source).unwrap();

    let output_dir = temp_dir.path().join("build");
    fs::create_dir_all(&output_dir).unwrap();

    let wj = Command::new(env!("CARGO_BIN_EXE_wj"))
        .arg("build")
        .arg("--target")
        .arg("javascript")
        .arg("--no-cargo")
        .arg(&test_file)
        .current_dir(temp_dir.path())
        .output()
        .expect("Failed to execute wj");

    if !wj.status.success() {
        return BackendResult {
            backend: "js".into(),
            stdout: String::new(),
            success: false,
            error: format!(
                "wj compilation failed: {}",
                String::from_utf8_lossy(&wj.stderr)
            ),
        };
    }

    let js_file = output_dir.join("output.js");
    if !js_file.exists() {
        return BackendResult {
            backend: "js".into(),
            stdout: String::new(),
            success: false,
            error: "No output.js generated".into(),
        };
    }

    let js_code = fs::read_to_string(&js_file).unwrap();
    let stripped = js_code
        .replace("export function", "function")
        .replace("export class", "class")
        .replace("export const", "const")
        .replace("export let", "let");

    let stripped = if let Some(idx) = stripped.find("// Auto-run main") {
        stripped[..idx].to_string()
    } else {
        stripped
    };

    let runner = output_dir.join("_run.mjs");
    fs::write(
        &runner,
        format!("{}\nif (typeof main === 'function') main();\n", stripped),
    )
    .unwrap();

    let run = Command::new("node")
        .arg(&runner)
        .current_dir(&output_dir)
        .output()
        .expect("Failed to execute node");

    BackendResult {
        backend: "js".into(),
        stdout: String::from_utf8(run.stdout).unwrap_or_default(),
        success: run.status.success(),
        error: String::from_utf8(run.stderr).unwrap_or_default(),
    }
}

fn interpret(source: &str, test_name: &str) -> BackendResult {
    let mut lex = windjammer::lexer::Lexer::new(source);
    let tokens = lex.tokenize_with_locations();
    let mut parser = windjammer::parser::Parser::new_with_source(
        tokens,
        format!("{}.wj", test_name),
        source.to_string(),
    );
    let program = match parser.parse() {
        Ok(p) => p,
        Err(e) => {
            return BackendResult {
                backend: "interpreter".into(),
                stdout: String::new(),
                success: false,
                error: format!("Parse error: {}", e),
            };
        }
    };

    let mut interp = windjammer::interpreter::Interpreter::new_capturing();
    match interp.run(&program) {
        Ok(_) => BackendResult {
            backend: "interpreter".into(),
            stdout: interp.get_output(),
            success: true,
            error: String::new(),
        },
        Err(e) => BackendResult {
            backend: "interpreter".into(),
            stdout: interp.get_output(),
            success: false,
            error: e,
        },
    }
}

/// Run a test case across all backends, assert identical output
fn run_integration_test(test_name: &str, source: &str) {
    let rust_result = compile_and_run_rust(source, test_name);
    let go_result = compile_and_run_go(source, test_name);
    let js_result = compile_and_run_js(source, test_name);
    let interp_result = interpret(source, test_name);

    assert!(
        rust_result.success,
        "[{}] Rust failed: {}",
        test_name, rust_result.error
    );
    assert!(
        rust_result.stdout.contains("PASSED"),
        "[{}] Rust output missing PASSED: {}",
        test_name,
        rust_result.stdout
    );

    if let Some(ref go) = go_result {
        assert!(go.success, "[{}] Go failed: {}", test_name, go.error);
        assert_eq!(
            rust_result.stdout, go.stdout,
            "[{}] Rust vs Go mismatch",
            test_name
        );
    }

    assert!(
        js_result.success,
        "[{}] JS failed: {}",
        test_name, js_result.error
    );
    assert_eq!(
        rust_result.stdout, js_result.stdout,
        "[{}] Rust vs JS mismatch",
        test_name
    );

    assert!(
        interp_result.success,
        "[{}] Interpreter failed: {}",
        test_name, interp_result.error
    );
    assert_eq!(
        rust_result.stdout, interp_result.stdout,
        "[{}] Rust vs Interpreter mismatch",
        test_name
    );
}

fn load_test_case(name: &str) -> String {
    let path = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("integration_wj")
        .join("test_cases")
        .join(format!("{}.wj", name));
    fs::read_to_string(&path).unwrap_or_else(|e| panic!("Failed to read {}: {}", path.display(), e))
}

// ========== Integration Tests ==========

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_integration_basic() {
    run_integration_test("basic", &load_test_case("basic"));
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_integration_ownership() {
    run_integration_test("ownership", &load_test_case("ownership"));
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_integration_patterns() {
    run_integration_test("patterns", &load_test_case("patterns"));
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_integration_strings() {
    run_integration_test("strings", &load_test_case("strings"));
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_integration_collections() {
    run_integration_test("collections", &load_test_case("collections"));
}

/// Traits test: Rust + Interpreter only (Go/JS may not support full trait system yet)
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_integration_traits() {
    let source = load_test_case("traits");
    let rust_result = compile_and_run_rust(&source, "traits");
    let interp_result = interpret(&source, "traits");

    assert!(
        rust_result.success,
        "[traits] Rust failed: {}",
        rust_result.error
    );
    assert!(
        interp_result.success,
        "[traits] Interpreter failed: {}",
        interp_result.error
    );
    assert_eq!(
        rust_result.stdout, interp_result.stdout,
        "[traits] Rust vs Interpreter mismatch"
    );
    assert!(
        rust_result.stdout.contains("PASSED"),
        "[traits] Output missing PASSED"
    );
}