Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
QuaternaryOperator.cs
1using System;
2
3namespace Waher.Script.Model
4{
8 public abstract class QuaternaryOperator : TernaryOperator
9 {
14
26 : base(Left, Middle1, Right, Start, Length, Expression)
27 {
28 this.middle2 = Middle2;
29 this.middle2?.SetParent(this);
30
31 this.CalcIsAsync();
32 }
33
37 protected override void CalcIsAsync()
38 {
39 this.isAsync =
40 (this.left?.IsAsynchronous ?? false) ||
41 (this.middle?.IsAsynchronous ?? false) ||
42 (this.middle2?.IsAsynchronous ?? false) ||
43 (this.right?.IsAsynchronous ?? false);
44 }
45
50
58 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
59 {
60 if (Order == SearchMethod.DepthFirst)
61 {
62 if (!(this.left?.ForAllChildNodes(Callback, State, Order) ?? true))
63 return false;
64
65 if (!(this.middle?.ForAllChildNodes(Callback, State, Order) ?? true))
66 return false;
67
68 if (!(this.middle2?.ForAllChildNodes(Callback, State, Order) ?? true))
69 return false;
70
71 if (!(this.right?.ForAllChildNodes(Callback, State, Order) ?? true))
72 return false;
73 }
74
75 ScriptNode NewNode;
76 bool RecalcIsAsync = false;
77 bool b;
78
79 if (!(this.left is null))
80 {
81 b = !Callback(this.left, out NewNode, State);
82 if (!(NewNode is null))
83 {
84 this.left = NewNode;
85 this.left.SetParent(this);
86
87 RecalcIsAsync = true;
88 }
89
90 if (b || (Order == SearchMethod.TreeOrder && !this.left.ForAllChildNodes(Callback, State, Order)))
91 {
92 if (RecalcIsAsync)
93 this.CalcIsAsync();
94
95 return false;
96 }
97 }
98
99 if (!(this.middle is null))
100 {
101 b = !Callback(this.middle, out NewNode, State);
102 if (!(NewNode is null))
103 {
104 this.middle = NewNode;
105 this.middle.SetParent(this);
106
107 RecalcIsAsync = true;
108 }
109
110 if (b || (Order == SearchMethod.TreeOrder && !this.middle.ForAllChildNodes(Callback, State, Order)))
111 {
112 if (RecalcIsAsync)
113 this.CalcIsAsync();
114
115 return false;
116 }
117 }
118
119 if (!(this.middle2 is null))
120 {
121 b = !Callback(this.middle2, out NewNode, State);
122 if (!(NewNode is null))
123 {
124 this.middle2 = NewNode;
125 this.middle2.SetParent(this);
126
127 RecalcIsAsync = true;
128 }
129
130 if (b || (Order == SearchMethod.TreeOrder && !this.middle2.ForAllChildNodes(Callback, State, Order)))
131 {
132 if (RecalcIsAsync)
133 this.CalcIsAsync();
134
135 return false;
136 }
137 }
138
139 if (!(this.right is null))
140 {
141 b = !Callback(this.right, out NewNode, State);
142 if (!(NewNode is null))
143 {
144 this.right = NewNode;
145 this.right.SetParent(this);
146
147 RecalcIsAsync = true;
148 }
149
150 if (b || (Order == SearchMethod.TreeOrder && !this.right.ForAllChildNodes(Callback, State, Order)))
151 {
152 if (RecalcIsAsync)
153 this.CalcIsAsync();
154
155 return false;
156 }
157 }
158
159 if (RecalcIsAsync)
160 this.CalcIsAsync();
161
162 if (Order == SearchMethod.BreadthFirst)
163 {
164 if (!(this.left?.ForAllChildNodes(Callback, State, Order) ?? true))
165 return false;
166
167 if (!(this.middle?.ForAllChildNodes(Callback, State, Order) ?? true))
168 return false;
169
170 if (!(this.middle2?.ForAllChildNodes(Callback, State, Order) ?? true))
171 return false;
172
173 if (!(this.right?.ForAllChildNodes(Callback, State, Order) ?? true))
174 return false;
175 }
176
177 return true;
178 }
179
181 public override bool Equals(object obj)
182 {
183 return obj is QuaternaryOperator O &&
184 AreEqual(this.middle2, O.middle2) &&
185 base.Equals(obj);
186 }
187
189 public override int GetHashCode()
190 {
191 int Result = base.GetHashCode();
192 Result ^= Result << 5 ^ GetHashCode(this.middle2);
193 return Result;
194 }
195
196 }
197}
Class managing a script expression.
Definition: Expression.cs:39
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.
Base class for all quaternary operators.
override void CalcIsAsync()
Recalculates if operator is asynchronous or not.
ScriptNode Middle2Operand
Second middle operand.
ScriptNode middle2
Second Middle operand.
QuaternaryOperator(ScriptNode Left, ScriptNode Middle1, ScriptNode Middle2, ScriptNode Right, int Start, int Length, Expression Expression)
Base class for all quaternary operators.
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
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
Base class for all ternary operators.
ScriptNode middle
Middle operand.
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