public override string ToString() { if (Item != null) { return(Item.ToString() + ": " + Vector.ToString()); } return(Type.ToString() + ": " + ItemIndex.ToString() + ": " + VectorIndex.ToString()); }
/// <summary> /// Evaluates the node, using the variables provided in the <paramref name="Variables"/> collection. /// </summary> /// <param name="Variables">Variables collection.</param> /// <returns>Result.</returns> public override IElement Evaluate(Variables Variables) { IElement Left = this.left.Evaluate(Variables); IElement Index = this.middle.Evaluate(Variables); IElement Value = this.right.Evaluate(Variables); if (Left is IVector V) { double d; if (!(Index is DoubleNumber IE) || (d = IE.Value) < 0 || d > int.MaxValue || d != Math.Truncate(d)) { throw new ScriptRuntimeException("Index must be a non-negative integer.", this); } V.SetElement((int)d, Value); return(Value); } else if (Left.IsScalar) { object Object = Left.AssociatedObjectValue; if (Object is null) { throw new ScriptRuntimeException("Vector is null.", this); } if (Object is IDictionary <string, IElement> ObjExNihilo) { ObjExNihilo[Index.AssociatedObjectValue?.ToString()] = Value; } else { Type T = Object.GetType(); if (!VectorIndex.TryGetIndexProperty(T, out PropertyInfo ItemProperty, out ParameterInfo[] Parameters)) { throw new ScriptRuntimeException("Vector element assignment operates on vectors.", this); } if (Index.TryConvertTo(Parameters[0].ParameterType, out object IndexValue)) { ItemProperty.SetValue(Object, Value.AssociatedObjectValue, new object[] { IndexValue }); } else { throw new ScriptRuntimeException("Provided index value not compatible with expected index type.", this); } } return(Value); } else { throw new ScriptRuntimeException("Vector element assignment can only be performed on vectors or on objects with a suitable index property defined.", this); } }
/// <summary> /// Vector Index Assignment operator. /// </summary> /// <param name="VectorIndex">Vector Index</param> /// <param name="Operand">Operand.</param> /// <param name="Start">Start position in script expression.</param> /// <param name="Length">Length of expression covered by node.</param> /// <param name="Expression">Expression containing script.</param> public VectorIndexAssignment(VectorIndex VectorIndex, ScriptNode Operand, int Start, int Length, Expression Expression) : base(VectorIndex.LeftOperand, VectorIndex.RightOperand, Operand, Start, Length, Expression) { }
/// <summary> /// Evaluates the node, using the variables provided in the <paramref name="Variables"/> collection. /// </summary> /// <param name="Variables">Variables collection.</param> /// <returns>Result.</returns> public override IElement Evaluate(Variables Variables) { IElement Operand = this.op.Evaluate(Variables); object Value = Operand.AssociatedObjectValue; object Instance; if (Value is null && this.nullCheck) { return(ObjectValue.Null); } Type T; T = Value as Type; if (T is null) { Instance = Value; T = Value?.GetType() ?? typeof(object); } else { Instance = null; } lock (this.synchObject) { if (T != this.type) { this.type = T; this.field = null; this._event = null; this.methods = null; this.nameIndex = null; this.property = T.GetRuntimeProperty(this.name); if (this.property is null) { this.field = T.GetRuntimeField(this.name); if (this.field is null) { this._event = T.GetRuntimeEvent(this.name); if (this._event is null) { List <MethodLambda> Methods = null; foreach (MethodInfo MI in T.GetRuntimeMethods()) { if (MI.Name == Name) { if (Methods is null) { Methods = new List <MethodLambda>(); } Methods.Add(new MethodLambda(Instance, MI)); } } this.methods = Methods?.ToArray(); if (this.methods is null) { if (VectorIndex.TryGetIndexProperty(T, out this.property, out _)) { this.nameIndex = new string[] { this.name } } ; } } } } } object Result = null; if (!(this.property is null)) { try { if (!(this.nameIndex is null)) { Result = this.property.GetValue(Instance, this.nameIndex); } else { Result = this.property.GetValue(Instance, null); } }