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
use crate::;
/// Evaluate an expression with the default context.
///
/// See also: `eval_with_static_context` and `eval_with_mutable_context`.
///
/// You cannot declare functions or variables using this function.
///
/// ## Examples
/// This works:
/// ```
/// use num_parser::*;
///
/// let result = eval("2 + 2").unwrap();
///
/// assert_eq!(result, Value::from(4));
/// ```
///
///
/// This panics:
/// ```should_panic
/// use num_parser::*;
///
/// // Unwraps an error.
/// let result = eval("f(x) = 2x").unwrap();
/// ```
/// Evaluate an expression not allowing context changes.
///
/// See also `eval` and`eval_with_mutable_context`.
///
/// As in `eval` you cannot declare functions or variables using this function, but
/// in this case the context can already have its own declarations and use your own settings.
///
/// ## Examples
/// ```
/// use num_parser::*;
///
/// // Create context with custom settings
/// let context = Context::new(
/// settings::Rounding::Round(2),
/// settings::AngleUnit::Turn,
/// settings::DepthLimit::Limit(500)
/// );
///
/// let res = eval_with_static_context("pi + 1", &context).unwrap();
///
/// assert_eq!(res, Value::from(4.14));
/// ```
/// Evaluate an expression or add a declaration to the provided context.
///
/// See also `eval` and `eval_with_static_context`.
///
/// Returns `Ok(Some(Value))` if the request was an evaluation, or `Ok(None)` if it
/// was a declaration.
///
///
/// ## Examples
/// ```
/// use num_parser::*;
///
/// let mut context = Context::default();
///
/// // Add a declaration
/// let var_res = eval_with_mutable_context("a = 4", &mut context).unwrap();
/// let func_res = eval_with_mutable_context("g(x,y) = xsin(y)", &mut context).unwrap();
/// // Both results are None
/// assert_eq!(var_res, None);
/// assert_eq!(func_res, None);
///
/// let res = eval_with_mutable_context("g(1,pi/2) + a", &mut context).unwrap();
///
/// assert_eq!(res, Some(Value::from(5)));
///
/// ```