Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Right.cs
1using System;
2using System.Collections.Generic;
8
10{
15 {
25 : base(new ScriptNode[] { X, N }, new ArgumentType[] { ArgumentType.Normal, ArgumentType.Scalar },
27 {
28 }
29
33 public override string FunctionName => nameof(Right);
34
38 public override string[] DefaultArgumentNames
39 {
40 get { return new string[] { "x", "N" }; }
41 }
42
50 {
51 double N = Expression.ToDouble(Arguments[1].AssociatedObjectValue);
52 if (N < 0 || N > int.MaxValue || N != Math.Truncate(N))
53 throw new ScriptRuntimeException("Expected nonnegative integer in second argument.", this);
54
55 int c = (int)N;
56
57 if (Arguments[0].AssociatedObjectValue is string s)
58 {
59 if (c > s.Length)
60 return Arguments[0];
61 else
62 return new StringValue(s.Substring(s.Length - c, c));
63 }
64 else
65 {
66 ICollection<IElement> ChildElements;
67
68 if (Arguments[0] is IVector Vector)
69 {
70 if (Vector.Dimension <= c)
71 return Vector;
72
73 ChildElements = Vector.VectorElements;
74 }
75 else if (Arguments[0] is ISet Set)
76 {
77 int? Size = Set.Size;
78
79 if (Size.HasValue && Size.Value <= c)
80 return Set;
81
82 ChildElements = Set.ChildElements;
83 }
84 else
85 throw new ScriptRuntimeException("First argument expected to be a string, vector or a set.", this);
86
87 IElement[] Result = new IElement[c];
88
89 if (ChildElements is IElement[] V)
90 Array.Copy(V, V.Length - c, Result, 0, c);
91 else if (c > 0)
92 {
93 int i = 0;
94 int j = c - ChildElements.Count;
95
96 foreach (IElement Element in ChildElements)
97 {
98 if (j++ >= 0)
99 Result[i++] = Element;
100 }
101 }
102
103 return Arguments[0].Encapsulate(Result, this);
104 }
105 }
106
107 }
108}
Base class for all types of elements.
Definition: Element.cs:13
Base class for all types of sets.
Definition: Set.cs:14
virtual ? int Size
Size of set, if finite and known, otherwise null is returned.
Definition: Set.cs:92
override ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: Set.cs:73
Class managing a script expression.
Definition: Expression.cs:39
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:4824
override string[] DefaultArgumentNames
Default Argument names
Definition: Right.cs:39
Right(ScriptNode X, ScriptNode N, int Start, int Length, Expression Expression)
Right(x,N)
Definition: Right.cs:24
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function on a vector argument.
Definition: Right.cs:49
override string FunctionName
Name of the function
Definition: Right.cs:33
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
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
Basic interface for vectors.
Definition: IVector.cs:9
Basic interface for all types of sets.
Definition: ISet.cs:10
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9