Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FunctionOneVariable.cs
1using System.Threading.Tasks;
3
4namespace Waher.Script.Model
5{
9 public abstract class FunctionOneVariable : Function
10 {
11 private ScriptNode argument;
12 private bool isAsync;
13
22 : base(Start, Length, Expression)
23 {
24 this.argument = Argument;
25 this.argument?.SetParent(this);
26
27 this.isAsync = Argument?.IsAsynchronous ?? false;
28 }
29
33 public ScriptNode Argument => this.argument;
34
38 public override string[] DefaultArgumentNames => new string[] { "x" };
39
44 public override bool IsAsynchronous => this.isAsync;
45
52 {
53 IElement Arg = this.argument.Evaluate(Variables);
54 return this.Evaluate(Arg, Variables);
55 }
56
62 public override async Task<IElement> EvaluateAsync(Variables Variables)
63 {
64 if (!this.IsAsynchronous)
65 return this.Evaluate(Variables);
66
67 IElement Arg = await this.argument.EvaluateAsync(Variables);
68 return await this.EvaluateAsync(Arg, Variables);
69 }
70
78
85 public virtual Task<IElement> EvaluateAsync(IElement Argument, Variables Variables)
86 {
87 return Task.FromResult(this.Evaluate(Argument, Variables));
88 }
89
97 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
98 {
99 if (Order == SearchMethod.DepthFirst)
100 {
101 if (!(this.argument?.ForAllChildNodes(Callback, State, Order) ?? true))
102 return false;
103 }
104
105 if (!(this.argument is null))
106 {
107 bool b = !Callback(this.argument, out ScriptNode NewNode, State);
108 if (!(NewNode is null))
109 {
110 this.argument = NewNode;
111 this.argument.SetParent(this);
112
113 this.isAsync = NewNode.IsAsynchronous;
114 }
115
116 if (b || (Order == SearchMethod.TreeOrder && !this.argument.ForAllChildNodes(Callback, State, Order)))
117 return false;
118 }
119
120 if (Order == SearchMethod.BreadthFirst)
121 {
122 if (!(this.argument?.ForAllChildNodes(Callback, State, Order) ?? true))
123 return false;
124 }
125
126 return true;
127 }
128
130 public override bool Equals(object obj)
131 {
132 return obj is FunctionOneVariable O &&
133 this.argument.Equals(O.argument) &&
134 base.Equals(obj);
135 }
136
138 public override int GetHashCode()
139 {
140 int Result = base.GetHashCode();
141 Result ^= Result << 5 ^ this.argument.GetHashCode();
142 return Result;
143 }
144
145 }
146}
Class managing a script expression.
Definition: Expression.cs:39
Base class for all funcions.
Definition: Function.cs:9
Base class for funcions of one variable.
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
ScriptNode Argument
Function argument.
override string[] DefaultArgumentNames
Default Argument names
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
FunctionOneVariable(ScriptNode Argument, int Start, int Length, Expression Expression)
Base class for funcions of one variable.
abstract IElement Evaluate(IElement Argument, Variables Variables)
Evaluates the function.
override async 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...
virtual Task< IElement > EvaluateAsync(IElement Argument, Variables Variables)
Evaluates the function.
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
override bool Equals(object obj)
Definition: ScriptNode.cs:258
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
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
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