Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
VectorIndexAssignment.cs
1using System;
2using System.Collections.Generic;
3using System.Reflection;
4using System.Threading.Tasks;
10
12{
17 {
28 {
29 }
30
37 {
38 IElement Left = this.left.Evaluate(Variables);
39 IElement Index = this.middle.Evaluate(Variables);
40 IElement Value = this.right.Evaluate(Variables);
41
42 return this.Evaluate(Left, Index, Value);
43 }
44
50 public override async Task<IElement> EvaluateAsync(Variables Variables)
51 {
52 if (!this.isAsync)
53 return this.Evaluate(Variables);
54
55 IElement Left = await this.left.EvaluateAsync(Variables);
56 IElement Index = await this.middle.EvaluateAsync(Variables);
57 IElement Value = await this.right.EvaluateAsync(Variables);
58
59 return this.Evaluate(Left, Index, Value);
60 }
61
62 private IElement Evaluate(IElement Left, IElement Index, IElement Value)
63 {
64 if (Left is IVector V)
65 {
66 if (!(Index.AssociatedObjectValue is double d) || d < 0 || d > int.MaxValue || d != Math.Truncate(d))
67 throw new ScriptRuntimeException("Index must be a non-negative integer.", this);
68
69 V.SetElement((int)d, Value);
70
71 return Value;
72 }
73 else if (Left.IsScalar)
74 {
75 object Object = Left.AssociatedObjectValue;
76 if (Object is null)
77 throw new ScriptRuntimeException("Vector is null.", this);
78
79 if (Object is IDictionary<string, IElement> ObjExNihilo)
80 ObjExNihilo[Index.AssociatedObjectValue?.ToString()] = Value;
81 else
82 {
83 Type T = Object.GetType();
84 if (!VectorIndex.TryGetIndexProperty(T, false, true, out PropertyInfo ItemProperty, out ParameterInfo[] Parameters))
85 throw new ScriptRuntimeException("Vector element assignment operates on vectors.", this);
86
87 if (Index.TryConvertTo(Parameters[0].ParameterType, out object IndexValue))
88 ItemProperty.SetValue(Object, Value.AssociatedObjectValue, new object[] { IndexValue });
89 else
90 throw new ScriptRuntimeException("Provided index value not compatible with expected index type.", this);
91 }
92
93 return Value;
94 }
95 else
96 throw new ScriptRuntimeException("Vector element assignment can only be performed on vectors or on objects with a suitable index property defined.", this);
97 }
98
99 }
100}
Class managing a script expression.
Definition: Expression.cs:39
ScriptNode RightOperand
Right operand.
ScriptNode LeftOperand
Left operand.
ScriptNode right
Right operand.
ScriptNode left
Left operand.
bool isAsync
If subtree is asynchroneous.
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
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
Base class for all ternary operators.
ScriptNode middle
Middle operand.
VectorIndexAssignment(VectorIndex VectorIndex, ScriptNode Operand, int Start, int Length, Expression Expression)
Vector Index Assignment operator.
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
static bool TryGetIndexProperty(Type T, bool ForReading, bool ForWriting, out PropertyInfo PropertyInfo, out ParameterInfo[] Parameters)
Tries to get a one-dimensional index property of a Type.
Definition: VectorIndex.cs:133
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
bool TryConvertTo(Type DesiredType, out object Value)
Converts the value to a .NET type.
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
bool IsScalar
If the element represents a scalar value.
Definition: IElement.cs:41
Basic interface for vectors.
Definition: IVector.cs:9