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
use crate::nodes::*;
use crate::process::{NodeProcessor, NodeVisitor};
pub trait Scope {
fn push(&mut self);
fn pop(&mut self);
fn insert(&mut self, identifier: &mut String);
fn insert_local(&mut self, identifier: &mut String, value: Option<&mut Expression>);
fn insert_local_function(&mut self, function: &mut LocalFunctionStatement);
}
pub struct ScopeVisitor;
impl ScopeVisitor {
fn visit_block_without_push<T: NodeProcessor + Scope>(block: &mut Block, scope: &mut T) {
scope.process_block(block);
block
.iter_mut_statements()
.for_each(|statement| Self::visit_statement(statement, scope));
if let Some(last_statement) = block.mutate_last_statement() {
scope.process_last_statement(last_statement);
if let LastStatement::Return(expressions) = last_statement {
expressions
.iter_mut_expressions()
.for_each(|expression| Self::visit_expression(expression, scope));
};
};
}
}
impl<T: NodeProcessor + Scope> NodeVisitor<T> for ScopeVisitor {
fn visit_block(block: &mut Block, scope: &mut T) {
scope.push();
Self::visit_block_without_push(block, scope);
scope.pop();
}
fn visit_local_assign(statement: &mut LocalAssignStatement, scope: &mut T) {
scope.process_local_assign_statement(statement);
statement
.iter_mut_values()
.for_each(|value| Self::visit_expression(value, scope));
statement.for_each_assignment(|variable, expression| {
scope.insert_local(variable.mutate_name(), expression)
});
}
fn visit_function_expression(function: &mut FunctionExpression, scope: &mut T) {
scope.process_function_expression(function);
scope.push();
function
.mutate_parameters()
.iter_mut()
.for_each(|parameter| scope.insert(parameter.mutate_name()));
Self::visit_block(function.mutate_block(), scope);
scope.pop();
}
fn visit_function_statement(statement: &mut FunctionStatement, scope: &mut T) {
scope.process_function_statement(statement);
scope.process_variable_expression(statement.mutate_function_name().mutate_identifier());
scope.push();
statement
.mutate_parameters()
.iter_mut()
.for_each(|parameter| scope.insert(parameter.mutate_name()));
Self::visit_block(statement.mutate_block(), scope);
scope.pop();
}
fn visit_local_function(statement: &mut LocalFunctionStatement, scope: &mut T) {
scope.process_local_function_statement(statement);
scope.insert_local_function(statement);
scope.push();
statement
.mutate_parameters()
.iter_mut()
.for_each(|parameter| scope.insert(parameter.mutate_name()));
Self::visit_block(statement.mutate_block(), scope);
scope.pop();
}
fn visit_generic_for(statement: &mut GenericForStatement, scope: &mut T) {
scope.process_generic_for_statement(statement);
statement
.iter_mut_expressions()
.for_each(|expression| Self::visit_expression(expression, scope));
statement
.iter_mut_identifiers()
.for_each(|identifier| scope.insert(identifier.mutate_name()));
Self::visit_block(statement.mutate_block(), scope);
}
fn visit_numeric_for(statement: &mut NumericForStatement, scope: &mut T) {
scope.process_numeric_for_statement(statement);
Self::visit_expression(statement.mutate_start(), scope);
Self::visit_expression(statement.mutate_end(), scope);
if let Some(step) = statement.mutate_step() {
Self::visit_expression(step, scope);
};
scope.push();
scope.insert(statement.mutate_identifier().mutate_name());
Self::visit_block(statement.mutate_block(), scope);
scope.pop();
}
fn visit_repeat_statement(statement: &mut RepeatStatement, scope: &mut T) {
scope.process_repeat_statement(statement);
scope.push();
Self::visit_block_without_push(statement.mutate_block(), scope);
Self::visit_expression(statement.mutate_condition(), scope);
scope.pop();
}
}