lambdust 0.1.1

A Scheme dialect with gradual typing and effect systems
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
#![allow(unused_variables)]
//! Asynchronous I/O operations with high-performance backends.
//!
//! This module provides:
//! - Async file operations with tokio
//! - High-performance I/O with epoll/kqueue/io_uring
//! - Async buffering strategies
//! - Non-blocking I/O primitives
//! - Future-based I/O operations

use crate::diagnostics::{Error as DiagnosticError, Result};
use crate::eval::value::{
    Value, PrimitiveProcedure, PrimitiveImpl, ThreadSafeEnvironment
};
use crate::effects::Effect;
use std::sync::Arc;
use std::sync::OnceLock;

#[cfg(feature = "async")]
use tokio::io::{AsyncReadExt, AsyncWriteExt, AsyncBufReadExt};
#[cfg(feature = "async")]
use tokio::fs::File as AsyncFile;
#[cfg(feature = "async")]
use tokio::runtime::{Runtime, Handle};
#[cfg(feature = "async")]
use std::future::Future;
// Unused async utilities - commented out
// #[cfg(feature = "async")]
// use std::pin::Pin;
// #[cfg(feature = "async")]
// use std::task::{Context, Poll};

/// Async I/O runtime manager
#[cfg(feature = "async")]
pub struct AsyncIoRuntime {
    runtime: Option<Runtime>,
}

#[cfg(feature = "async")]
impl AsyncIoRuntime {
    pub fn new() -> std::result::Result<Self, Box<dyn std::error::Error>> {
        let runtime = Runtime::new()?;
        Ok(AsyncIoRuntime {
            runtime: Some(runtime),
        })
    }
    
    pub fn get_handle(&self) -> Option<Handle> {
        self.runtime.as_ref().map(|rt| rt.handle().clone())
    }
    
    pub fn block_on<F>(&self, future: F) -> F::Output
    where
        F: Future,
    {
        if let Some(rt) = &self.runtime {
            rt.block_on(future)
        } else {
            // Fallback to current runtime if available
            Handle::current().block_on(future)
        }
    }
}

#[cfg(not(feature = "async"))]
pub struct AsyncIoRuntime;

#[cfg(not(feature = "async"))]
impl AsyncIoRuntime {
    pub fn new() -> std::result::Result<Self, Box<dyn std::error::Error>> {
        Ok(AsyncIoRuntime)
    }
}

/// Global async runtime instance
static ASYNC_RUNTIME: OnceLock<AsyncIoRuntime> = OnceLock::new();

pub fn get_async_runtime() -> &'static AsyncIoRuntime {
    ASYNC_RUNTIME.get_or_init(|| {
        AsyncIoRuntime::new().expect("Failed to create async runtime")
    })
}

/// Creates async I/O operation bindings.
pub fn create_async_io_bindings(env: &Arc<ThreadSafeEnvironment>) {
    // Async file operations
    bind_async_file_operations(env);
    
    // Async buffered I/O
    bind_async_buffered_operations(env);
    
    // High-performance I/O operations
    bind_high_performance_operations(env);
    
    // Future combinators
    bind_future_operations(env);
}

// ============= ASYNC FILE OPERATIONS =============

fn bind_async_file_operations(env: &Arc<ThreadSafeEnvironment>) {
    // async-read-file
    env.define("async-read-file".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "async-read-file".to_string(),
        arity_min: 1,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_async_read_file),
        effects: vec![Effect::IO],
    })));
    
    // async-write-file
    env.define("async-write-file".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "async-write-file".to_string(),
        arity_min: 2,
        arity_max: Some(3),
        implementation: PrimitiveImpl::RustFn(primitive_async_write_file),
        effects: vec![Effect::IO],
    })));
    
    // async-append-file
    env.define("async-append-file".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "async-append-file".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_async_append_file),
        effects: vec![Effect::IO],
    })));
    
    // async-copy-file
    env.define("async-copy-file".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "async-copy-file".to_string(),
        arity_min: 2,
        arity_max: Some(3),
        implementation: PrimitiveImpl::RustFn(primitive_async_copy_file),
        effects: vec![Effect::IO],
    })));
}

fn bind_async_buffered_operations(env: &Arc<ThreadSafeEnvironment>) {
    // async-read-lines
    env.define("async-read-lines".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "async-read-lines".to_string(),
        arity_min: 1,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_async_read_lines),
        effects: vec![Effect::IO],
    })));
    
    // async-read-chunks
    env.define("async-read-chunks".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "async-read-chunks".to_string(),
        arity_min: 2,
        arity_max: Some(3),
        implementation: PrimitiveImpl::RustFn(primitive_async_read_chunks),
        effects: vec![Effect::IO],
    })));
    
    // async-write-chunks
    env.define("async-write-chunks".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "async-write-chunks".to_string(),
        arity_min: 2,
        arity_max: Some(3),
        implementation: PrimitiveImpl::RustFn(primitive_async_write_chunks),
        effects: vec![Effect::IO],
    })));
}

fn bind_high_performance_operations(env: &Arc<ThreadSafeEnvironment>) {
    // io-uring-read (Linux-specific)
    env.define("io-uring-read".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "io-uring-read".to_string(),
        arity_min: 2,
        arity_max: Some(4),
        implementation: PrimitiveImpl::RustFn(primitive_io_uring_read),
        effects: vec![Effect::IO],
    })));
    
    // io-uring-write (Linux-specific)
    env.define("io-uring-write".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "io-uring-write".to_string(),
        arity_min: 2,
        arity_max: Some(4),
        implementation: PrimitiveImpl::RustFn(primitive_io_uring_write),
        effects: vec![Effect::IO],
    })));
    
    // batch-io-operations
    env.define("batch-io-operations".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "batch-io-operations".to_string(),
        arity_min: 1,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_batch_io_operations),
        effects: vec![Effect::IO],
    })));
}

fn bind_future_operations(env: &Arc<ThreadSafeEnvironment>) {
    // await-future
    env.define("await-future".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "await-future".to_string(),
        arity_min: 1,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_await_future),
        effects: vec![Effect::IO],
    })));
    
    // spawn-task
    env.define("spawn-task".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "spawn-task".to_string(),
        arity_min: 1,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_spawn_task),
        effects: vec![Effect::IO],
    })));
    
    // join-tasks
    env.define("join-tasks".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "join-tasks".to_string(),
        arity_min: 1,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_join_tasks),
        effects: vec![Effect::IO],
    })));
}

// ============= IMPLEMENTATION FUNCTIONS =============

// === Async File Operations ===

#[cfg(feature = "async")]
pub fn primitive_async_read_file(args: &[Value]) -> Result<Value> {
    if args.is_empty() || args.len() > 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("async-read-file expects 1 or 2 arguments, got {}", args.len()),
            None,
        )));
    }
    
    let path = extract_string(&args[0], "async-read-file")?;
    let as_binary = if args.len() > 1 {
        extract_boolean(&args[1], "async-read-file")?
    } else {
        false
    };
    
    let runtime = get_async_runtime();
    
    runtime.block_on(async move {
        if as_binary {
            match AsyncFile::open(&path).await {
                Ok(mut file) => {
                    let mut contents = Vec::new();
                    match file.read_to_end(&mut contents).await {
                        Ok(_) => Ok(Value::bytevector(contents)),
                        Err(e) => Err(Box::new(DiagnosticError::runtime_error(
                            format!("Error reading file '{path}': {e}"),
                            None,
                        ))),
                    }
                }
                Err(e) => Err(Box::new(DiagnosticError::runtime_error(
                    format!("Cannot open file '{path}': {e}"),
                    None,
                ))),
            }
        } else {
            match tokio::fs::read_to_string(&path).await {
                Ok(contents) => Ok(Value::string(contents)),
                Err(e) => Err(Box::new(DiagnosticError::runtime_error(
                    format!("Error reading file '{path}': {e}"),
                    None,
                ))),
            }
        }
    })
}

#[cfg(not(feature = "async"))]
pub fn primitive_async_read_file(_args: &[Value]) -> Result<Value> {
    Err(Box::new(DiagnosticError::runtime_error(
        "async-read-file requires async feature".to_string(),
        None,
    )))
}

#[cfg(feature = "async")]
pub fn primitive_async_write_file(args: &[Value]) -> Result<Value> {
    if args.len() < 2 || args.len() > 3 {
        return Err(Box::new(DiagnosticError::runtime_error(
            {
                let arg_count = args.len();
                format!("async-write-file expects 2 or 3 arguments, got {arg_count}")
            },
            None,
        )));
    }
    
    let path = extract_string(&args[0], "async-write-file")?;
    let create_dirs = if args.len() > 2 {
        extract_boolean(&args[2], "async-write-file")?
    } else {
        false
    };
    
    let runtime = get_async_runtime();
    
    runtime.block_on(async move {
        if create_dirs {
            if let Some(parent) = std::path::Path::new(&path).parent() {
                if let Err(e) = tokio::fs::create_dir_all(parent).await {
                    return Err(Box::new(DiagnosticError::runtime_error(
                        format!("Cannot create parent directories for '{path}': {e}"),
                        None,
                    )));
                }
            }
        }
        
        match &args[1] {
            Value::Literal(crate::ast::Literal::String(content)) => {
                match tokio::fs::write(&path, content).await {
                    Ok(()) => Ok(Value::Unspecified),
                    Err(e) => Err(Box::new(DiagnosticError::runtime_error(
                        format!("Error writing to file '{path}': {e}"),
                        None,
                    ))),
                }
            }
            Value::Literal(crate::ast::Literal::Bytevector(content)) => {
                match tokio::fs::write(&path, content).await {
                    Ok(()) => Ok(Value::Unspecified),
                    Err(e) => Err(Box::new(DiagnosticError::runtime_error(
                        format!("Error writing to file '{path}': {e}"),
                        None,
                    ))),
                }
            }
            _ => Err(Box::new(DiagnosticError::runtime_error(
                "async-write-file requires string or bytevector content".to_string(),
                None,
            ))),
        }
    })
}

#[cfg(not(feature = "async"))]
pub fn primitive_async_write_file(_args: &[Value]) -> Result<Value> {
    Err(Box::new(DiagnosticError::runtime_error(
        "async-write-file requires async feature".to_string(),
        None,
    )))
}

#[cfg(feature = "async")]
pub fn primitive_async_append_file(args: &[Value]) -> Result<Value> {
    if args.len() != 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            {
                let arg_count = args.len();
                format!("async-append-file expects 2 arguments, got {arg_count}")
            },
            None,
        )));
    }
    
    let path = extract_string(&args[0], "async-append-file")?;
    
    let runtime = get_async_runtime();
    
    
    
    runtime.block_on(async move {
        let mut file = match tokio::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&path)
            .await
        {
            Ok(file) => file,
            Err(e) => {
                return Err(Box::new(DiagnosticError::runtime_error(
                    format!("Cannot open file '{path}' for appending: {e}"),
                    None,
                )));
            }
        };
        
        match &args[1] {
            Value::Literal(crate::ast::Literal::String(content)) => {
                match file.write_all(content.as_bytes()).await {
                    Ok(()) => {
                        match file.flush().await {
                            Ok(()) => Ok(Value::Unspecified),
                            Err(e) => Err(Box::new(DiagnosticError::runtime_error(
                                format!("Error flushing file '{path}': {e}"),
                                None,
                            ))),
                        }
                    }
                    Err(e) => Err(Box::new(DiagnosticError::runtime_error(
                        format!("Error appending to file '{path}': {e}"),
                        None,
                    ))),
                }
            }
            Value::Literal(crate::ast::Literal::Bytevector(content)) => {
                match file.write_all(content).await {
                    Ok(()) => {
                        match file.flush().await {
                            Ok(()) => Ok(Value::Unspecified),
                            Err(e) => Err(Box::new(DiagnosticError::runtime_error(
                                format!("Error flushing file '{path}': {e}"),
                                None,
                            ))),
                        }
                    }
                    Err(e) => Err(Box::new(DiagnosticError::runtime_error(
                        format!("Error appending to file '{path}': {e}"),
                        None,
                    ))),
                }
            }
            _ => Err(Box::new(DiagnosticError::runtime_error(
                "async-append-file requires string or bytevector content".to_string(),
                None,
            ))),
        }
    })
}

#[cfg(not(feature = "async"))]
pub fn primitive_async_append_file(_args: &[Value]) -> Result<Value> {
    Err(Box::new(DiagnosticError::runtime_error(
        "async-append-file requires async feature".to_string(),
        None,
    )))
}

#[cfg(feature = "async")]
pub fn primitive_async_copy_file(args: &[Value]) -> Result<Value> {
    if args.len() < 2 || args.len() > 3 {
        return Err(Box::new(DiagnosticError::runtime_error(
            {
                let arg_count = args.len();
                format!("async-copy-file expects 2 or 3 arguments, got {arg_count}")
            },
            None,
        )));
    }
    
    let src = extract_string(&args[0], "async-copy-file")?;
    let dst = extract_string(&args[1], "async-copy-file")?;
    let overwrite = if args.len() > 2 {
        extract_boolean(&args[2], "async-copy-file")?
    } else {
        false
    };
    
    let runtime = get_async_runtime();
    
    
    
    runtime.block_on(async move {
        if !overwrite && tokio::fs::try_exists(&dst).await.unwrap_or(true) {
            return Err(Box::new(DiagnosticError::runtime_error(
                format!("Destination file '{dst}' already exists"),
                None,
            )));
        }
        
        match tokio::fs::copy(&src, &dst).await {
            Ok(bytes_copied) => Ok(Value::integer(bytes_copied as i64)),
            Err(e) => Err(Box::new(DiagnosticError::runtime_error(
                format!("Cannot copy file '{src}' to '{dst}': {e}"),
                None,
            ))),
        }
    })
}

#[cfg(not(feature = "async"))]
pub fn primitive_async_copy_file(_args: &[Value]) -> Result<Value> {
    Err(Box::new(DiagnosticError::runtime_error(
        "async-copy-file requires async feature".to_string(),
        None,
    )))
}

// === Async Buffered Operations ===

#[cfg(feature = "async")]
pub fn primitive_async_read_lines(args: &[Value]) -> Result<Value> {
    if args.is_empty() || args.len() > 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            {
                let arg_count = args.len();
                format!("async-read-lines expects 1 or 2 arguments, got {arg_count}")
            },
            None,
        )));
    }
    
    let path = extract_string(&args[0], "async-read-lines")?;
    let max_lines = if args.len() > 1 {
        Some(extract_integer(&args[1], "async-read-lines")? as usize)
    } else {
        None
    };
    
    let runtime = get_async_runtime();
    
    
    
    runtime.block_on(async move {
        let file = match AsyncFile::open(&path).await {
            Ok(file) => file,
            Err(e) => {
                return Err(Box::new(DiagnosticError::runtime_error(
                    format!("Cannot open file '{path}': {e}"),
                    None,
                )));
            }
        };
        
        let reader = tokio::io::BufReader::new(file);
        let mut lines = reader.lines();
        let mut result = Vec::new();
        let mut count = 0;
        
        while let Some(line) = lines.next_line().await.map_err(|e| {
            Box::new(DiagnosticError::runtime_error(
                format!("Error reading line from file '{path}': {e}"),
                None,
            ))
        })? {
            result.push(Value::string(line));
            count += 1;
            
            if let Some(max) = max_lines {
                if count >= max {
                    break;
                }
            }
        }
        
        Ok(list_to_value(result))
    })
}

#[cfg(not(feature = "async"))]
pub fn primitive_async_read_lines(_args: &[Value]) -> Result<Value> {
    Err(Box::new(DiagnosticError::runtime_error(
        "async-read-lines requires async feature".to_string(),
        None,
    )))
}

#[cfg(feature = "async")]
pub fn primitive_async_read_chunks(args: &[Value]) -> Result<Value> {
    if args.len() < 2 || args.len() > 3 {
        return Err(Box::new(DiagnosticError::runtime_error(
            {
                let arg_count = args.len();
                format!("async-read-chunks expects 2 or 3 arguments, got {arg_count}")
            },
            None,
        )));
    }
    
    let path = extract_string(&args[0], "async-read-chunks")?;
    let chunk_size = extract_integer(&args[1], "async-read-chunks")? as usize;
    let max_chunks = if args.len() > 2 {
        Some(extract_integer(&args[2], "async-read-chunks")? as usize)
    } else {
        None
    };
    
    let runtime = get_async_runtime();
    
    
    
    runtime.block_on(async move {
        let mut file = match AsyncFile::open(&path).await {
            Ok(file) => file,
            Err(e) => {
                return Err(Box::new(DiagnosticError::runtime_error(
                    format!("Cannot open file '{path}': {e}"),
                    None,
                )));
            }
        };
        
        let mut result = Vec::new();
        let mut count = 0;
        
        loop {
            let mut buffer = vec![0u8; chunk_size];
            match file.read(&mut buffer).await {
                Ok(0) => break, // EOF
                Ok(n) => {
                    buffer.truncate(n);
                    result.push(Value::bytevector(buffer));
                    count += 1;
                    
                    if let Some(max) = max_chunks {
                        if count >= max {
                            break;
                        }
                    }
                }
                Err(e) => {
                    return Err(Box::new(DiagnosticError::runtime_error(
                        format!("Error reading chunk from file '{path}': {e}"),
                        None,
                    )));
                }
            }
        }
        
        Ok(list_to_value(result))
    })
}

#[cfg(not(feature = "async"))]
pub fn primitive_async_read_chunks(_args: &[Value]) -> Result<Value> {
    Err(Box::new(DiagnosticError::runtime_error(
        "async-read-chunks requires async feature".to_string(),
        None,
    )))
}

#[cfg(feature = "async")]
pub fn primitive_async_write_chunks(args: &[Value]) -> Result<Value> {
    if args.len() < 2 || args.len() > 3 {
        return Err(Box::new(DiagnosticError::runtime_error(
            {
                let arg_count = args.len();
                format!("async-write-chunks expects 2 or 3 arguments, got {arg_count}")
            },
            None,
        )));
    }
    
    let path = extract_string(&args[0], "async-write-chunks")?;
    let chunks = extract_list(&args[1], "async-write-chunks")?;
    let append = if args.len() > 2 {
        extract_boolean(&args[2], "async-write-chunks")?
    } else {
        false
    };
    
    let runtime = get_async_runtime();
    
    
    
    runtime.block_on(async move {
        let file = if append {
            tokio::fs::OpenOptions::new()
                .create(true)
                .append(true)
                .open(&path)
                .await
        } else {
            tokio::fs::OpenOptions::new()
                .create(true)
                .write(true)
                .truncate(true)
                .open(&path)
                .await
        };
        
        let mut file = match file {
            Ok(file) => file,
            Err(e) => {
                return Err(Box::new(DiagnosticError::runtime_error(
                    format!("Cannot open file '{path}': {e}"),
                    None,
                )));
            }
        };
        
        let mut total_written = 0u64;
        
        for chunk in chunks {
            match chunk {
                Value::Literal(crate::ast::Literal::Bytevector(bytes)) => {
                    match file.write_all(&bytes).await {
                        Ok(()) => total_written += bytes.len() as u64,
                        Err(e) => {
                            return Err(Box::new(DiagnosticError::runtime_error(
                                format!("Error writing chunk to file '{path}': {e}"),
                                None,
                            )));
                        }
                    }
                }
                Value::Literal(crate::ast::Literal::String(text)) => {
                    match file.write_all(text.as_bytes()).await {
                        Ok(()) => total_written += text.len() as u64,
                        Err(e) => {
                            return Err(Box::new(DiagnosticError::runtime_error(
                                format!("Error writing chunk to file '{path}': {e}"),
                                None,
                            )));
                        }
                    }
                }
                _ => {
                    return Err(Box::new(DiagnosticError::runtime_error(
                        "async-write-chunks requires bytevector or string chunks".to_string(),
                        None,
                    )));
                }
            }
        }
        
        match file.flush().await {
            Ok(()) => Ok(Value::integer(total_written as i64)),
            Err(e) => Err(Box::new(DiagnosticError::runtime_error(
                format!("Error flushing file '{path}': {e}"),
                None,
            ))),
        }
    })
}

#[cfg(not(feature = "async"))]
pub fn primitive_async_write_chunks(_args: &[Value]) -> Result<Value> {
    Err(Box::new(DiagnosticError::runtime_error(
        "async-write-chunks requires async feature".to_string(),
        None,
    )))
}

// === High-performance Operations ===

pub fn primitive_io_uring_read(_args: &[Value]) -> Result<Value> {
    // TODO: Implement io_uring-based reading for Linux
    #[cfg(target_os = "linux")]
    {
        Err(Box::new(DiagnosticError::runtime_error(
            "io-uring-read not yet implemented".to_string(),
            None,
        )))
    }
    
    #[cfg(not(target_os = "linux"))]
    {
        Err(Box::new(DiagnosticError::runtime_error(
            "io-uring-read is only available on Linux".to_string(),
            None,
        )))
    }
}

pub fn primitive_io_uring_write(_args: &[Value]) -> Result<Value> {
    // TODO: Implement io_uring-based writing for Linux
    #[cfg(target_os = "linux")]
    {
        Err(Box::new(DiagnosticError::runtime_error(
            "io-uring-write not yet implemented".to_string(),
            None,
        )))
    }
    
    #[cfg(not(target_os = "linux"))]
    {
        Err(Box::new(DiagnosticError::runtime_error(
            "io-uring-write is only available on Linux".to_string(),
            None,
        )))
    }
}

pub fn primitive_batch_io_operations(_args: &[Value]) -> Result<Value> {
    // TODO: Implement batched I/O operations
    Err(Box::new(DiagnosticError::runtime_error(
        "batch-io-operations not yet implemented".to_string(),
        None,
    )))
}

// === Future Operations ===

pub fn primitive_await_future(_args: &[Value]) -> Result<Value> {
    // TODO: Implement future awaiting
    Err(Box::new(DiagnosticError::runtime_error(
        "await-future not yet implemented".to_string(),
        None,
    )))
}

pub fn primitive_spawn_task(_args: &[Value]) -> Result<Value> {
    // TODO: Implement task spawning
    Err(Box::new(DiagnosticError::runtime_error(
        "spawn-task not yet implemented".to_string(),
        None,
    )))
}

pub fn primitive_join_tasks(_args: &[Value]) -> Result<Value> {
    // TODO: Implement task joining
    Err(Box::new(DiagnosticError::runtime_error(
        "join-tasks not yet implemented".to_string(),
        None,
    )))
}

// ============= HELPER FUNCTIONS =============

/// Extracts a string from a Value.
fn extract_string(value: &Value, operation: &str) -> Result<String> {
    match value {
        Value::Literal(crate::ast::Literal::String(s)) => Ok(s.clone()),
        _ => Err(Box::new(DiagnosticError::runtime_error(
            format!("{operation} requires string arguments"),
            None,
        ))),
    }
}

/// Extracts a boolean from a Value.
fn extract_boolean(value: &Value, operation: &str) -> Result<bool> {
    match value {
        Value::Literal(crate::ast::Literal::Boolean(b)) => Ok(*b),
        _ => Err(Box::new(DiagnosticError::runtime_error(
            format!("{operation} requires boolean arguments"),
            None,
        ))),
    }
}

/// Extracts an integer from a Value.
fn extract_integer(value: &Value, operation: &str) -> Result<i64> {
    match value {
        Value::Literal(lit) => {
            if let Some(i) = lit.to_i64() {
                Ok(i)
            } else {
                Err(Box::new(DiagnosticError::runtime_error(
                    format!("{operation} requires integer arguments"),
                    None,
                )))
            }
        }
        _ => Err(Box::new(DiagnosticError::runtime_error(
            format!("{operation} requires integer arguments"),
            None,
        ))),
    }
}

/// Extracts a list from a Value.
fn extract_list(value: &Value, operation: &str) -> Result<Vec<Value>> {
    fn list_to_vec(value: &Value, acc: &mut Vec<Value>) -> Result<()> {
        match value {
            Value::Nil => Ok(()),
            Value::Pair(car, cdr) => {
                acc.push((**car).clone());
                list_to_vec(cdr, acc)
            }
            _ => Err(Box::new(DiagnosticError::runtime_error(
                "Invalid list structure".to_string(),
                None,
            ))),
        }
    }
    
    let mut result = Vec::new();
    list_to_vec(value, &mut result)?;
    Ok(result)
}

/// Converts a vector of values to a Scheme list.
fn list_to_value(values: Vec<Value>) -> Value {
    values.into_iter().rev().fold(Value::Nil, |acc, val| {
        Value::Pair(Arc::new(val), Arc::new(acc))
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;
    
    #[cfg(feature = "async")]
    #[tokio::test]
    async fn test_async_file_operations() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("async_test.txt");
        let file_path = test_file.to_string_lossy().to_string();
        
        // Test async write
        let args = vec![
            Value::string(file_path.clone()),
            Value::string("Hello, async world!".to_string()),
        ];
        let result = primitive_async_write_file(&args);
        assert!(result.is_ok());
        
        // Test async read
        let args = vec![Value::string(file_path)];
        let result = primitive_async_read_file(&args);
        assert!(result.is_ok());
        
        if let Ok(Value::Literal(crate::ast::Literal::String(content))) = result {
            assert_eq!(content, "Hello, async world!");
        } else {
            panic!("Expected string result");
        }
    }
    
    #[test]
    fn test_runtime_creation() {
        let runtime = get_async_runtime();
        // Just verify we can create the runtime without panicking
        assert!(true);
    }
}