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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//
// Copyright 2024 Formata, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
use std::{collections::BTreeMap, ops::DerefMut};
use crate::{lang::SError, Library, SData, SDoc, SFunc, SNum, SVal};
/// Map library.
#[derive(Default, Debug)]
pub struct MapLibrary;
impl MapLibrary {
/// Call map operation.
pub fn operate(&self, pid: &str, doc: &mut SDoc, name: &str, map: &mut BTreeMap<SVal, SVal>, parameters: &mut Vec<SVal>) -> Result<SVal, SError> {
match name {
// Move all elements from another map onto this map, leaving other map empty.
// Signature: Map.append(map, other: map): void
"append" => {
if parameters.len() < 1 {
return Err(SError::map(pid, &doc, "append", "map argument not found for append operation"));
}
match &mut parameters[0] {
SVal::Map(other) => {
map.append(other);
Ok(SVal::Void)
},
SVal::Boxed(other) => {
let mut other = other.lock().unwrap();
let other = other.deref_mut();
match other {
SVal::Map(other) => {
map.append(other);
Ok(SVal::Void)
},
_ => {
Err(SError::map(pid, &doc, "append", "map argument not found"))
}
}
},
_ => {
Err(SError::map(pid, &doc, "append", "map argument not found"))
}
}
},
// Clear the map, removing all elements.
// Signature: Map.clear(map): void
"clear" => {
map.clear();
Ok(SVal::Void)
},
// Contains a key?
// Signature: Map.contains(map, key: unknown): bool
"contains" => {
if parameters.len() < 1 {
return Err(SError::map(pid, &doc, "contains", "key argument not found"));
}
Ok(SVal::Bool(map.contains_key(¶meters[0])))
},
// Get the first key and value pair in this ordered map.
// Signature: Map.first(map): null | (key: unknown, value: unknown)
"first" => {
if let Some((key, value)) = map.first_key_value() {
return Ok(SVal::Tuple(vec![key.clone(), value.clone()]));
}
Ok(SVal::Null)
},
// Get the last key and value pair in this ordered map.
// Signature: Map.last(map): null | (key: unknown, value: unknown)
"last" => {
if let Some((key, value)) = map.last_key_value() {
return Ok(SVal::Tuple(vec![key.clone(), value.clone()]));
}
Ok(SVal::Null)
},
// Get a value in this map.
// Signature: Map.get(map, key: unknown): null | unknown
"get" => {
if parameters.len() < 1 {
return Err(SError::map(pid, &doc, "get", "key argument for retrieving a value not found"));
}
if let Some(value) = map.get(¶meters[0]) {
return Ok(value.clone());
}
Ok(SVal::Null)
},
// Insert a key-value pair into this map, returning the old value if already in map
// Signature: Map.insert(map, key: unknown, value: unknown): null | unknown
"insert" => {
if parameters.len() < 2 {
return Err(SError::map(pid, &doc, "insert", "key and value arguments not found for insert operation"));
}
let value = parameters.pop().unwrap();
let key = parameters.pop().unwrap();
if let Some(old) = map.insert(key, value) {
Ok(old)
} else {
Ok(SVal::Null)
}
},
// Is this map empty?
// Signature: Map.empty(map): bool
"empty" => {
Ok(SVal::Bool(map.is_empty()))
},
// Does this map contain any values?
// Signature: Map.any(map): bool
"any" => {
Ok(SVal::Bool(!map.is_empty()))
},
// Return the keys of this map in a vec.
// Signature: Map.keys(map): vec
"keys" => {
Ok(SVal::Array(map.keys().cloned().collect()))
},
// Return the values of this map in a vec.
// Signature: Map.values(map): vec
"values" => {
Ok(SVal::Array(map.values().cloned().collect()))
},
// Length of this map. Enables iteration as well... (for loops)
// Signature: Map.len(map): int
"len" => {
Ok(SVal::Number(SNum::I64(map.len() as i64)))
},
// Get an item at a specific index in this map. Enables iteration as well... (for loops)
// Signature: Map.at(map, index): (key: unknown, value: unknown)
"at" => {
if parameters.len() < 1 {
return Err(SError::map(pid, &doc, "at", "index argument not found"));
}
match ¶meters[0] {
SVal::Number(index) => {
let index = index.int() as usize;
if index >= map.len() {
return Err(SError::map(pid, &doc, "at", "index out of bounds"));
}
if let Some((key, value)) = map.iter().nth(index) {
Ok(SVal::Tuple(vec![key.clone(), value.clone()]))
} else {
Ok(SVal::Null)
}
},
_ => {
if let Some((key, value)) = map.get_key_value(¶meters[0]) {
Ok(SVal::Tuple(vec![key.clone(), value.clone()]))
} else {
Ok(SVal::Null)
}
}
}
},
// Pop first value in this map.
// Signature: Map.popFirst(map): null | (key: unknown, value: unknown)
"popFirst" => {
if let Some((key, value)) = map.pop_first() {
Ok(SVal::Tuple(vec![key, value]))
} else {
Ok(SVal::Null)
}
},
// Pop last value in this map.
// Signature: Map.popLast(map): null | (key: unknown, value: unknown)
"popLast" => {
if let Some((key, value)) = map.pop_last() {
Ok(SVal::Tuple(vec![key, value]))
} else {
Ok(SVal::Null)
}
},
// Remove an entry in this map.
// Signature: Map.remove(map, key: unknown): null | unknown
"remove" => {
if parameters.len() < 1 {
return Err(SError::map(pid, &doc, "remove", "key argument not found for removal"));
}
if let Some(value) = map.remove(¶meters[0]) {
Ok(value)
} else {
Ok(SVal::Null)
}
},
// Retain only the elements specified by the predicate.
// Signagure: Map.retain(map, pred: fn): void
"retain" => {
if parameters.len() < 1 {
return Err(SError::map(pid, &doc, "retain", "predicate argument not found"));
}
match ¶meters[0] {
SVal::FnPtr(dref) => {
if let Some(func) = SData::get::<SFunc>(&doc.graph, dref) {
let rtype = func.rtype.clone();
let statements = func.statements.clone();
let params = func.params.clone();
map.retain(|k, v| {
if let Ok(res) = SFunc::call_internal(dref, pid, doc, vec![k.clone(), v.clone()], true, ¶ms, &statements, &rtype) {
res.truthy()
} else {
false
}
});
}
Ok(SVal::Void)
},
_ => {
Err(SError::map(pid, &doc, "retain", "predicate not found"))
}
}
},
_ => {
Err(SError::map(pid, &doc, "NotFound", &format!("{} is not a function in the Map Library", name)))
}
}
}
}
impl Library for MapLibrary {
fn scope(&self) -> String {
"Map".to_string()
}
fn call(&self, pid: &str, doc: &mut SDoc, name: &str, parameters: &mut Vec<SVal>) -> Result<SVal, SError> {
if parameters.len() > 0 {
match name {
"toString" => {
return Ok(SVal::String(parameters[0].print(doc)));
},
"or" => {
for param in parameters.drain(..) {
if !param.is_empty() {
return Ok(param);
}
}
return Ok(SVal::Null);
},
_ => {}
}
let mut params;
if parameters.len() > 1 {
params = parameters.drain(1..).collect();
} else {
params = Vec::new();
}
match &mut parameters[0] {
SVal::Map(map) => {
return self.operate(pid, doc, name, map, &mut params);
},
SVal::Boxed(val) => {
let mut val = val.lock().unwrap();
let val = val.deref_mut();
match val {
SVal::Map(map) => {
return self.operate(pid, doc, name, map, &mut params);
},
_ => {
return Err(SError::map(pid, &doc, "InvalidArgument", "map argument not found"));
}
}
},
_ => {
return Err(SError::map(pid, &doc, "InvalidArgument", "map argument not found"));
}
}
} else {
return Err(SError::map(pid, &doc, "InvalidArgument", "map argument not found"));
}
}
}