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
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
//! WGSL code generation
//!
//! Generates WGSL shader code from Windjammer AST.

use crate::codegen::backend::{CodegenBackend, CodegenConfig, CodegenOutput, Target};
use crate::parser::{
    BinaryOp, Expression, FunctionDecl, Item, Literal, Pattern, Program, Statement,
};
use crate::parser_impl::StructDecl;
use anyhow::Result;

use super::structs::StructLayout;
use super::types::map_type_to_wgsl;
use super::validation::validate_for_gpu;

pub struct WgslBackend;

impl Default for WgslBackend {
    fn default() -> Self {
        Self::new()
    }
}

impl WgslBackend {
    pub fn new() -> Self {
        WgslBackend
    }

    fn generate_struct(&self, struct_decl: &StructDecl) -> Result<String> {
        // Calculate layout with automatic padding
        let layout = StructLayout::from_struct_decl(struct_decl)?;

        // Generate WGSL with padding fields included
        let mut output = layout.to_wgsl_string();
        output.push('\n');

        Ok(output)
    }

    fn generate_extern_let(
        &self,
        name: &str,
        type_: &crate::parser::ast::types::Type,
        decorators: &[crate::parser::Decorator],
        _is_pub: bool,
    ) -> Result<String> {
        let mut output = String::new();

        // Generate decorators (@group, @binding)
        for decorator in decorators {
            // Skip storage-class decorators (@uniform, @storage) - handled below
            if decorator.name == "uniform" || decorator.name == "storage" {
                continue;
            }

            output.push('@');
            output.push_str(&decorator.name);

            if !decorator.arguments.is_empty() {
                output.push('(');
                for (i, (key, value)) in decorator.arguments.iter().enumerate() {
                    if i > 0 {
                        output.push_str(", ");
                    }

                    if !key.is_empty() {
                        output.push_str(key);
                        output.push_str(" = ");
                    }

                    output.push_str(&self.generate_expression(value)?);
                }
                output.push(')');
            }

            output.push('\n');
        }

        // Determine storage class from decorators
        let is_uniform = decorators.iter().any(|d| d.name == "uniform");

        let storage_class = if is_uniform {
            "uniform"
        } else if let Some(storage_dec) = decorators.iter().find(|d| d.name == "storage") {
            // @storage(read_write) or @storage(read)
            if let Some((_, value)) = storage_dec.arguments.first() {
                // Extract the access mode from expression
                match self.generate_expression(value)?.as_str() {
                    "read_write" => "storage, read_write",
                    "read" => "storage, read",
                    _ => "storage",
                }
            } else {
                "storage"
            }
        } else {
            // Default to uniform if no storage decorator
            "uniform"
        };

        // Generate: var<uniform> name: Type;
        output.push_str("var<");
        output.push_str(storage_class);
        output.push_str("> ");
        output.push_str(name);
        output.push_str(": ");

        let mut wgsl_type = map_type_to_wgsl(type_)?;

        // CRITICAL FIX: Convert u32 → f32 in uniform buffers!
        // This prevents the black screen bug where host sends f32 but shader expects u32
        if is_uniform {
            let original_type = wgsl_type.clone();
            wgsl_type = wgsl_type.to_uniform_safe_type();

            // Add comment if type was converted
            if original_type != wgsl_type {
                output.push_str("/* auto-converted from ");
                output.push_str(&original_type.to_wgsl_string());
                output.push_str(" */ ");
            }
        }

        output.push_str(&wgsl_type.to_wgsl_string());

        output.push_str(";\n");

        Ok(output)
    }

    fn generate_function(&self, func: &FunctionDecl) -> Result<String> {
        let mut output = String::new();

        // Process GPU decorators
        for decorator in &func.decorators {
            match decorator.name.as_str() {
                "compute" => {
                    output.push_str("@compute");

                    // Extract workgroup_size argument
                    if let Some((_key, Expression::Array { elements, .. })) = decorator
                        .arguments
                        .iter()
                        .find(|(k, _)| k == "workgroup_size")
                    {
                        if elements.len() == 3 {
                            output.push_str(" @workgroup_size(");
                            for (i, elem) in elements.iter().enumerate() {
                                if i > 0 {
                                    output.push_str(", ");
                                }
                                output.push_str(&self.generate_expression(elem)?);
                            }
                            output.push(')');
                        }
                    }
                    output.push('\n');
                }
                "vertex" => {
                    output.push_str("@vertex\n");
                }
                "fragment" => {
                    output.push_str("@fragment\n");
                }
                _ => {} // Ignore other decorators
            }
        }

        // Function signature
        output.push_str("fn ");
        output.push_str(&func.name);
        output.push('(');

        // Parameters
        for (i, param) in func.parameters.iter().enumerate() {
            if i > 0 {
                output.push_str(", ");
            }

            // Generate parameter decorators (@builtin, @location, etc.)
            for decorator in &param.decorators {
                output.push('@');
                output.push_str(&decorator.name);

                // Generate decorator arguments
                if !decorator.arguments.is_empty() {
                    output.push('(');
                    for (j, (key, value)) in decorator.arguments.iter().enumerate() {
                        if j > 0 {
                            output.push_str(", ");
                        }

                        if !key.is_empty() {
                            // Named argument: key = value
                            output.push_str(key);
                            output.push_str(" = ");
                        }

                        // Generate value expression
                        output.push_str(&self.generate_expression(value)?);
                    }
                    output.push(')');
                }

                output.push(' ');
            }

            output.push_str(&param.name);
            output.push_str(": ");

            let wgsl_type = map_type_to_wgsl(&param.type_)?;
            output.push_str(&wgsl_type.to_wgsl_string());
        }

        output.push(')');

        // Return type
        if let Some(ref return_type) = func.return_type {
            output.push_str(" -> ");
            let wgsl_type = map_type_to_wgsl(return_type)?;
            output.push_str(&wgsl_type.to_wgsl_string());
        }

        output.push_str(" {\n");

        // Function body
        let body_len = func.body.len();
        for (i, stmt) in func.body.iter().enumerate() {
            let is_last = i == body_len - 1;

            // WGSL requires explicit returns, so convert last expression to return
            if is_last && matches!(stmt, Statement::Expression { .. }) {
                if let Statement::Expression { expr, .. } = stmt {
                    output.push_str("    return ");
                    output.push_str(&self.generate_expression(expr)?);
                    output.push_str(";\n");
                    continue;
                }
            }

            output.push_str(&self.generate_statement(stmt, 1)?);
        }

        output.push_str("}\n");

        Ok(output)
    }

    fn generate_statement(&self, stmt: &Statement, indent: usize) -> Result<String> {
        let indent_str = "    ".repeat(indent);
        let mut output = String::new();

        match stmt {
            Statement::Let {
                pattern,
                value,
                mutable,
                type_,
                ..
            } => {
                output.push_str(&indent_str);

                // WGSL uses 'var' for mutable locals, 'let' for immutable
                if *mutable {
                    output.push_str("var ");
                } else {
                    output.push_str("let ");
                }

                // Extract variable name from pattern
                let var_name = match pattern {
                    Pattern::Identifier(name) => name.clone(),
                    _ => "tmp".to_string(), // TODO: Handle complex patterns
                };

                output.push_str(&var_name);

                // Add type annotation if present (required for var without initializer)
                if let Some(type_annotation) = type_ {
                    output.push_str(": ");
                    let wgsl_type = map_type_to_wgsl(type_annotation)?;
                    output.push_str(&wgsl_type.to_wgsl_string());
                }

                output.push_str(" = ");
                output.push_str(&self.generate_expression(value)?);
                output.push_str(";\n");
            }

            Statement::Return { value, .. } => {
                output.push_str(&indent_str);
                output.push_str("return");
                if let Some(expr) = value {
                    output.push(' ');
                    output.push_str(&self.generate_expression(expr)?);
                }
                output.push_str(";\n");
            }

            Statement::Expression { expr, .. } => {
                output.push_str(&indent_str);
                output.push_str(&self.generate_expression(expr)?);
                output.push_str(";\n");
            }

            Statement::Assignment {
                target,
                value,
                compound_op,
                ..
            } => {
                output.push_str(&indent_str);
                output.push_str(&self.generate_expression(target)?);

                if let Some(op) = compound_op {
                    // Compound assignment: +=, -=, *=, /=, |=, &=, etc.
                    use crate::parser::CompoundOp;
                    let op_str = match op {
                        CompoundOp::Add => " += ",
                        CompoundOp::Sub => " -= ",
                        CompoundOp::Mul => " *= ",
                        CompoundOp::Div => " /= ",
                        CompoundOp::Mod => " %= ",
                        CompoundOp::BitAnd => " &= ",
                        CompoundOp::BitOr => " |= ",
                        CompoundOp::BitXor => " ^= ",
                        CompoundOp::Shl => " <<= ",
                        CompoundOp::Shr => " >>= ",
                    };
                    output.push_str(op_str);
                } else {
                    output.push_str(" = ");
                }

                output.push_str(&self.generate_expression(value)?);
                output.push_str(";\n");
            }

            Statement::If {
                condition,
                then_block,
                else_block,
                ..
            } => {
                output.push_str(&indent_str);
                output.push_str("if (");
                output.push_str(&self.generate_expression(condition)?);
                output.push_str(") {\n");

                for stmt in then_block {
                    output.push_str(&self.generate_statement(stmt, indent + 1)?);
                }

                output.push_str(&indent_str);
                output.push('}');

                if let Some(else_stmts) = else_block {
                    output.push_str(" else {\n");
                    for stmt in else_stmts {
                        output.push_str(&self.generate_statement(stmt, indent + 1)?);
                    }
                    output.push_str(&indent_str);
                    output.push('}');
                }

                output.push('\n');
            }

            Statement::While {
                condition, body, ..
            } => {
                output.push_str(&indent_str);
                output.push_str("while (");
                output.push_str(&self.generate_expression(condition)?);
                output.push_str(") {\n");

                for stmt in body {
                    output.push_str(&self.generate_statement(stmt, indent + 1)?);
                }

                output.push_str(&indent_str);
                output.push_str("}\n");
            }

            _ => {
                // TODO: Implement other statement types
            }
        }

        Ok(output)
    }

    fn generate_expression(&self, expr: &Expression) -> Result<String> {
        match expr {
            Expression::Identifier { name, .. } => Ok(name.clone()),

            Expression::Literal { value, .. } => match value {
                Literal::Int(n) | Literal::IntSuffixed(n, _) => Ok(n.to_string()),
                Literal::Float(f) => Ok(f.to_string()),
                Literal::Bool(b) => Ok(b.to_string()),
                Literal::String(s) => Ok(format!("\"{}\"", s)),
                Literal::Char(c) => Ok(format!("'{}'", c)),
            },

            Expression::Binary {
                left, op, right, ..
            } => {
                let left_str = self.generate_expression(left)?;
                let right_str = self.generate_expression(right)?;
                let op_str = match op {
                    BinaryOp::Add => "+",
                    BinaryOp::Sub => "-",
                    BinaryOp::Mul => "*",
                    BinaryOp::Div => "/",
                    BinaryOp::Mod => "%",
                    BinaryOp::Eq => "==",
                    BinaryOp::Ne => "!=",
                    BinaryOp::Lt => "<",
                    BinaryOp::Le => "<=",
                    BinaryOp::Gt => ">",
                    BinaryOp::Ge => ">=",
                    BinaryOp::And => "&&",
                    BinaryOp::Or => "||",
                    BinaryOp::BitAnd => "&",
                    BinaryOp::BitOr => "|",
                    BinaryOp::BitXor => "^",
                    BinaryOp::Shl => "<<",
                    BinaryOp::Shr => ">>",
                };
                Ok(format!("({} {} {})", left_str, op_str, right_str))
            }

            Expression::Call {
                function,
                arguments,
                ..
            } => {
                let mut output = String::new();

                // Get function name
                if let Expression::Identifier { name, .. } = &**function {
                    output.push_str(name);
                } else {
                    output.push_str(&self.generate_expression(function)?);
                }

                output.push('(');

                for (i, (_, arg)) in arguments.iter().enumerate() {
                    if i > 0 {
                        output.push_str(", ");
                    }
                    output.push_str(&self.generate_expression(arg)?);
                }

                output.push(')');
                Ok(output)
            }

            Expression::FieldAccess { object, field, .. } => {
                let object_str = self.generate_expression(object)?;
                Ok(format!("{}.{}", object_str, field))
            }

            Expression::Index { object, index, .. } => {
                let object_str = self.generate_expression(object)?;
                let index_str = self.generate_expression(index)?;
                Ok(format!("{}[{}]", object_str, index_str))
            }

            Expression::Cast { expr, type_, .. } => {
                // Type cast: float(x) or uint(x)
                let wgsl_type = map_type_to_wgsl(type_)?;
                let type_str = wgsl_type.to_wgsl_string();
                let expr_str = self.generate_expression(expr)?;
                Ok(format!("{}({})", type_str, expr_str))
            }

            Expression::Unary { op, operand, .. } => {
                use crate::parser::UnaryOp;
                let op_str = match op {
                    UnaryOp::Neg => "-",
                    UnaryOp::Not => "!",
                    UnaryOp::Ref | UnaryOp::MutRef | UnaryOp::Deref => {
                        // Not supported in WGSL
                        return Ok("/* Unsupported unary op */".to_string());
                    }
                };
                let operand_str = self.generate_expression(operand)?;
                Ok(format!("{}{}", op_str, operand_str))
            }

            Expression::StructLiteral { name, fields, .. } => {
                // Struct literal: Output { position: vec4(...), color: vec4(...) }
                let mut output = String::new();
                output.push_str(name);
                output.push('(');

                for (i, (_field_name, field_expr)) in fields.iter().enumerate() {
                    if i > 0 {
                        output.push_str(", ");
                    }
                    output.push_str(&self.generate_expression(field_expr)?);
                }

                output.push(')');
                Ok(output)
            }

            _ => {
                // TODO: Implement other expression types
                Ok("/* TODO */".to_string())
            }
        }
    }
}

impl CodegenBackend for WgslBackend {
    fn name(&self) -> &str {
        "WGSL"
    }

    fn target(&self) -> Target {
        Target::Wgsl
    }

    fn generate(&self, program: &Program, _config: &CodegenConfig) -> Result<CodegenOutput> {
        // Validate program can be compiled to GPU
        validate_for_gpu(program)?;

        let mut output = String::new();

        // Generate all items (structs first, then globals, then functions)
        for item in &program.items {
            if let Item::Struct { decl, .. } = item {
                output.push_str(&self.generate_struct(decl)?);
                output.push('\n');
            }
        }

        for item in &program.items {
            if let Item::ExternLet {
                name,
                type_,
                decorators,
                is_pub,
                ..
            } = item
            {
                output.push_str(&self.generate_extern_let(name, type_, decorators, *is_pub)?);
                output.push('\n');
            }
        }

        for item in &program.items {
            if let Item::Function { decl, .. } = item {
                output.push_str(&self.generate_function(decl)?);
                output.push('\n');
            }
        }

        Ok(CodegenOutput::new(output, "wgsl".to_string()))
    }
}

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

    #[test]
    fn test_wgsl_backend_exists() {
        let backend = WgslBackend::new();
        assert_eq!(backend.name(), "WGSL");
        assert_eq!(backend.target(), Target::Wgsl);
    }
}