Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Optional.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
7
9{
14 {
24 {
25 }
26
30 public override string FunctionName => nameof(Optional);
31
38 {
39 try
40 {
41 return this.Argument.Evaluate(Variables);
42 }
43 catch (Exception)
44 {
45 return ObjectValue.Null;
46 }
47 }
48
54 public override async Task<IElement> EvaluateAsync(Variables Variables)
55 {
56 try
57 {
58 return await this.Argument.EvaluateAsync(Variables);
59 }
60 catch (Exception)
61 {
62 return ObjectValue.Null;
63 }
64 }
65
73 {
74 return Argument;
75 }
76
83 public override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary<string, IElement> AlreadyFound)
84 {
85 object Obj = CheckAgainst.AssociatedObjectValue;
86
87 if ((Obj is null) || (Obj is string s && string.IsNullOrEmpty(s)))
88 {
89 return this.ForAllChildNodes((ScriptNode Node, out ScriptNode NewNode, object State) =>
90 {
91 NewNode = null;
92
93 if (Node is VariableReference Ref)
94 {
95 if (AlreadyFound.TryGetValue(Ref.VariableName, out IElement Value) &&
96 !(Value.AssociatedObjectValue is null))
97 {
98 return false;
99 }
100
101 AlreadyFound[Ref.VariableName] = null;
102 }
103
104 return true;
105 }, null, SearchMethod.TreeOrder) ? PatternMatchResult.Match : PatternMatchResult.NoMatch;
106 }
107 else
108 return this.Argument.PatternMatch(CheckAgainst, AlreadyFound);
109 }
110 }
111}
Class managing a script expression.
Definition: Expression.cs:39
Makes sure an expression is defined. Otherwise, an exception is thrown.
Definition: Optional.cs:14
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: Optional.cs:37
override IElement Evaluate(IElement Argument, Variables Variables)
Evaluates the function.
Definition: Optional.cs:72
Optional(ScriptNode Argument, int Start, int Length, Expression Expression)
Makes sure an expression is defined. Otherwise, an exception is thrown.
Definition: Optional.cs:22
override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
Definition: Optional.cs:83
override string FunctionName
Name of the function
Definition: Optional.cs:30
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: Optional.cs:54
Base class for funcions of one variable.
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
ScriptNode Argument
Function argument.
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
virtual PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
Definition: ScriptNode.cs:169
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
Represents a variable reference.
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:86
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
PatternMatchResult
Status result of a pattern matching operation.
Definition: ScriptNode.cs:17
SearchMethod
Method to traverse the expression structure
Definition: ScriptNode.cs:38