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
//! C type system integration and data marshalling for FFI operations.
//!
//! This module provides comprehensive support for converting between Rust/Lambdust
//! values and C types, including structs, arrays, pointers, and complex nested data.

use std::collections::HashMap;
use std::ffi::{CStr, CString, c_void};
use std::fmt;
use std::mem;
use std::slice;

use crate::eval::Value;
use crate::ast::Literal;
use crate::diagnostics::Error;

/// C type definitions
#[derive(Debug, Clone, PartialEq)]
pub enum CType {
    /// Void type (for function returns)
    Void,
    /// Boolean (mapped to c_int)
    Bool,
    /// Signed integers
    Int8,
    Int16,
    Int32,
    Int64,
    /// Unsigned integers
    UInt8,
    UInt16,
    UInt32,
    UInt64,
    /// Platform-dependent integer types
    CInt,
    CUInt,
    CSizeT,
    /// Floating point
    Float,
    Double,
    /// Character types
    Char,
    WChar,
    /// Pointer types
    Pointer(Box<CType>),
    /// Array types
    Array(Box<CType>, usize),
    /// String types
    CString,
    WString,
    /// Structure types
    Struct {
        name: String,
        fields: Vec<CField>,
        alignment: usize,
        size: usize,
    },
    /// Union types
    Union {
        name: String,
        fields: Vec<CField>,
        size: usize,
    },
    /// Function pointer types
    Function {
        return_type: Box<CType>,
        parameters: Vec<CType>,
        variadic: bool,
    },
    /// Opaque handle (void pointer with type name)
    Handle(String),
}

/// C structure field
#[derive(Debug, Clone, PartialEq)]
pub struct CField {
    pub name: String,
    pub c_type: CType,
    pub offset: usize,
}

impl CType {
    /// Get the size of this type in bytes
    pub fn size(&self) -> usize {
        match self {
            CType::Void => 0,
            CType::Bool => mem::size_of::<i32>(),
            CType::Int8 => 1,
            CType::Int16 => 2,
            CType::Int32 => 4,
            CType::Int64 => 8,
            CType::UInt8 => 1,
            CType::UInt16 => 2,
            CType::UInt32 => 4,
            CType::UInt64 => 8,
            CType::CInt => mem::size_of::<libc::c_int>(),
            CType::CUInt => mem::size_of::<libc::c_uint>(),
            CType::CSizeT => mem::size_of::<libc::size_t>(),
            CType::Float => 4,
            CType::Double => 8,
            CType::Char => 1,
            CType::WChar => mem::size_of::<libc::wchar_t>(),
            CType::Pointer(_) => mem::size_of::<*const c_void>(),
            CType::Array(element_type, count) => element_type.size() * count,
            CType::CString => mem::size_of::<*const libc::c_char>(),
            CType::WString => mem::size_of::<*const libc::wchar_t>(),
            CType::Struct { size, .. } => *size,
            CType::Union { size, .. } => *size,
            CType::Function { .. } => mem::size_of::<*const c_void>(),
            CType::Handle(_) => mem::size_of::<*const c_void>(),
        }
    }

    /// Get the alignment of this type
    pub fn alignment(&self) -> usize {
        match self {
            CType::Void => 1,
            CType::Bool => mem::align_of::<i32>(),
            CType::Int8 => 1,
            CType::Int16 => 2,
            CType::Int32 => 4,
            CType::Int64 => 8,
            CType::UInt8 => 1,
            CType::UInt16 => 2,
            CType::UInt32 => 4,
            CType::UInt64 => 8,
            CType::CInt => mem::align_of::<libc::c_int>(),
            CType::CUInt => mem::align_of::<libc::c_uint>(),
            CType::CSizeT => mem::align_of::<libc::size_t>(),
            CType::Float => 4,
            CType::Double => 8,
            CType::Char => 1,
            CType::WChar => mem::align_of::<libc::wchar_t>(),
            CType::Pointer(_) => mem::align_of::<*const c_void>(),
            CType::Array(element_type, _) => element_type.alignment(),
            CType::CString => mem::align_of::<*const libc::c_char>(),
            CType::WString => mem::align_of::<*const libc::wchar_t>(),
            CType::Struct { alignment, .. } => *alignment,
            CType::Union { fields, .. } => {
                fields.iter()
                    .map(|f| f.c_type.alignment())
                    .max()
                    .unwrap_or(1)
            }
            CType::Function { .. } => mem::align_of::<*const c_void>(),
            CType::Handle(_) => mem::align_of::<*const c_void>(),
        }
    }

    /// Check if this is a pointer type
    pub fn is_pointer(&self) -> bool {
        matches!(self, CType::Pointer(_) | CType::CString | CType::WString | CType::Function { .. } | CType::Handle(_))
    }

    /// Check if this is a numeric type
    pub fn is_numeric(&self) -> bool {
        matches!(self, 
            CType::Int8 | CType::Int16 | CType::Int32 | CType::Int64 |
            CType::UInt8 | CType::UInt16 | CType::UInt32 | CType::UInt64 |
            CType::CInt | CType::CUInt | CType::CSizeT |
            CType::Float | CType::Double
        )
    }

    /// Get the element type for arrays and pointers
    pub fn element_type(&self) -> Option<&CType> {
        match self {
            CType::Pointer(elem) => Some(elem),
            CType::Array(elem, _) => Some(elem),
            _ => None,
        }
    }
}

impl fmt::Display for CType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CType::Void => write!(f, "void"),
            CType::Bool => write!(f, "bool"),
            CType::Int8 => write!(f, "int8_t"),
            CType::Int16 => write!(f, "int16_t"),
            CType::Int32 => write!(f, "int32_t"),
            CType::Int64 => write!(f, "int64_t"),
            CType::UInt8 => write!(f, "uint8_t"),
            CType::UInt16 => write!(f, "uint16_t"),
            CType::UInt32 => write!(f, "uint32_t"),
            CType::UInt64 => write!(f, "uint64_t"),
            CType::CInt => write!(f, "int"),
            CType::CUInt => write!(f, "unsigned int"),
            CType::CSizeT => write!(f, "size_t"),
            CType::Float => write!(f, "float"),
            CType::Double => write!(f, "double"),
            CType::Char => write!(f, "char"),
            CType::WChar => write!(f, "wchar_t"),
            CType::Pointer(inner) => write!(f, "{inner}*"),
            CType::Array(inner, size) => write!(f, "{inner}[{size}]"),
            CType::CString => write!(f, "char*"),
            CType::WString => write!(f, "wchar_t*"),
            CType::Struct { name, .. } => write!(f, "struct {name}"),
            CType::Union { name, .. } => write!(f, "union {name}"),
            CType::Function { return_type, parameters, variadic }  => {
                write!(f, "{return_type} (")?;
                for (i, param) in parameters.iter().enumerate() {
                    if i > 0 { write!(f, ", ")?; }
                    write!(f, "{param}")?;
                }
                if *variadic {
                    if !parameters.is_empty() { write!(f, ", ")?; }
                    write!(f, "...")?;
                }
                write!(f, ")")
            }
            CType::Handle(name) => write!(f, "{name}*"),
        }
    }
}

/// Errors that can occur during type conversion
#[derive(Debug, Clone)]
pub enum ConversionError {
    /// Type mismatch
    TypeMismatch {
        expected: CType,
        actual: String,
    },
    /// Invalid pointer or null pointer dereference
    InvalidPointer,
    /// Buffer overflow or underflow
    BufferOverflow {
        buffer_size: usize,
        requested_size: usize,
    },
    /// String conversion error
    StringConversion(String),
    /// Struct field not found
    FieldNotFound {
        struct_name: String,
        field_name: String,
    },
    /// Array index out of bounds
    IndexOutOfBounds {
        index: usize,
        length: usize,
    },
    /// Memory allocation failure
    AllocationFailed(usize),
}

impl fmt::Display for ConversionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ConversionError::TypeMismatch { expected, actual }  => {
                write!(f, "Type mismatch: expected {expected}, got {actual}")
            }
            ConversionError::InvalidPointer  => {
                write!(f, "Invalid or null pointer")
            }
            ConversionError::BufferOverflow { buffer_size, requested_size }  => {
                write!(f, "Buffer overflow: buffer size {buffer_size}, requested {requested_size}")
            }
            ConversionError::StringConversion(msg)  => {
                write!(f, "String conversion error: {msg}")
            }
            ConversionError::FieldNotFound { struct_name, field_name }  => {
                write!(f, "Field '{field_name}' not found in struct '{struct_name}'")
            }
            ConversionError::IndexOutOfBounds { index, length }  => {
                write!(f, "Array index {index} out of bounds (length {length})")
            }
            ConversionError::AllocationFailed(size)  => {
                write!(f, "Memory allocation failed for {size} bytes")
            }
        }
    }
}

impl std::error::Error for ConversionError {}

impl From<ConversionError> for Error {
    fn from(conv_error: ConversionError) -> Self {
        Error::runtime_error(conv_error.to_string(), None)
    }
}

/// C data buffer for holding converted values
#[derive(Debug)]
pub struct CDataBuffer {
    /// Raw data buffer
    data: Vec<u8>,
    /// Type information
    c_type: CType,
    /// Whether this buffer owns the data
    owns_data: bool,
}

impl CDataBuffer {
    /// Create a new buffer for the given type
    pub fn new(c_type: CType) -> Self {
        let size = c_type.size();
        Self {
            data: vec![0u8; size],
            c_type,
            owns_data: true,
        }
    }

    /// Create a buffer from existing data (non-owning)
    ///
    /// # Safety
    /// 
    /// The caller must ensure that:
    /// - `data` is a valid pointer to at least `c_type.size()` bytes
    /// - The memory pointed to by `data` remains valid for the lifetime of the returned buffer
    /// - The memory layout matches the expectations of `c_type`
    pub unsafe fn from_raw(data: *const u8, c_type: CType) -> Self {
        let size = c_type.size();
        let data_slice = unsafe { slice::from_raw_parts(data, size) };
        Self {
            data: data_slice.to_vec(),
            c_type,
            owns_data: false,
        }
    }

    /// Get the type of this buffer
    pub fn c_type(&self) -> &CType {
        &self.c_type
    }

    /// Get raw data pointer
    pub fn as_ptr(&self) -> *const u8 {
        self.data.as_ptr()
    }

    /// Get mutable raw data pointer
    pub fn as_mut_ptr(&mut self) -> *mut u8 {
        self.data.as_mut_ptr()
    }

    /// Get size in bytes
    pub fn size(&self) -> usize {
        self.data.len()
    }

    /// Convert to a specific type
    ///
    /// # Safety
    /// 
    /// The caller must ensure that:
    /// - The buffer contains valid data for type `T`
    /// - The buffer size is at least `size_of::<T>()`
    /// - The data alignment is compatible with type `T`
    /// - The resulting reference does not outlive the buffer
    pub unsafe fn as_type<T>(&self) -> &T {
        unsafe { &*(self.as_ptr() as *const T) }
    }

    /// Convert to a mutable specific type
    ///
    /// # Safety
    /// 
    /// The caller must ensure that:
    /// - The buffer contains valid data for type `T`
    /// - The buffer size is at least `size_of::<T>()`
    /// - The data alignment is compatible with type `T`
    /// - The resulting mutable reference does not outlive the buffer
    /// - No other references to the buffer exist during the lifetime of the returned reference
    pub unsafe fn as_type_mut<T>(&mut self) -> &mut T {
        unsafe { &mut *(self.as_mut_ptr() as *mut T) }
    }
}

/// Type marshaller for converting between Lambdust values and C types
#[derive(Debug)]
pub struct TypeMarshaller {
    /// Registered struct definitions
    structs: HashMap<String, CType>,
    /// Type aliases
    aliases: HashMap<String, CType>,
    /// String cache for C strings
    string_cache: Vec<CString>,
}

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

impl TypeMarshaller {
    /// Create a new type marshaller
    pub fn new() -> Self {
        let mut marshaller = Self {
            structs: HashMap::new(),
            aliases: HashMap::new(),
            string_cache: Vec::new(),
        };
        
        // Register common type aliases
        marshaller.register_alias("int".to_string(), CType::CInt);
        marshaller.register_alias("uint".to_string(), CType::CUInt);
        marshaller.register_alias("size_t".to_string(), CType::CSizeT);
        marshaller.register_alias("char*".to_string(), CType::CString);
        marshaller.register_alias("string".to_string(), CType::CString);
        
        marshaller
    }

    /// Register a struct definition
    pub fn register_struct(&mut self, name: String, fields: Vec<CField>) -> std::result::Result<(), ConversionError> {
        // Calculate struct layout
        let mut offset = 0;
        let mut max_alignment = 1;
        let mut calculated_fields = Vec::new();
        
        for field in fields {
            let field_alignment = field.c_type.alignment();
            max_alignment = max_alignment.max(field_alignment);
            
            // Align offset to field alignment
            offset = (offset + field_alignment - 1) & !(field_alignment - 1);
            
            calculated_fields.push(CField {
                name: field.name,
                c_type: field.c_type,
                offset,
            });
            
            offset += calculated_fields.last().unwrap().c_type.size();
        }
        
        // Align total size to struct alignment
        let size = (offset + max_alignment - 1) & !(max_alignment - 1);
        
        let struct_type = CType::Struct {
            name: name.clone(),
            fields: calculated_fields,
            alignment: max_alignment,
            size,
        };
        
        self.structs.insert(name, struct_type);
        Ok(())
    }

    /// Register a type alias
    pub fn register_alias(&mut self, alias: String, c_type: CType) {
        self.aliases.insert(alias, c_type);
    }

    /// Resolve a type name to a CType
    pub fn resolve_type(&self, name: &str) -> Option<&CType> {
        self.aliases.get(name).or_else(|| self.structs.get(name))
    }

    /// Convert a Lambdust value to C data
    pub fn to_c_data(&mut self, value: &Value, c_type: &CType) -> std::result::Result<CDataBuffer, ConversionError> {
        let mut buffer = CDataBuffer::new(c_type.clone());
        self.write_value_to_buffer(value, c_type, &mut buffer, 0)?;
        Ok(buffer)
    }

    /// Convert C data to a Lambdust value
    pub fn from_c_data(&self, buffer: &CDataBuffer) -> std::result::Result<Value, ConversionError> {
        self.read_value_from_buffer(buffer.c_type(), buffer, 0)
    }

    /// Helper function to extract numeric value from literal
    fn extract_numeric_value(literal: &Literal) -> Option<f64> {
        match literal {
            Literal::ExactInteger(i) => Some(*i as f64),
            Literal::InexactReal(f) => Some(*f),
            Literal::Rational { numerator, denominator } => Some(*numerator as f64 / *denominator as f64),
            Literal::Complex { real, imaginary: _ } => Some(*real), // Use real part only
            _ => None,
        }
    }

    /// Write a value to a buffer at the given offset
    fn write_value_to_buffer(&mut self, value: &Value, c_type: &CType, buffer: &mut CDataBuffer, offset: usize) 
        -> std::result::Result<(), ConversionError> {
        
        if offset + c_type.size() > buffer.size() {
            return Err(ConversionError::BufferOverflow {
                buffer_size: buffer.size(),
                requested_size: offset + c_type.size(),
            });
        }

        unsafe {
            let ptr = buffer.as_mut_ptr().add(offset);
            
            match (value, c_type) {
                (Value::Literal(literal), CType::Int8) if Self::extract_numeric_value(literal).is_some() => {
                    let val = Self::extract_numeric_value(literal).unwrap();
                    if val.fract() == 0.0 {
                        *(ptr as *mut i8) = val as i8;
                    } else {
                        return Err(ConversionError::TypeMismatch {
                            expected: c_type.clone(),
                            actual: format!("{value:?}"),
                        });
                    }
                }
                (Value::Literal(literal), CType::Int16) if Self::extract_numeric_value(literal).is_some() => {
                    let val = Self::extract_numeric_value(literal).unwrap();
                    if val.fract() == 0.0 {
                        *(ptr as *mut i16) = val as i16;
                    } else {
                        return Err(ConversionError::TypeMismatch {
                            expected: c_type.clone(),
                            actual: format!("{value:?}"),
                        });
                    }
                }
                (Value::Literal(literal), CType::Int32) if Self::extract_numeric_value(literal).is_some() => {
                    let val = Self::extract_numeric_value(literal).unwrap();
                    *(ptr as *mut i32) = val as i32;
                }
                (Value::Literal(literal), CType::Int64) if Self::extract_numeric_value(literal).is_some() => {
                    let val = Self::extract_numeric_value(literal).unwrap();
                    *(ptr as *mut i64) = val as i64;
                }
                (Value::Literal(literal), CType::UInt8) if Self::extract_numeric_value(literal).is_some() => {
                    let val = Self::extract_numeric_value(literal).unwrap();
                    *ptr = val as u8;
                }
                (Value::Literal(literal), CType::UInt16) if Self::extract_numeric_value(literal).is_some() => {
                    let val = Self::extract_numeric_value(literal).unwrap();
                    *(ptr as *mut u16) = val as u16;
                }
                (Value::Literal(literal), CType::UInt32) if Self::extract_numeric_value(literal).is_some() => {
                    let val = Self::extract_numeric_value(literal).unwrap();
                    *(ptr as *mut u32) = val as u32;
                }
                (Value::Literal(literal), CType::UInt64) if Self::extract_numeric_value(literal).is_some() => {
                    let val = Self::extract_numeric_value(literal).unwrap();
                    *(ptr as *mut u64) = val as u64;
                }
                (Value::Literal(literal), CType::CInt) if Self::extract_numeric_value(literal).is_some() => {
                    let val = Self::extract_numeric_value(literal).unwrap();
                    *(ptr as *mut libc::c_int) = val as libc::c_int;
                }
                (Value::Literal(literal), CType::CUInt) if Self::extract_numeric_value(literal).is_some() => {
                    let val = Self::extract_numeric_value(literal).unwrap();
                    *(ptr as *mut libc::c_uint) = val as libc::c_uint;
                }
                (Value::Literal(literal), CType::CSizeT) if Self::extract_numeric_value(literal).is_some() => {
                    let val = Self::extract_numeric_value(literal).unwrap();
                    *(ptr as *mut libc::size_t) = val as libc::size_t;
                }
                (Value::Literal(literal), CType::Float) if Self::extract_numeric_value(literal).is_some() => {
                    let val = Self::extract_numeric_value(literal).unwrap();
                    *(ptr as *mut f32) = val as f32;
                }
                (Value::Literal(literal), CType::Double) if Self::extract_numeric_value(literal).is_some() => {
                    let val = Self::extract_numeric_value(literal).unwrap();
                    *(ptr as *mut f64) = val;
                }
                (Value::Literal(Literal::Boolean(b)), CType::Bool)  => {
                    *(ptr as *mut i32) = if *b { 1 } else { 0 };
                }
                (Value::Literal(Literal::String(s)), CType::CString)  => {
                    let c_string = CString::new(s.as_str())
                        .map_err(|e| ConversionError::StringConversion(e.to_string()))?;
                    *(ptr as *mut *const libc::c_char) = c_string.as_ptr();
                    self.string_cache.push(c_string); // Keep alive
                }
                (Value::Literal(Literal::Character(c)), CType::Char)  => {
                    *(ptr as *mut libc::c_char) = *c as libc::c_char;
                }
                _  => {
                    return Err(ConversionError::TypeMismatch {
                        expected: c_type.clone(),
                        actual: format!("{value:?}"),
                    });
                }
            }
        }

        Ok(())
    }

    /// Read a value from a buffer at the given offset
    fn read_value_from_buffer(&self, c_type: &CType, buffer: &CDataBuffer, offset: usize) 
        -> std::result::Result<Value, ConversionError> {
        
        if offset + c_type.size() > buffer.size() {
            return Err(ConversionError::BufferOverflow {
                buffer_size: buffer.size(),
                requested_size: offset + c_type.size(),
            });
        }

        unsafe {
            let ptr = buffer.as_ptr().add(offset);
            
            let value = match c_type {
                CType::Int8 => Value::Literal(Literal::InexactReal(*(ptr as *const i8) as f64)),
                CType::Int16 => Value::Literal(Literal::InexactReal(*(ptr as *const i16) as f64)),
                CType::Int32 => Value::Literal(Literal::InexactReal(*(ptr as *const i32) as f64)),
                CType::Int64 => Value::Literal(Literal::InexactReal(*(ptr as *const i64) as f64)),
                CType::UInt8 => Value::Literal(Literal::InexactReal(*ptr as f64)),
                CType::UInt16 => Value::Literal(Literal::InexactReal(*(ptr as *const u16) as f64)),
                CType::UInt32 => Value::Literal(Literal::InexactReal(*(ptr as *const u32) as f64)),
                CType::UInt64 => Value::Literal(Literal::InexactReal(*(ptr as *const u64) as f64)),
                CType::CInt => Value::Literal(Literal::InexactReal(*(ptr as *const libc::c_int) as f64)),
                CType::CUInt => Value::Literal(Literal::InexactReal(*(ptr as *const libc::c_uint) as f64)),
                CType::CSizeT => Value::Literal(Literal::InexactReal(*(ptr as *const libc::size_t) as f64)),
                CType::Float => Value::Literal(Literal::InexactReal(*(ptr as *const f32) as f64)),
                CType::Double => Value::Literal(Literal::InexactReal(*(ptr as *const f64))),
                CType::Bool  => {
                    let int_val = *(ptr as *const i32);
                    Value::Literal(Literal::Boolean(int_val != 0))
                }
                CType::Char  => {
                    let char_val = *(ptr as *const libc::c_char);
                    Value::Literal(Literal::Character(char_val as u8 as char))
                }
                CType::CString  => {
                    let c_str_ptr = *(ptr as *const *const libc::c_char);
                    if c_str_ptr.is_null() {
                        Value::Literal(Literal::String("".to_string()))
                    } else {
                        let c_str = CStr::from_ptr(c_str_ptr);
                        let rust_str = c_str.to_str()
                            .map_err(|e| ConversionError::StringConversion(e.to_string()))?;
                        Value::Literal(Literal::String(rust_str.to_string()))
                    }
                }
                _  => {
                    return Err(ConversionError::TypeMismatch {
                        expected: c_type.clone(),
                        actual: "unsupported type".to_string(),
                    });
                }
            };

            Ok(value)
        }
    }

    /// Clear the string cache
    pub fn clear_cache(&mut self) {
        self.string_cache.clear();
    }
}

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

    #[test]
    fn test_c_type_sizes() {
        assert_eq!(CType::Int32.size(), 4);
        assert_eq!(CType::Int64.size(), 8);
        assert_eq!(CType::Float.size(), 4);
        assert_eq!(CType::Double.size(), 8);
        assert!(CType::Pointer(Box::new(CType::Int32)).size() >= mem::size_of::<*const c_void>());
    }

    #[test]
    fn test_c_type_display() {
        assert_eq!(CType::Int32.to_string(), "int32_t");
        assert_eq!(CType::Pointer(Box::new(CType::Int32)).to_string(), "int32_t*");
        assert_eq!(CType::Array(Box::new(CType::Int32), 10).to_string(), "int32_t[10]");
    }

    #[test]
    fn test_type_marshaller_creation() {
        let marshaller = TypeMarshaller::new();
        assert!(marshaller.resolve_type("int").is_some());
        assert!(marshaller.resolve_type("string").is_some());
    }

    #[test]
    fn test_basic_conversion() {
        let mut marshaller = TypeMarshaller::new();
        let value = Value::Literal(Literal::Number(42.0));
        let buffer = marshaller.to_c_data(&value, &CType::Int32).unwrap();
        
        unsafe {
            let int_val = *(buffer.as_ptr() as *const i32);
            assert_eq!(int_val, 42);
        }
    }

    #[test] 
    fn test_string_conversion() {
        let mut marshaller = TypeMarshaller::new();
        let value = Value::Literal(Literal::String("hello".to_string()));
        let buffer = marshaller.to_c_data(&value, &CType::CString).unwrap();
        
        unsafe {
            let c_str_ptr = *(buffer.as_ptr() as *const *const libc::c_char);
            assert!(!c_str_ptr.is_null());
            
            let c_str = CStr::from_ptr(c_str_ptr);
            assert_eq!(c_str.to_str().unwrap(), "hello");
        }
    }
}