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
//! Import functionality for bringing modules into scope.
//!
//! Handles various import patterns:
//! - (import (lambdust string)) - Import all exports
//! - (import (lambdust string) (only string-length string-ref)) - Import specific symbols
//! - (import (lambdust string) (except string-fill!)) - Import all except specific symbols  
//! - (import (lambdust string) (rename (string-length str-len))) - Import with renaming
//! - (import (lambdust string) (prefix string:)) - Import with prefix

use super::{ImportSpec, ImportConfig, ModuleError};
use crate::diagnostics::{Error, Result, Spanned};
use crate::ast::Expr;
use crate::eval::Value;
use std::collections::HashMap;

/// Applies import configuration to module exports to get final bindings.
pub fn apply_import_config(
    exports: &HashMap<String, Value>,
    config: &ImportConfig,
) -> Result<HashMap<String, Value>> {
    match config {
        ImportConfig::All => Ok(exports.clone()),
        ImportConfig::Only(symbols) => apply_only_import(exports, symbols),
        ImportConfig::Except(symbols) => apply_except_import(exports, symbols),
        ImportConfig::Rename(rename_map) => apply_rename_import(exports, rename_map),
        ImportConfig::Prefix(prefix) => apply_prefix_import(exports, prefix),
    }
}

/// Imports only the specified symbols.
fn apply_only_import(
    exports: &HashMap<String, Value>,
    symbols: &[String],
) -> Result<HashMap<String, Value>> {
    let mut result = HashMap::new();
    
    for symbol in symbols {
        if let Some(value) = exports.get(symbol) {
            result.insert(symbol.clone(), value.clone());
        } else {
            return Err(Box::new(Error::from(ModuleError::ImportConflict(
                format!("Symbol '{symbol}' not found in module exports")
            ))));
        }
    }
    
    Ok(result)
}

/// Imports all symbols except the specified ones.
fn apply_except_import(
    exports: &HashMap<String, Value>,
    except_symbols: &[String],
) -> Result<HashMap<String, Value>> {
    let mut result = HashMap::new();
    
    for (symbol, value) in exports {
        if !except_symbols.contains(symbol) {
            result.insert(symbol.clone(), value.clone());
        }
    }
    
    Ok(result)
}

/// Imports symbols with renaming.
fn apply_rename_import(
    exports: &HashMap<String, Value>,
    rename_map: &HashMap<String, String>,
) -> Result<HashMap<String, Value>> {
    let mut result = HashMap::new();
    
    for (original_name, new_name) in rename_map {
        if let Some(value) = exports.get(original_name) {
            result.insert(new_name.clone(), value.clone());
        } else {
            return Err(Box::new(Error::from(ModuleError::ImportConflict(
                format!("Symbol '{original_name}' not found in module exports")
            ))));
        }
    }
    
    Ok(result)
}

/// Imports all symbols with a prefix.
fn apply_prefix_import(
    exports: &HashMap<String, Value>,
    prefix: &str,
) -> Result<HashMap<String, Value>> {
    let mut result = HashMap::new();
    
    for (symbol, value) in exports {
        let prefixed_name = format!("{prefix}{symbol}");
        result.insert(prefixed_name, value.clone());
    }
    
    Ok(result)
}

/// Parses import specifications from Scheme syntax.
pub fn parse_import_spec(import_form: &[Spanned<Expr>]) -> Result<ImportSpec> {
    if import_form.is_empty() {
        return Err(Box::new(Error::syntax_error(
            "Empty import specification".to_string(),
            None,
        )));
    }

    // First element should be the module identifier
    let module_name = extract_module_name(&import_form[0])?;
    let module_id = super::name::parse_module_name(&module_name)?;
    
    // Parse import configuration from remaining elements
    let config = if import_form.len() == 1 {
        ImportConfig::All
    } else {
        parse_import_config(&import_form[1..])?
    };

    Ok(ImportSpec {
        module_id,
        config,
    })
}

/// Extracts module name from an expression.
fn extract_module_name(expr: &Spanned<Expr>) -> Result<String> {
    use crate::ast::Expr;
    
    match &expr.inner {
        Expr::List(elements) => {
            // Convert list of symbols to module name string
            let mut parts = Vec::new();
            for element in elements {
                match &element.inner {
                    Expr::Symbol(symbol) => parts.push(symbol.clone()),
                    _ => return Err(Box::new(Error::syntax_error(
                        "Module name must contain only symbols".to_string(),
                        Some(element.span),
                    ))),
                }
            }
            Ok(format!("({})", parts.join(" ")))
        }
        Expr::Symbol(symbol) => {
            // Single symbol module name
            Ok(format!("({symbol})"))
        }
        _ => Err(Box::new(Error::syntax_error(
            "Invalid module name format".to_string(),
            Some(expr.span),
        ))),
    }
}

/// Parses import configuration (only, except, rename, prefix).
fn parse_import_config(config_forms: &[Spanned<Expr>]) -> Result<ImportConfig> {
    use crate::ast::Expr;
    
    if config_forms.len() != 1 {
        return Err(Box::new(Error::syntax_error(
            "Import configuration must be a single form".to_string(),
            None,
        )));
    }

    match &config_forms[0].inner {
        Expr::List(elements) if !elements.is_empty() => {
            match &elements[0].inner {
                Expr::Symbol(keyword) => {
                    match keyword.as_str() {
                        "only" => parse_only_config(&elements[1..]),
                        "except" => parse_except_config(&elements[1..]),
                        "rename" => parse_rename_config(&elements[1..]),
                        "prefix" => parse_prefix_config(&elements[1..]),
                        _ => Err(Box::new(Error::syntax_error(
                            format!("Unknown import keyword: {keyword}"),
                            Some(elements[0].span),
                        ))),
                    }
                }
                _ => Err(Box::new(Error::syntax_error(
                    "Import configuration must start with a keyword".to_string(),
                    Some(elements[0].span),
                ))),
            }
        }
        _ => Err(Box::new(Error::syntax_error(
            "Import configuration must be a list".to_string(),
            Some(config_forms[0].span),
        ))),
    }
}

/// Parses 'only' import configuration.
fn parse_only_config(elements: &[Spanned<Expr>]) -> Result<ImportConfig> {
    let mut symbols = Vec::new();
    
    for element in elements {
        match &element.inner {
            Expr::Identifier(symbol) => symbols.push(symbol.clone()),
            _ => return Err(Box::new(Error::syntax_error(
                "Only configuration must contain only symbols".to_string(),
                Some(element.span),
            ))),
        }
    }
    
    Ok(ImportConfig::Only(symbols))
}

/// Parses 'except' import configuration.
fn parse_except_config(elements: &[Spanned<Expr>]) -> Result<ImportConfig> {
    let mut symbols = Vec::new();
    
    for element in elements {
        match &element.inner {
            Expr::Identifier(symbol) => symbols.push(symbol.clone()),
            _ => return Err(Box::new(Error::syntax_error(
                "Except configuration must contain only symbols".to_string(),
                Some(element.span),
            ))),
        }
    }
    
    Ok(ImportConfig::Except(symbols))
}

/// Parses 'rename' import configuration.
fn parse_rename_config(elements: &[Spanned<Expr>]) -> Result<ImportConfig> {
    let mut rename_map = HashMap::new();
    
    for element in elements {
        match &element.inner {
            Expr::Application { operator, operands } if operands.len() == 1 => {
                let original = match &operator.inner {
                    Expr::Identifier(symbol) => symbol.clone(),
                    _ => return Err(Box::new(Error::syntax_error(
                        "Rename pair must contain symbols".to_string(),
                        Some(operator.span),
                    ))),
                };
                
                let new_name = match &operands[0].inner {
                    Expr::Identifier(symbol) => symbol.clone(),
                    _ => return Err(Box::new(Error::syntax_error(
                        "Rename pair must contain symbols".to_string(),
                        Some(operands[0].span),
                    ))),
                };
                
                rename_map.insert(original, new_name);
            }
            Expr::Pair { car, cdr } => {
                let original = match &car.inner {
                    Expr::Identifier(symbol) => symbol.clone(),
                    _ => return Err(Box::new(Error::syntax_error(
                        "Rename pair must contain symbols".to_string(),
                        Some(car.span),
                    ))),
                };
                
                let new_name = match &cdr.inner {
                    Expr::Identifier(symbol) => symbol.clone(),
                    _ => return Err(Box::new(Error::syntax_error(
                        "Rename pair must contain symbols".to_string(),
                        Some(cdr.span),
                    ))),
                };
                
                rename_map.insert(original, new_name);
            }
            _ => return Err(Box::new(Error::syntax_error(
                "Rename configuration must contain pairs of symbols".to_string(),
                Some(element.span),
            ))),
        }
    }
    
    Ok(ImportConfig::Rename(rename_map))
}

/// Parses 'prefix' import configuration.
fn parse_prefix_config(elements: &[Spanned<Expr>]) -> Result<ImportConfig> {
    if elements.len() != 1 {
        return Err(Box::new(Error::syntax_error(
            "Prefix configuration must contain exactly one symbol".to_string(),
            None,
        )));
    }
    
    match &elements[0].inner {
        Expr::Identifier(prefix) => Ok(ImportConfig::Prefix(prefix.clone())),
        _ => Err(Box::new(Error::syntax_error(
            "Prefix must be a symbol".to_string(),
            Some(elements[0].span),
        ))),
    }
}

/// Validates an import specification.
pub fn validate_import_spec(spec: &ImportSpec) -> Result<()> {
    super::name::validate_module_id(&spec.module_id)?;
    
    match &spec.config {
        ImportConfig::Only(symbols) | ImportConfig::Except(symbols) => {
            if symbols.is_empty() {
                return Err(Box::new(Error::syntax_error(
                    "Import configuration cannot be empty".to_string(),
                    None,
                )));
            }
        }
        ImportConfig::Rename(rename_map) => {
            if rename_map.is_empty() {
                return Err(Box::new(Error::syntax_error(
                    "Rename configuration cannot be empty".to_string(),
                    None,
                )));
            }
            
            // Check for duplicate target names
            let mut target_names = std::collections::HashSet::new();
            for target in rename_map.values() {
                if !target_names.insert(target) {
                    return Err(Box::new(Error::syntax_error(
                        format!("Duplicate rename target: {target}"),
                        None,
                    )));
                }
            }
        }
        ImportConfig::Prefix(prefix) => {
            if prefix.is_empty() {
                return Err(Box::new(Error::syntax_error(
                    "Prefix cannot be empty".to_string(),
                    None,
                )));
            }
        }
        ImportConfig::All => {
            // No validation needed for 'all' imports
        }
    }
    
    Ok(())
}

/// Merges multiple import bindings, detecting conflicts.
pub fn merge_import_bindings(
    bindings_list: &[HashMap<String, Value>],
) -> Result<HashMap<String, Value>> {
    let mut result = HashMap::new();
    
    for bindings in bindings_list {
        for (symbol, value) in bindings {
            if let Some(existing_value) = result.get(symbol) {
                // Check if it's the same value (allowing re-import of same binding)
                if !values_equivalent(existing_value, value) {
                    return Err(Box::new(Error::from(ModuleError::ImportConflict(
                        format!("Symbol '{symbol}' imported from multiple modules with different values")
                    ))));
                }
            } else {
                result.insert(symbol.clone(), value.clone());
            }
        }
    }
    
    Ok(result)
}

/// Checks if two values are equivalent for import conflict detection.
fn values_equivalent(a: &Value, b: &Value) -> bool {
    // For now, use structural equality
    // In a full implementation, this might check for procedure identity
    a == b
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::eval::Value;
    use std::collections::HashMap;

    #[test]
    fn test_apply_only_import() {
        let mut exports = HashMap::new();
        exports.insert("string-length".to_string(), Value::integer(42));
        exports.insert("string-ref".to_string(), Value::integer(43));
        exports.insert("string-set!".to_string(), Value::integer(44));

        let symbols = vec!["string-length".to_string(), "string-ref".to_string()];
        let result = apply_only_import(&exports, &symbols).unwrap();

        assert_eq!(result.len(), 2);
        assert!(result.contains_key("string-length"));
        assert!(result.contains_key("string-ref"));
        assert!(!result.contains_key("string-set!"));
    }

    #[test]
    fn test_apply_except_import() {
        let mut exports = HashMap::new();
        exports.insert("string-length".to_string(), Value::integer(42));
        exports.insert("string-ref".to_string(), Value::integer(43));
        exports.insert("string-set!".to_string(), Value::integer(44));

        let except_symbols = vec!["string-set!".to_string()];
        let result = apply_except_import(&exports, &except_symbols).unwrap();

        assert_eq!(result.len(), 2);
        assert!(result.contains_key("string-length"));
        assert!(result.contains_key("string-ref"));
        assert!(!result.contains_key("string-set!"));
    }

    #[test]
    fn test_apply_rename_import() {
        let mut exports = HashMap::new();
        exports.insert("string-length".to_string(), Value::integer(42));
        exports.insert("string-ref".to_string(), Value::integer(43));

        let mut rename_map = HashMap::new();
        rename_map.insert("string-length".to_string(), "str-len".to_string());
        rename_map.insert("string-ref".to_string(), "str-ref".to_string());

        let result = apply_rename_import(&exports, &rename_map).unwrap();

        assert_eq!(result.len(), 2);
        assert!(result.contains_key("str-len"));
        assert!(result.contains_key("str-ref"));
        assert!(!result.contains_key("string-length"));
        assert!(!result.contains_key("string-ref"));
    }

    #[test]
    fn test_apply_prefix_import() {
        let mut exports = HashMap::new();
        exports.insert("length".to_string(), Value::integer(42));
        exports.insert("ref".to_string(), Value::integer(43));

        let result = apply_prefix_import(&exports, "string:").unwrap();

        assert_eq!(result.len(), 2);
        assert!(result.contains_key("string:length"));
        assert!(result.contains_key("string:ref"));
        assert!(!result.contains_key("length"));
        assert!(!result.contains_key("ref"));
    }

    #[test]
    fn test_merge_import_bindings_no_conflict() {
        let mut bindings1 = HashMap::new();
        bindings1.insert("a".to_string(), Value::integer(1));
        bindings1.insert("b".to_string(), Value::integer(2));

        let mut bindings2 = HashMap::new();
        bindings2.insert("c".to_string(), Value::integer(3));
        bindings2.insert("d".to_string(), Value::integer(4));

        let result = merge_import_bindings(&[bindings1, bindings2]).unwrap();

        assert_eq!(result.len(), 4);
        assert!(result.contains_key("a"));
        assert!(result.contains_key("b"));
        assert!(result.contains_key("c"));
        assert!(result.contains_key("d"));
    }

    #[test]
    fn test_merge_import_bindings_with_conflict() {
        let mut bindings1 = HashMap::new();
        bindings1.insert("a".to_string(), Value::integer(1));

        let mut bindings2 = HashMap::new();
        bindings2.insert("a".to_string(), Value::integer(2)); // Different value

        let result = merge_import_bindings(&[bindings1, bindings2]);
        assert!(result.is_err());
    }

    #[test]
    fn test_merge_import_bindings_same_value() {
        let mut bindings1 = HashMap::new();
        bindings1.insert("a".to_string(), Value::integer(1));

        let mut bindings2 = HashMap::new();
        bindings2.insert("a".to_string(), Value::integer(1)); // Same value

        let result = merge_import_bindings(&[bindings1, bindings2]).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result.get("a"), Some(&Value::integer(1)));
    }
}