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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! Contains everything you need to create your own built-in function.
//!
//! ## Creating your own function
//!
//! To create your own function declare your `fn` function and then convert it
//! to a `Function` struct. This module provides useful functions and macros to
//! easily create a simple one.
//!
//! ### Declaring and creating a function
//!
//! The first to do is declaring a function, you can do this using the `decl_func!`
//! macro.
//!
//! This macro takes three parameters:
//! - the function name,
//! - the function type,
//! - a predicate, which is the actual function that takes a value as input,
//! - the target type, a `ValueType`.
//!
//! Mark your function as `FunctionType::Trig` if it expects an angle.
//! Mark your function as `FunctionType::InverseTrig` if returns an angle if you
//! use `type_wrapper` (included in `decl_func!`).
//!
//! All angles are automatically converted to radians at function call.
//!
//! The predicate expects a `EvalResult<Value>` as output.
//!
//! Remember that functions name can only be alphabetic strings.
//!
//! Then use the `create_func!` macro to create a `Function` object. It takes
//! the function name and a `Arguments` as parameters. Then pass the created
//! object to `builtin::add_built_in_function`
//!
//!
//! ```
//! use num_parser::{*, function::*};
//!
//! fn main() {
//! // Declare the function
//! decl_func!(
//! // Function name
//! addone,
//! // Function type
//! FunctionType::Std,
//! // Predicate
//! | v: Value | {
//! Value::add(v, Value::from(1))
//! },
//! // The target type. This is the type at which the value will be converted to
//! ValueType::ComplexType
//! );
//!
//! // Add the function to the built-in ones.
//! builtin::add_built_in_function(
//! create_func!(addone, Arguments::Const(1))
//! );
//!
//! assert_eq!(eval("addone(1)").unwrap(), Value::from(2));
//!
//! }
//! ```
//!
//! ### Reading multiple parameters
//!
//! You can read multiple parameters by specifying `ValueType::VectorType` as your
//! target type and using the `read_vec_values!` macro. It takes the input value as
//! first parameter, and the any other identifier will be assigned a value.
//!
//! ```
//! # use num_parser::{*, function:: *};
//! #
//! # fn main() {
//! #
//! decl_func!(
//! // Function name
//! addtriplet,
//! // Function type
//! FunctionType::Std,
//! // Predicate
//! | v: Value | {
//! read_vec_values!(v, foo, bar, baz);
//! // foo, bar, baz are automatically declared. They are `&Value` of
//! // unknown type.
//!
//! Value::add(
//! Value::add(foo.clone(), bar.clone())?,
//! baz.clone()
//! )
//! },
//! // Use VectorType as target
//! ValueType::VectorType
//! );
//!
//! builtin::add_built_in_function(
//! create_func!(addtriplet, Arguments::Const(3))
//! );
//!
//! assert_eq!(eval("addtriplet(1,2,3)").unwrap(), Value::from(6));
//! # }
//! ```
//!
//! ### Creating a function with a dynamic argument counts
//!
//! You can create a function with a dynamic arguments count specifying
//! `Arguments::Dynamic` in the `create_func!` macro and by reading the arguments
//! manually.
//!
//! Example:
//! ```
//! # use num_parser::{*, function:: *};
//! #
//! # fn main() {
//! #
//! decl_func!(
//! min,
//! FunctionType::Std,
//! |v: Value| {
//! let vec = v.as_vector();
//! let mut min = vec[0].as_float()?;
//! for elem in vec {
//! if elem.as_float()? < min {
//! min = elem.as_float()?;
//! }
//! }
//! Ok(Value::Float(min))
//! },
//! ValueType::VectorType
//! );
//!
//! builtin::add_built_in_function(
//! create_func!(min, Arguments::Dynamic)
//! );
//!
//! assert_eq!(eval("min(-2,3,8)").unwrap(), Value::from(-2));
//! #
//! #
//! # }
//! ```
//!
use crate::;
/// A function object. You can pass this object to `builtin::add_built_in_function` to
/// make it available in all evaluations.
/// Contains the possible expected parameters for a function.
/// The function type. Handles angle conversions.
/// A function wrapper around a predicate to handle types.
///
/// This function does essentially three things:
/// 1. Converts the input value to the target type.
/// 2. Executes the predicate and insert the output into a value.
/// 3. Tries to convert it back to the original value type, if it is
/// of lower complexity.
/// Returns the output value(s) of the function arguments.
///
/// Arguments are actually expressions. This function calculates all of them and returns a `Value`,
/// which can be either a `VectorType` with all the values inside, or any other type if the argument
/// was just one.
/// Given a function name, a `FunctionType`, a predicate and a target `ValueType` declares a function. It generates
/// a wrapper that handles types and unbox parameters.
///
/// The predicate expects a `Value` as parameter and an `EvalResult<Value>` as output.
///
/// ## Examples
/// ```
/// // Remember to import everything under the `function` module.
/// use num_parser::{*, function::*};
///
/// decl_func!(
/// // Function name
/// hypotenuse,
/// // Function type
/// FunctionType::Std,
/// // Predicate
/// |v: Value| {
/// // Read the contained data using read_vec_values
/// read_vec_values!(v, a, b);
/// // Convert the data to the desired type
/// let a_as_number = a.as_float()?;
/// let b_as_number = b.as_float()?;
///
/// Ok(
/// Value::Float(
/// a_as_number.powi(2) + b_as_number.powi(2)
/// )
/// )
/// },
/// // Expect a vector as input, as we need multiple parameters.
/// ValueType::VectorType
/// );
/// ```
///
/// ### Generated code
///
/// ```
/// use num_parser::{*, function::*};
///
/// fn hypotenuse(arguments: &Vec<Box<Expression>>, context: &Context, depth: u32) -> EvalResult<Value> {
/// let unboxed = unbox_parameters(arguments, context, depth)?;
/// type_wrapper(
/// unboxed,
/// FunctionType::Std,
/// ValueType::VectorType,
/// context,
/// |v: Value| {
/// read_vec_values!(v, a, b);
///
/// let a_as_number = a.as_float()?;
/// let b_as_number = b.as_float()?;
///
/// Ok(
/// Value::Float(
/// a_as_number.powi(2) + b_as_number.powi(2)
/// )
/// )
/// }
/// )
/// }
/// ```
///
/// Given a `Value` of type `ValueType::VectorType` it declares variables with the provided names.
///
/// The generated code may return and `Err(ErrorType)` and should be executed in a function that expects
/// an `EvalResult<...>` as result.
///
/// ## Examples
/// ```
/// use num_parser::{*, function:: *};
///
/// fn read_values() -> EvalResult<()> {
///
/// let vec_values = Value::Vector(vec![
/// Value::from(1),
/// Value::from(7),
/// Value::from(-9)
/// ]);
///
/// read_vec_values!(vec_values, foo, bar, baz);
///
/// assert_eq!(foo, &Value::from(1));
/// assert_eq!(bar, &Value::from(7));
/// assert_eq!(baz, &Value::from(-9));
/// Ok(())
/// }
/// ```
/// Creates a `Function` object from an actual function and an `Arguments` object.
///
/// See `Function::new` for additional information.
///
/// The generated `Function` has the same name as the provided function. The provided function
/// needs `&Vec<Box<Expression>>`, a `&Context` and a u32 (for depth controls) as parameters and
/// returns an `EvalResult<Values>`. You can easily declare one using the `decl_func!` macro.
///
/// ## Examples
/// ```
/// use num_parser::{*, function::*};
///
/// // Declare the function
/// decl_func!(
/// // Function name
/// hypotenuse,
/// // Function type
/// FunctionType::Std,
/// // Predicate
/// |v: Value| {
/// // ...
/// # // Read the contained data using read_vec_values
/// # read_vec_values!(v, a, b);
/// # // Convert the data to the desired type
/// # let a_as_number = a.as_float()?;
/// # let b_as_number = b.as_float()?;
/// #
/// # Ok(
/// # Value::Float(
/// # a_as_number.powi(2) + b_as_number.powi(2)
/// # )
/// # )
/// },
/// // Expect a vector as input, as we need multiple parameters.
/// ValueType::VectorType
/// );
///
/// let hyp_func = create_func!(hypotenuse, Arguments::Const(2));
///
/// builtin::add_built_in_function(hyp_func);
///
///
/// ```
/// Converts angle units and executes the function. Useful for functions that take angles as inputs.
/// Executes the function and converts the output. Useful for functions that return angles.