nepali-core 0.1.0

Nepali programming language core: lexer, parser, AST, interpreter
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
#![no_std]

extern crate alloc;
#[cfg(test)]
extern crate std;

pub mod ast;
pub mod bytecode;
pub mod interpreter;
pub mod lexer;
pub mod parser;
pub mod resolver;
pub mod tokens;
pub mod vm;

pub use ast::{BinOp, Expr, Stmt};
pub use bytecode::Compiler;
pub use interpreter::{
    HostAi, HostCache, HostChannel, HostCommand, HostDb, HostFs, HostJs, HostProcess, HostPython,
    HostRust, Interpreter, Value,
};
pub use lexer::Lexer;
pub use parser::Parser;
pub use resolver::Resolver;
pub use tokens::{Location, Token, TokenType};
pub use vm::Vm;

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::string::{String, ToString};
    use alloc::vec::Vec;

    #[test]
    fn test_nepali_tokens() {
        let code = "राखौँ तलब = ५००००। भनौँ(\"नमस्ते\");";
        let mut lexer = Lexer::new(code);
        let tokens = lexer.tokenize();

        assert_eq!(tokens[0].token_type, TokenType::Let);
        assert_eq!(tokens[1].token_type, TokenType::Ident);
        assert_eq!(tokens[1].literal, "तलब");
        assert_eq!(tokens[3].token_type, TokenType::Number);
        assert_eq!(tokens[3].number_value, Some(50000.0));
        assert_eq!(tokens[4].token_type, TokenType::Purnabiram);
        assert_eq!(tokens[5].token_type, TokenType::Print);
    }

    #[test]
    fn test_parse_let_and_print() {
        let mut parser = Parser::new("राखौँ x = 5। भनौँ(x)।");
        let program = parser.parse_program().unwrap();
        assert_eq!(program.len(), 2);
        assert_eq!(program[0], Stmt::Let("x".into(), Expr::Number(5.0)));
        assert_eq!(program[1], Stmt::Print(alloc::vec![Expr::Ident("x".into())]));
    }

    #[test]
    fn test_parse_if_while_function() {
        let src = "काम add(a, b) { पठाउँ a + b। } राखौँ i = 0। भएसम्म i < 3 { यदि i == 1 { भनौँ(i)। } i = i + 1। }";
        let mut parser = Parser::new(src);
        let program = parser.parse_program().unwrap();
        assert_eq!(program.len(), 3);
        match &program[0] {
            Stmt::FunctionDecl(name, params, body) => {
                assert_eq!(name, "add");
                assert_eq!(params, &["a".to_string(), "b".to_string()]);
                assert_eq!(body.len(), 1);
            }
            other => panic!("expected FunctionDecl, got {:?}", other),
        }
        match &program[2] {
            Stmt::While(cond, body) => {
                assert_eq!(
                    *cond,
                    Expr::Binary(
                        BinOp::Lt,
                        alloc::boxed::Box::new(Expr::Ident("i".into())),
                        alloc::boxed::Box::new(Expr::Number(3.0))
                    )
                );
                assert_eq!(body.len(), 2);
            }
            other => panic!("expected While, got {:?}", other),
        }
    }

    fn run(src: &str) -> Vec<String> {
        let mut parser = Parser::new(src);
        let program = parser.parse_program().expect("parse error");
        Resolver::resolve(&program).expect("resolution error");
        let mut interp = Interpreter::new();
        interp.run(&program).expect("runtime error");
        interp.output
    }

    #[test]
    fn test_resolver_catches_undefined_variable() {
        let mut parser = Parser::new("भनौँ(x)।");
        let program = parser.parse_program().unwrap();
        let errors = Resolver::resolve(&program).unwrap_err();
        assert_eq!(errors, alloc::vec!["undefined variable 'x'".to_string()]);
    }

    #[test]
    fn test_resolver_catches_arity_mismatch() {
        let mut parser =
            Parser::new("काम add(a, b) { पठाउँ a + b। } भनौँ(add(1))।");
        let program = parser.parse_program().unwrap();
        let errors = Resolver::resolve(&program).unwrap_err();
        assert_eq!(
            errors,
            alloc::vec!["function 'add' expects 2 argument(s), got 1".to_string()]
        );
    }

    #[test]
    fn test_resolver_accepts_recursion_and_forward_use_within_function() {
        let mut parser = Parser::new(
            "काम fib(n) { यदि n < 2 { पठाउँ n। } पठाउँ fib(n - 1) + fib(n - 2)। } भनौँ(fib(5))।",
        );
        let program = parser.parse_program().unwrap();
        assert_eq!(Resolver::resolve(&program), Ok(()));
    }

    #[test]
    fn test_if_else_actually_executes() {
        let out = run("यदि गलत { भनौँ(\"a\")। } नत्र { भनौँ(\"b\")। }");
        assert_eq!(out, alloc::vec!["b".to_string()]);
    }

    #[test]
    fn test_while_actually_executes() {
        let out = run(
            "राखौँ i = 0। भएसम्म i < 3 { भनौँ(i)। i = i + 1। }",
        );
        assert_eq!(out, alloc::vec!["0".to_string(), "1".to_string(), "2".to_string()]);
    }

    #[test]
    fn test_recursive_fibonacci() {
        let out = run(
            "काम fib(n) { यदि n < 2 { पठाउँ n। } पठाउँ fib(n - 1) + fib(n - 2)। } भनौँ(fib(10))।",
        );
        assert_eq!(out, alloc::vec!["55".to_string()]);
    }

    #[test]
    fn test_romanized_keywords_are_fully_typeable_ascii() {
        // Every statement keyword this test exercises has no Devanagari
        // anywhere in it - real usability for a plain physical keyboard
        // (e.g. the live kernel REPL), not just an internal convenience.
        let out = run(
            "kaam fib(n) { yadi n < 2 bhaye { pathau n; } pathau fib(n - 1) + fib(n - 2); } \
             rakha i = 0; bhayesamma i < 3 { bhana(i); i = i + 1; } bhana(fib(6));",
        );
        assert_eq!(
            out,
            alloc::vec!["0".to_string(), "1".to_string(), "2".to_string(), "8".to_string()]
        );
    }

    #[test]
    fn test_romanized_keywords_do_not_break_devanagari_identifier_usage() {
        // "rakha" being a keyword must not stop "राखौँ" (its Devanagari
        // form, unrelated bytes) from working exactly as before.
        let out = run("राखौँ x = sahi; yadi x bhaye { bhana(\"ok\"); }");
        assert_eq!(out, alloc::vec!["ok".to_string()]);
    }

    #[test]
    fn test_closures_capture_outer_scope() {
        let out = run(
            "काम बनाउ(x) { काम थप्नु(y) { पठाउँ x + y। } पठाउँ थप्नु। } राखौँ जोड_५ = बनाउ(5)। भनौँ(जोड_५(3))।",
        );
        assert_eq!(out, alloc::vec!["8".to_string()]);
    }

    fn run_vm(src: &str) -> Vec<String> {
        let mut parser = Parser::new(src);
        let program = parser.parse_program().expect("parse error");
        Resolver::resolve(&program).expect("resolution error");
        let (chunk, functions) = bytecode::Compiler::compile(&program);
        let mut vm = Vm::new(functions);
        vm.run(&chunk).expect("VM runtime error");
        vm.output
    }

    #[test]
    fn test_vm_if_else() {
        let out = run_vm("यदि गलत { भनौँ(\"a\")। } नत्र { भनौँ(\"b\")। }");
        assert_eq!(out, alloc::vec!["b".to_string()]);
    }

    #[test]
    fn test_vm_while_loop() {
        let out = run_vm("राखौँ i = 0। भएसम्म i < 3 { भनौँ(i)। i = i + 1। }");
        assert_eq!(out, alloc::vec!["0".to_string(), "1".to_string(), "2".to_string()]);
    }

    #[test]
    fn test_vm_recursive_fibonacci() {
        let out = run_vm(
            "काम fib(n) { यदि n < 2 { पठाउँ n। } पठाउँ fib(n - 1) + fib(n - 2)। } भनौँ(fib(10))।",
        );
        assert_eq!(out, alloc::vec!["55".to_string()]);
    }

    #[test]
    fn test_vm_multi_arg_print_and_arithmetic() {
        let out = run_vm("राखौँ x = 4। राखौँ y = 5। भनौँ(\"x*y=\", x * y)।");
        assert_eq!(out, alloc::vec!["x*y= 20".to_string()]);
    }

    #[test]
    fn test_builtin_string_functions_interpreter() {
        let out = run(
            "राखौँ s = \"काठमाडौं\"। भनौँ(लम्बाइ(s))। भनौँ(अक्षर(s, 0))। भनौँ(संकेत(अक्षर(s, 0)))।",
        );
        assert_eq!(out.len(), 3);
        // "काठमाडौं" is 8 Devanagari scalar values, not 8 UTF-8 bytes -
        // लम्बाइ counts chars, matching what अक्षर can actually index.
        assert_eq!(out[0], "8");
        assert_eq!(out[1], "");
        let expected_code = '' as u32;
        assert_eq!(out[2], alloc::format!("{}", expected_code));
    }

    #[test]
    fn test_builtin_string_functions_vm() {
        let out = run_vm("भनौँ(लम्बाइ(\"hello\"))। भनौँ(अक्षर(\"hello\", 1))।");
        assert_eq!(out, alloc::vec!["5".to_string(), "e".to_string()]);
    }

    #[test]
    fn test_builtin_out_of_range_returns_empty_string() {
        let out = run("भनौँ(अक्षर(\"hi\", 99))।");
        assert_eq!(out, alloc::vec!["".to_string()]);
    }

    #[test]
    fn test_resolver_catches_arithmetic_type_mismatch() {
        let mut parser = Parser::new("राखौँ x = 5 - सहि।");
        let program = parser.parse_program().unwrap();
        let errors = Resolver::resolve(&program).unwrap_err();
        assert_eq!(
            errors,
            alloc::vec!["arithmetic (-) on non-number types: number and boolean".to_string()]
        );
    }

    #[test]
    fn test_resolver_catches_comparison_type_mismatch() {
        let mut parser = Parser::new("राखौँ x = \"hi\" < 5।");
        let program = parser.parse_program().unwrap();
        let errors = Resolver::resolve(&program).unwrap_err();
        assert_eq!(
            errors,
            alloc::vec!["comparison (<) on non-number types: string and number".to_string()]
        );
    }

    #[test]
    fn test_resolver_allows_add_with_string_concat_fallback() {
        // Add never errors - it's the interpreter/VM's string-concat
        // fallback path, not a type error, whenever either side isn't a
        // number.
        let mut parser = Parser::new("राखौँ x = \"n = \" + 5।");
        let program = parser.parse_program().unwrap();
        assert_eq!(Resolver::resolve(&program), Ok(()));
    }

    #[test]
    fn test_resolver_does_not_flag_unknown_typed_function_params() {
        // A function parameter's type is never known statically (no type
        // annotations exist), so arithmetic on it must never be flagged -
        // even though the body looks exactly like the confirmed-mismatch
        // case above with `x` swapped for a literal.
        let mut parser = Parser::new("काम f(x) { पठाउँ x - 1। } भनौँ(f(5))।");
        let program = parser.parse_program().unwrap();
        assert_eq!(Resolver::resolve(&program), Ok(()));
    }

    #[test]
    fn test_resolver_widens_type_after_reassignment_to_avoid_false_positive() {
        // x starts as a number, then gets reassigned a string - a real,
        // legal dynamic-typing move. The checker must not use x's stale
        // "number" type for the later comparison against a string.
        let mut parser = Parser::new("राखौँ x = 5। x = \"hi\"। भनौँ(x == \"hi\")।");
        let program = parser.parse_program().unwrap();
        assert_eq!(Resolver::resolve(&program), Ok(()));
    }

    #[test]
    fn test_resolver_allows_builtin_calls() {
        let mut parser = Parser::new("भनौँ(लम्बाइ(\"hi\"))।");
        let program = parser.parse_program().unwrap();
        assert_eq!(Resolver::resolve(&program), Ok(()));
    }

    #[test]
    fn test_array_literal_and_index_read() {
        let out = run("राखौँ x = [10, 20, 30]। भनौँ(x[0])। भनौँ(x[2])।");
        assert_eq!(out, alloc::vec!["10".to_string(), "30".to_string()]);
    }

    #[test]
    fn test_array_index_assignment_mutates_in_place() {
        // Arrays are reference types here (Rc<RefCell<..>>) - assigning
        // through one binding must be visible through another that refers
        // to the same array, not just the one it was assigned through.
        let out = run("राखौँ x = [1, 2, 3]। राखौँ y = x। y[1] = 99। भनौँ(x[1])।");
        assert_eq!(out, alloc::vec!["99".to_string()]);
    }

    #[test]
    fn test_array_length_and_push_builtins() {
        let out = run(
            "राखौँ x = [1, 2]। भनौँ(लम्बाइ(x))। थप्नुहोस्(x, 3)। भनौँ(लम्बाइ(x))। भनौँ(x[2])।",
        );
        assert_eq!(
            out,
            alloc::vec!["2".to_string(), "3".to_string(), "3".to_string()]
        );
    }

    #[test]
    fn test_array_out_of_bounds_is_a_real_runtime_error() {
        let mut parser = Parser::new("राखौँ x = [1, 2]। भनौँ(x[5])।");
        let program = parser.parse_program().unwrap();
        let mut interp = Interpreter::new();
        let err = interp.run(&program).unwrap_err();
        assert_eq!(err, "array index 5 out of bounds (length 2)");
    }

    #[test]
    fn test_array_while_loop_iteration() {
        let out = run(
            "राखौँ x = [5, 6, 7]। राखौँ i = 0। भएसम्म i < लम्बाइ(x) { भनौँ(x[i])। i = i + 1। }",
        );
        assert_eq!(
            out,
            alloc::vec!["5".to_string(), "6".to_string(), "7".to_string()]
        );
    }

    #[test]
    fn test_resolver_allows_array_indexing() {
        let mut parser = Parser::new("राखौँ x = [1, 2, 3]। राखौँ i = 0। भनौँ(x[i])। x[i] = 9।");
        let program = parser.parse_program().unwrap();
        assert_eq!(Resolver::resolve(&program), Ok(()));
    }

    #[test]
    fn test_logical_and_or_not() {
        let out = run(
            "भनौँ(सहि र सहि)। भनौँ(सहि र गलत)। भनौँ(गलत वा सहि)। \
             भनौँ(गलत वा गलत)। भनौँ(होइन सहि)। भनौँ(होइन गलत)।",
        );
        assert_eq!(
            out,
            alloc::vec![
                "सहि".to_string(),
                "गलत".to_string(),
                "सहि".to_string(),
                "गलत".to_string(),
                "गलत".to_string(),
                "सहि".to_string(),
            ]
        );
    }

    #[test]
    fn test_logical_and_or_short_circuit_real_side_effects() {
        // Not just "produces the right boolean" - the right operand must
        // never actually run once the left side already decides the
        // result. A function call is used specifically because it has an
        // observable side effect (a print) that would show up in `out`
        // if short-circuiting weren't real.
        let out = run(
            "काम se() { भनौँ(\"called\")। पठाउँ सहि। } \
             भनौँ(गलत र se())। भनौँ(सहि वा se())।",
        );
        assert_eq!(out, alloc::vec!["गलत".to_string(), "सहि".to_string()]);
    }

    #[test]
    fn test_and_has_lower_precedence_than_comparison() {
        let out = run("राखौँ x = 5। यदि x > 0 र x < 10 { भनौँ(\"in range\")। }");
        assert_eq!(out, alloc::vec!["in range".to_string()]);
    }

    #[test]
    fn test_romanized_logical_keywords() {
        let out = run("bhana(sahi ra galat)। bhana(hoina galat)।");
        assert_eq!(out, alloc::vec!["गलत".to_string(), "सहि".to_string()]);
    }

    #[test]
    fn test_resolver_allows_logical_operators() {
        let mut parser = Parser::new("भनौँ((सहि र गलत) वा (होइन गलत))।");
        let program = parser.parse_program().unwrap();
        assert_eq!(Resolver::resolve(&program), Ok(()));
    }

    #[test]
    fn test_host_fs_builtins_fail_clearly_without_a_host() {
        // No set_host_fs call - the exact situation nepali-core-cli or
        // any test harness is in unless it opts in, and the situation
        // any .nep program run without a real disk mounted is in inside
        // the kernel too. Must fail with a real, specific error, not
        // panic and not silently pretend to succeed.
        let mut parser = Parser::new("भनौँ(ओएस_पढ्नुहोस्(\"x.txt\"))।");
        let program = parser.parse_program().unwrap();
        let mut interp = Interpreter::new();
        let err = interp.run(&program).unwrap_err();
        assert!(err.contains("needs a host filesystem"), "unexpected error: {}", err);
    }

    /// A tiny in-memory `HostFs` - not the kernel's real FAT filesystem
    /// (that's `kernel/src/fs.rs`, tested separately, live, in QEMU) but
    /// enough to prove `Interpreter::set_host_fs` and the three
    /// `ओएस_*` builtins actually reach whatever implementation is handed
    /// to them, correctly, without needing a whole kernel to test that.
    struct FakeFs {
        files: core::cell::RefCell<alloc::collections::BTreeMap<String, String>>,
    }

    impl HostFs for FakeFs {
        fn read_file(&self, path: &str) -> Result<String, String> {
            self.files
                .borrow()
                .get(path)
                .cloned()
                .ok_or_else(|| alloc::format!("no such file: {}", path))
        }
        fn write_file(&self, path: &str, contents: &str) -> Result<(), String> {
            self.files
                .borrow_mut()
                .insert(path.to_string(), contents.to_string());
            Ok(())
        }
        fn list_dir(&self, _path: &str) -> Result<Vec<String>, String> {
            Ok(self.files.borrow().keys().cloned().collect())
        }
    }

    #[test]
    fn test_host_fs_builtins_with_a_real_host_round_trip() {
        let mut parser = Parser::new(
            "ओएस_लेख्नुहोस्(\"a.txt\", \"hello\")। भनौँ(ओएस_पढ्नुहोस्(\"a.txt\"))।",
        );
        let program = parser.parse_program().unwrap();
        let mut interp = Interpreter::new();
        interp.set_host_fs(alloc::rc::Rc::new(FakeFs {
            files: core::cell::RefCell::new(alloc::collections::BTreeMap::new()),
        }));
        interp.run(&program).expect("runtime error");
        assert_eq!(interp.output, alloc::vec!["hello".to_string()]);
    }

    #[test]
    fn test_host_process_builtins_fail_clearly_without_a_host() {
        let mut parser = Parser::new("नयाँ_प्रक्रिया(\"x\")।");
        let program = parser.parse_program().unwrap();
        let mut interp = Interpreter::new();
        let err = interp.run(&program).unwrap_err();
        assert!(err.contains("needs a host process manager"), "unexpected error: {}", err);
    }

    /// A tiny in-memory `HostProcess` - proves the language-level
    /// mechanism (dispatch, argument passing, return values) is correct
    /// on its own, independent of `kernel/src/process.rs`'s real
    /// preemptible-process machinery, which is tested separately, live,
    /// in QEMU.
    struct FakeProcess {
        names: core::cell::RefCell<Vec<String>>,
    }

    impl HostProcess for FakeProcess {
        fn spawn(&self, name: &str) -> Result<f64, String> {
            let mut names = self.names.borrow_mut();
            let pid = names.len() as f64;
            names.push(name.to_string());
            Ok(pid)
        }
        fn list(&self) -> Result<Vec<String>, String> {
            Ok(self.names.borrow().clone())
        }
    }

    #[test]
    fn test_host_process_builtins_with_a_real_host() {
        let mut parser = Parser::new(
            "राखौँ pid1 = नयाँ_प्रक्रिया(\"a\")। राखौँ pid2 = नयाँ_प्रक्रिया(\"b\")। \
             भनौँ(pid1)। भनौँ(pid2)। राखौँ names = प्रक्रिया_सूची()। भनौँ(लम्बाइ(names))। \
             भनौँ(names[0])। भनौँ(names[1])।",
        );
        let program = parser.parse_program().unwrap();
        let mut interp = Interpreter::new();
        interp.set_host_process(alloc::rc::Rc::new(FakeProcess {
            names: core::cell::RefCell::new(Vec::new()),
        }));
        interp.run(&program).expect("runtime error");
        assert_eq!(
            interp.output,
            alloc::vec![
                "0".to_string(),
                "1".to_string(),
                "2".to_string(),
                "a".to_string(),
                "b".to_string(),
            ]
        );
    }

    #[test]
    fn test_host_channel_builtins_fail_clearly_without_a_host() {
        let mut parser = Parser::new("नयाँ_च्यानल()।");
        let program = parser.parse_program().unwrap();
        let mut interp = Interpreter::new();
        let err = interp.run(&program).unwrap_err();
        assert!(err.contains("needs a host channel manager"), "unexpected error: {}", err);
    }

    /// A tiny in-memory `HostChannel` (a `BTreeMap<id, VecDeque<String>>`)
    /// - proves the language-level mechanism (dispatch, argument passing,
    /// non-blocking empty-queue behavior) is correct on its own, independent
    /// of `kernel/src/channel.rs`'s real kernel-state version, which is
    /// tested separately, live, in QEMU.
    struct FakeChannel {
        queues: core::cell::RefCell<Vec<alloc::collections::VecDeque<String>>>,
    }

    impl HostChannel for FakeChannel {
        fn create(&self) -> Result<f64, String> {
            let mut queues = self.queues.borrow_mut();
            let id = queues.len() as f64;
            queues.push(alloc::collections::VecDeque::new());
            Ok(id)
        }
        fn send(&self, id: f64, msg: &str) -> Result<(), String> {
            let mut queues = self.queues.borrow_mut();
            let queue = queues
                .get_mut(id as usize)
                .ok_or_else(|| alloc::format!("no such channel: {}", id))?;
            queue.push_back(msg.to_string());
            Ok(())
        }
        fn recv(&self, id: f64) -> Result<Option<String>, String> {
            let mut queues = self.queues.borrow_mut();
            let queue = queues
                .get_mut(id as usize)
                .ok_or_else(|| alloc::format!("no such channel: {}", id))?;
            Ok(queue.pop_front())
        }
    }

    #[test]
    fn test_host_channel_builtins_with_a_real_host() {
        let mut parser = Parser::new(
            "राखौँ ch = नयाँ_च्यानल()। \
             भनौँ(च्यानल_पाउनुहोस्(ch))। \
             च्यानल_पठाउनुहोस्(ch, \"halo\")। \
             च्यानल_पठाउनुहोस्(ch, \"pheri\")। \
             भनौँ(च्यानल_पाउनुहोस्(ch))। \
             भनौँ(च्यानल_पाउनुहोस्(ch))। \
             भनौँ(च्यानल_पाउनुहोस्(ch))।",
        );
        let program = parser.parse_program().unwrap();
        let mut interp = Interpreter::new();
        interp.set_host_channel(alloc::rc::Rc::new(FakeChannel {
            queues: core::cell::RefCell::new(Vec::new()),
        }));
        interp.run(&program).expect("runtime error");
        assert_eq!(
            interp.output,
            alloc::vec![
                "केहीछैन".to_string(),
                "halo".to_string(),
                "pheri".to_string(),
                "केहीछैन".to_string(),
            ]
        );
    }

    #[test]
    fn test_host_fs_list_dir_returns_a_real_array() {
        let mut parser = Parser::new(
            "ओएस_लेख्नुहोस्(\"a.txt\", \"1\")। ओएस_लेख्नुहोस्(\"b.txt\", \"2\")। \
             राखौँ files = ओएस_सूची(\".\")। भनौँ(लम्बाइ(files))।",
        );
        let program = parser.parse_program().unwrap();
        let mut interp = Interpreter::new();
        interp.set_host_fs(alloc::rc::Rc::new(FakeFs {
            files: core::cell::RefCell::new(alloc::collections::BTreeMap::new()),
        }));
        interp.run(&program).expect("runtime error");
        assert_eq!(interp.output, alloc::vec!["2".to_string()]);
    }
}