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
use ::metadata;
use ::expression::{ Expression, Evaluation };
use std;
#[derive(Debug)]
pub enum Error {
ArgumentError,
TypeError,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", *self)
}
}
impl std::error::Error for Error {
fn description(&self) -> &str {
match self {
&Error::ArgumentError => "Argument error",
&Error::TypeError => "Type error",
}
}
fn cause(&self) -> Option<&std::error::Error> {
None
}
}
pub type FunctionClosure<T> = Fn(&[Box<Expression<T>>], &T) -> Result<Evaluation, Error>;
pub struct Function<T: metadata::Provider> {
closure: Box<FunctionClosure<T>>,
name: String,
}
macro_rules! function_object_maker {
($func_name: ident) => {
pub fn make_function_object<T: metadata::Provider>() -> Function<T> {
Function::new(
stringify!($func_name),
Box::new(|expressions: &[Box<Expression<T>>], provider: &T| -> Result<Evaluation, Error> { $func_name(expressions, provider) })
)
}
}
}
#[macro_export]
macro_rules! try_integer_result {
($expression: expr, $provider: expr, $type: ty) => (
{
let eval = $expression.apply($provider);
let i_opt : Option<$type> = {
match eval.value() {
&Value::Integer(term) => Some(term as $type),
&Value::Double(term) => Some(term as $type),
&Value::Text(ref s) => {
match s.parse::<$type>() {
Ok(term) => Some(term),
_ => None,
}
}
_ => None,
}
};
if let Some(i) = i_opt {
Some((i, eval.truth()))
}
else {
None
}
}
);
($expression: expr, $provider: expr) => (
try_integer_result!($expression, $provider, i32)
);
}
#[macro_export]
macro_rules! expect_integer_result {
($expression: expr, $provider: expr, $type: ty) => (
match try_integer_result!($expression, $provider, $type) {
Some(eval) => eval,
None => return Err(Error::TypeError),
}
);
($expression: expr, $provider: expr) => (
expect_integer_result!($expression, $provider, i32)
);
}
#[macro_export]
macro_rules! expect_string_result {
($expression: expr, $provider: expr) => (
{
let eval = $expression.apply($provider);
(eval.to_string(), eval.truth())
}
);
}
mod arithmetic;
use self::arithmetic::*;
mod logical;
use self::logical::*;
mod control_flow;
use self::control_flow::*;
mod string;
use self::string::*;
pub fn standard_functions<T: metadata::Provider>() -> Vec<Box<Function<T>>> {
let mut s = Vec::new();
macro_rules! add_function {
($func_name: ident) => {
s.push(Box::new($func_name::make_function_object::<T>()));
}
}
add_function!(add);
add_function!(div);
add_function!(greater);
add_function!(max);
add_function!(min);
s.push(Box::new(mod_::make_function_object::<T>()));
add_function!(mul);
add_function!(muldiv);
add_function!(sub);
add_function!(and);
add_function!(or);
add_function!(not);
add_function!(xor);
s.push(Box::new(if_::make_function_object::<T>()));
add_function!(if2);
add_function!(if3);
add_function!(ifequal);
add_function!(ifgreater);
add_function!(iflonger);
add_function!(select);
add_function!(abbr);
add_function!(caps);
add_function!(caps2);
add_function!(crlf);
add_function!(cut);
add_function!(directory);
add_function!(directory_path);
add_function!(ext);
add_function!(filename);
s
}
impl<T: metadata::Provider> Function<T> {
pub fn new(name_param: &str, closure: Box<FunctionClosure<T>>) -> Function<T> {
Function {
closure,
name: name_param.to_lowercase(),
}
}
pub fn apply(&self, arguments: &[Box<Expression<T>>], provider: &T) -> Result<Evaluation, Error> {
(self.closure)(&arguments, &provider)
}
pub fn name(&self) -> &str {
self.name.as_str()
}
}