Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Remove.cs
1using System;
2using System.Reflection;
3using System.Threading.Tasks;
9
11{
16 {
17 private string variableName;
18 private ScriptNode @object = null;
19
29 {
30 this.CheckArgument();
31 }
32
33 private void CheckArgument()
34 {
35 if (this.Argument is null)
36 {
37 this.@object = null;
38 this.variableName = string.Empty;
39 }
40 else
41 {
42 if (this.Argument is VariableReference Ref)
43 {
44 this.@object = null;
45 this.variableName = Ref.VariableName;
46 }
47 else if (this.Argument is NamedMember Member)
48 {
49 this.@object = Member.Operand;
50 this.variableName = Member.Name;
51 }
52 else
53 throw new SyntaxException("Variable reference or named property expected.", this.Argument.Start, string.Empty);
54 }
55 }
56
60 public override string[] DefaultArgumentNames => new string[] { "var" };
61
65 public string VariableName => this.variableName;
66
70 public override string FunctionName => nameof(Remove);
71
78 {
79 if (this.@object is null)
80 {
81 if (Variables.TryGetVariable(this.variableName, out Variable v))
82 {
83 Variables.Remove(this.variableName);
84 return v.ValueElement;
85 }
86 else
87 return ObjectValue.Null;
88 }
89 else
90 {
91 IElement Obj = this.@object.Evaluate(Variables);
92 Type T = Obj.AssociatedObjectValue.GetType();
93 MethodInfo MI = T.GetRuntimeMethod("Remove", stringArgument)
94 ?? throw new ScriptRuntimeException("Unable to remove property " + this.variableName + " from objects of type " + T.FullName + ".", this);
95
96 object Result = MI.Invoke(Obj.AssociatedObjectValue, new object[] { this.variableName });
97
99 }
100 }
101
108 public override async Task<IElement> EvaluateAsync(Variables Variables)
109 {
110 if (this.@object is null)
111 {
112 if (Variables.TryGetVariable(this.variableName, out Variable v))
113 {
114 Variables.Remove(this.variableName);
115 return v.ValueElement;
116 }
117 else
118 return ObjectValue.Null;
119 }
120 else
121 {
122 IElement Obj = await this.@object.EvaluateAsync(Variables);
123 Type T = Obj.AssociatedObjectValue.GetType();
124 MethodInfo MI = T.GetRuntimeMethod("Remove", stringArgument)
125 ?? throw new ScriptRuntimeException("Unable to remove property " + this.variableName + " from objects of type " + T.FullName + ".", this);
126
127 object Result = MI.Invoke(Obj.AssociatedObjectValue, new object[] { this.variableName });
128
129 return Expression.Encapsulate(await WaitPossibleTask(Result));
130 }
131 }
132
133 internal static readonly Type[] stringArgument = new Type[] { typeof(string) };
134
142 {
143 return Argument;
144 }
145
152 public override Task<IElement> EvaluateAsync(IElement Argument, Variables Variables)
153 {
154 return Task.FromResult<IElement>(Argument);
155 }
156
164 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
165 {
167 bool Result = base.ForAllChildNodes(Callback, State, Order);
168
169 if (Argument != this.Argument)
170 this.CheckArgument();
171
172 return Result;
173 }
174
175
176 }
177}
Class managing a script expression.
Definition: Expression.cs:39
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:4955
Removes a variable from the variables collection, without destroying its value.
Definition: Remove.cs:16
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: Remove.cs:77
override IElement Evaluate(IElement Argument, Variables Variables)
Evaluates the function.
Definition: Remove.cs:141
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
Definition: Remove.cs:108
override Task< IElement > EvaluateAsync(IElement Argument, Variables Variables)
Evaluates the function.
Definition: Remove.cs:152
Remove(ScriptNode Argument, int Start, int Length, Expression Expression)
Removes a variable from the variables collection, without destroying its value.
Definition: Remove.cs:27
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
Definition: Remove.cs:164
override string[] DefaultArgumentNames
Default Argument names
Definition: Remove.cs:60
override string FunctionName
Name of the function
Definition: Remove.cs:70
string VariableName
Name of variable.
Definition: Remove.cs:65
Base class for funcions of one variable.
ScriptNode Argument
Function argument.
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 async Task< object > WaitPossibleTask(object Result)
Waits for any asynchronous process to terminate.
Definition: ScriptNode.cs:417
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
static object UnnestPossibleTaskSync(object Result)
Checks if Result is an asynchronous results. If so, blocks the current thread until the result is co...
Definition: ScriptNode.cs:436
Represents a variable reference.
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:86
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
virtual bool Remove(string VariableName)
Removes a varaiable from the collection.
Definition: Variables.cs:147
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
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