Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
BinaryDoubleOperator.cs
1using System;
2using System.Threading.Tasks;
6
7namespace Waher.Script.Model
8{
13 {
23 : base(Left, Right, Start, Length, Expression)
24 {
25 }
26
33 {
34 IElement L = this.left.Evaluate(Variables);
35 IElement R = this.right.Evaluate(Variables);
36
37 if (L.AssociatedObjectValue is double DL && R.AssociatedObjectValue is double DR)
38 return this.Evaluate(DL, DR);
39 else
40 return this.Evaluate(L, R, Variables);
41 }
42
48 public override async Task<IElement> EvaluateAsync(Variables Variables)
49 {
50 if (!this.isAsync)
51 return this.Evaluate(Variables);
52
53 IElement L = await this.left.EvaluateAsync(Variables);
54 IElement R = await this.right.EvaluateAsync(Variables);
55
56 if (L.AssociatedObjectValue is double DL && R.AssociatedObjectValue is double DR)
57 return await this.EvaluateAsync(DL, DR);
58 else
59 return await this.EvaluateAsync(L, R, Variables);
60 }
61
70 {
71 object L = Left.AssociatedObjectValue;
72 object R = Right.AssociatedObjectValue;
73
74 if (!(L is double l) && !Expression.TryConvert(Left.AssociatedObjectValue, out l))
75 throw new ScriptRuntimeException("Scalar operands must be double values.", this);
76
77 if (!(R is double r) && !Expression.TryConvert(Right.AssociatedObjectValue, out r))
78 throw new ScriptRuntimeException("Scalar operands must be double values.", this);
79
80 return this.Evaluate(l, r);
81 }
82
90 public override Task<IElement> EvaluateScalarAsync(IElement Left, IElement Right, Variables Variables)
91 {
92 object L = Left.AssociatedObjectValue;
93 object R = Right.AssociatedObjectValue;
94
95 if (!(L is double l) && !Expression.TryConvert(Left.AssociatedObjectValue, out l))
96 throw new ScriptRuntimeException("Scalar operands must be double values.", this);
97
98 if (!(R is double r) && !Expression.TryConvert(Right.AssociatedObjectValue, out r))
99 throw new ScriptRuntimeException("Scalar operands must be double values.", this);
100
101 return this.EvaluateAsync(l, r);
102 }
103
110 public abstract IElement Evaluate(double Left, double Right);
111
118 public virtual Task<IElement> EvaluateAsync(double Left, double Right)
119 {
120 return Task.FromResult<IElement>(this.Evaluate(Left, Right));
121 }
122
123 }
124}
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 double operators.
BinaryDoubleOperator(ScriptNode Left, ScriptNode Right, int Start, int Length, Expression Expression)
Base class for binary double operators.
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
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.
override Task< IElement > EvaluateScalarAsync(IElement Left, IElement Right, Variables Variables)
Evaluates the operator on scalar operands.
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
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