Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DynamicFuctionCall.cs
1using System.Threading.Tasks;
6
8{
13 {
14 private readonly ScriptNode[] arguments;
15 private readonly int nrArguments;
16
27 : base(Function, NullCheck, Start, Length, Expression)
28 {
29 this.arguments = Arguments;
30 this.arguments?.SetParent(this);
31
32 this.nrArguments = this.arguments.Length;
33
34 this.CalcIsAsync();
35 }
36
37 private void CalcIsAsync()
38 {
39 this.isAsync = this.op?.IsAsynchronous ?? false;
40
41 for (int i = 0; i < this.nrArguments; i++)
42 {
43 if (this.arguments[i]?.IsAsynchronous ?? false)
44 {
45 this.isAsync = true;
46 break;
47 }
48 }
49 }
50
54 public ScriptNode[] Arguments => this.arguments;
55
60 public override bool IsAsynchronous => this.isAsync || base.IsAsynchronous;
61
69 {
70 object Obj = Operand.AssociatedObjectValue;
71 if (Obj is null && this.nullCheck)
72 return ObjectValue.Null;
73
74 if (!(Obj is ILambdaExpression Lambda))
75 throw new ScriptRuntimeException("Expected a lambda expression.", this);
76
77 if (this.nrArguments != Lambda.NrArguments)
78 throw new ScriptRuntimeException("Expected " + Lambda.NrArguments.ToString() + " arguments.", this);
79
80 IElement[] Arg = new IElement[this.nrArguments];
81 ScriptNode Node;
82 int i;
83
84 for (i = 0; i < this.nrArguments; i++)
85 {
86 Node = this.arguments[i];
87 if (Node is null)
88 Arg[i] = ObjectValue.Null;
89 else
90 Arg[i] = Node.Evaluate(Variables);
91 }
92
93 return Lambda.Evaluate(Arg, Variables);
94 }
95
102 public override async Task<IElement> EvaluateScalarAsync(IElement Operand, Variables Variables)
103 {
104 object Obj = Operand.AssociatedObjectValue;
105 if (Obj is null && this.nullCheck)
106 return ObjectValue.Null;
107
108 if (!(Obj is ILambdaExpression Lambda))
109 throw new ScriptRuntimeException("Expected a lambda expression.", this);
110
111 if (this.nrArguments != Lambda.NrArguments)
112 throw new ScriptRuntimeException("Expected " + Lambda.NrArguments.ToString() + " arguments.", this);
113
114 IElement[] Arg = new IElement[this.nrArguments];
115 ScriptNode Node;
116 int i;
117
118 for (i = 0; i < this.nrArguments; i++)
119 {
120 Node = this.arguments[i];
121 if (Node is null)
122 Arg[i] = ObjectValue.Null;
123 else
124 Arg[i] = await Node.EvaluateAsync(Variables);
125 }
126
127 return await Lambda.EvaluateAsync(Arg, Variables);
128 }
129
137 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
138 {
139 int i;
140
141 if (Order == SearchMethod.DepthFirst)
142 {
143 if (!this.arguments.ForAllChildNodes(Callback, State, Order))
144 return false;
145 }
146
147 ScriptNode Node;
148 bool RecalcIsAsync = false;
149
150 for (i = 0; i < this.nrArguments; i++)
151 {
152 Node = this.arguments[i];
153 if (!(Node is null))
154 {
155 bool b = !Callback(Node, out ScriptNode NewNode, State);
156 if (!(NewNode is null))
157 {
158 this.arguments[i] = Node = NewNode;
159 NewNode.SetParent(this);
160
161 RecalcIsAsync = true;
162 }
163
164 if (b || (Order == SearchMethod.TreeOrder && !Node.ForAllChildNodes(Callback, State, Order)))
165 {
166 if (RecalcIsAsync)
167 this.CalcIsAsync();
168
169 return false;
170 }
171 }
172 }
173
174 if (RecalcIsAsync)
175 this.CalcIsAsync();
176
177 if (Order == SearchMethod.BreadthFirst)
178 {
179 if (!this.arguments.ForAllChildNodes(Callback, State, Order))
180 return false;
181 }
182
183 return true;
184 }
185
187 public override bool Equals(object obj)
188 {
189 return obj is DynamicFunctionCall O &&
190 AreEqual(this.arguments, O.arguments) &&
191 base.Equals(obj);
192 }
193
195 public override int GetHashCode()
196 {
197 int Result = base.GetHashCode();
198 Result ^= Result << 5 ^ GetHashCode(this.arguments);
199 return Result;
200 }
201 }
202}
Class managing a script expression.
Definition: Expression.cs:39
Base class for all funcions.
Definition: Function.cs:9
Base class for all unary scalar operators performing operand null checks.
readonly bool nullCheck
If null should be returned if operand is null.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, bool DepthFirst)
Calls the callback method for all child nodes.
Definition: ScriptNode.cs:243
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
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
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:86
override IElement EvaluateScalar(IElement Operand, Variables Variables)
Evaluates the operator on scalar operands.
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
DynamicFunctionCall(ScriptNode Function, ScriptNode[] Arguments, bool NullCheck, int Start, int Length, Expression Expression)
Dynamic function call operator
override async Task< IElement > EvaluateScalarAsync(IElement Operand, Variables Variables)
Evaluates the operator on scalar operands.
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
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