@@ -13,6 +13,7 @@ pub fn evaluate_node(
13
13
functions : & HashMap < String , Vec < MathNode > > ,
14
14
) -> Result < f64 , String > {
15
15
let head = nodes[ head_idx] . clone ( ) ;
16
+ //dbg!(values);
16
17
match head {
17
18
MathNode :: Root ( root) => {
18
19
if root. children . len ( ) != 1 {
@@ -99,9 +100,14 @@ pub fn evaluate_node(
99
100
for operand in apply. operands {
100
101
argument_values. push ( evaluate_node ( nodes, operand, values, functions) ?) ;
101
102
}
102
- //println!("evaluating");
103
- res = Some ( evaluate_lambda ( lambda, 0 , & argument_values, functions) ?) ;
104
- //dbg!(res);
103
+ res = Some ( evaluate_lambda (
104
+ lambda,
105
+ 0 ,
106
+ & argument_values,
107
+ values,
108
+ functions,
109
+ ) ?) ;
110
+ //dbg!(lambda_name, res);
105
111
}
106
112
}
107
113
if let Some ( value) = res {
@@ -128,6 +134,20 @@ pub fn evaluate_node(
128
134
Err ( "Wrong type" . to_string ( ) )
129
135
}
130
136
}
137
+ Some ( NumType :: Rational ) => {
138
+ if let Some ( Number :: Rational ( x, y) ) = cn. value {
139
+ Ok ( ( x as f64 ) / ( y as f64 ) )
140
+ } else {
141
+ Err ( "Wrong type" . to_string ( ) )
142
+ }
143
+ }
144
+ Some ( NumType :: ENotation ) => {
145
+ if let Some ( Number :: ENotation ( x, y) ) = cn. value {
146
+ Ok ( x * 10.0_f64 . powf ( y as f64 ) )
147
+ } else {
148
+ Err ( "Wrong type" . to_string ( ) )
149
+ }
150
+ }
131
151
_ => Err ( "Invalid Cn type" . to_string ( ) ) ,
132
152
} ,
133
153
MathNode :: Ci ( ci) => {
@@ -153,6 +173,7 @@ pub fn evaluate_lambda(
153
173
nodes : & [ MathNode ] ,
154
174
head_idx : NodeIndex ,
155
175
argument_values : & [ f64 ] ,
176
+ values : & HashMap < String , f64 > ,
156
177
functions : & HashMap < String , Vec < MathNode > > ,
157
178
) -> Result < f64 , String > {
158
179
let head = nodes[ head_idx] . clone ( ) ;
@@ -161,7 +182,7 @@ pub fn evaluate_lambda(
161
182
if root. children . len ( ) != 1 {
162
183
return Err ( "Root with multiple/zero children!" . to_string ( ) ) ;
163
184
}
164
- evaluate_lambda ( nodes, root. children [ 0 ] , argument_values, functions)
185
+ evaluate_lambda ( nodes, root. children [ 0 ] , argument_values, values , functions)
165
186
}
166
187
MathNode :: Lambda ( lambda) => {
167
188
let mut argument_names = Vec :: new ( ) ;
@@ -182,18 +203,11 @@ pub fn evaluate_lambda(
182
203
for i in 0 ..argument_values. len ( ) {
183
204
assignments. insert ( argument_names[ i] . clone ( ) , argument_values[ i] ) ;
184
205
}
185
- Ok ( evaluate_node (
186
- nodes,
187
- lambda. expr . unwrap ( ) ,
188
- & assignments,
189
- functions,
190
- ) ?)
206
+ let res = evaluate_node ( nodes, lambda. expr . unwrap ( ) , & assignments, functions) ?;
207
+ Ok ( res)
191
208
}
192
209
}
193
- _ => {
194
- //dbg!(head);
195
- Err ( "couldn't parse lambda" . to_string ( ) )
196
- }
210
+ _ => evaluate_node ( nodes, head_idx, values, functions) ,
197
211
}
198
212
}
199
213
@@ -292,40 +306,66 @@ pub fn evaluate_condition(
292
306
let mut result = None ;
293
307
// If this is a regular mathematical operator, go ahead
294
308
if let Ok ( op) = op_result {
295
- let mut a = None ;
296
- let mut b = None ;
309
+ let mut operand_results = Vec :: < f64 > :: new ( ) ;
310
+ let mut child_condition_results = Vec :: < bool > :: new ( ) ;
297
311
match op {
298
312
Op :: Eq | Op :: Neq | Op :: Geq | Op :: Leq | Op :: Gt | Op :: Lt => {
299
313
if apply. operands . len ( ) != 2 {
300
314
return Err ( "Invalid number of operands." . to_string ( ) ) ;
301
315
}
302
- a = Some ( evaluate_node ( nodes, apply. operands [ 0 ] , values, functions) ?) ;
303
- b = Some ( evaluate_node ( nodes, apply. operands [ 1 ] , values, functions) ?) ;
316
+ for operand_location in apply. operands {
317
+ operand_results. push ( evaluate_node (
318
+ nodes,
319
+ operand_location,
320
+ values,
321
+ functions,
322
+ ) ?) ;
323
+ }
324
+ }
325
+ Op :: And | Op :: Or | Op :: Xor => {
326
+ for operand_location in apply. operands {
327
+ child_condition_results. push ( evaluate_condition (
328
+ nodes,
329
+ operand_location,
330
+ values,
331
+ functions,
332
+ ) ?) ;
333
+ }
304
334
}
305
335
_ => { }
306
336
}
307
- if let Some ( first_value) = a {
308
- if let Some ( second_value) = b {
309
- match op {
310
- Op :: Eq => {
311
- result = Some ( ( first_value - second_value) . abs ( ) <= f64:: EPSILON )
312
- }
313
- Op :: Neq => {
314
- result = Some ( ( first_value - second_value) . abs ( ) > f64:: EPSILON )
315
- }
316
- Op :: Gt => result = Some ( first_value > second_value) ,
317
- Op :: Lt => result = Some ( first_value < second_value) ,
318
- Op :: Geq => result = Some ( first_value >= second_value) ,
319
- Op :: Leq => result = Some ( first_value <= second_value) ,
320
- _ => { }
321
- }
337
+
338
+ let condition_count = child_condition_results. len ( ) ;
339
+ let true_count = child_condition_results. iter ( ) . filter ( |x| * * x) . count ( ) ;
340
+ match op {
341
+ Op :: Eq => {
342
+ result =
343
+ Some ( ( operand_results[ 0 ] - operand_results[ 1 ] ) . abs ( ) <= f64:: EPSILON )
322
344
}
345
+ Op :: Neq => {
346
+ result =
347
+ Some ( ( operand_results[ 0 ] - operand_results[ 1 ] ) . abs ( ) > f64:: EPSILON )
348
+ }
349
+ Op :: Gt => result = Some ( operand_results[ 0 ] > operand_results[ 1 ] ) ,
350
+ Op :: Lt => result = Some ( operand_results[ 0 ] < operand_results[ 1 ] ) ,
351
+ Op :: Geq => result = Some ( operand_results[ 0 ] >= operand_results[ 1 ] ) ,
352
+ Op :: Leq => result = Some ( operand_results[ 0 ] <= operand_results[ 1 ] ) ,
353
+ Op :: And => result = Some ( condition_count == true_count) ,
354
+ Op :: Or => result = Some ( true_count > 0 ) ,
355
+ Op :: Xor => {
356
+ result = Some ( true_count % 2 == 1 ) ;
357
+ //dbg!(child_condition_results);
358
+ //if result.unwrap() {
359
+ //dbg!(result.unwrap());
360
+ //}
361
+ }
362
+ _ => { }
323
363
}
324
364
}
325
365
if let Some ( value) = result {
326
366
Ok ( value)
327
367
} else {
328
- Err ( "Invalid operator " . to_string ( ) )
368
+ Err ( "Condition not supported " . to_string ( ) )
329
369
}
330
370
}
331
371
_ => {
0 commit comments