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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use std::collections::{HashMap, HashSet};
use crate::ast::YololNode;
use crate::error::TranspileError;
use crate::function::Function;
use crate::value::{ArrayExpr, NumberExpr, Value};
#[cfg(test)]
mod tests;
#[derive(Debug, Clone)]
pub struct Context {
exported: HashSet<String>,
}
impl Context {
fn new() -> Context {
Context {
exported: HashSet::new(),
}
}
fn push_exported(&mut self, ident: &str) {
self.exported.insert(ident.to_string());
}
pub fn exported(&self) -> HashSet<String> {
self.exported.clone()
}
}
#[derive(Debug, Clone)]
pub struct Environment {
context: Context,
imports: HashSet<String>,
variables: HashMap<String, Value>,
functions: HashMap<String, Function>,
exports: HashSet<String>,
keywords: HashSet<String>,
}
impl Environment {
pub fn new() -> Environment {
Environment {
context: Context::new(),
imports: HashSet::new(),
variables: HashMap::new(),
functions: HashMap::new(),
exports: HashSet::new(),
keywords: [
"import".to_string(),
"define".to_string(),
"let".to_string(),
"export".to_string(),
"not".to_string(),
"abs".to_string(),
"sqrt".to_string(),
"sin".to_string(),
"cos".to_string(),
"tan".to_string(),
"asin".to_string(),
"acos".to_string(),
"atan".to_string(),
"and".to_string(),
"or".to_string(),
"sum".to_string(),
"product".to_string(),
]
.iter()
.cloned()
.collect(),
}
}
pub fn context(&self) -> Context {
self.context.clone()
}
pub fn variable(&self, ident: &str) -> Result<Value, TranspileError> {
match self.variables.get(ident) {
Some(value) => Ok(value.clone()),
None => Err(TranspileError::GetUndefinedVariable(ident.to_string())),
}
}
pub fn function(&self, ident: &str) -> Result<Function, TranspileError> {
match self.functions.get(ident) {
Some(function) => Ok(function.clone()),
None => Err(TranspileError::GetUndefinedFunction(ident.to_string())),
}
}
pub fn import(&mut self, ident: &str) -> Result<(), TranspileError> {
if self.imports.contains(ident) {
Err(TranspileError::ImportTwice(ident.to_string()))
} else if self.variables.contains_key(ident) {
Err(TranspileError::ImportExisting(ident.to_string()))
} else if self.keywords.contains(ident) {
Err(TranspileError::ImportKeyword(ident.to_string()))
} else {
self.imports.insert(ident.to_string());
self.variables.insert(
ident.to_string(),
Value::Number(NumberExpr::from_ident(ident)),
);
Ok(())
}
}
pub fn define(&mut self, ident: &str, function: Function) -> Result<(), TranspileError> {
if self.functions.contains_key(ident) {
Err(TranspileError::RedefineFunction(ident.to_string()))
} else if self.keywords.contains(ident) {
Err(TranspileError::DefineKeyword(ident.to_string()))
} else {
self.functions.insert(ident.to_string(), function);
Ok(())
}
}
pub fn let_value(
&mut self,
ident: &str,
value: Value,
) -> Result<Vec<YololNode>, TranspileError> {
if self.imports.contains(ident) || self.variables.contains_key(ident) {
Err(TranspileError::ReassignVariable(ident.to_string()))
} else if self.keywords.contains(ident) {
Err(TranspileError::AssignToKeyword(ident.to_string()))
} else if self
.variables
.iter()
.map(|(s, _)| s.to_lowercase())
.collect::<Vec<String>>()
.contains(&ident.to_lowercase())
{
Err(TranspileError::AssignSameLowercase(ident.to_string()))
} else {
match value {
Value::Number(number) => {
let assign_stmt = number.to_assign_stmt(&ident);
self.variables.insert(
ident.to_string(),
Value::Number(NumberExpr::from_ident(&ident)),
);
Ok(vec![assign_stmt])
}
Value::Array(array) => {
let assign_stmts = array.to_assign_stmts(&ident);
self.variables.insert(
ident.to_string(),
Value::Array(ArrayExpr::from_ident(&ident, assign_stmts.len())),
);
Ok(assign_stmts)
}
}
}
}
pub fn export(&mut self, ident: &str) -> Result<(), TranspileError> {
if self.exports.contains(ident) {
Err(TranspileError::ExportTwice(ident.to_string()))
} else {
match self.variables.get(ident) {
Some(Value::Number(number)) => match number.as_expr() {
YololNode::Ident(s) => {
self.context.push_exported(&s);
}
_ => panic!("expected identifier, but got: {:?}", number.as_expr()),
},
Some(Value::Array(array)) => {
for expr in array.as_exprs().iter() {
match expr {
YololNode::Ident(s) => {
self.context.push_exported(&s);
}
_ => panic!("expected identifier, but got: {:?}", expr),
}
}
}
None => return Err(TranspileError::ExportUndefined(ident.to_string())),
}
self.exports.insert(ident.to_string());
Ok(())
}
}
}