Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Coalesce.cs
1using System;
2using System.Runtime.ExceptionServices;
3using System.Threading.Tasks;
4using Waher.Script;
8
10{
15 {
23 public Coalesce(ScriptNode Argument1, int Start, int Length, Expression Expression)
24 : this(new ScriptNode[] { Argument1 }, Start, Length, Expression)
25 {
26 }
27
36 public Coalesce(ScriptNode Argument1, ScriptNode Argument2, int Start, int Length, Expression Expression)
37 : this(new ScriptNode[] { Argument1, Argument2 }, Start, Length, Expression)
38 {
39 }
40
50 public Coalesce(ScriptNode Argument1, ScriptNode Argument2, ScriptNode Argument3, int Start, int Length, Expression Expression)
51 : this(new ScriptNode[] { Argument1, Argument2, Argument3 }, Start, Length, Expression)
52 {
53 }
54
65 public Coalesce(ScriptNode Argument1, ScriptNode Argument2, ScriptNode Argument3,
66 ScriptNode Argument4, int Start, int Length, Expression Expression)
67 : this(new ScriptNode[] { Argument1, Argument2, Argument3, Argument4 }, Start, Length, Expression)
68 {
69 }
70
82 public Coalesce(ScriptNode Argument1, ScriptNode Argument2, ScriptNode Argument3,
83 ScriptNode Argument4, ScriptNode Argument5, int Start, int Length, Expression Expression)
84 : this(new ScriptNode[] { Argument1, Argument2, Argument3, Argument4, Argument5 }, Start, Length, Expression)
85 {
86 }
87
100 public Coalesce(ScriptNode Argument1, ScriptNode Argument2, ScriptNode Argument3,
101 ScriptNode Argument4, ScriptNode Argument5, ScriptNode Argument6, int Start, int Length, Expression Expression)
102 : this(new ScriptNode[] { Argument1, Argument2, Argument3, Argument4, Argument5, Argument6 }, Start, Length, Expression)
103 {
104 }
105
114 : base(Arguments, NormalTypes(Arguments), Start, Length, Expression)
115 {
116 }
117
118 private static ArgumentType[] NormalTypes(ScriptNode[] Arguments)
119 {
120 int i, c = Arguments.Length;
121 ArgumentType[] Types = new ArgumentType[c];
122
123 for (i = 0; i < c; i++)
124 Types[i] = ArgumentType.Normal;
125
126 return Types;
127 }
128
132 public override string FunctionName => nameof(Coalesce);
133
137 public override string[] DefaultArgumentNames => new string[] { "Exp", "..." };
138
145 {
146 Exception LastException = null;
147
148 foreach (ScriptNode Argument in this.Arguments)
149 {
150 try
151 {
152 return Argument.Evaluate(Variables);
153 }
155 {
156 ExceptionDispatchInfo.Capture(ex).Throw();
158 }
159 catch (ScriptBreakLoopException ex)
160 {
161 ExceptionDispatchInfo.Capture(ex).Throw();
162 if (ex.HasLoopValue)
163 throw new ScriptBreakLoopException(ex.LoopValue);
164 else
165 throw new ScriptBreakLoopException();
166 }
168 {
169 ExceptionDispatchInfo.Capture(ex).Throw();
170 if (ex.HasLoopValue)
171 throw new ScriptContinueLoopException(ex.LoopValue);
172 else
173 throw new ScriptContinueLoopException();
174 }
175 catch (Exception ex)
176 {
177 LastException = ex;
178 }
179 }
180
181 if (!(LastException is null))
182 ExceptionDispatchInfo.Capture(LastException).Throw();
183
184 throw new ScriptRuntimeException("No valid result found.", this);
185 }
186
192 public override async Task<IElement> EvaluateAsync(Variables Variables)
193 {
194 Exception LastException = null;
195
196 foreach (ScriptNode Argument in this.Arguments)
197 {
198 try
199 {
200 return await Argument.EvaluateAsync(Variables);
201 }
203 {
204 ExceptionDispatchInfo.Capture(ex).Throw();
206 }
207 catch (ScriptBreakLoopException ex)
208 {
209 ExceptionDispatchInfo.Capture(ex).Throw();
210 if (ex.HasLoopValue)
211 throw new ScriptBreakLoopException(ex.LoopValue);
212 else
213 throw new ScriptBreakLoopException();
214 }
216 {
217 ExceptionDispatchInfo.Capture(ex).Throw();
218 if (ex.HasLoopValue)
219 throw new ScriptContinueLoopException(ex.LoopValue);
220 else
221 throw new ScriptContinueLoopException();
222 }
223 catch (Exception ex)
224 {
225 LastException = ex;
226 }
227 }
228
229 if (!(LastException is null))
230 ExceptionDispatchInfo.Capture(LastException).Throw();
231
232 throw new ScriptRuntimeException("No valid result found.", this);
233 }
234
242 {
243 return Arguments[0];
244 }
245
252 public override Task<IElement> EvaluateAsync(IElement[] Arguments, Variables Variables)
253 {
254 return Task.FromResult(this.Evaluate(Arguments, Variables));
255 }
256 }
257}
Coalesce(Expression, ...)
Definition: Coalesce.cs:15
Coalesce(ScriptNode Argument1, ScriptNode Argument2, ScriptNode Argument3, int Start, int Length, Expression Expression)
Coalesce(Exp1,Exp2,Exp3)
Definition: Coalesce.cs:50
override string[] DefaultArgumentNames
Default Argument names
Definition: Coalesce.cs:137
Coalesce(ScriptNode Argument1, ScriptNode Argument2, ScriptNode Argument3, ScriptNode Argument4, ScriptNode Argument5, int Start, int Length, Expression Expression)
Coalesce(Exp1,Exp2,Exp3,Exp4,Exp5)
Definition: Coalesce.cs:82
Coalesce(ScriptNode Argument1, int Start, int Length, Expression Expression)
Coalesce(Exp1)
Definition: Coalesce.cs:23
Coalesce(ScriptNode[] Arguments, int Start, int Length, Expression Expression)
Coalesce(Expression, ...)
Definition: Coalesce.cs:113
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: Coalesce.cs:144
Coalesce(ScriptNode Argument1, ScriptNode Argument2, ScriptNode Argument3, ScriptNode Argument4, int Start, int Length, Expression Expression)
Coalesce(Exp1,Exp2,Exp3,Exp4)
Definition: Coalesce.cs:65
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: Coalesce.cs:192
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: Coalesce.cs:241
Coalesce(ScriptNode Argument1, ScriptNode Argument2, ScriptNode Argument3, ScriptNode Argument4, ScriptNode Argument5, ScriptNode Argument6, int Start, int Length, Expression Expression)
Coalesce(Exp1,Exp2,Exp3,Exp4,Exp5,Exp6)
Definition: Coalesce.cs:100
override Task< IElement > EvaluateAsync(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: Coalesce.cs:252
override string FunctionName
Name of the function
Definition: Coalesce.cs:132
Coalesce(ScriptNode Argument1, ScriptNode Argument2, int Start, int Length, Expression Expression)
Coalesce(Exp1,Exp2)
Definition: Coalesce.cs:36
Class managing a script expression.
Definition: Expression.cs:41
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
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
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
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
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:21
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9