Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
As.cs
1using System;
2using System.Reflection;
3using System.Threading.Tasks;
8
10{
14 public class As : BinaryScalarOperator
15 {
24 public As(ScriptNode Left, ScriptNode Right, int Start, int Length, Expression Expression)
25 : base(Left, Right, Start, Length, Expression)
26 {
27 }
28
36 public override IElement Evaluate(IElement Left, IElement Right, Variables Variables)
37 {
38 if (Right is TypeValue TypeValue)
39 {
40 Type T = TypeValue.Value;
41 object Obj = Left.AssociatedObjectValue;
42
43 if (!(Obj is null) && T.GetTypeInfo().IsAssignableFrom(Obj.GetType().GetTypeInfo()))
44 return Left;
45 else
46 return ObjectValue.Null;
47 }
48 else
49 return base.Evaluate(Left, Right, Variables);
50 }
51
59 public override Task<IElement> EvaluateAsync(IElement Left, IElement Right, Variables Variables)
60 {
61 if (Right is TypeValue TypeValue)
62 {
63 Type T = TypeValue.Value;
64 object Obj = Left.AssociatedObjectValue;
65
66 if (!(Obj is null) && T.GetTypeInfo().IsAssignableFrom(Obj.GetType().GetTypeInfo()))
67 return Task.FromResult<IElement>(Left);
68 else
69 return Task.FromResult<IElement>(ObjectValue.Null);
70 }
71 else
72 return base.EvaluateAsync(Left, Right, Variables);
73 }
74
83 {
84 if (Right is TypeValue TypeValue)
85 {
86 Type T = TypeValue.Value;
87 object Obj = Left.AssociatedObjectValue;
88
89 if (!(Obj is null) && T.GetTypeInfo().IsAssignableFrom(Obj.GetType().GetTypeInfo()))
90 return Left;
91 else
92 return ObjectValue.Null;
93 }
94 else
95 throw new ScriptRuntimeException("Right operand in an AS operation must be a type value.", this);
96 }
97
105 public override Task<IElement> EvaluateScalarAsync(IElement Left, IElement Right, Variables Variables)
106 {
107 if (Right is TypeValue TypeValue)
108 {
109 Type T = TypeValue.Value;
110 object Obj = Left.AssociatedObjectValue;
111
112 if (!(Obj is null) && T.GetTypeInfo().IsAssignableFrom(Obj.GetType().GetTypeInfo()))
113 return Task.FromResult<IElement>(Left);
114 else
115 return Task.FromResult<IElement>(ObjectValue.Null);
116 }
117 else
118 throw new ScriptRuntimeException("Right operand in an AS operation must be a type value.", this);
119 }
120
124 public override UpgradeBehaviour ScalarUpgradeBehaviour => UpgradeBehaviour.DifferentTypesOk;
125 }
126}
Class managing a script expression.
Definition: Expression.cs:39
Base class for binary scalar operators.
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
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:86
override Task< IElement > EvaluateAsync(IElement Left, IElement Right, Variables Variables)
Evaluates the operator.
Definition: As.cs:59
override IElement Evaluate(IElement Left, IElement Right, Variables Variables)
Evaluates the operator.
Definition: As.cs:36
As(ScriptNode Left, ScriptNode Right, int Start, int Length, Expression Expression)
As operator.
Definition: As.cs:24
override UpgradeBehaviour ScalarUpgradeBehaviour
How scalar operands of different types are to be treated. By default, scalar operands are required to...
Definition: As.cs:124
override IElement EvaluateScalar(IElement Left, IElement Right, Variables Variables)
Evaluates the operator on scalar operands.
Definition: As.cs:82
override Task< IElement > EvaluateScalarAsync(IElement Left, IElement Right, Variables Variables)
Evaluates the operator on scalar operands.
Definition: As.cs:105
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
UpgradeBehaviour
How operands are to be handled if not of the same type.