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
//! Centralized type classification for the Windjammer compiler.
//!
//! This module is the **single source of truth** for classifying type names,
//! trait names, constructor names, and container types. ALL code that needs
//! to know about type categories MUST query this module instead of scattering
//! hardcoded `matches!("i8" | "i16" | ...)` across the codebase.
//!
//! For method-level classification, see `method_registry`.

// =============================================================================
// Primitive Type Classification
// =============================================================================

/// Integer types recognized by the Windjammer compiler.
pub fn is_integer_type(name: &str) -> bool {
    matches!(
        name,
        "i8" | "i16"
            | "i32"
            | "i64"
            | "i128"
            | "isize"
            | "u8"
            | "u16"
            | "u32"
            | "u64"
            | "u128"
            | "usize"
    )
}

/// Float types recognized by the Windjammer compiler.
pub fn is_float_type(name: &str) -> bool {
    matches!(name, "f32" | "f64")
}

/// All numeric types (integer + float).
pub fn is_numeric_type(name: &str) -> bool {
    is_integer_type(name) || is_float_type(name)
}

/// Primitive types that implement `Copy` (numeric + bool + char).
pub fn is_copy_primitive(name: &str) -> bool {
    is_numeric_type(name) || matches!(name, "bool" | "char")
}

/// Valid numeric literal suffixes in the lexer.
pub fn is_numeric_suffix(name: &str) -> bool {
    matches!(
        name,
        "u64" | "i64" | "u32" | "i32" | "u16" | "i16" | "u8" | "i8" | "usize" | "isize"
    )
}

/// Types that never need `use` imports (prelude, primitives, or synthesized).
pub fn is_prelude_or_primitive(name: &str) -> bool {
    is_copy_primitive(name) || matches!(name, "str" | "string" | "String" | "Self" | "self" | "()")
}

// =============================================================================
// Copy Type Classification (Structural)
// =============================================================================

/// Determine if a `Type` tree is Copy given sets of known Copy structs/enums.
/// This is the canonical implementation used by both single-file and library
/// compilation for registry-building passes.
pub fn is_type_copy_with_registries(
    ty: &crate::parser::ast::types::Type,
    copy_structs: &std::collections::HashSet<String>,
    copy_enums: &std::collections::HashSet<String>,
) -> bool {
    use crate::parser::ast::types::Type;
    match ty {
        Type::Int | Type::Int32 | Type::Uint | Type::Float | Type::Bool => true,
        Type::Reference(_) => true,
        Type::MutableReference(_) => false,
        Type::Tuple(types) => types
            .iter()
            .all(|t| is_type_copy_with_registries(t, copy_structs, copy_enums)),
        Type::Option(inner) => is_type_copy_with_registries(inner, copy_structs, copy_enums),
        Type::Result(ok, err) => {
            is_type_copy_with_registries(ok, copy_structs, copy_enums)
                && is_type_copy_with_registries(err, copy_structs, copy_enums)
        }
        Type::Array(inner, _) => is_type_copy_with_registries(inner, copy_structs, copy_enums),
        Type::Vec(_) | Type::String => false,
        Type::RawPointer { pointee, .. } => {
            is_type_copy_with_registries(pointee.as_ref(), copy_structs, copy_enums)
        }
        Type::FunctionPointer { .. } => true,
        Type::Custom(name) => {
            copy_structs.contains(name) || copy_enums.contains(name) || is_copy_primitive(name)
        }
        _ => false,
    }
}

// =============================================================================
// Container / Stdlib Type Classification
// =============================================================================

/// Standard library generic containers — types that don't need `use` imports
/// and whose inner type arguments should be recursed into.
pub fn is_stdlib_container(name: &str) -> bool {
    matches!(
        name,
        "Vec"
            | "Option"
            | "Result"
            | "HashMap"
            | "HashSet"
            | "BTreeMap"
            | "BTreeSet"
            | "Box"
            | "Arc"
            | "Rc"
            | "RefCell"
            | "Cell"
            | "Mutex"
            | "RwLock"
            | "Weak"
            | "Pin"
            | "PhantomData"
            | "NonNull"
            | "VecDeque"
            | "BinaryHeap"
            | "LinkedList"
            | "SmallVec"
            | "Cow"
            | "Iter"
            | "Slice"
            | "Signal"
    )
}

/// Returns true if the given `Type` is a stdlib container or wrapper.
/// Used by mutation detection to decide whether known-mutating heuristics
/// (like `push`, `insert`, `clear`) should apply even when a qualified
/// registry lookup missed.
pub fn is_stdlib_collection_or_wrapper(ty: &crate::parser::ast::types::Type) -> bool {
    use crate::parser::ast::types::Type;
    match ty {
        Type::Vec(_) | Type::Option(_) | Type::Result(_, _) | Type::String => true,
        Type::Parameterized(name, _) | Type::Custom(name) => is_stdlib_container(name),
        Type::Array(_, _) => true,
        _ => false,
    }
}

/// Large collection types (high heap allocation cost, never Copy).
pub fn is_large_collection(name: &str) -> bool {
    matches!(
        name,
        "HashMap" | "BTreeMap" | "HashSet" | "BTreeSet" | "IndexMap"
    )
}

/// Medium-sized collection types.
pub fn is_medium_collection(name: &str) -> bool {
    matches!(name, "Vec" | "VecDeque" | "LinkedList")
}

/// Map/associative container types.
pub fn is_map_type(name: &str) -> bool {
    matches!(name, "HashMap" | "BTreeMap" | "IndexMap" | "Map")
}

/// Heap-owning types that are never Copy.
pub fn is_heap_container(name: &str) -> bool {
    matches!(name, "Vec" | "HashMap" | "String")
}

// =============================================================================
// Drop / Lifetime Classification
// =============================================================================

/// Types with important `Drop` semantics (not safe to defer dropping).
pub fn has_significant_drop(name: &str) -> bool {
    matches!(
        name,
        "Mutex"
            | "RwLock"
            | "File"
            | "TcpStream"
            | "UdpSocket"
            | "Channel"
            | "Receiver"
            | "Sender"
            | "JoinHandle"
            | "MutexGuard"
            | "RwLockReadGuard"
            | "RwLockWriteGuard"
    )
}

// =============================================================================
// Trait Classification
// =============================================================================

/// Operator traits that consume `self` (owned receiver).
pub fn is_consuming_operator_trait(name: &str) -> bool {
    let base = name.rsplit("::").next().unwrap_or(name);
    matches!(
        base,
        "Add"
            | "Sub"
            | "Mul"
            | "Div"
            | "Rem"
            | "Neg"
            | "Not"
            | "BitAnd"
            | "BitOr"
            | "BitXor"
            | "Shl"
            | "Shr"
    )
}

/// Conversion traits that consume `self` (owned receiver).
pub fn is_consuming_conversion_trait(name: &str) -> bool {
    let base = name.rsplit("::").next().unwrap_or(name);
    matches!(base, "Into" | "From" | "TryInto" | "TryFrom")
}

/// All traits where `self` should be owned (operators + conversions).
pub fn is_owned_self_trait(name: &str) -> bool {
    is_consuming_operator_trait(name) || is_consuming_conversion_trait(name)
}

/// Derive-style traits that use `&self` (borrowed receiver).
pub fn is_ref_receiver_trait(name: &str) -> bool {
    let base = name.rsplit("::").next().unwrap_or(name);
    matches!(
        base,
        "Display"
            | "Debug"
            | "Hash"
            | "PartialEq"
            | "Eq"
            | "PartialOrd"
            | "Ord"
            | "Clone"
            | "Copy"
            | "Default"
            | "Iterator"
            | "IntoIterator"
            | "AsRef"
            | "Deref"
    )
}

// =============================================================================
// Constructor / Factory Names
// =============================================================================

/// Common constructor / factory method names (no `self` receiver).
pub fn is_constructor_name(name: &str) -> bool {
    matches!(
        name,
        "new"
            | "default"
            | "from"
            | "from_str"
            | "from_bytes"
            | "with_capacity"
            | "empty"
            | "zero"
            | "one"
    )
}

// =============================================================================
// Method Classification (ownership-producing methods)
// =============================================================================

/// Methods that produce an owned value regardless of receiver ownership.
pub fn is_ownership_producing_method(name: &str) -> bool {
    matches!(name, "clone" | "to_owned" | "to_string" | "into_iter")
}

/// Methods that operate on float receivers and whose arguments should
/// match the receiver's float type.
pub fn is_float_receiver_method(name: &str) -> bool {
    matches!(
        name,
        "clamp"
            | "max"
            | "min"
            | "abs"
            | "copysign"
            | "recip"
            | "to_degrees"
            | "to_radians"
            | "signum"
            | "powf"
            | "powi"
            | "sqrt"
            | "cbrt"
            | "log"
            | "log2"
            | "log10"
            | "exp"
            | "exp2"
            | "sin"
            | "cos"
            | "tan"
            | "asin"
            | "acos"
            | "atan"
            | "atan2"
            | "sinh"
            | "cosh"
            | "tanh"
            | "ceil"
            | "floor"
            | "round"
            | "fract"
            | "trunc"
            | "hypot"
            | "mul_add"
            | "ln"
            | "fma"
    )
}

// =============================================================================
// Rust-to-Windjammer Type Mapping
// =============================================================================

/// Map a Rust type name to a Windjammer type name (for diagnostics).
pub fn rust_type_to_windjammer(rust_type: &str) -> &str {
    match rust_type {
        "i32" | "i64" | "isize" => "int",
        "u32" | "u64" | "usize" => "uint",
        "f32" | "f64" => "float",
        "&str" | "String" => "string",
        "bool" => "bool",
        "()" => "void",
        other => other,
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_integer_types() {
        assert!(is_integer_type("i32"));
        assert!(is_integer_type("usize"));
        assert!(!is_integer_type("f32"));
        assert!(!is_integer_type("bool"));
        assert!(!is_integer_type("String"));
    }

    #[test]
    fn test_copy_primitives() {
        assert!(is_copy_primitive("i32"));
        assert!(is_copy_primitive("f64"));
        assert!(is_copy_primitive("bool"));
        assert!(is_copy_primitive("char"));
        assert!(!is_copy_primitive("String"));
        assert!(!is_copy_primitive("Vec"));
    }

    #[test]
    fn test_stdlib_containers() {
        assert!(is_stdlib_container("Vec"));
        assert!(is_stdlib_container("HashMap"));
        assert!(is_stdlib_container("Option"));
        assert!(!is_stdlib_container("MyStruct"));
    }

    #[test]
    fn test_trait_classification() {
        assert!(is_consuming_operator_trait("Add"));
        assert!(is_consuming_operator_trait("std::ops::Sub"));
        assert!(!is_consuming_operator_trait("Display"));

        assert!(is_ref_receiver_trait("Debug"));
        assert!(is_ref_receiver_trait("Clone"));
        assert!(!is_ref_receiver_trait("Add"));

        assert!(is_owned_self_trait("Into"));
        assert!(is_owned_self_trait("Add"));
        assert!(!is_owned_self_trait("Display"));
    }

    #[test]
    fn test_constructor_names() {
        assert!(is_constructor_name("new"));
        assert!(is_constructor_name("default"));
        assert!(is_constructor_name("from"));
        assert!(!is_constructor_name("update"));
    }

    #[test]
    fn test_prelude_or_primitive() {
        assert!(is_prelude_or_primitive("i32"));
        assert!(is_prelude_or_primitive("String"));
        assert!(is_prelude_or_primitive("Self"));
        assert!(!is_prelude_or_primitive("MyType"));
    }

    #[test]
    fn test_float_methods() {
        assert!(is_float_receiver_method("clamp"));
        assert!(is_float_receiver_method("sin"));
        assert!(!is_float_receiver_method("push"));
    }

    #[test]
    fn test_significant_drop() {
        assert!(has_significant_drop("Mutex"));
        assert!(has_significant_drop("File"));
        assert!(!has_significant_drop("Vec"));
    }
}