Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
BinaryBooleanOperator.cs
1using System;
2using System.Threading.Tasks;
6
8{
13 {
14 private bool? bothBool = null;
15
25 : base(Left, Right, Start, Length, Expression)
26 {
27 }
28
35 {
36 IElement L;
37 bool ScalarDirectly = false;
38
39 try
40 {
41 L = this.left.Evaluate(Variables);
42 }
43 catch (Exception exLeft)
44 {
45 L = new ObjectValue(exLeft);
46 ScalarDirectly = true;
47 }
48
49 BooleanValue BL = L as BooleanValue;
50 BooleanValue BR;
51 IElement Result;
52 IElement R;
53
54 if (this.bothBool.HasValue && this.bothBool.Value && !(BL is null))
55 {
56 bool LValue = BL.Value;
57 Result = this.EvaluateOptimizedResult(LValue);
58 if (!(Result is null))
59 return Result;
60
61 try
62 {
63 R = this.right.Evaluate(Variables);
64 }
65 catch (Exception exRight)
66 {
67 R = new ObjectValue(exRight);
68 ScalarDirectly = true;
69 }
70
71 BR = R as BooleanValue;
72
73 if (!(BR is null))
74 return this.Evaluate(LValue, BR.Value);
75 else
76 this.bothBool = false;
77 }
78 else
79 {
80 try
81 {
82 R = this.right.Evaluate(Variables);
83 }
84 catch (Exception exRight)
85 {
86 R = new ObjectValue(exRight);
87 ScalarDirectly = true;
88 }
89
90 BR = R as BooleanValue;
91
92 if (!(BL is null) && !(BR is null))
93 {
94 if (!this.bothBool.HasValue)
95 this.bothBool = true;
96
97 return this.Evaluate(BL.Value, BR.Value);
98 }
99 else
100 {
101 this.bothBool = false;
102
103 if (ScalarDirectly)
104 return this.EvaluateScalar(L, R, Variables);
105 else
106 return this.Evaluate(L, R, Variables);
107 }
108 }
109
110 if (ScalarDirectly)
111 return this.EvaluateScalar(L, R, Variables);
112 else
113 return this.Evaluate(L, R, Variables);
114 }
115
121 public override async Task<IElement> EvaluateAsync(Variables Variables)
122 {
123 if (!this.isAsync)
124 return this.Evaluate(Variables);
125
126 IElement L;
127
128 try
129 {
130 L = await this.left.EvaluateAsync(Variables);
131 }
132 catch (Exception exLeft)
133 {
134 L = new ObjectValue(exLeft);
135 }
136
137 BooleanValue BL = L as BooleanValue;
138 BooleanValue BR;
139 IElement Result;
140 IElement R;
141
142 if (this.bothBool.HasValue && this.bothBool.Value && !(BL is null))
143 {
144 bool LValue = BL.Value;
145 Result = await this.EvaluateOptimizedResultAsync(LValue);
146 if (!(Result is null))
147 return Result;
148
149 try
150 {
151 R = await this.right.EvaluateAsync(Variables);
152 }
153 catch (Exception exRight)
154 {
155 R = new ObjectValue(exRight);
156 }
157
158 BR = R as BooleanValue;
159
160 if (!(BR is null))
161 return await this.EvaluateAsync(LValue, BR.Value);
162 else
163 this.bothBool = false;
164 }
165 else
166 {
167 try
168 {
169 R = await this.right.EvaluateAsync(Variables);
170 }
171 catch (Exception exRight)
172 {
173 R = new ObjectValue(exRight);
174 }
175
176 BR = R as BooleanValue;
177
178 if (!(BL is null) && !(BR is null))
179 {
180 if (!this.bothBool.HasValue)
181 this.bothBool = true;
182
183 return await this.EvaluateAsync(BL.Value, BR.Value);
184 }
185 else
186 {
187 this.bothBool = false;
188 return await this.EvaluateAsync(L, R, Variables);
189 }
190 }
191
192 return await this.EvaluateAsync(L, R, Variables);
193 }
194
203 {
204 if (Left.AssociatedObjectValue is Exception exLeft)
205 {
206 if (Right.AssociatedObjectValue is Exception exRight)
207 return this.Evaluate(exLeft, exRight);
208 else
209 {
210 if (!(Right.AssociatedObjectValue is bool r)
212 {
213 throw new ScriptRuntimeException("Scalar operands must be boolean values.", this);
214 }
215
216 return this.Evaluate(exLeft, r);
217 }
218 }
219 else if (Right.AssociatedObjectValue is Exception exRight)
220 {
221 if (!(Left.AssociatedObjectValue is bool l) &&
223 {
224 throw new ScriptRuntimeException("Scalar operands must be boolean values.", this);
225 }
226
227 return this.Evaluate(l, exRight);
228 }
229 else
230 {
231 if (!(Left.AssociatedObjectValue is bool l) &&
233 {
234 throw new ScriptRuntimeException("Scalar operands must be boolean values.", this);
235 }
236
237 if (!(Right.AssociatedObjectValue is bool r)
239 {
240 throw new ScriptRuntimeException("Scalar operands must be boolean values.", this);
241 }
242
243 return this.Evaluate(l, r);
244 }
245 }
246
254 public override async Task<IElement> EvaluateScalarAsync(IElement Left, IElement Right, Variables Variables)
255 {
256 if (Left.AssociatedObjectValue is Exception exLeft)
257 {
258 if (Right.AssociatedObjectValue is Exception exRight)
259 return await this.EvaluateAsync(exLeft, exRight);
260 else
261 {
262 if (!(Right.AssociatedObjectValue is bool r)
264 {
265 throw new ScriptRuntimeException("Scalar operands must be boolean values.", this);
266 }
267
268 return await this.EvaluateAsync(exLeft, r);
269 }
270 }
271 else if (Right.AssociatedObjectValue is Exception exRight)
272 {
273 if (!(Left.AssociatedObjectValue is bool l) &&
275 {
276 throw new ScriptRuntimeException("Scalar operands must be boolean values.", this);
277 }
278
279 return await this.EvaluateAsync(l, exRight);
280 }
281 else
282 {
283 if (!(Left.AssociatedObjectValue is bool l) &&
285 {
286 throw new ScriptRuntimeException("Scalar operands must be boolean values.", this);
287 }
288
289 if (!(Right.AssociatedObjectValue is bool r)
291 {
292 throw new ScriptRuntimeException("Scalar operands must be boolean values.", this);
293 }
294
295 return await this.EvaluateAsync(l, r);
296 }
297 }
298
305 public abstract IElement EvaluateOptimizedResult(bool Left);
306
313 public abstract IElement Evaluate(bool Left, bool Right);
314
321 public abstract IElement Evaluate(Exception Left, bool Right);
322
329 public abstract IElement Evaluate(bool Left, Exception Right);
330
337 public abstract IElement Evaluate(Exception Left, Exception Right);
338
345 public virtual Task<IElement> EvaluateOptimizedResultAsync(bool Left)
346 {
347 return Task.FromResult(this.EvaluateOptimizedResult(Left));
348 }
349
356 public virtual Task<IElement> EvaluateAsync(bool Left, bool Right)
357 {
358 return Task.FromResult(this.Evaluate(Left, Right));
359 }
360
367 public virtual Task<IElement> EvaluateAsync(Exception Left, bool Right)
368 {
369 return Task.FromResult(this.Evaluate(Left, Right));
370 }
371
378 public virtual Task<IElement> EvaluateAsync(bool Left, Exception Right)
379 {
380 return Task.FromResult(this.Evaluate(Left, Right));
381 }
382
389 public virtual Task<IElement> EvaluateAsync(Exception Left, Exception Right)
390 {
391 return Task.FromResult(this.Evaluate(Left, Right));
392 }
393 }
394}
Class managing a script expression.
Definition: Expression.cs:39
static bool TryConvert(object Value, Type DesiredType, out object Result)
Tries to convert an object Value to an object of type DesiredType .
Definition: Expression.cs:5268
Base class for binary boolean operators.
virtual Task< IElement > EvaluateAsync(Exception Left, bool Right)
Evaluates the boolean operator.
abstract IElement Evaluate(bool Left, Exception Right)
Evaluates the boolean operator.
virtual Task< IElement > EvaluateOptimizedResultAsync(bool Left)
Gives the operator a chance to optimize its execution if it knows the value of the left operand....
abstract IElement Evaluate(bool Left, bool Right)
Evaluates the boolean operator.
BinaryBooleanOperator(ScriptNode Left, ScriptNode Right, int Start, int Length, Expression Expression)
Base class for binary boolean operators.
override IElement EvaluateScalar(IElement Left, IElement Right, Variables Variables)
Evaluates the operator on scalar operands.
abstract IElement EvaluateOptimizedResult(bool Left)
Gives the operator a chance to optimize its execution if it knows the value of the left operand....
virtual Task< IElement > EvaluateAsync(bool Left, bool Right)
Evaluates the boolean operator.
virtual Task< IElement > EvaluateAsync(bool Left, Exception Right)
Evaluates the boolean operator.
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
virtual Task< IElement > EvaluateAsync(Exception Left, Exception Right)
Evaluates the boolean operator.
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
abstract IElement Evaluate(Exception Left, bool Right)
Evaluates the boolean operator.
abstract IElement Evaluate(Exception Left, Exception Right)
Evaluates the boolean operator.
override async Task< IElement > EvaluateScalarAsync(IElement Left, IElement Right, Variables Variables)
Evaluates the operator on scalar operands.
ScriptNode right
Right operand.
ScriptNode left
Left operand.
bool isAsync
If subtree is asynchroneous.
Base class for binary scalar operators.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
abstract IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
virtual Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
Definition: ScriptNode.cs:158
Boolean-valued number.
Definition: BooleanValue.cs:12
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33