mimium-lang 4.0.1

mimium(minimal-musical-medium) an infrastructural programming language for sound and music.
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/// Red Tree - Abstract Syntax Tree (AST) without trivia
/// Based on the Red-Green Syntax Tree pattern
///
/// Red nodes have absolute positions and are created from Green nodes.
/// They represent the actual AST without comments and whitespace.
/// Red nodes maintain parent references for bottom-up traversal.
use super::green::{GreenNodeArena, GreenNodeId, SyntaxKind};
use super::token::{Token, TokenKind};
use std::sync::{Arc, Weak};

/// Red node - represents an AST node with position information
/// This is the "Red" part of Red-Green Syntax Tree
#[derive(Debug, Clone)]
pub struct RedNode {
    /// The underlying green node ID
    green_id: GreenNodeId,
    /// Absolute position in the source
    offset: usize,
    /// Parent node (weak reference to avoid cycles)
    parent: Option<Weak<RedNode>>,
}

impl RedNode {
    /// Create a new red node from a green node ID
    pub fn new(green_id: GreenNodeId, offset: usize) -> Arc<Self> {
        Arc::new(RedNode {
            green_id,
            offset,
            parent: None,
        })
    }

    /// Create a new red node with a parent reference
    pub fn new_with_parent(
        green_id: GreenNodeId,
        offset: usize,
        parent: Weak<RedNode>,
    ) -> Arc<Self> {
        Arc::new(RedNode {
            green_id,
            offset,
            parent: Some(parent),
        })
    }

    /// Get the absolute position of this node
    pub fn offset(&self) -> usize {
        self.offset
    }

    /// Get the width (length) of this node
    pub fn width(&self, arena: &GreenNodeArena) -> usize {
        arena.width(self.green_id)
    }

    /// Get the end position of this node
    pub fn end(&self, arena: &GreenNodeArena) -> usize {
        self.offset + self.width(arena)
    }

    /// Get the syntax kind of this node
    pub fn kind(&self, arena: &GreenNodeArena) -> Option<SyntaxKind> {
        arena.kind(self.green_id)
    }

    /// Get the underlying green node ID
    pub fn green_id(&self) -> GreenNodeId {
        self.green_id
    }

    /// Get the parent node if it exists
    pub fn parent(&self) -> Option<Arc<RedNode>> {
        self.parent.as_ref().and_then(|weak| weak.upgrade())
    }

    /// Get children as red nodes with parent references
    pub fn children(self: &Arc<Self>, arena: &GreenNodeArena) -> Vec<Arc<RedNode>> {
        if let Some(green_children) = arena.children(self.green_id) {
            let mut offset = self.offset;

            green_children
                .iter()
                .map(|&child_id| {
                    let child = RedNode::new_with_parent(child_id, offset, Arc::downgrade(self));
                    offset += arena.width(child_id);
                    child
                })
                .collect()
        } else {
            Vec::new()
        }
    }

    /// Get ancestors of this node (bottom-up traversal)
    pub fn ancestors(&self) -> Vec<Arc<RedNode>> {
        let mut result = Vec::new();
        let mut current = self.parent();

        while let Some(node) = current {
            result.push(node.clone());
            current = node.parent();
        }

        result
    }

    /// Check if this node is a descendant of another node
    pub fn is_descendant_of(&self, ancestor: &RedNode) -> bool {
        self.ancestors()
            .iter()
            .any(|node| node.green_id == ancestor.green_id && node.offset == ancestor.offset)
    }

    /// Get the text of this node from the source
    pub fn text<'a>(&self, source: &'a str, arena: &GreenNodeArena) -> &'a str {
        &source[self.offset..self.end(arena)]
    }
}

/// AST representation - simplified from Red Tree
#[derive(Debug, Clone)]
pub enum AstNode {
    Program {
        statements: Vec<AstNode>,
    },

    FunctionDecl {
        name: String,
        params: Vec<String>,
        body: Box<AstNode>,
    },

    LetDecl {
        name: String,
        value: Box<AstNode>,
    },

    LetRecDecl {
        name: String,
        value: Box<AstNode>,
    },

    BinaryExpr {
        op: String,
        left: Box<AstNode>,
        right: Box<AstNode>,
    },

    CallExpr {
        callee: Box<AstNode>,
        args: Vec<AstNode>,
    },

    IfExpr {
        condition: Box<AstNode>,
        then_branch: Box<AstNode>,
        else_branch: Option<Box<AstNode>>,
    },

    BlockExpr {
        statements: Vec<AstNode>,
    },

    TupleExpr {
        elements: Vec<AstNode>,
    },

    RecordExpr {
        fields: Vec<(String, AstNode)>,
    },

    IntLiteral(i64),
    FloatLiteral(f64),
    StringLiteral(String),
    Identifier(String),

    Error,
}

/// Convert Red Tree to AST
pub fn red_to_ast(
    red: &Arc<RedNode>,
    source: &str,
    tokens: &[Token],
    arena: &GreenNodeArena,
) -> AstNode {
    match red.kind(arena) {
        Some(SyntaxKind::Program) => {
            let children = red.children(arena);
            let mut statements: Vec<AstNode> = children
                .iter()
                .map(|child| red_to_ast(child, source, tokens, arena))
                .collect();

            // Transform flat statement list into Let-body-then chain
            statements = vec![transform_let_chain(statements)];

            AstNode::Program { statements }
        }

        Some(SyntaxKind::Statement) => {
            let children = red.children(arena);
            if let Some(first) = children.first() {
                red_to_ast(first, source, tokens, arena)
            } else {
                AstNode::Error
            }
        }

        Some(SyntaxKind::FunctionDecl) => {
            let children = red.children(arena);
            let mut name = String::new();
            let mut params = Vec::new();
            let mut body = None;

            for (i, child) in children.iter().enumerate() {
                let green = arena.get(child.green_id());
                match green {
                    super::green::GreenNode::Token { token_index, .. } => {
                        if let Some(token) = tokens.get(*token_index)
                            && matches!(token.kind, TokenKind::Ident | TokenKind::IdentFunction)
                            && i == 1
                        {
                            name = token.text(source).to_string();
                        }
                    }
                    _ => {
                        if child.kind(arena) == Some(SyntaxKind::ParamList) {
                            params = extract_params(child, source, tokens, arena);
                        } else if child.kind(arena) == Some(SyntaxKind::BlockExpr) {
                            body = Some(Box::new(red_to_ast(child, source, tokens, arena)));
                        }
                    }
                }
            }

            AstNode::FunctionDecl {
                name,
                params,
                body: body.unwrap_or_else(|| Box::new(AstNode::Error)),
            }
        }

        Some(SyntaxKind::LetDecl) => {
            let children = red.children(arena);
            let mut name = String::new();
            let mut value = None;

            for (i, child) in children.iter().enumerate() {
                let green = arena.get(child.green_id());
                match green {
                    super::green::GreenNode::Token { token_index, .. } => {
                        if let Some(token) = tokens.get(*token_index)
                            && matches!(token.kind, TokenKind::Ident | TokenKind::IdentVariable)
                            && i == 1
                        {
                            name = token.text(source).to_string();
                        }
                    }
                    _ => {
                        // Attempt to extract identifier from pattern subtree if name is empty
                        if name.is_empty()
                            && matches!(
                                child.kind(arena),
                                Some(SyntaxKind::Pattern)
                                    | Some(SyntaxKind::SinglePattern)
                                    | Some(SyntaxKind::TuplePattern)
                                    | Some(SyntaxKind::RecordPattern)
                            )
                        {
                            // DFS over subtree to find first identifier token
                            let mut stack = vec![child.clone()];
                            while let Some(node) = stack.pop() {
                                let g = arena.get(node.green_id());
                                if let super::green::GreenNode::Token { token_index, .. } = g {
                                    if let Some(tok) = tokens.get(*token_index)
                                        && matches!(
                                            tok.kind,
                                            TokenKind::Ident | TokenKind::IdentVariable
                                        )
                                    {
                                        name = tok.text(source).to_string();
                                        break;
                                    }
                                } else {
                                    stack.extend(node.children(arena));
                                }
                            }
                        } else if value.is_none() {
                            value = Some(Box::new(red_to_ast(child, source, tokens, arena)));
                        }
                    }
                }
            }

            AstNode::LetDecl {
                name,
                value: value.unwrap_or_else(|| Box::new(AstNode::Error)),
            }
        }

        Some(SyntaxKind::BlockExpr) => {
            let children = red.children(arena);
            let statements = children
                .iter()
                .filter_map(|child| {
                    let green = arena.get(child.green_id());
                    if matches!(green, super::green::GreenNode::Token { .. }) {
                        None
                    } else {
                        Some(red_to_ast(child, source, tokens, arena))
                    }
                })
                .collect();
            AstNode::BlockExpr { statements }
        }

        Some(SyntaxKind::IntLiteral) => {
            let children = red.children(arena);
            if let Some(child) = children.first() {
                let green = arena.get(child.green_id());
                if let super::green::GreenNode::Token { token_index, .. } = green
                    && let Some(token) = tokens.get(*token_index)
                {
                    let text = token.text(source);
                    if let Ok(value) = text.parse::<i64>() {
                        return AstNode::IntLiteral(value);
                    }
                }
            }
            AstNode::Error
        }

        Some(SyntaxKind::FloatLiteral) => {
            let children = red.children(arena);
            if let Some(child) = children.first() {
                let green = arena.get(child.green_id());
                if let super::green::GreenNode::Token { token_index, .. } = green
                    && let Some(token) = tokens.get(*token_index)
                {
                    let text = token.text(source);
                    if let Ok(value) = text.parse::<f64>() {
                        return AstNode::FloatLiteral(value);
                    }
                }
            }
            AstNode::Error
        }

        Some(SyntaxKind::StringLiteral) => {
            let children = red.children(arena);
            if let Some(child) = children.first() {
                let green = arena.get(child.green_id());
                if let super::green::GreenNode::Token { token_index, .. } = green
                    && let Some(token) = tokens.get(*token_index)
                {
                    let text = token.text(source);
                    // Remove quotes
                    let unquoted = text.trim_matches('"');
                    return AstNode::StringLiteral(unquoted.to_string());
                }
            }
            AstNode::Error
        }

        Some(SyntaxKind::Identifier) => {
            let children = red.children(arena);
            if let Some(child) = children.first() {
                let green = arena.get(child.green_id());
                if let super::green::GreenNode::Token { token_index, .. } = green
                    && let Some(token) = tokens.get(*token_index)
                {
                    return AstNode::Identifier(token.text(source).to_string());
                }
            }
            AstNode::Error
        }

        Some(SyntaxKind::TupleExpr) => {
            let children = red.children(arena);
            let elements = children
                .iter()
                .filter_map(|child| {
                    // Skip tokens (parens, commas), only process expressions
                    if child.kind(arena).is_some() {
                        Some(red_to_ast(child, source, tokens, arena))
                    } else {
                        None
                    }
                })
                .collect();
            AstNode::TupleExpr { elements }
        }

        Some(SyntaxKind::RecordExpr) => {
            let children = red.children(arena);
            let mut fields = Vec::new();
            let mut current_field_name = None;

            for child in children.iter() {
                let green = arena.get(child.green_id());
                match green {
                    super::green::GreenNode::Token { token_index, .. } => {
                        if let Some(token) = tokens.get(*token_index)
                            && matches!(token.kind, TokenKind::Ident | TokenKind::IdentVariable)
                        {
                            current_field_name = Some(token.text(source).to_string());
                        }
                    }
                    _ => {
                        // This is an expression node
                        if let Some(field_name) = current_field_name.take() {
                            let value = red_to_ast(child, source, tokens, arena);
                            fields.push((field_name, value));
                        }
                    }
                }
            }
            AstNode::RecordExpr { fields }
        }

        _ => AstNode::Error,
    }
}

/// Extract parameter names from ParamList node
fn extract_params(
    red: &Arc<RedNode>,
    source: &str,
    tokens: &[Token],
    arena: &GreenNodeArena,
) -> Vec<String> {
    let mut params = Vec::new();

    for child in red.children(arena) {
        let green = arena.get(child.green_id());
        if let super::green::GreenNode::Token { token_index, .. } = green
            && let Some(token) = tokens.get(*token_index)
            && matches!(token.kind, TokenKind::Ident | TokenKind::IdentParameter)
        {
            params.push(token.text(source).to_string());
        }
    }

    params
}

/// Transform a flat list of statements into a Let-body-then chain
///
/// For example:
/// ```text
/// [LetDecl(x, 1), LetDecl(y, 2), Expr(x+y)]
/// ```
/// becomes:
/// ```text
/// LetDecl(x, 1, LetDecl(y, 2, Expr(x+y)))
/// ```
fn transform_let_chain(statements: Vec<AstNode>) -> AstNode {
    if statements.is_empty() {
        return AstNode::Error;
    }

    // Work backwards through the statements, building the chain from the inside out
    let result = statements.into_iter().rev().reduce(|body, stmt| {
        match stmt {
            AstNode::LetDecl { name, value } => {
                // Transform LetDecl into a nested structure with body
                // We destructure the Box, so value is AstNode, not Box<AstNode>
                AstNode::LetDecl {
                    name,
                    value: Box::new(AstNode::BlockExpr {
                        statements: vec![*value, body],
                    }),
                }
            }
            AstNode::LetRecDecl { name, value } => {
                // Same for LetRecDecl
                AstNode::LetRecDecl {
                    name,
                    value: Box::new(AstNode::BlockExpr {
                        statements: vec![*value, body],
                    }),
                }
            }
            _ => {
                // For non-Let statements, wrap with the body in a block
                AstNode::BlockExpr {
                    statements: vec![stmt, body],
                }
            }
        }
    });

    result.unwrap_or(AstNode::Error)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::compiler::parser::cst_parser::parse_cst;
    use crate::compiler::parser::preparser::preparse;
    use crate::compiler::parser::tokenizer::tokenize;

    #[test]
    fn test_red_node_creation() {
        let source = "42";
        let tokens = tokenize(source);
        let preparsed = preparse(&tokens);
        let (root_id, arena, _tokens, errors) = parse_cst(tokens, &preparsed);
        let red = RedNode::new(root_id, 0);

        assert_eq!(red.offset(), 0);
        assert!(red.width(&arena) > 0);
        assert!(errors.is_empty(), "Expected no errors, got {errors:?}");
    }

    #[test]
    fn test_red_to_ast_simple() {
        let source = "42";
        let tokens = tokenize(source);
        let preparsed = preparse(&tokens);
        let (root_id, arena, annotated_tokens, errors) = parse_cst(tokens, &preparsed);
        let red = RedNode::new(root_id, 0);
        let ast = red_to_ast(&red, source, &annotated_tokens, &arena);

        match ast {
            AstNode::Program { .. } => {} // Expected
            _ => panic!("Expected Program node"),
        }

        assert!(errors.is_empty(), "Expected no errors, got {errors:?}");
    }

    #[test]
    fn test_red_to_ast_function() {
        let source = "fn add(x, y) { 42 }";
        let tokens = tokenize(source);
        let preparsed = preparse(&tokens);
        let (root_id, arena, annotated_tokens, errors) = parse_cst(tokens, &preparsed);
        let red = RedNode::new(root_id, 0);
        let ast = red_to_ast(&red, source, &annotated_tokens, &arena);

        match ast {
            AstNode::Program { statements } => {
                assert!(!statements.is_empty());
            }
            _ => panic!("Expected Program node"),
        }

        assert!(errors.is_empty(), "Expected no errors, got {errors:?}");
    }

    #[test]
    fn test_parent_references() {
        let source = "fn add(x, y) { 42 }";
        let tokens = tokenize(source);
        let preparsed = preparse(&tokens);
        let (root_id, arena, _tokens, errors) = parse_cst(tokens, &preparsed);
        let root = RedNode::new(root_id, 0);

        // Root should have no parent
        assert!(root.parent().is_none());

        // Get children with parent references
        let children = root.children(&arena);

        // Children should have parent references pointing to root
        for child in children.iter() {
            let parent = child.parent();
            assert!(parent.is_some(), "Child should have a parent reference");

            let parent = parent.unwrap();
            assert_eq!(parent.offset(), root.offset());
            assert_eq!(parent.green_id(), root.green_id());
        }

        assert!(errors.is_empty(), "Expected no errors, got {errors:?}");
    }

    #[test]
    fn test_ancestors() {
        let source = "fn add(x, y) { let z = 42 }";
        let tokens = tokenize(source);
        let preparsed = preparse(&tokens);
        let (root_id, arena, _tokens, errors) = parse_cst(tokens, &preparsed);
        let root = RedNode::new(root_id, 0);

        // Get first child (statement)
        let children = root.children(&arena);
        if let Some(statement) = children.first() {
            // Get ancestors
            let ancestors = statement.ancestors();

            // Should have at least the root as ancestor
            assert!(!ancestors.is_empty(), "Statement should have ancestors");

            // First ancestor should be the root
            if let Some(first_ancestor) = ancestors.first() {
                assert_eq!(first_ancestor.offset(), root.offset());
            }
        }

        assert!(errors.is_empty(), "Expected no errors, got {errors:?}");
    }

    #[test]
    fn test_is_descendant_of() {
        let source = "fn add(x, y) { 42 }";
        let tokens = tokenize(source);
        let preparsed = preparse(&tokens);
        let (root_id, arena, _tokens, errors) = parse_cst(tokens, &preparsed);
        let root = RedNode::new(root_id, 0);

        let children = root.children(&arena);
        if let Some(child) = children.first() {
            // Child should be descendant of root
            assert!(
                child.is_descendant_of(&root),
                "Child should be descendant of root"
            );

            // Root should not be descendant of child
            assert!(
                !root.is_descendant_of(child),
                "Root should not be descendant of child"
            );
        }

        assert!(errors.is_empty(), "Expected no errors, got {errors:?}");
    }

    #[test]
    fn test_let_chain_transformation() {
        let source = "let x = 1\nlet y = 2\nx";
        let tokens = tokenize(source);
        let preparsed = preparse(&tokens);
        let (root_id, arena, annotated_tokens, errors) = parse_cst(tokens, &preparsed);
        let red = RedNode::new(root_id, 0);
        let ast = red_to_ast(&red, source, &annotated_tokens, &arena);

        // The AST should have transformed the flat statement list into a chain
        match ast {
            AstNode::Program { statements } => {
                assert_eq!(statements.len(), 1);
                // The single statement should be a nested structure
                // of Let bindings, not a flat list
            }
            _ => panic!("Expected Program node"),
        }

        assert!(errors.is_empty(), "Expected no errors, got {errors:?}");
    }
}