mumu 0.11.1

Lava Mumu is a language for those in the now and that know
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
use crate::parser::ast::{Expr, DestructurePattern};
use crate::parser::types::Value;
use crate::parser::interpreter::Interpreter;
use std::sync::{Arc, Mutex};

pub fn bool_like(v: &Value) -> Result<bool, String> {
    match v {
        Value::Bool(b) => Ok(*b),
        Value::Int(i) => Ok(*i != 0),
        Value::Float(ff) => Ok(*ff != 0.0),
        Value::Long(l) => Ok(*l != 0),
        _ => Err(format!("Cannot interpret as boolean: {:?}", v)),
    }
}

pub fn eval_bracketed_array(interp: &mut Interpreter, exprs: &[Expr]) -> Result<Value, String> {
    let mut subvals = Vec::with_capacity(exprs.len());
    for e in exprs {
        let val = interp.eval_expression(e)?;
        subvals.push(val);
    }
    if subvals.is_empty() {
        return Ok(Value::IntArray(vec![]));
    }

    if subvals.iter().all(|v| matches!(v, Value::SingleString(_))) {
        let ss = subvals.into_iter().map(|v| match v {
            Value::SingleString(s) => s,
            _ => unreachable!(),
        }).collect();
        return Ok(Value::StrArray(ss));
    }

    let is_all_arrays = subvals.iter().all(|v| matches!(v, Value::IntArray(_) | Value::FloatArray(_) | Value::StrArray(_) | Value::BoolArray(_)));
    let same_len = if is_all_arrays {
        let first_len = match &subvals[0] {
            Value::IntArray(xs) => xs.len(),
            Value::FloatArray(xs) => xs.len(),
            Value::StrArray(xs) => xs.len(),
            Value::BoolArray(xs) => xs.len(),
            _ => 0,
        };
        subvals.iter().all(|v| match v {
            Value::IntArray(xs) => xs.len() == first_len,
            Value::FloatArray(xs) => xs.len() == first_len,
            Value::StrArray(xs) => xs.len() == first_len,
            Value::BoolArray(xs) => xs.len() == first_len,
            _ => false,
        })
    } else {
        false
    };

    if is_all_arrays && same_len && !subvals.is_empty() {
        let mut only_int = true;
        let mut has_float = false;
        for v in &subvals {
            match v {
                Value::FloatArray(_) => {
                    only_int = false;
                    has_float = true;
                }
                Value::IntArray(_) => {}
                _ => {
                    only_int = false;
                }
            }
        }
        if only_int {
            let mut int_rows = Vec::new();
            for v in subvals {
                match v {
                    Value::IntArray(xs) => int_rows.push(xs),
                    _ => {}
                }
            }
            return Ok(Value::Int2DArray(int_rows));
        } else if has_float && subvals.iter().all(|v| matches!(v, Value::IntArray(_) | Value::FloatArray(_))) {
            let mut float_rows = Vec::new();
            for v in subvals {
                match v {
                    Value::IntArray(xs) => float_rows.push(xs.iter().map(|&i| i as f64).collect()),
                    Value::FloatArray(fs) => float_rows.push(fs),
                    _ => {}
                }
            }
            return Ok(Value::Float2DArray(float_rows));
        } else if subvals.iter().all(|v| matches!(v, Value::FloatArray(_))) {
            let mut float_rows = Vec::new();
            for v in subvals {
                match v {
                    Value::FloatArray(fs) => float_rows.push(fs),
                    _ => {}
                }
            }
            return Ok(Value::Float2DArray(float_rows));
        } else {
            return Ok(Value::MixedArray(subvals));
        }
    }

    let mut found_int = false;
    let mut found_float = false;
    let mut found_bool = false;
    let mut found_str = false;
    let mut found_other = false;
    let mut ints = Vec::new();
    let mut floats = Vec::new();
    let mut bools = Vec::new();
    let mut strs = Vec::new();

    for item in &subvals {
        match item {
            Value::Int(i) => {
                if found_str || found_bool || found_other {
                    found_other = true;
                } else if found_float {
                    floats.push(*i as f64);
                } else {
                    found_int = true;
                    ints.push(*i);
                }
            }
            Value::Float(ff) => {
                if found_str || found_bool || found_other {
                    found_other = true;
                } else {
                    if !found_float {
                        found_float = true;
                        for old_i in ints.drain(..) {
                            floats.push(old_i as f64);
                        }
                    }
                    floats.push(*ff);
                }
            }
            Value::Long(l) => {
                if found_str || found_bool || found_other {
                    found_other = true;
                } else if found_float {
                    floats.push(*l as f64);
                } else {
                    found_int = true;
                    ints.push(*l as i32);
                }
            }
            Value::Bool(b) => {
                if found_int || found_float || found_str || found_other {
                    found_other = true;
                } else {
                    found_bool = true;
                    bools.push(*b);
                }
            }
            Value::SingleString(s) => {
                if found_int || found_float || found_bool || found_other {
                    found_other = true;
                } else {
                    found_str = true;
                    strs.push(s.clone());
                }
            }
            _ => {
                found_other = true;
            }
        }
    }

    if found_other {
        return Ok(Value::MixedArray(subvals));
    }
    if found_float {
        return Ok(Value::FloatArray(floats));
    }
    if found_int {
        return Ok(Value::IntArray(ints));
    }
    if found_bool {
        return Ok(Value::BoolArray(bools));
    }
    if found_str {
        return Ok(Value::StrArray(strs));
    }
    Ok(Value::IntArray(vec![]))
}

pub fn eval_keyed_array(interp: &mut Interpreter, kv_pairs: &[(String, Expr)]) -> Result<Value, String> {
    use indexmap::IndexMap;
    let mut map = IndexMap::new();
    for (k, subexpr) in kv_pairs {
        let val = interp.eval_expression(subexpr)?;
        map.insert(k.clone(), val);
    }
    Ok(Value::KeyedArray(map))
}

pub fn assign_to_expr(interp: &mut Interpreter, left_expr: &Expr, value: Value) -> Result<(), String> {
    match left_expr {
        Expr::Ident { name, .. } => {
            interp.set_variable(name, value);
            Ok(())
        }
        Expr::Index { base, index, .. } => {
            assign_index(interp, base, index, value)
        }
        _ => Err("Left side of assignment must be a variable or array index".to_string()),
    }
}

pub fn assign_keyed_destructure(interp: &mut Interpreter, pattern: &DestructurePattern, value: Value) -> Result<(), String> {
    match pattern {
        DestructurePattern::Keyed(keys) => {
            match value {
                Value::KeyedArray(map) => {
                    for k in keys {
                        if let Some(v) = map.get(k) {
                            interp.set_variable(k, v.clone());
                        } else {
                            interp.set_variable(k, Value::Placeholder);
                        }
                    }
                    Ok(())
                }
                other => Err(format!("Keyed destructure requires a KeyedArray, got {:?}", other)),
            }
        }
        DestructurePattern::Tuple(names) => {
            match value {
                Value::IntArray(xs) => {
                    for (i, name) in names.iter().enumerate() {
                        if i < xs.len() {
                            interp.set_variable(name, Value::Int(xs[i]));
                        } else {
                            interp.set_variable(name, Value::Placeholder);
                        }
                    }
                    Ok(())
                }
                Value::FloatArray(xs) => {
                    for (i, name) in names.iter().enumerate() {
                        if i < xs.len() {
                            interp.set_variable(name, Value::Float(xs[i]));
                        } else {
                            interp.set_variable(name, Value::Placeholder);
                        }
                    }
                    Ok(())
                }
                Value::StrArray(xs) => {
                    for (i, name) in names.iter().enumerate() {
                        if i < xs.len() {
                            interp.set_variable(name, Value::SingleString(xs[i].clone()));
                        } else {
                            interp.set_variable(name, Value::Placeholder);
                        }
                    }
                    Ok(())
                }
                Value::MixedArray(xs) => {
                    for (i, name) in names.iter().enumerate() {
                        if i < xs.len() {
                            interp.set_variable(name, xs[i].clone());
                        } else {
                            interp.set_variable(name, Value::Placeholder);
                        }
                    }
                    Ok(())
                }
                v => {
                    for name in names {
                        interp.set_variable(name, v.clone());
                    }
                    Ok(())
                }
            }
        }
    }
}

fn extract_mutex_for_indexing(val: &Value) -> Option<Arc<Mutex<Value>>> {
    match val {
        Value::Ref(arc_mutex) => Some(arc_mutex.clone()),
        _ => None,
    }
}

fn assign_index(interp: &mut Interpreter, base: &Expr, idx_expr: &Expr, new_val: Value) -> Result<(), String> {
    let base_val = interp.eval_expression(base)?;
    let idx_val = interp.eval_expression(idx_expr)?;

    // If base_val is a reference, mutate through it
    if let Some(arc_mutex) = extract_mutex_for_indexing(&base_val) {
        let mut mut_val = arc_mutex.lock().unwrap();
        return match (&mut *mut_val, idx_val, new_val) {
            (Value::IntArray(ref mut xs), Value::Int(i), Value::Int(new_i)) => {
                if i < 0 || (i as usize) >= xs.len() {
                    return Err(format!("Array index out of range: {}", i));
                }
                xs[i as usize] = new_i;
                Ok(())
            }
            (Value::FloatArray(ref mut xs), Value::Int(i), Value::Float(new_f)) => {
                if i < 0 || (i as usize) >= xs.len() {
                    return Err(format!("Array index out of range: {}", i));
                }
                xs[i as usize] = new_f;
                Ok(())
            }
            (Value::FloatArray(ref mut xs), Value::Int(i), Value::Int(new_i)) => {
                if i < 0 || (i as usize) >= xs.len() {
                    return Err(format!("Array index out of range: {}", i));
                }
                xs[i as usize] = new_i as f64;
                Ok(())
            }
            (Value::StrArray(ref mut xs), Value::Int(i), Value::SingleString(new_s)) => {
                if i < 0 || (i as usize) >= xs.len() {
                    return Err(format!("Array index out of range: {}", i));
                }
                xs[i as usize] = new_s;
                Ok(())
            }
            (Value::Int2DArray(ref mut rows), Value::Int(i), Value::IntArray(new_row)) => {
                if i < 0 || (i as usize) >= rows.len() {
                    return Err(format!("Array index out of range: {}", i));
                }
                rows[i as usize] = new_row;
                Ok(())
            }
            (Value::Float2DArray(ref mut rows), Value::Int(i), Value::FloatArray(new_row)) => {
                if i < 0 || (i as usize) >= rows.len() {
                    return Err(format!("Array index out of range: {}", i));
                }
                rows[i as usize] = new_row;
                Ok(())
            }
            (Value::Float2DArray(ref mut rows), Value::Int(i), Value::IntArray(new_row)) => {
                if i < 0 || (i as usize) >= rows.len() {
                    return Err(format!("Array index out of range: {}", i));
                }
                rows[i as usize] = new_row.iter().map(|&n| n as f64).collect();
                Ok(())
            }
            (Value::MixedArray(ref mut items), Value::Int(i), nv) => {
                if i < 0 || (i as usize) >= items.len() {
                    return Err(format!("Array index out of range: {}", i));
                }
                items[i as usize] = nv;
                Ok(())
            }
            _ => Err("Unsupported or type-mismatched index assignment through Ref".to_string()),
        };
    }

    // Otherwise: treat as non-reference (legacy value copy logic)
    match (base_val, idx_val, new_val) {
        (Value::IntArray(xs), Value::Int(i), Value::Int(new_i)) => {
            if i < 0 || (i as usize) >= xs.len() {
                return Err(format!("Array index out of range: {}", i));
            }
            let mut copy = xs.clone();
            copy[i as usize] = new_i;
            match base {
                Expr::Ident { name, .. } => {
                    interp.set_variable(name, Value::IntArray(copy));
                    Ok(())
                }
                Expr::Index { base: prev_base, index: prev_idx, .. } => {
                    assign_index(interp, prev_base, prev_idx, Value::IntArray(copy))
                }
                _ => Err("Unsupported nested indexing base".to_string()),
            }
        }
        (Value::FloatArray(xs), Value::Int(i), Value::Float(new_f)) => {
            if i < 0 || (i as usize) >= xs.len() {
                return Err(format!("Array index out of range: {}", i));
            }
            let mut copy = xs.clone();
            copy[i as usize] = new_f;
            match base {
                Expr::Ident { name, .. } => {
                    interp.set_variable(name, Value::FloatArray(copy));
                    Ok(())
                }
                Expr::Index { base: prev_base, index: prev_idx, .. } => {
                    assign_index(interp, prev_base, prev_idx, Value::FloatArray(copy))
                }
                _ => Err("Unsupported nested indexing base".to_string()),
            }
        }
        (Value::FloatArray(xs), Value::Int(i), Value::Int(new_i)) => {
            if i < 0 || (i as usize) >= xs.len() {
                return Err(format!("Array index out of range: {}", i));
            }
            let mut copy = xs.clone();
            copy[i as usize] = new_i as f64;
            match base {
                Expr::Ident { name, .. } => {
                    interp.set_variable(name, Value::FloatArray(copy));
                    Ok(())
                }
                Expr::Index { base: prev_base, index: prev_idx, .. } => {
                    assign_index(interp, prev_base, prev_idx, Value::FloatArray(copy))
                }
                _ => Err("Unsupported nested indexing base".to_string()),
            }
        }
        (Value::StrArray(xs), Value::Int(i), Value::SingleString(new_s)) => {
            if i < 0 || (i as usize) >= xs.len() {
                return Err(format!("Array index out of range: {}", i));
            }
            let mut copy = xs.clone();
            copy[i as usize] = new_s;
            match base {
                Expr::Ident { name, .. } => {
                    interp.set_variable(name, Value::StrArray(copy));
                    Ok(())
                }
                Expr::Index { base: prev_base, index: prev_idx, .. } => {
                    assign_index(interp, prev_base, prev_idx, Value::StrArray(copy))
                }
                _ => Err("Unsupported nested indexing base".to_string()),
            }
        }
        (Value::Int2DArray(rows), Value::Int(i), Value::IntArray(new_row)) => {
            if i < 0 || (i as usize) >= rows.len() {
                return Err(format!("Array index out of range: {}", i));
            }
            let mut copy = rows.clone();
            copy[i as usize] = new_row;
            match base {
                Expr::Ident { name, .. } => {
                    interp.set_variable(name, Value::Int2DArray(copy));
                    Ok(())
                }
                Expr::Index { base: prev_base, index: prev_idx, .. } => {
                    assign_index(interp, prev_base, prev_idx, Value::Int2DArray(copy))
                }
                _ => Err("Unsupported nested indexing base".to_string()),
            }
        }
        (Value::Float2DArray(rows), Value::Int(i), Value::FloatArray(new_row)) => {
            if i < 0 || (i as usize) >= rows.len() {
                return Err(format!("Array index out of range: {}", i));
            }
            let mut copy = rows.clone();
            copy[i as usize] = new_row;
            match base {
                Expr::Ident { name, .. } => {
                    interp.set_variable(name, Value::Float2DArray(copy));
                    Ok(())
                }
                Expr::Index { base: prev_base, index: prev_idx, .. } => {
                    assign_index(interp, prev_base, prev_idx, Value::Float2DArray(copy))
                }
                _ => Err("Unsupported nested indexing base".to_string()),
            }
        }
        (Value::Float2DArray(rows), Value::Int(i), Value::IntArray(new_row)) => {
            if i < 0 || (i as usize) >= rows.len() {
                return Err(format!("Array index out of range: {}", i));
            }
            let mut copy = rows.clone();
            copy[i as usize] = new_row.iter().map(|&n| n as f64).collect();
            match base {
                Expr::Ident { name, .. } => {
                    interp.set_variable(name, Value::Float2DArray(copy));
                    Ok(())
                }
                Expr::Index { base: prev_base, index: prev_idx, .. } => {
                    assign_index(interp, prev_base, prev_idx, Value::Float2DArray(copy))
                }
                _ => Err("Unsupported nested indexing base".to_string()),
            }
        }
        (Value::MixedArray(mut items), Value::Int(i), nv) => {
            if i < 0 || (i as usize) >= items.len() {
                return Err(format!("Array index out of range: {}", i));
            }
            items[i as usize] = nv;
            match base {
                Expr::Ident { name, .. } => {
                    interp.set_variable(name, Value::MixedArray(items));
                    Ok(())
                }
                Expr::Index { base: prev_b, index: prev_i, .. } => {
                    assign_index(interp, prev_b, prev_i, Value::MixedArray(items))
                }
                _ => Err("Unsupported nested indexing base for MixedArray".to_string()),
            }
        }
        _ => Err(format!("Cannot assign via array index with provided types.")),
    }
}

pub fn deep_flatten_upcast_array(items: &[Value]) -> Value {
    let mut floats = Vec::new();
    let mut ints = Vec::new();
    let mut strs = Vec::new();
    let mut found_float = false;
    let mut found_int = false;
    let mut found_str = false;
    let mut found_other = false;

    fn flatten(
        v: &Value,
        ints: &mut Vec<i32>,
        floats: &mut Vec<f64>,
        strs: &mut Vec<String>,
        found_int: &mut bool,
        found_float: &mut bool,
        found_str: &mut bool,
        found_other: &mut bool,
    ) {
        match v {
            Value::Int(i) => {
                *found_int = true;
                ints.push(*i);
            }
            Value::Float(f) => {
                *found_float = true;
                floats.push(*f);
            }
            Value::SingleString(s) => {
                *found_str = true;
                strs.push(s.clone());
            }
            Value::StrArray(ss) => {
                *found_str = true;
                strs.extend(ss.clone());
            }
            Value::IntArray(xs) => {
                *found_int = true;
                ints.extend(xs.iter().cloned());
            }
            Value::FloatArray(fs) => {
                *found_float = true;
                floats.extend(fs.iter().cloned());
            }
            Value::MixedArray(inner) => for x in inner {
                flatten(x, ints, floats, strs, found_int, found_float, found_str, found_other)
            },
            _ => {
                *found_other = true;
            }
        }
    }
    for v in items {
        flatten(v, &mut ints, &mut floats, &mut strs, &mut found_int, &mut found_float, &mut found_str, &mut found_other);
    }

    if found_other {
        let mut out = Vec::new();
        for v in items {
            match v {
                Value::MixedArray(inner) => out.extend(inner.clone()),
                _ => out.push(v.clone())
            }
        }
        Value::MixedArray(out)
    } else if found_float || (found_int && found_float) {
        let mut all_floats = floats;
        all_floats.extend(ints.iter().map(|&i| i as f64));
        Value::FloatArray(all_floats)
    } else if found_int {
        Value::IntArray(ints)
    } else if found_str {
        Value::StrArray(strs)
    } else {
        Value::MixedArray(items.to_vec())
    }
}