Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NamedFuctionCall.cs
1using System.Threading.Tasks;
6
8{
13 {
14 private readonly string functionName;
15 private readonly ScriptNode[] arguments;
16 private readonly int nrArguments;
17 private readonly bool nullCheck;
18
29 : base(Start, Length, Expression)
30 {
31 this.functionName = FunctionName;
32
33 this.arguments = Arguments;
34 this.arguments?.SetParent(this);
35
36 this.nullCheck = NullCheck;
37 this.nrArguments = this.arguments.Length;
38 }
39
43 public string FunctionName => this.functionName;
44
48 public ScriptNode[] Arguments => this.arguments;
49
54 public override bool IsAsynchronous => true;
55
62 {
63 return this.EvaluateAsync(Variables).Result;
64 }
65
71 public override Task<IElement> EvaluateAsync(Variables Variables)
72 {
73 string s = this.nrArguments.ToString();
74
75 if ((Variables.TryGetVariable(this.functionName + " " + s, out Variable v) ||
76 Variables.TryGetVariable(this.functionName, out v)) &&
77 v.ValueObject is ILambdaExpression f)
78 {
79 return this.EvaluateAsync(Variables, f);
80 }
81
82 if (Variables.TryGetVariable("Global", out v) &&
83 v.ValueObject is Variables GlobalVariables &&
84 (GlobalVariables.TryGetVariable(this.functionName + " " + s, out v) ||
85 GlobalVariables.TryGetVariable(this.functionName, out v)) &&
86 v.ValueObject is ILambdaExpression f2)
87 {
88 return this.EvaluateAsync(Variables, f2);
89 }
90
91 if (this.nullCheck)
92 return Task.FromResult<IElement>(ObjectValue.Null);
93
94 if (this.nrArguments == 1)
95 throw new ScriptRuntimeException("No function defined having 1 argument named '" + this.functionName + "' found.", this);
96 else
97 throw new ScriptRuntimeException("No function defined having " + s + " arguments named '" + this.functionName + "' found.", this);
98 }
99
100 private async Task<IElement> EvaluateAsync(Variables Variables, ILambdaExpression f)
101 {
102 IElement[] Arg = new IElement[this.nrArguments];
103 ScriptNode Node;
104 int i;
105
106 for (i = 0; i < this.nrArguments; i++)
107 {
108 Node = this.arguments[i];
109 if (Node is null)
110 Arg[i] = ObjectValue.Null;
111 else
112 Arg[i] = await Node.EvaluateAsync(Variables);
113 }
114
115 if (f.IsAsynchronous)
116 return await f.EvaluateAsync(Arg, Variables);
117 else
118 return f.Evaluate(Arg, Variables);
119 }
120
128 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
129 {
130 int i;
131
132 if (Order == SearchMethod.DepthFirst)
133 {
134 if (!this.arguments.ForAllChildNodes(Callback, State, Order))
135 return false;
136 }
137
138 ScriptNode Node;
139
140 for (i = 0; i < this.nrArguments; i++)
141 {
142 Node = this.arguments[i];
143 if (!(Node is null))
144 {
145 bool b = !Callback(Node, out ScriptNode NewNode, State);
146 if (!(NewNode is null))
147 {
148 this.arguments[i] = Node = NewNode;
149 NewNode.SetParent(this);
150 }
151
152 if (b || (Order == SearchMethod.TreeOrder && !Node.ForAllChildNodes(Callback, State, Order)))
153 return false;
154 }
155 }
156
157 if (Order == SearchMethod.BreadthFirst)
158 {
159 if (!this.arguments.ForAllChildNodes(Callback, State, Order))
160 return false;
161 }
162
163 return true;
164 }
165
167 public override bool Equals(object obj)
168 {
169 return obj is NamedFunctionCall O &&
170 this.functionName.Equals(O.functionName) &&
171 this.nullCheck.Equals(O.nullCheck) &&
172 AreEqual(this.arguments, O.arguments) &&
173 base.Equals(obj);
174 }
175
177 public override int GetHashCode()
178 {
179 int Result = base.GetHashCode();
180 Result ^= Result << 5 ^ this.functionName.GetHashCode();
181 Result ^= Result << 5 ^ this.nullCheck.GetHashCode();
182 Result ^= Result << 5 ^ GetHashCode(this.arguments);
183 return Result;
184 }
185 }
186}
Class managing a script expression.
Definition: Expression.cs:39
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
void SetParent(ScriptNode Parent)
Sets the parent node. Can only be used when expression is being parsed.
Definition: ScriptNode.cs:132
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 Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
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.
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
NamedFunctionCall(string FunctionName, ScriptNode[] Arguments, bool NullCheck, int Start, int Length, Expression Expression)
Named function call operator
Contains information about a variable.
Definition: Variable.cs:10
Collection of variables.
Definition: Variables.cs:25
virtual bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
Definition: Variables.cs:52
Basic interface for all types of elements.
Definition: IElement.cs:20
Base interface for lambda expressions.
bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Task< IElement > EvaluateAsync(IElement[] Arguments, Variables Variables)
Evaluates the lambda expression.
IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the lambda expression.
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