Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
BinaryOperator.cs
3
4namespace Waher.Script.Model
5{
9 public abstract class BinaryOperator : ScriptNode
10 {
14 protected ScriptNode left;
15
19 protected ScriptNode right;
20
24 protected bool isAsync;
25
35 : base(Start, Length, Expression)
36 {
37 this.left = Left;
38 this.left?.SetParent(this);
39
40 this.right = Right;
41 this.right?.SetParent(this);
42
43 this.CalcIsAsync();
44 }
45
49 protected virtual void CalcIsAsync()
50 {
51 this.isAsync =
52 (this.left?.IsAsynchronous ?? false) ||
53 (this.right?.IsAsynchronous ?? false);
54 }
55
59 public ScriptNode LeftOperand => this.left;
60
65
69 public virtual string DefaultVariableName
70 {
71 get
72 {
73 if (this.left is IDifferentiable Left &&
74 this.right is IDifferentiable Right)
75 {
76 string s = Left.DefaultVariableName;
77 if (s is null)
78 return null;
79 else if (s == Right.DefaultVariableName)
80 return s;
81 else
82 return null;
83 }
84 else
85 return null;
86 }
87 }
88
93 public override bool IsAsynchronous => this.isAsync;
94
102 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
103 {
104 if (Order == SearchMethod.DepthFirst)
105 {
106 if (!(this.left?.ForAllChildNodes(Callback, State, Order) ?? true))
107 return false;
108
109 if (!(this.right?.ForAllChildNodes(Callback, State, Order) ?? true))
110 return false;
111 }
112
113 ScriptNode NewNode;
114 bool RecalcIsAsync = false;
115 bool b;
116
117 if (!(this.left is null))
118 {
119 b = !Callback(this.left, out NewNode, State);
120 if (!(NewNode is null))
121 {
122 this.left = NewNode;
123 this.left.SetParent(this);
124
125 RecalcIsAsync = true;
126 }
127
128 if (b || (Order == SearchMethod.TreeOrder && !this.left.ForAllChildNodes(Callback, State, Order)))
129 {
130 if (RecalcIsAsync)
131 this.CalcIsAsync();
132
133 return false;
134 }
135 }
136
137 if (!(this.right is null))
138 {
139 b = !Callback(this.right, out NewNode, State);
140 if (!(NewNode is null))
141 {
142 this.right = NewNode;
143 this.right.SetParent(this);
144
145 RecalcIsAsync = true;
146 }
147
148 if (b || (Order == SearchMethod.TreeOrder && !this.right.ForAllChildNodes(Callback, State, Order)))
149 {
150 if (RecalcIsAsync)
151 this.CalcIsAsync();
152
153 return false;
154 }
155 }
156
157 if (RecalcIsAsync)
158 this.CalcIsAsync();
159
160 if (Order == SearchMethod.BreadthFirst)
161 {
162 if (!(this.left?.ForAllChildNodes(Callback, State, Order) ?? true))
163 return false;
164
165 if (!(this.right?.ForAllChildNodes(Callback, State, Order) ?? true))
166 return false;
167 }
168
169 return true;
170 }
171
173 public override bool Equals(object obj)
174 {
175 return obj is BinaryOperator O &&
176 AreEqual(this.left, O.left) &&
177 AreEqual(this.right, O.right) &&
178 base.Equals(obj);
179 }
180
182 public override int GetHashCode()
183 {
184 int Result = base.GetHashCode();
185 Result ^= Result << 5 ^ GetHashCode(this.left);
186 Result ^= Result << 5 ^ GetHashCode(this.right);
187 return Result;
188 }
189
200 public static IElement EvaluateNamedOperator(string Name, IElement Left, IElement Right, ScriptNode Node)
201 {
202 ScriptNode[] ArgumentNodes = new ScriptNode[] { Node, Node };
203 NamedMethodCall OperatorMethod = new NamedMethodCall(Node, Name, ArgumentNodes,
204 false, Node?.Start ?? 0, Node?.Length ?? 0, Node?.Expression);
205
206 IElement[] Arguments = new IElement[] { Left, Right };
207
208 IElement Result = OperatorMethod.EvaluateAsync(
209 Left.AssociatedObjectValue?.GetType() ?? typeof(object),
210 null, Arguments, null).Result;
211
212 if (!(Result is null))
213 return Result;
214
215 Result = OperatorMethod.EvaluateAsync(
216 Right.AssociatedObjectValue?.GetType() ?? typeof(object),
217 null, Arguments, null).Result;
218
219 return Result;
220 }
221 }
222}
Class managing a script expression.
Definition: Expression.cs:39
Base class for all binary operators.
ScriptNode RightOperand
Right operand.
virtual void CalcIsAsync()
Recalculates if operator is asynchronous or not.
ScriptNode LeftOperand
Left operand.
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
ScriptNode right
Right operand.
ScriptNode left
Left operand.
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
override bool Equals(object obj)
static IElement EvaluateNamedOperator(string Name, IElement Left, IElement Right, ScriptNode Node)
Evaluates a named operator available in code-behind.
BinaryOperator(ScriptNode Left, ScriptNode Right, int Start, int Length, Expression Expression)
Base class for all binary operators.
bool isAsync
If subtree is asynchroneous.
virtual string DefaultVariableName
Default variable name, if any, null otherwise.
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
static bool AreEqual(ScriptNode S1, ScriptNode S2)
Compares if two script nodes are equal.
Definition: ScriptNode.cs:275
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
virtual bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: ScriptNode.cs:142
void SetParent(ScriptNode Parent)
Sets the parent node. Can only be used when expression is being parsed.
Definition: ScriptNode.cs:132
override async Task< IElement > EvaluateAsync(IElement Operand, Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
Base interface for lambda expressions.
delegate bool ScriptNodeEventHandler(ScriptNode Node, out ScriptNode NewNode, object State)
Delegate for ScriptNode callback methods.
SearchMethod
Method to traverse the expression structure
Definition: ScriptNode.cs:38