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
use crate::ast::*;
use crate::runtime::op::interpolated_variables;

pub(super) trait ScopeConsumer {
    fn consumes(&self) -> Vec<String>;
}

impl ScopeConsumer for Statement {
    fn consumes(&self) -> Vec<String> {
        use Statement::*;

        match self {
            Print(expr) => expr.consumes(),
            Quit => vec![],
            Subst(_, expr) => expr.consumes(),
            Gsubst(_, expr) => expr.consumes(),
            Read(expr) => expr.consumes(),
            Write(expr) => expr.consumes(),
            Exec(expr) => expr.consumes(),
            Append(expr) => expr.consumes(),
            Set(expr) => expr.consumes(),
        }
    }
}

impl ScopeConsumer for Expression {
    fn consumes(&self) -> Vec<String> {
        use Expression::*;
        match self {
            Identifier(name) => vec![name.to_string()],
            String(_, false) => vec![],

            String(content, true) => interpolated_variables(content),
        }
    }
}

pub(super) fn env_vars() -> Vec<String> {
    let mut vars = Vec::new();
    for (key, _) in std::env::vars() {
        vars.push(key)
    }
    vars
}