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
//! Macro environment management for Lambdust.
//!
//! This module implements the macro environment system, which is separate from
//! the runtime environment and manages macro definitions and their scoping.
//! Macro environments handle the lexical scoping of macro definitions and
//! support proper macro visibility rules.

use super::{MacroTransformer, MacroContext, next_hygiene_id};
// use crate::diagnostics::{Error, Result, Span};
// use crate::eval::Environment;
use std::collections::HashMap;
use std::rc::Rc;
use std::cell::RefCell;

/// A macro environment that manages macro definitions.
#[derive(Debug)]
pub struct MacroEnvironment {
    /// The parent environment (for lexical scoping)
    parent: Option<Rc<MacroEnvironment>>,
    /// Macro definitions in this environment
    macros: RefCell<HashMap<String, MacroTransformer>>,
    /// Environment ID for debugging
    id: u64,
    /// Generation for garbage collection
    generation: u64,
}

impl MacroEnvironment {
    /// Creates a new empty macro environment.
    pub fn new() -> Self {
        Self {
            parent: None,
            macros: RefCell::new(HashMap::new()),
            id: next_hygiene_id(),
            generation: 0,
        }
    }
    
    /// Creates a new macro environment with a parent.
    pub fn with_parent(parent: Rc<MacroEnvironment>) -> Self {
        Self {
            parent: Some(parent),
            macros: RefCell::new(HashMap::new()),
            id: next_hygiene_id(),
            generation: 0,
        }
    }
    
    /// Creates a child environment.
    pub fn extend(self: &Rc<Self>) -> Rc<MacroEnvironment> {
        Rc::new(MacroEnvironment::with_parent(self.clone()))
    }
    
    /// Defines a macro in this environment.
    pub fn define(&self, name: String, transformer: MacroTransformer) {
        self.macros.borrow_mut().insert(name, transformer);
    }
    
    /// Looks up a macro by name.
    pub fn lookup(&self, name: &str) -> Option<MacroTransformer> {
        // First check this environment
        if let Some(transformer) = self.macros.borrow().get(name) {
            return Some(transformer.clone());
        }
        
        // Then check parent environments
        if let Some(parent) = &self.parent {
            parent.lookup(name)
        } else {
            None
        }
    }
    
    /// Checks if a macro is defined in this environment (not parent).
    pub fn locally_defined(&self, name: &str) -> bool {
        self.macros.borrow().contains_key(name)
    }
    
    /// Removes a macro definition from this environment.
    pub fn undefine(&self, name: &str) -> bool {
        self.macros.borrow_mut().remove(name).is_some()
    }
    
    /// Gets all macro names defined in this environment.
    pub fn local_names(&self) -> Vec<String> {
        self.macros.borrow().keys().cloned().collect()
    }
    
    /// Gets all macro names visible in this environment.
    pub fn all_names(&self) -> Vec<String> {
        let mut names = self.local_names();
        
        if let Some(parent) = &self.parent {
            let parent_names = parent.all_names();
            for name in parent_names {
                if !names.contains(&name) {
                    names.push(name);
                }
            }
        }
        
        names.sort();
        names
    }
    
    /// Gets the environment ID.
    pub fn id(&self) -> u64 {
        self.id
    }
    
    /// Gets the generation.
    pub fn generation(&self) -> u64 {
        self.generation
    }
    
    /// Checks if this environment is an ancestor of another.
    pub fn is_ancestor_of(&self, other: &MacroEnvironment) -> bool {
        if let Some(parent) = &other.parent {
            self.id == parent.id || self.is_ancestor_of(parent)
        } else {
            false
        }
    }
    
    /// Creates a new macro context for this environment.
    pub fn create_context(&self) -> MacroContext {
        MacroContext::new(self.id)
    }
}

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

/// A builder for constructing macro environments with predefined macros.
#[derive(Debug)]
pub struct MacroEnvironmentBuilder {
    environment: MacroEnvironment,
}

impl MacroEnvironmentBuilder {
    /// Creates a new builder.
    pub fn new() -> Self {
        Self {
            environment: MacroEnvironment::new(),
        }
    }
    
    /// Creates a builder with a parent environment.
    pub fn with_parent(parent: Rc<MacroEnvironment>) -> Self {
        Self {
            environment: MacroEnvironment::with_parent(parent),
        }
    }
    
    /// Adds a macro definition to the environment.
    pub fn define_macro(self, name: impl Into<String>, transformer: MacroTransformer) -> Self {
        self.environment.define(name.into(), transformer);
        self
    }
    
    /// Builds the environment.
    pub fn build(self) -> MacroEnvironment {
        self.environment
    }
}

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

/// A scope for managing macro definitions with automatic cleanup.
#[derive(Debug)]
pub struct MacroScope {
    /// The environment for this scope
    environment: Rc<MacroEnvironment>,
    /// Names of macros defined in this scope (for cleanup)
    defined_names: Vec<String>,
}

impl MacroScope {
    /// Creates a new macro scope.
    pub fn new(environment: Rc<MacroEnvironment>) -> Self {
        Self {
            environment,
            defined_names: Vec::new(),
        }
    }
    
    /// Defines a macro in this scope.
    pub fn define(&mut self, name: impl Into<String>, transformer: MacroTransformer) {
        let name = name.into();
        self.environment.define(name.clone(), transformer);
        self.defined_names.push(name);
    }
    
    /// Looks up a macro in this scope.
    pub fn lookup(&self, name: &str) -> Option<MacroTransformer> {
        self.environment.lookup(name)
    }
    
    /// Gets the environment for this scope.
    pub fn environment(&self) -> &Rc<MacroEnvironment> {
        &self.environment
    }
    
    /// Creates a child scope.
    pub fn child(&self) -> MacroScope {
        MacroScope::new(self.environment.extend())
    }
}

impl Drop for MacroScope {
    fn drop(&mut self) {
        // Clean up macros defined in this scope
        for name in &self.defined_names {
            self.environment.undefine(name);
        }
    }
}

/// A macro resolver that manages macro visibility and resolution.
#[derive(Debug)]
pub struct MacroResolver {
    /// Stack of macro environments
    environment_stack: Vec<Rc<MacroEnvironment>>,
    /// Current macro environment
    current_environment: Rc<MacroEnvironment>,
}

impl MacroResolver {
    /// Creates a new macro resolver.
    pub fn new(environment: Rc<MacroEnvironment>) -> Self {
        Self {
            environment_stack: vec![environment.clone()],
            current_environment: environment,
        }
    }
    
    /// Pushes a new environment onto the stack.
    pub fn push_environment(&mut self, environment: Rc<MacroEnvironment>) {
        self.environment_stack.push(self.current_environment.clone());
        self.current_environment = environment;
    }
    
    /// Pops the current environment from the stack.
    pub fn pop_environment(&mut self) -> Option<Rc<MacroEnvironment>> {
        if let Some(previous) = self.environment_stack.pop() {
            let current = self.current_environment.clone();
            self.current_environment = previous;
            Some(current)
        } else {
            None
        }
    }
    
    /// Gets the current environment.
    pub fn current_environment(&self) -> &Rc<MacroEnvironment> {
        &self.current_environment
    }
    
    /// Resolves a macro name to a transformer.
    pub fn resolve(&self, name: &str) -> Option<MacroTransformer> {
        self.current_environment.lookup(name)
    }
    
    /// Defines a macro in the current environment.
    pub fn define(&self, name: impl Into<String>, transformer: MacroTransformer) {
        self.current_environment.define(name.into(), transformer);
    }
    
    /// Checks if a name refers to a macro.
    pub fn is_macro(&self, name: &str) -> bool {
        self.resolve(name).is_some()
    }
    
    /// Creates a new scope within the current environment.
    pub fn enter_scope(&mut self) -> MacroScope {
        let child_env = self.current_environment.extend();
        self.push_environment(child_env.clone());
        MacroScope::new(child_env)
    }
    
    /// Exits the current scope.
    pub fn exit_scope(&mut self) {
        self.pop_environment();
    }
}

/// Utility functions for macro environment management.
pub mod utils {
    use super::*;
    
    /// Creates a global macro environment with built-in macros.
    pub fn global_macro_environment() -> Rc<MacroEnvironment> {
        // Built-in macros will be added here by the builtins module
        
        Rc::new(MacroEnvironment::new())
    }
    
    /// Merges two macro environments, with the second taking precedence.
    pub fn merge_environments(
        base: Rc<MacroEnvironment>,
        overlay: Rc<MacroEnvironment>,
    ) -> Rc<MacroEnvironment> {
        let merged = base.extend();
        
        // Copy all macros from overlay to merged
        for name in overlay.local_names() {
            if let Some(transformer) = overlay.lookup(&name) {
                merged.define(name, transformer);
            }
        }
        
        merged
    }
    
    /// Creates a macro environment with only the specified macros.
    pub fn filtered_environment(
        source: &MacroEnvironment,
        allowed_names: &[String],
    ) -> Rc<MacroEnvironment> {
        let filtered = Rc::new(MacroEnvironment::new());
        
        for name in allowed_names {
            if let Some(transformer) = source.lookup(name) {
                filtered.define(name.clone(), transformer);
            }
        }
        
        filtered
    }
    
    /// Checks if two environments have the same macro definitions.
    pub fn environments_equal(env1: &MacroEnvironment, env2: &MacroEnvironment) -> bool {
        let names1 = env1.all_names();
        let names2 = env2.all_names();
        
        if names1.len() != names2.len() {
            return false;
        }
        
        for name in &names1 {
            if !names2.contains(name) {
                return false;
            }
            
            // In a full implementation, we'd compare the actual transformers
            // For now, just check that both have the macro
            if env1.lookup(name).is_none() || env2.lookup(name).is_none() {
                return false;
            }
        }
        
        true
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::macro_system::{Pattern, Template};
    
    fn create_dummy_transformer(name: &str) -> MacroTransformer {
        MacroTransformer {
            pattern: Pattern::Variable(format!("{name}_pattern")),
            template: Template::Variable(format!("{name}_template")),
            definition_env: crate::eval::global_environment(),
            name: Some(name.to_string()),
            source: None,
        }
    }
    
    #[test]
    fn test_macro_environment_creation() {
        let env = MacroEnvironment::new();
        assert_eq!(env.local_names().len(), 0);
        assert!(env.parent.is_none());
    }
    
    #[test]
    fn test_macro_definition_and_lookup() {
        let env = MacroEnvironment::new();
        let transformer = create_dummy_transformer("test-macro");
        
        env.define("test-macro".to_string(), transformer.clone());
        
        let looked_up = env.lookup("test-macro");
        assert!(looked_up.is_some());
        assert_eq!(looked_up.unwrap().name, transformer.name);
    }
    
    #[test]
    fn test_parent_environment_lookup() {
        let parent = Rc::new(MacroEnvironment::new());
        let child = parent.extend();
        
        let transformer = create_dummy_transformer("parent-macro");
        parent.define("parent-macro".to_string(), transformer.clone());
        
        let looked_up = child.lookup("parent-macro");
        assert!(looked_up.is_some());
        assert_eq!(looked_up.unwrap().name, transformer.name);
    }
    
    #[test]
    fn test_local_vs_parent_definitions() {
        let parent = Rc::new(MacroEnvironment::new());
        let child = parent.extend();
        
        let parent_transformer = create_dummy_transformer("parent-version");
        let child_transformer = create_dummy_transformer("child-version");
        
        parent.define("macro".to_string(), parent_transformer);
        child.define("macro".to_string(), child_transformer.clone());
        
        let looked_up = child.lookup("macro");
        assert!(looked_up.is_some());
        assert_eq!(looked_up.unwrap().name, child_transformer.name);
    }
    
    #[test]
    fn test_macro_scope() {
        let env = Rc::new(MacroEnvironment::new());
        let transformer = create_dummy_transformer("scoped-macro");
        
        {
            let mut scope = MacroScope::new(env.clone());
            scope.define("scoped-macro", transformer.clone());
            
            assert!(scope.lookup("scoped-macro").is_some());
            assert!(env.lookup("scoped-macro").is_some());
        }
        
        // After scope is dropped, macro should be undefined
        assert!(env.lookup("scoped-macro").is_none());
    }
    
    #[test]
    fn test_macro_resolver() {
        let env = Rc::new(MacroEnvironment::new());
        let resolver = MacroResolver::new(env.clone());
        
        let transformer = create_dummy_transformer("resolved-macro");
        resolver.define("resolved-macro", transformer.clone());
        
        assert!(resolver.is_macro("resolved-macro"));
        assert!(!resolver.is_macro("nonexistent-macro"));
        
        let resolved = resolver.resolve("resolved-macro");
        assert!(resolved.is_some());
        assert_eq!(resolved.unwrap().name, transformer.name);
    }
    
    #[test]
    fn test_environment_builder() {
        let transformer = create_dummy_transformer("built-macro");
        
        let env = MacroEnvironmentBuilder::new()
            .define_macro("built-macro", transformer.clone())
            .build();
        
        let looked_up = env.lookup("built-macro");
        assert!(looked_up.is_some());
        assert_eq!(looked_up.unwrap().name, transformer.name);
    }
}