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
use std::cell::RefCell;
use std::collections::BTreeSet;
use std::rc::Rc;

/// Stack of local variables in nested scopes
///
/// Each scope represents a Ruby scope:
///
/// ```test
/// # 1
/// class A
///   # 1, 2
///   def m
///     # 1, 2, 3
///   end
///   # 1, 2
/// end
/// # 1
/// ```
///
/// In the example above comments show what's in the stack.
/// Basically, it's pushed when you enter a new scope
/// and it's popped when exit it.
#[derive(Debug, Clone, Default)]
pub struct StaticEnvironment {
    variables: Rc<RefCell<BTreeSet<String>>>,
    stack: Rc<RefCell<Vec<BTreeSet<String>>>>,
}

const FORWARD_ARGS: &str = "FORWARD_ARGS";

impl StaticEnvironment {
    /// Constructor
    pub fn new() -> Self {
        Self {
            variables: Rc::new(RefCell::new(BTreeSet::new())),
            stack: Rc::new(RefCell::new(vec![])),
        }
    }

    pub(crate) fn is_empty(&self) -> bool {
        self.stack.borrow().is_empty()
    }

    #[allow(dead_code)]
    pub(crate) fn reset(&self) {
        self.variables.borrow_mut().clear();
        self.stack.borrow_mut().clear();
    }

    /// Performs a push, doesn't inherit previously declared variables in the new scope
    ///
    /// Handles class/module scopes
    pub fn extend_static(&self) {
        let variables = std::mem::take(&mut *self.variables.borrow_mut());
        self.stack.borrow_mut().push(variables);
    }

    /// Performs a puch, inherits previously declared variables in the new scope
    ///
    /// Handles block/lambda scopes
    pub fn extend_dynamic(&self) {
        self.stack
            .borrow_mut()
            .push(self.variables.borrow().clone());
    }

    /// Performs pop
    pub fn unextend(&self) {
        *self.variables.borrow_mut() = self
            .stack
            .borrow_mut()
            .pop()
            .expect("expected static_env to have at least one frame");
    }

    /// Declares a new variable in the current scope
    pub fn declare(&self, name: &str) {
        self.variables.borrow_mut().insert(name.to_string());
    }

    /// Returns `true` if variable with a given `name` is declared in the current scope
    pub fn is_declared(&self, name: &str) -> bool {
        self.variables.borrow().get(name).is_some()
    }

    pub(crate) fn declare_forward_args(&self) {
        self.declare(FORWARD_ARGS);
    }

    pub(crate) fn is_forward_args_declared(&self) -> bool {
        self.is_declared(FORWARD_ARGS)
    }
}

#[test]
fn test_declare() {
    let env = StaticEnvironment::new();
    assert!(!env.is_declared("foo"));

    env.declare("foo");
    assert!(env.is_declared("foo"));
}

#[test]
fn test_extend_static() {
    let env = StaticEnvironment::new();

    env.declare("foo");
    env.extend_static();
    env.declare("bar");

    assert!(!env.is_declared("foo"));
    assert!(env.is_declared("bar"));
}

#[test]
fn test_extend_dynamic() {
    let env = StaticEnvironment::new();

    env.declare("foo");
    env.extend_dynamic();
    env.declare("bar");

    assert!(env.is_declared("foo"));
    assert!(env.is_declared("bar"));
}

#[test]
fn test_unextend() {
    let env = StaticEnvironment::new();

    env.declare("foo");
    env.extend_dynamic();
    env.declare("bar");
    env.unextend();

    assert!(env.is_declared("foo"));
    assert!(!env.is_declared("bar"));
}