Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
TernaryOperator.cs
1namespace Waher.Script.Model
2{
6 public abstract class TernaryOperator : BinaryOperator
7 {
11 protected ScriptNode middle;
12
23 : base(Left, Right, Start, Length, Expression)
24 {
25 this.middle = Middle;
26 this.middle?.SetParent(this);
27
28 this.CalcIsAsync();
29 }
30
34 protected override void CalcIsAsync()
35 {
36 this.isAsync =
37 (this.left?.IsAsynchronous ?? false) ||
38 (this.middle?.IsAsynchronous ?? false) ||
39 (this.right?.IsAsynchronous ?? false);
40 }
41
46
54 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
55 {
56 if (Order == SearchMethod.DepthFirst)
57 {
58 if (!(this.left?.ForAllChildNodes(Callback, State, Order) ?? true))
59 return false;
60
61 if (!(this.middle?.ForAllChildNodes(Callback, State, Order) ?? true))
62 return false;
63
64 if (!(this.right?.ForAllChildNodes(Callback, State, Order) ?? true))
65 return false;
66 }
67
68 ScriptNode NewNode;
69 bool RecalcIsAsync = false;
70 bool b;
71
72 if (!(this.left is null))
73 {
74 b = !Callback(this.left, out NewNode, State);
75 if (!(NewNode is null))
76 {
77 this.left = NewNode;
78 this.left.SetParent(this);
79
80 RecalcIsAsync = true;
81 }
82
83 if (b || (Order == SearchMethod.TreeOrder && !this.left.ForAllChildNodes(Callback, State, Order)))
84 {
85 if (RecalcIsAsync)
86 this.CalcIsAsync();
87
88 return false;
89 }
90 }
91
92 if (!(this.middle is null))
93 {
94 b = !Callback(this.middle, out NewNode, State);
95 if (!(NewNode is null))
96 {
97 this.middle = NewNode;
98 this.middle.SetParent(this);
99
100 RecalcIsAsync = true;
101 }
102
103 if (b || (Order == SearchMethod.TreeOrder && !this.middle.ForAllChildNodes(Callback, State, Order)))
104 {
105 if (RecalcIsAsync)
106 this.CalcIsAsync();
107
108 return false;
109 }
110 }
111
112 if (!(this.right is null))
113 {
114 b = !Callback(this.right, out NewNode, State);
115 if (!(NewNode is null))
116 {
117 this.right = NewNode;
118 this.right.SetParent(this);
119
120 RecalcIsAsync = true;
121 }
122
123 if (b || (Order == SearchMethod.TreeOrder && !this.right.ForAllChildNodes(Callback, State, Order)))
124 {
125 if (RecalcIsAsync)
126 this.CalcIsAsync();
127
128 return false;
129 }
130 }
131
132 if (RecalcIsAsync)
133 this.CalcIsAsync();
134
135 if (Order == SearchMethod.BreadthFirst)
136 {
137 if (!(this.left?.ForAllChildNodes(Callback, State, Order) ?? true))
138 return false;
139
140 if (!(this.middle?.ForAllChildNodes(Callback, State, Order) ?? true))
141 return false;
142
143 if (!(this.right?.ForAllChildNodes(Callback, State, Order) ?? true))
144 return false;
145 }
146
147 return true;
148 }
149
151 public override bool Equals(object obj)
152 {
153 return obj is TernaryOperator O &&
154 AreEqual(this.middle, O.middle) &&
155 base.Equals(obj);
156 }
157
159 public override int GetHashCode()
160 {
161 int Result = base.GetHashCode();
162 Result ^= Result << 5 ^ GetHashCode(this.middle);
163 return Result;
164 }
165
166 }
167}
Class managing a script expression.
Definition: Expression.cs:39
Base class for all binary operators.
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 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.
TernaryOperator(ScriptNode Left, ScriptNode Middle, ScriptNode Right, int Start, int Length, Expression Expression)
Base class for all ternary operators.
override void CalcIsAsync()
Recalculates if operator is asynchronous or not.
override bool Equals(object obj)
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
ScriptNode middle
Middle operand.
ScriptNode MiddleOperand
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