-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdlib.rs
204 lines (166 loc) · 5.25 KB
/
stdlib.rs
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
use crate::interpreter::{call_func, Context, Value};
fn arg_error(expected: usize, found: usize) -> Value {
Value::from(format!(
"TypeError: Expected {} arguments but got {}",
expected, found
))
}
pub fn type_of(args: Vec<Value>, _ctx: &mut Context) -> Result<Value, Value> {
args.into_iter()
.next()
.ok_or_else(|| Value::from("TypeError: Expected 1 argument but got 0"))
.map(|v| Value::from(v.get_type()))
}
pub fn seq(args: Vec<Value>, _ctx: &mut Context) -> Result<Value, Value> {
let mut iter = args.into_iter();
let lower = iter
.next()
.ok_or(arg_error(2, 0))
.and_then(|v| v.as_integer())?;
let upper = iter
.next()
.ok_or(arg_error(2, 1))
.and_then(|v| v.as_integer())?;
let list: Vec<Value> = (lower..upper).into_iter().map(Value::from).collect();
Ok(Value::from(list))
}
pub fn map(args: Vec<Value>, ctx: &mut Context) -> Result<Value, Value> {
let mut iter = args.into_iter();
let list = iter
.next()
.ok_or(arg_error(2, 0))
.and_then(|v| v.as_list())?;
let callback = iter
.next()
.ok_or(arg_error(2, 1))
.and_then(|v| v.as_function())?;
list.into_iter()
.map(|value| call_func(&callback, vec![value], ctx))
.collect::<Result<Vec<Value>, Value>>()
.map(Value::from)
}
pub fn for_each(args: Vec<Value>, ctx: &mut Context) -> Result<Value, Value> {
let mut iter = args.into_iter();
let list = iter
.next()
.ok_or(arg_error(2, 0))
.and_then(|v| v.as_list())?;
let callback = iter
.next()
.ok_or(arg_error(2, 1))
.and_then(|v| v.as_function())?;
for item in list {
call_func(&callback, vec![item.clone()], ctx)?.as_boolean()?;
}
Ok(Value::Void)
}
pub fn filter(args: Vec<Value>, ctx: &mut Context) -> Result<Value, Value> {
let mut iter = args.into_iter();
let list = iter
.next()
.ok_or(arg_error(2, 0))
.and_then(|v| v.as_list())?;
let callback = iter
.next()
.ok_or(arg_error(2, 1))
.and_then(|v| v.as_function())?;
let mut out = vec![];
for item in list {
let res = call_func(&callback, vec![item.clone()], ctx)?;
if res.as_boolean()? {
out.push(item);
}
}
Ok(Value::from(out))
}
pub fn reduce(args: Vec<Value>, ctx: &mut Context) -> Result<Value, Value> {
let mut iter = args.into_iter();
let list = iter
.next()
.ok_or(arg_error(3, 0))
.and_then(|v| v.as_list())?;
let callback = iter
.next()
.ok_or(arg_error(3, 1))
.and_then(|v| v.as_function())?;
let inital = iter.next().ok_or(arg_error(3, 2))?;
list.into_iter().try_fold(inital, |acc, curr| {
call_func(&callback, vec![acc, curr], ctx)
})
}
pub fn concat(args: Vec<Value>, _ctx: &mut Context) -> Result<Value, Value> {
let list: Vec<Value> = args.into_iter().fold(vec![], |list, v| {
[
list,
match v {
Value::List(v) => v,
_ => vec![v],
},
]
.concat()
});
Ok(Value::from(list))
}
pub mod console {
use super::*;
pub fn log(args: Vec<Value>, _ctx: &mut Context) -> Result<Value, Value> {
let text = args
.into_iter()
.map(|v| v.as_string())
.collect::<Result<Vec<String>, Value>>()?
.join(" ");
println!("{}", text);
Ok(Value::Void)
}
pub fn dir(args: Vec<Value>, _ctx: &mut Context) -> Result<Value, Value> {
for value in args {
println!("{}", value.print(0));
}
Ok(Value::Void)
}
pub fn inspect(args: Vec<Value>, _ctx: &mut Context) -> Result<Value, Value> {
for arg in args {
print!("{} ", arg.print(0));
}
println!("");
Ok(Value::Void)
}
}
pub mod math {
use super::*;
use std::f64;
pub fn sum(args: Vec<Value>, _ctx: &mut Context) -> Result<Value, Value> {
args.into_iter()
.map(|v| v.as_number())
.try_fold(0.0, |acc, curr| curr.map(|num| num + acc))
.map(Value::from)
}
pub fn greatest(args: Vec<Value>, _ctx: &mut Context) -> Result<Value, Value> {
args.into_iter()
.map(|v| v.as_number())
.try_fold(f64::NEG_INFINITY, |acc, curr| {
curr.map(|curr| if acc < curr { curr } else { acc })
})
.map(Value::from)
}
pub fn smallest(args: Vec<Value>, _ctx: &mut Context) -> Result<Value, Value> {
args.into_iter()
.map(|v| v.as_number())
.try_fold(f64::NEG_INFINITY, |acc, curr| {
curr.map(|curr| if acc > curr { curr } else { acc })
})
.map(Value::from)
}
pub fn random(_args: Vec<Value>, _ctx: &mut Context) -> Result<Value, Value> {
let value: f64 = rand::random();
Ok(Value::from(value))
}
pub fn round(args: Vec<Value>, _ctx: &mut Context) -> Result<Value, Value> {
let n = args
.into_iter()
.next()
.ok_or(arg_error(1, 0))
.and_then(|v| v.as_number())?;
Ok(Value::from(n.round()))
}
}