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
use crate::ParseError;
use rustc_hash::FxHashMap;
#[derive(Debug)]
#[allow(dead_code)]
pub struct Var {
name: String,
original_name: String,
is_function: bool,
is_const: bool,
is_global: bool,
}
/// Verifies variable and identifier names in scope and handles shadowing
pub struct IdVerifier {
scopes: Vec<FxHashMap<String, Var>>,
var_counter: i32,
}
impl Default for IdVerifier {
fn default() -> Self {
Self::new()
}
}
impl IdVerifier {
pub fn new() -> Self {
let mut inbuilt = FxHashMap::default();
let inbuilt_functions = vec![
"rotate2d",
"dot",
"dot2",
"dot3",
"cross",
"mix",
"smoothstep",
"length",
"length2",
"length3",
"normalize",
"sin",
"sin1",
"sin2",
"cos",
"cos1",
"cos2",
"sqrt",
"ceil",
"floor",
"fract",
"abs",
"tan",
"degrees",
"radians",
"min",
"max",
"pow",
"rand",
"clamp",
"sign",
"atan",
"atan2",
"mod",
"step",
"exp",
"log",
"print",
"sample",
"sample_normal",
"alloc",
"iterate",
"save",
"rotate2d",
"palette",
"round",
];
for func in inbuilt_functions {
inbuilt.insert(
func.to_string(),
Var {
name: func.to_string(),
original_name: func.to_string(),
is_function: true,
is_const: true,
is_global: true,
},
);
}
IdVerifier {
scopes: vec![inbuilt],
var_counter: 0,
}
}
/// Defines a new variable name. If the name already exists in the current scope, a new name is created.
pub fn define_var(
&mut self,
original_name: &str,
is_function: bool,
) -> Result<String, ParseError> {
let name = if !is_function {
self.create_var_name(original_name)
} else {
// TODO check if function name already exists
original_name.to_string()
};
let var = Var {
name: name.clone(),
original_name: original_name.to_string(),
is_function,
is_const: false,
is_global: self.is_global_scope(),
};
// let scope_ = self.current_scope();
if let Some(scope) = self.scopes.last_mut() {
// println!("insert {} at {}", name, scope_);
scope.insert(name.clone(), var);
}
Ok(name)
}
/// Checks if a variable with the original name exists, and if yes, returns the new name.
pub fn get_var_name(&mut self, original_name: &str) -> Option<String> {
for scope in self.scopes.iter().rev() {
for (name, var) in scope.iter() {
if var.original_name == original_name {
return Some(name.clone());
}
}
}
None
}
/// Gets the original name of a variable.
pub fn get_original_var_name(&mut self, name: &str) -> Option<String> {
for scope in self.scopes.iter().rev() {
if let Some(var) = scope.get(name) {
return Some(var.original_name.clone());
}
}
None
}
/// Creates a new var name based on the original name.
pub fn create_var_name(&mut self, name: &str) -> String {
// let mut new_name: String = name.to_string();
// let mut i = 0;
// while self.var_exists(&new_name) {
// i += 1;
// new_name = format!("{}_{}", name, i);
// }
// new_name
let new_name = format!("{}_{}", name, self.var_counter);
self.var_counter += 1;
new_name
}
pub fn var_exists(&self, name: &str) -> bool {
for scope in self.scopes.iter().rev() {
if scope.contains_key(name) {
return true;
}
}
false
}
/// Begin a new scope.
pub fn begin_scope(&mut self) {
self.scopes.push(FxHashMap::default());
}
/// End the current scope.
pub fn end_scope(&mut self) {
self.scopes.pop();
}
/// Returns true if the current scope is the global scope.
pub fn is_global_scope(&self) -> bool {
self.scopes.len() == 1
}
pub fn current_scope(&self) -> i32 {
self.scopes.len() as i32
}
}