Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Is.cs
1using System;
2using System.Reflection;
3using System.Threading.Tasks;
8
10{
14 public class Is : BinaryScalarOperator
15 {
24 public Is(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 return this.EvaluateIs(Left, TypeValue);
40 else
41 return base.Evaluate(Left, Right, Variables);
42 }
43
44 private BooleanValue EvaluateIs(IElement Left, TypeValue Right)
45 {
46 Type Expected = Right.Value;
47 object Obj = Left.AssociatedObjectValue;
48 Type Candidate = Obj?.GetType() ?? typeof(object);
49
50 if (Expected.GetTypeInfo().IsAssignableFrom(Candidate.GetTypeInfo()))
51 return BooleanValue.True;
52 else
53 return BooleanValue.False;
54 }
55
63 public override Task<IElement> EvaluateAsync(IElement Left, IElement Right, Variables Variables)
64 {
65 if (Right is TypeValue TypeValue)
66 return Task.FromResult<IElement>(this.EvaluateIs(Left, TypeValue));
67 else
68 return base.EvaluateAsync(Left, Right, Variables);
69 }
70
79 {
80 if (Right is TypeValue TypeValue)
81 return this.EvaluateIs(Left, TypeValue);
82 else
83 throw new ScriptRuntimeException("Right operand in an IS operation must be a type value.", this);
84 }
85
93 public override Task<IElement> EvaluateScalarAsync(IElement Left, IElement Right, Variables Variables)
94 {
95 if (Right is TypeValue TypeValue)
96 return Task.FromResult<IElement>(this.EvaluateIs(Left, TypeValue));
97 else
98 throw new ScriptRuntimeException("Right operand in an IS operation must be a type value.", this);
99 }
100
104 public override UpgradeBehaviour ScalarUpgradeBehaviour => UpgradeBehaviour.DifferentTypesOk;
105 }
106}
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
Boolean-valued number.
Definition: BooleanValue.cs:12
static readonly BooleanValue False
Constant false value.
static readonly BooleanValue True
Constant true value.
override IElement EvaluateScalar(IElement Left, IElement Right, Variables Variables)
Evaluates the operator on scalar operands.
Definition: Is.cs:78
override IElement Evaluate(IElement Left, IElement Right, Variables Variables)
Evaluates the operator.
Definition: Is.cs:36
override Task< IElement > EvaluateAsync(IElement Left, IElement Right, Variables Variables)
Evaluates the operator.
Definition: Is.cs:63
override UpgradeBehaviour ScalarUpgradeBehaviour
How scalar operands of different types are to be treated. By default, scalar operands are required to...
Definition: Is.cs:104
override Task< IElement > EvaluateScalarAsync(IElement Left, IElement Right, Variables Variables)
Evaluates the operator on scalar operands.
Definition: Is.cs:93
Is(ScriptNode Left, ScriptNode Right, int Start, int Length, Expression Expression)
Is operator.
Definition: Is.cs:24
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.