Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
BinaryDualBoolDoubleOperator.cs
1using System;
2using System.Runtime.ExceptionServices;
3using System.Threading.Tasks;
7
8namespace Waher.Script.Model
9{
14 {
15 private bool? bothBool = null;
16 private bool? bothDouble = null;
17
27 : base(Left, Right, Start, Length, Expression)
28 {
29 }
30
37 {
38 IElement L = this.left.Evaluate(Variables);
39 IElement R = null;
40 IElement Result;
41 BooleanValue BL = L as BooleanValue;
42 BooleanValue BR;
43
44 if (this.bothBool.HasValue && this.bothBool.Value && !(BL is null))
45 {
46 bool LValue = BL.Value;
47 Result = this.EvaluateOptimizedResult(LValue);
48 if (!(Result is null))
49 return Result;
50
51 R = this.right.Evaluate(Variables);
52 BR = R as BooleanValue;
53
54 if (!(BR is null))
55 return this.Evaluate(LValue, BR.Value);
56 else
57 this.bothBool = false;
58 }
59
60 if (R is null)
61 {
62 if (!(BL is null) && !this.bothBool.HasValue)
63 {
64 try
65 {
66 R = this.right.Evaluate(Variables);
67 BR = R as BooleanValue;
68
69 if (!(BR is null))
70 {
71 this.bothBool = true;
72 return this.Evaluate(BL.Value, BR.Value);
73 }
74 else
75 this.bothBool = false;
76 }
77 catch (Exception ex)
78 {
79 Result = this.EvaluateOptimizedResult(BL.Value);
80 if (Result is null)
81 ExceptionDispatchInfo.Capture(ex).Throw();
82 else
83 {
84 this.bothBool = true;
85 return Result;
86 }
87 }
88 }
89 else
90 R = this.right.Evaluate(Variables);
91 }
92
93 if (this.bothDouble.HasValue)
94 {
95 if (this.bothDouble.Value)
96 {
97 if (L.AssociatedObjectValue is double DL && R.AssociatedObjectValue is double DR)
98 return this.Evaluate(DL, DR);
99 else
100 this.bothDouble = false;
101 }
102 }
103 else
104 {
105 if (L.AssociatedObjectValue is double DL && R.AssociatedObjectValue is double DR)
106 {
107 this.bothDouble = true;
108 return this.Evaluate(DL, DR);
109 }
110 else
111 this.bothDouble = false;
112 }
113
114 return this.Evaluate(L, R, Variables);
115 }
116
122 public override async Task<IElement> EvaluateAsync(Variables Variables)
123 {
124 if (!this.isAsync)
125 return this.Evaluate(Variables);
126
127 IElement L = await this.left.EvaluateAsync(Variables);
128 IElement R = null;
129 IElement Result;
130 BooleanValue BL = L as BooleanValue;
131 BooleanValue BR;
132
133 if (this.bothBool.HasValue && this.bothBool.Value && !(BL is null))
134 {
135 bool LValue = BL.Value;
136 Result = await this.EvaluateOptimizedResultAsync(LValue);
137 if (!(Result is null))
138 return Result;
139
140 R = await this.right.EvaluateAsync(Variables);
141 BR = R as BooleanValue;
142
143 if (!(BR is null))
144 return await this.EvaluateAsync(LValue, BR.Value);
145 else
146 this.bothBool = false;
147 }
148
149 if (R is null)
150 {
151 if (!(BL is null) && !this.bothBool.HasValue)
152 {
153 try
154 {
155 R = await this.right.EvaluateAsync(Variables);
156 BR = R as BooleanValue;
157
158 if (!(BR is null))
159 {
160 this.bothBool = true;
161 return await this.EvaluateAsync(BL.Value, BR.Value);
162 }
163 else
164 this.bothBool = false;
165 }
166 catch (Exception ex)
167 {
168 Result = await this.EvaluateOptimizedResultAsync(BL.Value);
169 if (Result is null)
170 ExceptionDispatchInfo.Capture(ex).Throw();
171 else
172 {
173 this.bothBool = true;
174 return Result;
175 }
176 }
177 }
178 else
179 R = await this.right.EvaluateAsync(Variables);
180 }
181
182 if (this.bothDouble.HasValue)
183 {
184 if (this.bothDouble.Value)
185 {
186 if (L.AssociatedObjectValue is double DL && R.AssociatedObjectValue is double DR)
187 return await this.EvaluateAsync(DL, DR);
188 else
189 this.bothDouble = false;
190 }
191 }
192 else
193 {
194 if (L.AssociatedObjectValue is double DL && R.AssociatedObjectValue is double DR)
195 {
196 this.bothDouble = true;
197 return await this.EvaluateAsync(DL, DR);
198 }
199 else
200 this.bothDouble = false;
201 }
202
203 return await this.EvaluateAsync(L, R, Variables);
204 }
205
214 {
215 if (Left.AssociatedObjectValue is bool BL &&
216 Right.AssociatedObjectValue is bool BR)
217 {
218 return this.Evaluate(BL, BR);
219 }
220 else
221 {
222 if (!(Left.AssociatedObjectValue is double l) &&
224 {
225 throw new ScriptRuntimeException("Scalar operands must be double values.", this);
226 }
227
228 if (!(Right.AssociatedObjectValue is double r) &&
230 {
231 throw new ScriptRuntimeException("Scalar operands must be double values.", this);
232 }
233
234 return this.Evaluate(l, r);
235 }
236 }
237
245 public override Task<IElement> EvaluateScalarAsync(IElement Left, IElement Right, Variables Variables)
246 {
247 if (Left.AssociatedObjectValue is bool BL &&
248 Right.AssociatedObjectValue is bool BR)
249 {
250 return this.EvaluateAsync(BL, BR);
251 }
252 else
253 {
254 if (!(Left.AssociatedObjectValue is double l) &&
256 {
257 throw new ScriptRuntimeException("Scalar operands must be double values.", this);
258 }
259
260 if (!(Right.AssociatedObjectValue is double r) &&
262 {
263 throw new ScriptRuntimeException("Scalar operands must be double values.", this);
264 }
265
266 return this.EvaluateAsync(l, r);
267 }
268 }
269
276 public abstract IElement EvaluateOptimizedResult(bool Left);
277
284 public abstract IElement Evaluate(bool Left, bool Right);
285
292 public abstract IElement Evaluate(double Left, double Right);
293
300 public virtual Task<IElement> EvaluateOptimizedResultAsync(bool Left)
301 {
302 return Task.FromResult<IElement>(this.EvaluateOptimizedResult(Left));
303 }
304
311 public virtual Task<IElement> EvaluateAsync(bool Left, bool Right)
312 {
313 return Task.FromResult<IElement>(this.Evaluate(Left, Right));
314 }
315
322 public virtual Task<IElement> EvaluateAsync(double Left, double Right)
323 {
324 return Task.FromResult<IElement>(this.Evaluate(Left, Right));
325 }
326
327 }
328}
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 dual double/bool operators.
override Task< IElement > EvaluateScalarAsync(IElement Left, IElement Right, Variables Variables)
Evaluates the operator on scalar operands.
override IElement EvaluateScalar(IElement Left, IElement Right, Variables Variables)
Evaluates the operator on scalar operands.
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
abstract IElement Evaluate(double Left, double Right)
Evaluates the double 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....
BinaryDualBoolDoubleOperator(ScriptNode Left, ScriptNode Right, int Start, int Length, Expression Expression)
Base class for binary dual double/bool operators.
virtual Task< IElement > EvaluateAsync(bool Left, bool Right)
Evaluates the boolean operator.
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
abstract IElement EvaluateOptimizedResult(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.
virtual Task< IElement > EvaluateAsync(double Left, double Right)
Evaluates the double operator.
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