-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoperator.c
245 lines (198 loc) · 6.19 KB
/
operator.c
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
#include "operator.h"
#include "command_class.h"
#include "command_parser.h"
#include "stdarg.h"
#include <service.h>
#include <stdarg.h>
#include <stdio.h>
variant_t* op_equal(operator_t* op, ...);
variant_t* op_less(operator_t* op, ...);
variant_t* op_more(operator_t* op, ...);
variant_t* op_and(operator_t* op, ...);
variant_t* op_or(operator_t* op, ...);
variant_t* op_plus(operator_t* op, ...);
variant_t* op_minus(operator_t* op, ...);
variant_t* op_device_function(operator_t* op, ...);
variant_t* op_service_method(operator_t* op, ...);
variant_t* op_unary_minus(operator_t* op, ...);
int unary_argument_count(struct operator_t* op);
int binary_argument_count(struct operator_t* op);
int function_argument_count(struct operator_t* op);
int service_method_argument_count(struct operator_t* op);
static operator_t operator_list[] = {
{OP_DEVICE_FUNCTION, NULL, &function_argument_count, &op_device_function},
{OP_SERVICE_METHOD, NULL, &service_method_argument_count, &op_service_method},
{OP_LEFT_PARETHESIS, NULL, NULL, NULL},
{OP_RIGHT_PARETHESIS, NULL, NULL, NULL},
{OP_LESS, NULL, &binary_argument_count, &op_less},
{OP_MORE, NULL, &binary_argument_count, &op_more},
{OP_CMP, NULL, &binary_argument_count, &op_equal},
{OP_AND, NULL, &binary_argument_count, &op_and},
{OP_OR, NULL, &binary_argument_count, &op_or},
{OP_PLUS, NULL, &binary_argument_count, &op_plus},
{OP_MINUS, NULL, &binary_argument_count, &op_minus},
{OP_UNARY_MINUS, NULL, &unary_argument_count, &op_unary_minus}
};
operator_t* operator_create(OperatorType type, void* operator_data)
{
operator_t* new_operator = (operator_t*)malloc(sizeof(operator_t));
operator_t* ref_operator = NULL;
int i;
for(i = 0; i < sizeof(operator_list) / sizeof(operator_t) && ref_operator == NULL; i++)
{
if(operator_list[i].type == type)
{
ref_operator = &operator_list[i];
}
}
if(NULL != ref_operator)
{
new_operator->type = type;
new_operator->operator_data = operator_data;
new_operator->argument_count = ref_operator->argument_count;
new_operator->eval_callback = ref_operator->eval_callback;
return new_operator;
}
else
{
return NULL;
}
}
variant_t* op_equal(operator_t* op, ...)
{
va_list args;
va_start(args, op);
variant_t* arg1 = va_arg(args, variant_t*);
variant_t* arg2 = va_arg(args, variant_t*);
va_end(args);
return variant_create_bool(variant_compare(arg1, arg2) == 0);
}
variant_t* op_less(operator_t* op, ...)
{
va_list args;
va_start(args, op);
variant_t* arg1 = va_arg(args, variant_t*);
variant_t* arg2 = va_arg(args, variant_t*);
va_end(args);
return variant_create_bool((variant_compare(arg1, arg2) < 0));
}
variant_t* op_more(operator_t* op, ...)
{
va_list args;
va_start(args, op);
variant_t* arg1 = va_arg(args, variant_t*);
variant_t* arg2 = va_arg(args, variant_t*);
va_end(args);
return variant_create_bool(variant_compare(arg1, arg2) > 0);
}
variant_t* op_and(operator_t* op, ...)
{
va_list args;
va_start(args, op);
variant_t* arg1 = va_arg(args, variant_t*);
variant_t* arg2 = va_arg(args, variant_t*);
va_end(args);
return variant_create_bool(variant_get_bool(arg1) && variant_get_bool(arg2));
}
variant_t* op_or(operator_t* op, ...)
{
va_list args;
va_start(args, op);
variant_t* arg1 = va_arg(args, variant_t*);
variant_t* arg2 = va_arg(args, variant_t*);
va_end(args);
return variant_create_bool(variant_get_bool(arg1) || variant_get_bool(arg2));
}
variant_t* op_plus(operator_t* op, ...)
{
va_list args;
va_start(args, op);
variant_t* arg1 = va_arg(args, variant_t*);
variant_t* arg2 = va_arg(args, variant_t*);
va_end(args);
return variant_add(arg1, arg2);
}
variant_t* op_minus(operator_t* op, ...)
{
va_list args;
va_start(args, op);
variant_t* arg1 = va_arg(args, variant_t*);
variant_t* arg2 = va_arg(args, variant_t*);
va_end(args);
return variant_subtract(arg1, arg2);
}
variant_t* op_unary_minus(operator_t* op, ...)
{
va_list args;
va_start(args, op);
variant_t* arg1 = va_arg(args, variant_t*);
variant_t* arg0 = variant_create_int32(DT_INT32, 0);
variant_t* result = variant_subtract(arg0, arg1);
variant_free(arg0);
va_end(args);
return result;
}
variant_t* op_device_function(operator_t* op, ...)
{
function_operator_data_t* op_data = (function_operator_data_t*)op->operator_data;
//return op_data->command_class->command_impl(op_data->command_method, op_data->instance_id, );
va_list args;
va_start(args, op);
if(NULL != op_data->command_class)
{
return op_data->command_class->command_impl((const char*)op_data->command_method, op_data->device_record, args);
}
else
{
return NULL;
}
}
variant_t* op_service_method(operator_t* op, ...)
{
service_method_t* method = (service_method_t*)op->operator_data;
va_list args;
va_start(args, op);
return method->eval_callback(method, args);
}
int unary_argument_count(struct operator_t* op)
{
return 1;
}
int binary_argument_count(struct operator_t* op)
{
return 2;
}
int function_argument_count(struct operator_t* op)
{
function_operator_data_t* op_data = (function_operator_data_t*)op->operator_data;
if(NULL == op_data->command_class)
{
return 0;
}
command_method_t* supported_methods = op_data->command_class->supported_method_list;
while(supported_methods->name)
{
if(strcmp(supported_methods->name, op_data->command_method) == 0)
{
return supported_methods->nargs;
}
supported_methods++;
}
return 0;
}
int service_method_argument_count(struct operator_t* op)
{
service_method_t* method = (service_method_t*)op->operator_data;
return method->args;
}
void operator_delete_function_operator(void* arg)
{
operator_t* op = (operator_t*)arg;
free(op->operator_data);
free(op);
}
void operator_delete_service_method_operator(void* arg)
{
operator_t* op = (operator_t*)arg;
free(op);
}