-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.rs
More file actions
208 lines (193 loc) · 6.48 KB
/
Copy pathmath.rs
File metadata and controls
208 lines (193 loc) · 6.48 KB
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
use crate::{StdFunction, StdlibModule, StdlibRegistry};
use std::collections::HashMap;
use std::rc::Rc;
use techscript_runtime::{
error::{RuntimeError, RuntimeErrorKind},
value::RuntimeValue,
RuntimeContext,
};
impl StdlibRegistry {
pub fn register_math(&mut self) {
let mut exports: HashMap<String, Rc<dyn techscript_runtime::function::Callable>> =
HashMap::new();
exports.insert(
"abs".to_string(),
Rc::new(StdFunction {
name: "abs".to_string(),
arity: 1,
callback: |_ctx, args| match &args[0] {
RuntimeValue::Int(i) => Ok(RuntimeValue::Int(i.abs())),
RuntimeValue::Float(f) => Ok(RuntimeValue::Float(f.abs())),
other => Err(RuntimeError::new(
RuntimeErrorKind::TypeMismatch {
expected: "Int or Float".to_string(),
found: other.runtime_type().to_string(),
},
None,
None,
)),
},
}),
);
exports.insert(
"sin".to_string(),
Rc::new(StdFunction {
name: "sin".to_string(),
arity: 1,
callback: |_ctx, args| {
let f = args[0].try_into_float()?;
Ok(RuntimeValue::Float(f.sin()))
},
}),
);
exports.insert(
"cos".to_string(),
Rc::new(StdFunction {
name: "cos".to_string(),
arity: 1,
callback: |_ctx, args| {
let f = args[0].try_into_float()?;
Ok(RuntimeValue::Float(f.cos()))
},
}),
);
exports.insert(
"tan".to_string(),
Rc::new(StdFunction {
name: "tan".to_string(),
arity: 1,
callback: |_ctx, args| {
let f = args[0].try_into_float()?;
Ok(RuntimeValue::Float(f.tan()))
},
}),
);
exports.insert(
"log".to_string(),
Rc::new(StdFunction {
name: "log".to_string(),
arity: 1,
callback: |_ctx, args| {
let f = args[0].try_into_float()?;
Ok(RuntimeValue::Float(f.ln()))
},
}),
);
exports.insert(
"exp".to_string(),
Rc::new(StdFunction {
name: "exp".to_string(),
arity: 1,
callback: |_ctx, args| {
let f = args[0].try_into_float()?;
Ok(RuntimeValue::Float(f.exp()))
},
}),
);
exports.insert(
"sqrt".to_string(),
Rc::new(StdFunction {
name: "sqrt".to_string(),
arity: 1,
callback: |_ctx, args| {
let f = args[0].try_into_float()?;
Ok(RuntimeValue::Float(f.sqrt()))
},
}),
);
exports.insert(
"pow".to_string(),
Rc::new(StdFunction {
name: "pow".to_string(),
arity: 2,
callback: |_ctx, args| {
let base = args[0].try_into_float()?;
let exponent = args[1].try_into_float()?;
Ok(RuntimeValue::Float(base.powf(exponent)))
},
}),
);
exports.insert(
"floor".to_string(),
Rc::new(StdFunction {
name: "floor".to_string(),
arity: 1,
callback: |_ctx, args| {
let f = args[0].try_into_float()?;
Ok(RuntimeValue::Float(f.floor()))
},
}),
);
exports.insert(
"ceil".to_string(),
Rc::new(StdFunction {
name: "ceil".to_string(),
arity: 1,
callback: |_ctx, args| {
let f = args[0].try_into_float()?;
Ok(RuntimeValue::Float(f.ceil()))
},
}),
);
exports.insert(
"round".to_string(),
Rc::new(StdFunction {
name: "round".to_string(),
arity: 1,
callback: |_ctx, args| {
let f = args[0].try_into_float()?;
Ok(RuntimeValue::Float(f.round()))
},
}),
);
exports.insert(
"random".to_string(),
Rc::new(StdFunction {
name: "random".to_string(),
arity: 0,
callback: |_ctx, _args| {
use std::cell::Cell;
use std::time::SystemTime;
thread_local! {
static SEED: Cell<u64> = Cell::new({
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_default()
.as_nanos() as u64
});
}
let next_seed = SEED.with(|cell| {
let current = cell.get();
let next = current
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
cell.set(next);
next
});
let rand_float = (next_seed as f64) / (u64::MAX as f64);
Ok(RuntimeValue::Float(rand_float))
},
}),
);
exports.insert(
"to_float".to_string(),
Rc::new(StdFunction {
name: "to_float".to_string(),
arity: 1,
callback: |_ctx, args| {
let f = args[0].try_into_float()?;
Ok(RuntimeValue::Float(f))
},
}),
);
self.register_module(
"std.math",
StdlibModule {
name: "std.math".to_string(),
version: "1.0.0".to_string(),
exports,
required_capabilities: Vec::new(),
},
);
}
}