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
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
#![allow(unused_variables)]
//! Advanced macro system with procedural macros and enhanced debugging.
//!
//! This module extends the existing macro system with procedural macros,
//! advanced pattern matching, macro debugging capabilities, and enhanced
//! hygiene controls.

use super::code_generation::{AstTransformer, AstTemplate};
use crate::ast::Expr;
use crate::diagnostics::{Error, Result, Span, Spanned};
use crate::eval::{Value, Environment, Evaluator};
use crate::macro_system::{MacroExpander, Pattern, HygieneContext};
use crate::utils::{intern_symbol, SymbolId};
use std::collections::{HashMap, VecDeque};
use std::rc::Rc;

/// A procedural macro that can generate code programmatically.
#[derive(Debug, Clone)]
pub struct ProceduralMacroDefinition {
    /// Name of the macro
    pub name: String,
    /// The procedure that transforms input to output
    pub transformer: Value, // Should be a procedure
    /// Environment where the macro was defined
    pub definition_env: Rc<Environment>,
    /// Metadata associated with the macro
    pub metadata: HashMap<String, Value>,
    /// Source location
    pub source: Option<Span>,
}

/// Enhanced pattern matching for macros.
#[derive(Debug, Clone)]
pub enum AdvancedPattern {
    /// Basic patterns from the existing system
    Basic(Pattern),
    /// Conditional pattern with guard
    Conditional {
        /// The underlying pattern to match
        pattern: Box<AdvancedPattern>,
        /// The guard condition that must be satisfied
        guard: GuardExpression,
    },
    /// Typed pattern with type constraint
    Typed {
        /// The underlying pattern to match
        pattern: Box<AdvancedPattern>,
        /// The type constraint that must be satisfied
        type_constraint: TypeConstraint,
    },
    /// Range pattern for numbers
    Range {
        /// Minimum value (inclusive if specified)
        min: Option<f64>,
        /// Maximum value (inclusive/exclusive based on inclusive flag)
        max: Option<f64>,
        /// Whether the range bounds are inclusive
        inclusive: bool,
    },
    /// Regular expression pattern for strings
    Regex {
        /// The regular expression pattern string
        pattern: String,
        /// Regex flags (e.g., "i" for case-insensitive)
        flags: Vec<String>,
    },
    /// Destructuring pattern for complex data
    Destructure {
        /// The type of structure being destructured
        structure_type: StructureType,
        /// Patterns for each field of the structure
        field_patterns: Vec<(String, AdvancedPattern)>,
    },
    /// Sequence pattern with length constraints
    Sequence {
        /// Pattern that each element must match
        element_pattern: Box<AdvancedPattern>,
        /// Minimum number of elements required
        min_length: Option<usize>,
        /// Maximum number of elements allowed
        max_length: Option<usize>,
    },
    /// Optional pattern (may or may not match)
    Optional(Box<AdvancedPattern>),
    /// Alternative patterns (OR)
    Alternative(Vec<AdvancedPattern>),
    /// Combination patterns (AND)
    Combination(Vec<AdvancedPattern>),
}

/// Guard expression for conditional patterns.
#[derive(Debug, Clone)]
pub enum GuardExpression {
    /// Predicate function call
    Predicate(String),
    /// Comparison operation
    Comparison {
        /// The comparison operator to use
        operator: ComparisonOp,
        /// Variable name for left side of comparison
        left: String,
        /// Value for right side of comparison
        right: GuardValue,
    },
    /// Boolean combination
    And(Vec<GuardExpression>),
    /// Logical OR of multiple guard expressions
    Or(Vec<GuardExpression>),
    /// Logical NOT of a guard expression
    Not(Box<GuardExpression>),
}

/// Comparison operators for guards.
#[derive(Debug, Clone, PartialEq)]
pub enum ComparisonOp {
    /// Equality comparison
    Equal,
    /// Inequality comparison
    NotEqual,
    /// Less than comparison
    LessThan,
    /// Less than or equal comparison
    LessEqual,
    /// Greater than comparison
    GreaterThan,
    /// Greater than or equal comparison
    GreaterEqual,
}

/// Values in guard expressions.
#[derive(Debug, Clone)]
pub enum GuardValue {
    /// Literal value in guard expression
    Literal(Value),
    /// Variable reference in guard expression
    Variable(String),
    /// Nested expression in guard
    Expression(Spanned<Expr>),
}

/// Type constraints for patterns.
#[derive(Debug, Clone)]
pub enum TypeConstraint {
    /// Exact type match
    Exact(String),
    /// Subtype match
    Subtype(String),
    /// Interface match
    Interface(Vec<String>), // method names
    /// Custom predicate
    Predicate(String),
}

/// Structure types for destructuring.
#[derive(Debug, Clone, PartialEq)]
pub enum StructureType {
    /// List structure type
    List,
    /// Vector structure type
    Vector,
    /// Record structure type with name
    Record(String), // record type name
    /// General object structure type
    Object,         // general object
}

/// Macro debugging information.
#[derive(Debug, Clone)]
pub struct MacroDebugInfo {
    /// Macro name
    pub macro_name: String,
    /// Input expression
    pub input: Spanned<Expr>,
    /// Pattern that matched
    pub matched_pattern: String,
    /// Variable bindings
    pub bindings: HashMap<String, Value>,
    /// Output expression
    pub output: Spanned<Expr>,
    /// Expansion timestamp
    pub timestamp: std::time::SystemTime,
    /// Expansion depth
    pub depth: usize,
}

/// Macro expansion trace for debugging.
#[derive(Debug)]
pub struct MacroExpansionTrace {
    /// Expansion steps
    steps: VecDeque<MacroDebugInfo>,
    /// Maximum trace length
    max_length: usize,
}

impl MacroExpansionTrace {
    /// Creates a new expansion trace.
    pub fn new(max_length: usize) -> Self {
        Self {
            steps: VecDeque::new(),
            max_length,
        }
    }

    /// Adds a debug step to the trace.
    pub fn add_step(&mut self, debug_info: MacroDebugInfo) {
        if self.steps.len() >= self.max_length {
            self.steps.pop_front();
        }
        self.steps.push_back(debug_info);
    }

    /// Gets all steps in the trace.
    pub fn steps(&self) -> &VecDeque<MacroDebugInfo> {
        &self.steps
    }

    /// Clears the trace.
    pub fn clear(&mut self) {
        self.steps.clear();
    }

    /// Gets the most recent step.
    pub fn last_step(&self) -> Option<&MacroDebugInfo> {
        self.steps.back()
    }
}

/// Enhanced hygiene system with more control.
#[derive(Debug)]
pub struct EnhancedHygiene {
    /// Base hygiene context
    base_context: HygieneContext,
    /// Hygiene policies
    pub policies: HashMap<String, HygienePolicy>,
    /// Identifier scopes
    scopes: Vec<IdentifierScope>,
}

/// Hygiene policy for controlling identifier renaming.
#[derive(Debug, Clone)]
pub enum HygienePolicy {
    /// Strict hygiene (default R7RS behavior)
    Strict,
    /// Relaxed hygiene (allow some captures)
    Relaxed {
        /// Identifiers that are allowed to be captured
        allowed_captures: Vec<String>,
    },
    /// Custom hygiene with user-defined rules
    Custom {
        /// User-defined rules for identifier renaming
        rename_rules: Vec<RenameRule>,
    },
    /// No hygiene (dangerous but sometimes useful)
    None,
}

/// Rule for identifier renaming.
#[derive(Debug, Clone)]
pub struct RenameRule {
    /// Pattern to match identifiers
    pub pattern: String, // Could be regex
    /// Rename strategy
    pub strategy: RenameStrategy,
    /// Conditions for application
    pub conditions: Vec<RenameCondition>,
}

/// Strategy for renaming identifiers.
#[derive(Debug, Clone)]
pub enum RenameStrategy {
    /// Prefix with string
    Prefix(String),
    /// Suffix with string
    Suffix(String),
    /// Replace with string
    Replace(String),
    /// Use function to generate name
    Function(String), // function name
    /// Don't rename
    Keep,
}

/// Condition for rename rules.
#[derive(Debug, Clone)]
pub enum RenameCondition {
    /// Only in specific contexts
    Context(String),
    /// Only for specific types
    Type(String),
    /// Custom predicate
    Predicate(String),
}

/// Identifier scope for hygiene tracking.
#[derive(Debug, Clone)]
pub struct IdentifierScope {
    /// Scope name
    pub name: String,
    /// Bound identifiers
    pub bindings: HashMap<String, SymbolId>,
    /// Parent scope
    pub parent: Option<Box<IdentifierScope>>,
}

/// Macro debugger for interactive macro development.
#[derive(Debug)]
pub struct MacroDebugger {
    /// Expansion trace
    trace: MacroExpansionTrace,
    /// Breakpoints
    breakpoints: HashMap<String, BreakpointCondition>,
    /// Step mode
    pub step_mode: StepMode,
    /// Debug flags
    debug_flags: DebugFlags,
}

/// Breakpoint condition.
#[derive(Debug, Clone)]
pub enum BreakpointCondition {
    /// Break on macro name
    MacroName(String),
    /// Break on pattern match
    PatternMatch(String),
    /// Break on depth
    Depth(usize),
    /// Break on custom condition
    Custom(String), // predicate function name
}

/// Step mode for macro debugging.
#[derive(Debug, Clone, PartialEq)]
pub enum StepMode {
    /// No stepping
    None,
    /// Step into macro expansions
    Into,
    /// Step over macro expansions
    Over,
    /// Step out of current macro
    Out,
}

/// Debug flags for controlling debug output.
#[derive(Debug, Clone)]
pub struct DebugFlags {
    /// Show pattern matching details
    pub show_patterns: bool,
    /// Show variable bindings
    pub show_bindings: bool,
    /// Show hygiene transformations
    pub show_hygiene: bool,
    /// Show performance information
    pub show_performance: bool,
}

impl MacroDebugger {
    /// Creates a new macro debugger.
    pub fn new() -> Self {
        Self {
            trace: MacroExpansionTrace::new(1000),
            breakpoints: HashMap::new(),
            step_mode: StepMode::None,
            debug_flags: DebugFlags {
                show_patterns: false,
                show_bindings: false,
                show_hygiene: false,
                show_performance: false,
            },
        }
    }

    /// Sets a breakpoint.
    pub fn set_breakpoint(&mut self, name: String, condition: BreakpointCondition) {
        self.breakpoints.insert(name, condition);
    }

    /// Removes a breakpoint.
    pub fn remove_breakpoint(&mut self, name: &str) {
        self.breakpoints.remove(name);
    }

    /// Sets step mode.
    pub fn set_step_mode(&mut self, mode: StepMode) {
        self.step_mode = mode;
    }

    /// Checks if should break at current expansion.
    pub fn should_break(&self, debug_info: &MacroDebugInfo) -> bool {
        for condition in self.breakpoints.values() {
            match condition {
                BreakpointCondition::MacroName(name) => {
                    if debug_info.macro_name == *name {
                        return true;
                    }
                }
                BreakpointCondition::Depth(depth) => {
                    if debug_info.depth >= *depth {
                        return true;
                    }
                }
                _ => {
                    // Other conditions would be implemented
                }
            }
        }
        false
    }

    /// Gets the expansion trace.
    pub fn trace(&self) -> &MacroExpansionTrace {
        &self.trace
    }

    /// Gets mutable access to the trace.
    pub fn trace_mut(&mut self) -> &mut MacroExpansionTrace {
        &mut self.trace
    }
}

impl EnhancedHygiene {
    /// Creates a new enhanced hygiene system.
    pub fn new() -> Self {
        Self {
            base_context: HygieneContext::new(),
            policies: HashMap::new(),
            scopes: vec![],
        }
    }

    /// Sets a hygiene policy for a macro.
    pub fn set_policy(&mut self, macro_name: String, policy: HygienePolicy) {
        self.policies.insert(macro_name, policy);
    }

    /// Applies hygiene transformations with policy.
    pub fn apply_hygiene(
        &mut self,
        expr: Spanned<Expr>,
        macro_name: &str,
        definition_env: &Environment,
    ) -> Result<Spanned<Expr>> {
        let policy = self.policies.get(macro_name).cloned()
            .unwrap_or(HygienePolicy::Strict);

        match policy {
            HygienePolicy::Strict => {
                self.base_context.rename_identifiers(expr, definition_env)
            }
            HygienePolicy::None => Ok(expr),
            HygienePolicy::Relaxed { allowed_captures } => {
                self.apply_relaxed_hygiene(expr, definition_env, &allowed_captures)
            }
            HygienePolicy::Custom { rename_rules } => {
                self.apply_custom_hygiene(expr, definition_env, &rename_rules)
            }
        }
    }

    /// Applies relaxed hygiene with allowed captures.
    fn apply_relaxed_hygiene(
        &mut self,
        expr: Spanned<Expr>,
        definition_env: &Environment,
        allowed_captures: &[String],
    ) -> Result<Spanned<Expr>> {
        // Implement relaxed hygiene - allow specified identifiers to be captured
        // This is a simplified implementation
        match expr.inner {
            Expr::Identifier(ref name) => {
                if allowed_captures.contains(name) {
                    // Allow capture - don't rename
                    Ok(expr)
                } else {
                    // Apply strict hygiene
                    self.base_context.rename_identifiers(expr, definition_env)
                }
            }
            _ => {
                // Recursively apply to sub-expressions
                self.base_context.rename_identifiers(expr, definition_env)
            }
        }
    }

    /// Applies custom hygiene with rename rules.
    fn apply_custom_hygiene(
        &mut self,
        expr: Spanned<Expr>,
        _definition_env: &Environment,
        _rename_rules: &[RenameRule],
    ) -> Result<Spanned<Expr>> {
        // Custom hygiene implementation would go here
        // For now, just return the expression unchanged
        Ok(expr)
    }
}

/// Main procedural macro system.
#[derive(Debug)]
pub struct ProceduralMacro {
    /// Procedural macro definitions
    proc_macros: HashMap<String, ProceduralMacroDefinition>,
    /// Enhanced macro expander
    expander: MacroExpander,
    /// AST transformer for advanced patterns
    transformer: AstTransformer,
    /// Macro debugger
    debugger: MacroDebugger,
    /// Enhanced hygiene system
    hygiene: EnhancedHygiene,
}

impl ProceduralMacro {
    /// Creates a new procedural macro system.
    pub fn new() -> Self {
        Self {
            proc_macros: HashMap::new(),
            expander: MacroExpander::with_builtins(),
            transformer: AstTransformer::new(),
            debugger: MacroDebugger::new(),
            hygiene: EnhancedHygiene::new(),
        }
    }

    /// Gets a reference to the procedural macros map.
    pub fn proc_macros(&self) -> &HashMap<String, ProceduralMacroDefinition> {
        &self.proc_macros
    }

    /// Defines a procedural macro.
    pub fn define_procedural_macro(
        &mut self,
        name: String,
        transformer: Value,
        definition_env: Rc<Environment>,
    ) -> Result<()> {
        if !transformer.is_procedure() {
            return Err(Box::new(Error::runtime_error(
                "Procedural macro transformer must be a procedure".to_string(),
                None,
            )));
        }

        let proc_macro = ProceduralMacroDefinition {
            name: name.clone(),
            transformer,
            definition_env,
            metadata: HashMap::new(),
            source: None,
        };

        self.proc_macros.insert(name, proc_macro);
        Ok(())
    }

    /// Expands a procedural macro.
    pub fn expand_procedural_macro(
        &mut self,
        name: &str,
        input: &Spanned<Expr>,
        use_env: &Rc<Environment>,
    ) -> Result<Spanned<Expr>> {
        let proc_macro = self.proc_macros.get(name)
            .ok_or_else(|| Error::runtime_error(
                format!("Unknown procedural macro: {name}"),
                Some(input.span),
            ))?;

        // Create evaluator for transformer execution
        let mut evaluator = Evaluator::with_environment(proc_macro.definition_env.clone());

        // Convert input expression to value for transformer
        let input_value = self.expr_to_value(input)?;

        // Call the transformer procedure by evaluating a procedure call
        let call_expr = Spanned::new(
            Expr::Application {
                operator: Box::new(Spanned::new(Expr::Identifier("transformer".to_string()), input.span)),
                operands: vec![Spanned::new(Expr::Identifier("input".to_string()), input.span)],
            },
            input.span
        );
        
        // Create temporary environment with transformer and input bound
        let temp_env = Rc::new(Environment::new(None, 0));
        temp_env.define("transformer".to_string(), proc_macro.transformer.clone());
        temp_env.define("input".to_string(), input_value);
        
        let result_value = evaluator.eval(&call_expr, temp_env)?;

        // Convert result back to expression
        let result_expr = self.value_to_expr(&result_value, input.span)?;

        // Apply hygiene
        let hygienic_result = self.hygiene.apply_hygiene(
            result_expr,
            name,
            &proc_macro.definition_env,
        )?;

        // Record debug information
        let debug_info = MacroDebugInfo {
            macro_name: name.to_string(),
            input: input.clone(),
            matched_pattern: "procedural".to_string(),
            bindings: HashMap::new(),
            output: hygienic_result.clone(),
            timestamp: std::time::SystemTime::now(),
            depth: 0, // Would track actual depth
        };

        if self.debugger.should_break(&debug_info) {
            // Handle breakpoint (in a real implementation, this would
            // provide interactive debugging capabilities)
        }

        self.debugger.trace_mut().add_step(debug_info);

        Ok(hygienic_result)
    }

    /// Expands macro with advanced pattern matching.
    pub fn expand_with_advanced_patterns(
        &mut self,
        pattern: &AdvancedPattern,
        template: &AstTemplate,
        input: &Spanned<Expr>,
    ) -> Result<Option<Spanned<Expr>>> {
        let mut bindings = HashMap::new();
        if self.match_advanced_pattern(pattern, input, &mut bindings)? {
            let result = self.expand_template_with_bindings(template, &bindings, input.span)?;
            Ok(Some(result))
        } else {
            Ok(None)
        }
    }

    /// Matches an advanced pattern against an expression.
    fn match_advanced_pattern(
        &self,
        pattern: &AdvancedPattern,
        expr: &Spanned<Expr>,
        bindings: &mut HashMap<String, Value>,
    ) -> Result<bool> {
        match pattern {
            AdvancedPattern::Basic(basic_pattern) => {
                // Use existing pattern matching
                match basic_pattern.match_expr(expr) {
                    Ok(pattern_bindings) => {
                        // Convert pattern bindings to our format
                        for (name, value) in pattern_bindings.bindings() {
                            // value is a single Spanned<Expr>
                            bindings.insert(name.to_string(), self.expr_to_value(value)?);
                        }
                        Ok(true)
                    }
                    Err(_) => Ok(false),
                }
            }

            AdvancedPattern::Conditional { pattern, guard } => {
                if self.match_advanced_pattern(pattern, expr, bindings)? {
                    self.evaluate_guard(guard, bindings)
                } else {
                    Ok(false)
                }
            }

            AdvancedPattern::Range { min, max, inclusive: _ } => {
                match &expr.inner {
                    Expr::Literal(crate::ast::Literal::ExactInteger(n)) => {
                        let n = *n as f64;
                        let min_ok = min.unwrap_or(f64::NEG_INFINITY) <= n;
                        let max_ok = n <= max.unwrap_or(f64::INFINITY);
                        Ok(min_ok && max_ok)
                    }
                    Expr::Literal(crate::ast::Literal::InexactReal(n)) => {
                        let n = *n;
                        let min_ok = min.unwrap_or(f64::NEG_INFINITY) <= n;
                        let max_ok = n <= max.unwrap_or(f64::INFINITY);
                        Ok(min_ok && max_ok)
                    }
                    _ => Ok(false)
                }
            }

            AdvancedPattern::Alternative(patterns) => {
                for pattern in patterns {
                    if self.match_advanced_pattern(pattern, expr, bindings)? {
                        return Ok(true);
                    }
                }
                Ok(false)
            }

            _ => {
                // Other advanced patterns not fully implemented
                Ok(false)
            }
        }
    }

    /// Evaluates a guard expression.
    fn evaluate_guard(&self, guard: &GuardExpression, bindings: &HashMap<String, Value>) -> Result<bool> {
        match guard {
            GuardExpression::Predicate(pred_name) => {
                // Would call predicate function with bindings
                // For now, just return true
                Ok(pred_name == "true")
            }

            GuardExpression::Comparison { operator, left, right } => {
                let left_value = bindings.get(left)
                    .ok_or_else(|| Error::runtime_error(
                        format!("Unbound variable in guard: {left}"),
                        None,
                    ))?;

                let right_value = match right {
                    GuardValue::Literal(val) => val,
                    GuardValue::Variable(var) => bindings.get(var)
                        .ok_or_else(|| Error::runtime_error(
                            format!("Unbound variable in guard: {var}"),
                            None,
                        ))?,
                    GuardValue::Expression(_) => {
                        // Would evaluate expression
                        return Ok(false);
                    }
                };

                self.compare_values(left_value, right_value, operator)
            }

            GuardExpression::And(guards) => {
                for guard in guards {
                    if !self.evaluate_guard(guard, bindings)? {
                        return Ok(false);
                    }
                }
                Ok(true)
            }

            GuardExpression::Or(guards) => {
                for guard in guards {
                    if self.evaluate_guard(guard, bindings)? {
                        return Ok(true);
                    }
                }
                Ok(false)
            }

            GuardExpression::Not(guard) => {
                Ok(!self.evaluate_guard(guard, bindings)?)
            }
        }
    }

    /// Compares two values using an operator.
    fn compare_values(&self, left: &Value, right: &Value, op: &ComparisonOp) -> Result<bool> {
        match (left, right) {
            (Value::Literal(crate::ast::Literal::ExactInteger(a)), 
             Value::Literal(crate::ast::Literal::ExactInteger(b))) => {
                let a = *a as f64;
                let b = *b as f64;
                match op {
                    ComparisonOp::Equal => Ok((a - b).abs() < f64::EPSILON),
                    ComparisonOp::NotEqual => Ok((a - b).abs() >= f64::EPSILON),
                    ComparisonOp::LessThan => Ok(a < b),
                    ComparisonOp::LessEqual => Ok(a <= b),
                    ComparisonOp::GreaterThan => Ok(a > b),
                    ComparisonOp::GreaterEqual => Ok(a >= b),
                }
            }
            (Value::Literal(crate::ast::Literal::InexactReal(a)), 
             Value::Literal(crate::ast::Literal::InexactReal(b))) => {
                match op {
                    ComparisonOp::Equal => Ok((a - b).abs() < f64::EPSILON),
                    ComparisonOp::NotEqual => Ok((a - b).abs() >= f64::EPSILON),
                    ComparisonOp::LessThan => Ok(a < b),
                    ComparisonOp::LessEqual => Ok(a <= b),
                    ComparisonOp::GreaterThan => Ok(a > b),
                    ComparisonOp::GreaterEqual => Ok(a >= b),
                }
            }
            (Value::Literal(crate::ast::Literal::ExactInteger(a)), 
             Value::Literal(crate::ast::Literal::InexactReal(b))) => {
                let a = *a as f64;
                match op {
                    ComparisonOp::Equal => Ok((a - *b).abs() < f64::EPSILON),
                    ComparisonOp::NotEqual => Ok((a - *b).abs() >= f64::EPSILON),
                    ComparisonOp::LessThan => Ok(a < *b),
                    ComparisonOp::LessEqual => Ok(a <= *b),
                    ComparisonOp::GreaterThan => Ok(a > *b),
                    ComparisonOp::GreaterEqual => Ok(a >= *b),
                }
            }
            (Value::Literal(crate::ast::Literal::InexactReal(a)), 
             Value::Literal(crate::ast::Literal::ExactInteger(b))) => {
                let b = *b as f64;
                match op {
                    ComparisonOp::Equal => Ok((*a - b).abs() < f64::EPSILON),
                    ComparisonOp::NotEqual => Ok((*a - b).abs() >= f64::EPSILON),
                    ComparisonOp::LessThan => Ok(*a < b),
                    ComparisonOp::LessEqual => Ok(*a <= b),
                    ComparisonOp::GreaterThan => Ok(*a > b),
                    ComparisonOp::GreaterEqual => Ok(*a >= b),
                }
            }
            _ => {
                match op {
                    ComparisonOp::Equal => Ok(left == right),
                    ComparisonOp::NotEqual => Ok(left != right),
                    _ => Ok(false), // Can't compare non-numbers with ordering
                }
            }
        }
    }

    /// Expands a template with bindings.
    fn expand_template_with_bindings(
        &self,
        template: &AstTemplate,
        _bindings: &HashMap<String, Value>,
        span: Span,
    ) -> Result<Spanned<Expr>> {
        // Simplified template expansion
        match template {
            AstTemplate::Literal(lit) => Ok(Spanned::new(
                Expr::Literal(lit.clone()),
                span,
            )),
            AstTemplate::Identifier(name) => Ok(Spanned::new(
                Expr::Identifier(name.clone()),
                span,
            )),
            _ => Ok(Spanned::new(
                Expr::Literal(crate::ast::Literal::Boolean(true)),
                span,
            )),
        }
    }

    /// Converts an expression to a value (simplified).
    fn expr_to_value(&self, expr: &Spanned<Expr>) -> Result<Value> {
        match &expr.inner {
            Expr::Literal(lit) => Ok(Value::Literal(lit.clone())),
            Expr::Identifier(name) => Ok(Value::symbol(intern_symbol(name))),
            _ => Ok(Value::Literal(crate::ast::Literal::Boolean(true))), // Simplified
        }
    }

    /// Converts a value to an expression.
    fn value_to_expr(&self, value: &Value, span: Span) -> Result<Spanned<Expr>> {
        match value {
            Value::Literal(lit) => Ok(Spanned::new(Expr::Literal(lit.clone()), span)),
            Value::Symbol(sym) => Ok(Spanned::new(
                Expr::Identifier(crate::utils::symbol_name(*sym).unwrap_or_else(|| format!("symbol-{}", sym.0))),
                span,
            )),
            _ => Ok(Spanned::new(
                Expr::Literal(crate::ast::Literal::Boolean(true)),
                span,
            )),
        }
    }

    /// Gets the macro debugger.
    pub fn debugger(&self) -> &MacroDebugger {
        &self.debugger
    }

    /// Gets mutable access to the macro debugger.
    pub fn debugger_mut(&mut self) -> &mut MacroDebugger {
        &mut self.debugger
    }

    /// Gets the enhanced hygiene system.
    pub fn hygiene(&self) -> &EnhancedHygiene {
        &self.hygiene
    }

    /// Gets mutable access to the enhanced hygiene system.
    pub fn hygiene_mut(&mut self) -> &mut EnhancedHygiene {
        &mut self.hygiene
    }

    /// Installs advanced macro primitives.
    pub fn install_primitives(&self, _env: &Rc<Environment>) -> Result<()> {
        // Would install primitives like define-procedural-macro, macro-expand, etc.
        Ok(())
    }
}

/// Hygiene extension system.
#[derive(Debug)]
pub struct HygienicExtension {
    /// Enhanced hygiene system
    hygiene: EnhancedHygiene,
}

impl HygienicExtension {
    /// Creates a new hygienic extension.
    pub fn new() -> Self {
        Self {
            hygiene: EnhancedHygiene::new(),
        }
    }

    /// Applies enhanced hygiene.
    pub fn apply_enhanced_hygiene(
        &mut self,
        expr: Spanned<Expr>,
        macro_name: &str,
        definition_env: &Environment,
    ) -> Result<Spanned<Expr>> {
        self.hygiene.apply_hygiene(expr, macro_name, definition_env)
    }
}

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

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

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

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