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
//! System interface operations for R7RS compliance.
//!
//! This module implements the system interface procedures required by R7RS Section 6.14:
//! - Process control: exit, emergency-exit
//! - Command line access: command-line
//! - Environment variables: get-environment-variable, get-environment-variables
//! - Time functions: current-second, current-jiffy, jiffies-per-second
//! - System features: features

use crate::eval::value::{Value, PrimitiveProcedure, PrimitiveImpl, ThreadSafeEnvironment};
use crate::effects::Effect;
use crate::diagnostics::{Error as DiagnosticError, Result};
use std::sync::{Arc, Mutex, OnceLock};
// Removed unused HashMap import
use std::time::{SystemTime, UNIX_EPOCH, Instant};

/// Global state for system information
static SYSTEM_STATE: OnceLock<Arc<Mutex<SystemState>>> = OnceLock::new();

/// System state containing command line arguments and startup time
#[derive(Debug)]
struct SystemState {
    /// Command line arguments passed to the program
    command_line_args: Vec<String>,
    /// Time when the program started (for jiffy calculation)
    start_time: Instant,
}

impl SystemState {
    fn new() -> Self {
        Self {
            command_line_args: Vec::new(),
            start_time: Instant::now(),
        }
    }
}

/// Initialize the system state with command line arguments
pub fn initialize_system_state(args: Vec<String>) {
    let state = Arc::new(Mutex::new(SystemState {
        command_line_args: args,
        start_time: Instant::now(),
    }));
    
    let _ = SYSTEM_STATE.set(state);
}

/// Get or create the system state
fn get_system_state() -> Arc<Mutex<SystemState>> {
    SYSTEM_STATE.get_or_init(|| {
        Arc::new(Mutex::new(SystemState::new()))
    }).clone()
}

/// Bind all system interface procedures to the environment
pub fn create_system_bindings(env: &Arc<ThreadSafeEnvironment>) {
    // Bind system procedures using the mutable define method
    env.define("exit".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "exit".to_string(),
        arity_min: 0,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_exit),
        effects: vec![Effect::IO],
    })));

    env.define("emergency-exit".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "emergency-exit".to_string(),
        arity_min: 0,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_emergency_exit),
        effects: vec![Effect::IO],
    })));

    env.define("command-line".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "command-line".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_command_line),
        effects: vec![Effect::Pure],
    })));

    env.define("get-environment-variable".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "get-environment-variable".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_get_environment_variable),
        effects: vec![Effect::IO],
    })));

    env.define("get-environment-variables".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "get-environment-variables".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_get_environment_variables),
        effects: vec![Effect::IO],
    })));

    env.define("current-second".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "current-second".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_current_second),
        effects: vec![Effect::IO],
    })));

    env.define("current-jiffy".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "current-jiffy".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_current_jiffy),
        effects: vec![Effect::IO],
    })));

    env.define("jiffies-per-second".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "jiffies-per-second".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_jiffies_per_second),
        effects: vec![Effect::Pure],
    })));

    env.define("features".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "features".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_features),
        effects: vec![Effect::Pure],
    })));
}

/// Bind all system interface procedures using copy-on-write semantics
/// Returns a new environment with all system procedures bound
pub fn bind_system_procedures_cow(env: &Arc<ThreadSafeEnvironment>) -> Arc<ThreadSafeEnvironment> {
    // Bind all system procedures using chained define calls
    env.define_cow("exit".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "exit".to_string(),
        arity_min: 0,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_exit),
        effects: vec![Effect::IO],
    })))
    .define_cow("emergency-exit".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "emergency-exit".to_string(),
        arity_min: 0,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_emergency_exit),
        effects: vec![Effect::IO],
    })))
    .define_cow("command-line".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "command-line".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_command_line),
        effects: vec![Effect::Pure],
    })))
    .define_cow("get-environment-variable".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "get-environment-variable".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_get_environment_variable),
        effects: vec![Effect::IO],
    })))
    .define_cow("get-environment-variables".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "get-environment-variables".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_get_environment_variables),
        effects: vec![Effect::IO],
    })))
    .define_cow("current-second".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "current-second".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_current_second),
        effects: vec![Effect::IO],
    })))
    .define_cow("current-jiffy".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "current-jiffy".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_current_jiffy),
        effects: vec![Effect::IO],
    })))
    .define_cow("jiffies-per-second".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "jiffies-per-second".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_jiffies_per_second),
        effects: vec![Effect::Pure],
    })))
    .define_cow("features".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "features".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_features),
        effects: vec![Effect::Pure],
    })))
}

// ============= PROCESS CONTROL PROCEDURES =============

/// (exit [obj]) - Exit the program
/// - If obj is omitted or #t, exit with code 0
/// - If obj is #f, exit with code 1  
/// - If obj is an exact integer, exit with that code
/// - Otherwise exit with code 1
pub fn primitive_exit(args: &[Value]) -> Result<Value> {
    if args.len() > 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("exit expects 0 or 1 arguments, got {}", args.len()),
            None,
        )));
    }

    let exit_code = if args.is_empty() {
        0 // Default to success
    } else {
        match &args[0] {
            // #t means success (0)
            Value::Literal(crate::ast::Literal::Boolean(true)) => 0,
            // #f means failure (1)
            Value::Literal(crate::ast::Literal::Boolean(false)) => 1,
            // Number exit code (any numeric literal)
            Value::Literal(literal) if literal.is_number() => {
                if let Some(n) = literal.to_f64() {
                    // Clamp to valid exit code range (0-255 on most systems)
                    (n as i64).clamp(0, 255) as i32
                } else {
                    0 // Default to success if conversion fails
                }
            }
            // Keep old rational case for safety, though it should be caught above
            Value::Literal(crate::ast::Literal::Rational { numerator, denominator }) => {
                let value = (*numerator as f64 / *denominator as f64) as i64;
                value.clamp(0, 255) as i32
            }
            // All other values default to failure
            _ => 1,
        }
    };

    // In a real implementation, this would perform cleanup and then exit
    // For now, we'll simulate by returning an error that can be caught
    Err(Box::new(DiagnosticError::runtime_error(
        format!("Program exit requested with code {exit_code}"),
        None,
    )))
}

/// (emergency-exit [obj]) - Exit immediately without cleanup
/// Same semantics as exit but without running cleanup handlers
pub fn primitive_emergency_exit(args: &[Value]) -> Result<Value> {
    if args.len() > 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("emergency-exit expects 0 or 1 arguments, got {}", args.len()),
            None,
        )));
    }

    let exit_code = if args.is_empty() {
        0 // Default to success
    } else {
        match &args[0] {
            Value::Literal(crate::ast::Literal::Boolean(true)) => 0,
            Value::Literal(crate::ast::Literal::Boolean(false)) => 1,
            Value::Literal(literal) if literal.is_number() => {
                if let Some(n) = literal.to_f64() {
                    (n as i64).clamp(0, 255) as i32
                } else {
                    0
                }
            }
            Value::Literal(crate::ast::Literal::Rational { numerator, denominator }) => {
                let value = (*numerator as f64 / *denominator as f64) as i64;
                value.clamp(0, 255) as i32
            }
            _ => 1,
        }
    };

    // Emergency exit - immediate termination without cleanup
    Err(Box::new(DiagnosticError::runtime_error(
        format!("Emergency exit requested with code {exit_code}"),
        None,
    )))
}

// ============= COMMAND LINE ACCESS =============

/// (command-line) - Return list of command line arguments
pub fn primitive_command_line(_args: &[Value]) -> Result<Value> {
    let state = get_system_state();
    let state_guard = state.lock().map_err(|_| {
        Box::new(DiagnosticError::runtime_error("Failed to access system state".to_string(), None))
    })?;

    // Convert command line arguments to a Scheme list
    let mut result = Value::Nil;
    for arg in state_guard.command_line_args.iter().rev() {
        result = Value::Pair(
            Arc::new(Value::string(arg.clone())),
            Arc::new(result),
        );
    }

    Ok(result)
}

// ============= ENVIRONMENT VARIABLES =============

/// (get-environment-variable name) - Get environment variable value
pub fn primitive_get_environment_variable(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("get-environment-variable expects 1 argument, got {}", args.len()),
            None,
        )));
    }

    // Extract the variable name
    let var_name = match &args[0] {
        Value::Literal(crate::ast::Literal::String(s)) => s.clone(),
        _ => {
            return Err(Box::new(DiagnosticError::runtime_error(
                "get-environment-variable requires a string argument".to_string(),
                None,
            )));
        }
    };

    // Get the environment variable
    match std::env::var(&var_name) {
        Ok(value) => Ok(Value::string(value)),
        Err(_) => Ok(Value::Literal(crate::ast::Literal::Boolean(false))), // R7RS: return #f if not found
    }
}

/// (get-environment-variables) - Get all environment variables as alist
pub fn primitive_get_environment_variables(_args: &[Value]) -> Result<Value> {
    let mut result = Value::Nil;

    // Get all environment variables and build an association list
    let vars: Vec<_> = std::env::vars().collect();
    
    // Build the list in reverse order to maintain proper order
    for (key, value) in vars.into_iter().rev() {
        let pair = Value::Pair(
            Arc::new(Value::string(key)),
            Arc::new(Value::string(value)),
        );
        result = Value::Pair(Arc::new(pair), Arc::new(result));
    }

    Ok(result)
}

// ============= TIME FUNCTIONS =============

/// (current-second) - Return current time as seconds since Unix epoch
pub fn primitive_current_second(_args: &[Value]) -> Result<Value> {
    match SystemTime::now().duration_since(UNIX_EPOCH) {
        Ok(duration) => {
            // Return as exact integer (seconds) + fractional part as real
            let seconds = duration.as_secs();
            let nanos = duration.subsec_nanos();
            let fractional = seconds as f64 + (nanos as f64 / 1_000_000_000.0);
            Ok(Value::number(fractional))
        }
        Err(_) => {
            Err(Box::new(DiagnosticError::runtime_error(
                "Failed to get current time".to_string(),
                None,
            )))
        }
    }
}

/// (current-jiffy) - Return current time in jiffies
/// A jiffy is an implementation-defined unit of time
pub fn primitive_current_jiffy(_args: &[Value]) -> Result<Value> {
    let state = get_system_state();
    let state_guard = state.lock().map_err(|_| {
        Box::new(DiagnosticError::runtime_error("Failed to access system state".to_string(), None))
    })?;

    // Calculate jiffies since program start
    let elapsed = state_guard.start_time.elapsed();
    
    // Use nanoseconds as our jiffy unit for high precision
    let jiffies = elapsed.as_nanos() as i64;
    
    Ok(Value::integer(jiffies))
}

/// (jiffies-per-second) - Return number of jiffies per second
/// This is a constant for our implementation
pub fn primitive_jiffies_per_second(_args: &[Value]) -> Result<Value> {
    // We use nanoseconds as jiffies, so 1 billion jiffies per second  
    Ok(Value::integer(1_000_000_000))
}

// ============= SYSTEM FEATURES =============

/// (features) - Return list of supported feature identifiers
pub fn primitive_features(_args: &[Value]) -> Result<Value> {
    // R7RS-small required features
    let features = vec![
        "r7rs",                    // R7RS compliance
        "exact-closed",            // Exact arithmetic is closed under operations
        "exact-complex",           // Exact complex numbers supported
        "ieee-float",              // IEEE floating point
        "full-unicode",            // Full Unicode support
        "ratios",                  // Rational number support
        
        // Lambdust-specific features
        "lambdust",                // This implementation
        "gradual-typing",          // Gradual type system
        "effect-system",           // Effect system support
        "call/cc",                 // call-with-current-continuation
        "threads",                 // Threading support
        
        // Platform features
        #[cfg(unix)]
        "posix",
        #[cfg(windows)]
        "windows",
        #[cfg(target_pointer_width = "64")]
        "64bit",
        #[cfg(target_pointer_width = "32")]
        "32bit",
        
        // I/O features
        "port-position",           // Port position tracking
        "r7rs-io",                 // R7RS I/O system
        
        // Data structure features
        "bytevectors",             // Bytevector support
        "hashtables",              // Hash table support
        "records",                 // Record type support
    ];

    // Convert to Scheme list
    let mut result = Value::Nil;
    for feature in features.iter().rev() {
        let symbol_id = crate::utils::symbol::intern_symbol(feature.to_string());
        result = Value::Pair(
            Arc::new(Value::symbol(symbol_id)),
            Arc::new(result),
        );
    }

    Ok(result)
}

#[cfg(test)]
mod tests {
    use super::*;
    // Environment is not needed for tests since we use ThreadSafeEnvironment directly

    fn create_test_env() -> Arc<ThreadSafeEnvironment> {
        Arc::new(ThreadSafeEnvironment::new(None, 0))
    }

    #[test]
    fn test_system_bindings() {
        // Test that all system binding functions can be called without panicking
        let env = create_test_env();
        
        // This should not panic even if the COW semantics don't work as expected
        create_system_bindings(&env);
        
        // Test that we can at least call the primitive functions directly
        let result = primitive_jiffies_per_second(&[]);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), Value::integer(1_000_000_000));
        
        let result = primitive_features(&[]);
        assert!(result.is_ok());
        // Should return a list
        match result.unwrap() {
            Value::Pair(_, _) | Value::Nil => {
                // Good, it's a list
            }
            _ => panic!("features should return a list"),
        }
    }

    #[test]
    fn test_command_line() {
        // Initialize with test arguments
        initialize_system_state(vec![
            "lambdust".to_string(),
            "--version".to_string(),
            "test.scm".to_string(),
        ]);

        let result = primitive_command_line(&[]).unwrap();
        
        // Should return a list of strings
        match result {
            Value::Pair(car, _) => {
                // First element should be "lambdust"
                match car.as_ref() {
                    Value::Literal(crate::ast::Literal::String(s)) => {
                        assert_eq!(s, "lambdust");
                    }
                    _ => panic!("Expected string"),
                }
            }
            Value::Nil => {
                // Empty command line is also valid
            }
            _ => panic!("Expected list"),
        }
    }

    #[test]
    fn test_get_environment_variable() {
        // Test with a known environment variable (PATH should exist on most systems)
        let args = vec![Value::string("PATH")];
        let result = primitive_get_environment_variable(&args).unwrap();
        
        // Should return either a string (if PATH exists) or #f (if not)
        match result {
            Value::Literal(crate::ast::Literal::String(_)) => {
                // PATH exists - good
            }
            Value::Literal(crate::ast::Literal::Boolean(false)) => {
                // PATH doesn't exist - unlikely but possible
            }
            _ => panic!("Expected string or #f"),
        }

        // Test with non-existent variable
        let args = vec![Value::string("NONEXISTENT_VAR_12345")];
        let result = primitive_get_environment_variable(&args).unwrap();
        assert_eq!(result, Value::Literal(crate::ast::Literal::Boolean(false)));
    }

    #[test]
    fn test_get_environment_variables() {
        let result = primitive_get_environment_variables(&[]).unwrap();
        
        // Should return an association list
        match result {
            Value::Nil => {
                // Empty environment is theoretically possible
            }
            Value::Pair(_, _) => {
                // Non-empty list is expected
            }
            _ => panic!("Expected association list"),
        }
    }

    #[test]
    fn test_time_functions() {
        // Test current-second
        let result = primitive_current_second(&[]).unwrap();
        match result {
            Value::Literal(crate::ast::Literal::Number(seconds)) => {
                // Should be a reasonable timestamp (after year 2020)
                assert!(seconds > 1_600_000_000.0);
            }
            _ => panic!("Expected number"),
        }

        // Test jiffies-per-second
        let result = primitive_jiffies_per_second(&[]).unwrap();
        assert_eq!(result, Value::integer(1_000_000_000));

        // Test current-jiffy
        let result = primitive_current_jiffy(&[]).unwrap();
        match result {
            Value::Literal(crate::ast::Literal::Number(_)) => {
                // Should be some number (could be integer or float depending on internal representation)
            }
            _ => panic!("Expected number"),
        }
    }

    #[test]
    fn test_features() {
        let result = primitive_features(&[]).unwrap();
        
        // Should return a list containing required R7RS features
        let mut current = &result;
        let mut found_r7rs = false;
        let mut found_lambdust = false;

        while let Value::Pair(car, cdr) = current {
            if let Value::Symbol(sym_id) = car.as_ref() {
                if let Some(name) = crate::utils::symbol::symbol_name(*sym_id) {
                    if name == "r7rs" {
                        found_r7rs = true;
                    }
                    if name == "lambdust" {
                        found_lambdust = true;
                    }
                }
            }
            current = cdr;
        }

        assert!(found_r7rs, "Should include 'r7rs' feature");
        assert!(found_lambdust, "Should include 'lambdust' feature");
    }

    #[test]
    fn test_exit_semantics() {
        // Test exit with no arguments (should default to 0)
        let result = primitive_exit(&[]);
        assert!(result.is_err()); // Exit should cause an error

        // Test exit with #t (should exit with 0)
        let args = vec![Value::boolean(true)];
        let result = primitive_exit(&args);
        assert!(result.is_err());

        // Test exit with #f (should exit with 1)  
        let args = vec![Value::boolean(false)];
        let result = primitive_exit(&args);
        assert!(result.is_err());

        // Test exit with integer
        let args = vec![Value::integer(42)];
        let result = primitive_exit(&args);
        assert!(result.is_err());
    }

    #[test]
    fn test_error_conditions() {
        // Test wrong number of arguments
        let result = primitive_get_environment_variable(&[]);
        assert!(result.is_err());

        let result = primitive_get_environment_variable(&[Value::integer(1), Value::integer(2)]);
        assert!(result.is_err());

        // Test wrong argument types
        let result = primitive_get_environment_variable(&[Value::integer(42)]);
        assert!(result.is_err());
    }
}