41 lines
1.1 KiB
Rust
41 lines
1.1 KiB
Rust
use super::ast::{Expression, Program, Statement};
|
|
use internment::ArcIntern;
|
|
use std::collections::HashSet;
|
|
|
|
impl Program {
|
|
/// Get the complete list of strings used within the program.
|
|
///
|
|
/// For the purposes of this function, strings are the variables used in
|
|
/// `print` statements.
|
|
pub fn strings(&self) -> HashSet<ArcIntern<String>> {
|
|
let mut result = HashSet::new();
|
|
|
|
for stmt in self.statements.iter() {
|
|
stmt.register_strings(&mut result);
|
|
}
|
|
|
|
result
|
|
}
|
|
}
|
|
|
|
impl Statement {
|
|
fn register_strings(&self, string_set: &mut HashSet<ArcIntern<String>>) {
|
|
match self {
|
|
Statement::Binding(_, name, _, expr) => {
|
|
string_set.insert(name.clone());
|
|
expr.register_strings(string_set);
|
|
}
|
|
|
|
Statement::Print(_, _, name) => {
|
|
string_set.insert(name.clone());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Expression {
|
|
fn register_strings(&self, _string_set: &mut HashSet<ArcIntern<String>>) {
|
|
// nothing has a string in here, at the moment
|
|
}
|
|
}
|