4using System.Threading.Tasks;
20 private readonly
ISet superSet;
22 private readonly
In[] setConditions;
23 private readonly
Variables variables =
null;
25 private readonly
bool doubleColon;
39 this.pattern = Pattern;
40 this.superSet = SuperSet;
41 this.doubleColon = DoubleColon;
42 this.setConditions = SetConditions;
43 this.otherConditions = OtherConditions;
56 if (!(this.finiteSet is
null))
60 if (!(this.superSet is
null) && !this.superSet.Contains(
Element))
63 Dictionary<string, IElement>
Variables =
new Dictionary<string, IElement>();
68 this.variables.Push();
72 foreach (KeyValuePair<string, IElement> P
in Variables)
73 this.variables[P.Key] = P.Value;
75 return SatisfiesConditions(this.setConditions, this.otherConditions, this.variables);
94 if (!(SetConditions is
null))
96 foreach (
ScriptNode Condition
in SetConditions)
98 if (!SatisfiesCondition(Condition,
Variables))
103 if (!(OtherConditions is
null))
105 foreach (
ScriptNode Condition
in OtherConditions)
107 if (!SatisfiesCondition(Condition,
Variables))
117 if (!(SetConditions is
null))
119 foreach (
ScriptNode Condition
in SetConditions)
121 if (!await SatisfiesConditionAsync(Condition,
Variables))
126 if (!(OtherConditions is
null))
128 foreach (
ScriptNode Condition
in OtherConditions)
130 if (!await SatisfiesConditionAsync(Condition,
Variables))
169 if (!(this.finiteSet is
null))
172 if (!this.CalcSubset())
186 if (!(this.finiteSet is
null))
187 return this.finiteSet.
Size;
189 if (!this.CalcSubset())
192 return this.finiteSet.
Size;
196 private bool CalcSubset()
198 IEnumerable<IElement> Elements;
200 if (!(this.superSet is
null) && this.superSet.Size.HasValue)
201 Elements =
CalculateElements(this.pattern, this.superSet.ChildElements,
this.setConditions,
this.otherConditions,
this.variables);
203 Elements =
CalculateElements(this.pattern,
null, this.setConditions, this.otherConditions, this.variables);
205 if (Elements is
null)
209 this.finiteSet =
new FiniteSet(Elements);
226 if (!(SuperSetElements is
null))
228 Dictionary<string, IElement> LocalVariables =
new Dictionary<string, IElement>();
236 LocalVariables.Clear();
240 foreach (KeyValuePair<string, IElement> P
in LocalVariables)
245 if (!SatisfiesConditions(SetConditions, OtherConditions,
Variables))
273 else if (SuperSetElements is
null && !(SetConditions is
null))
275 int i, c = SetConditions.
Length;
276 IEnumerator<IElement>[] Enumerators =
new IEnumerator<IElement>[c];
277 string[][] AffectedVariables =
new string[c][];
282 for (i = 0; i < c; i++)
288 Enumerators[i] = Members.GetEnumerator();
289 if (!Enumerators[i].MoveNext())
297 Dictionary<string, IElement> LocalVariables =
new Dictionary<string, IElement>();
298 IEnumerator<IElement> e;
299 bool Collision =
false;
306 LocalVariables.Clear();
309 for (i = 0; i <= j; i++)
311 if (!(AffectedVariables[i] is
null))
313 foreach (
string s
in AffectedVariables[i])
314 LocalVariables.Remove(s);
321 for (i = 0; Match && (i <= j || (Collision && i < c)); i++)
325 if (AffectedVariables[i] is
null)
327 Dictionary<string, IElement> v =
new Dictionary<string, IElement>();
329 switch (SetConditions[i].LeftOperand.
PatternMatch(e.Current, v))
332 string[] v2 =
new string[v.Count];
333 v.Keys.CopyTo(v2, 0);
334 AffectedVariables[i] = v2;
336 foreach (KeyValuePair<string, IElement> P
in v)
338 if (LocalVariables.TryGetValue(P.Key, out
IElement E))
342 if (!e.Current.Equals(E))
346 LocalVariables[P.Key] = P.Value;
361 switch (SetConditions[i].LeftOperand.
PatternMatch(e.Current, LocalVariables))
379 foreach (KeyValuePair<string, IElement> P
in LocalVariables)
382 if (SatisfiesConditions(
null, OtherConditions,
Variables))
386 for (j = 0; j < c; j++)
405 for (i = 0; i < c; i++)
406 Enumerators[i]?.Dispose();
426 if (!(SuperSetElements is
null))
428 Dictionary<string, IElement> LocalVariables =
new Dictionary<string, IElement>();
436 LocalVariables.Clear();
440 foreach (KeyValuePair<string, IElement> P
in LocalVariables)
445 if (!await SatisfiesConditionsAsync(SetConditions, OtherConditions,
Variables))
473 else if (SuperSetElements is
null && !(SetConditions is
null))
475 int i, c = SetConditions.
Length;
476 IEnumerator<IElement>[] Enumerators =
new IEnumerator<IElement>[c];
477 string[][] AffectedVariables =
new string[c][];
482 for (i = 0; i < c; i++)
488 Enumerators[i] = Members.GetEnumerator();
489 if (!Enumerators[i].MoveNext())
497 Dictionary<string, IElement> LocalVariables =
new Dictionary<string, IElement>();
498 IEnumerator<IElement> e;
499 bool Collision =
false;
506 LocalVariables.Clear();
509 for (i = 0; i <= j; i++)
511 if (!(AffectedVariables[i] is
null))
513 foreach (
string s
in AffectedVariables[i])
514 LocalVariables.Remove(s);
521 for (i = 0; Match && (i <= j || (Collision && i < c)); i++)
525 if (AffectedVariables[i] is
null)
527 Dictionary<string, IElement> v =
new Dictionary<string, IElement>();
529 switch (SetConditions[i].LeftOperand.
PatternMatch(e.Current, v))
532 string[] v2 =
new string[v.Count];
533 v.Keys.CopyTo(v2, 0);
534 AffectedVariables[i] = v2;
536 foreach (KeyValuePair<string, IElement> P
in v)
538 if (LocalVariables.TryGetValue(P.Key, out
IElement E))
542 if (!e.Current.Equals(E))
546 LocalVariables[P.Key] = P.Value;
561 switch (SetConditions[i].LeftOperand.
PatternMatch(e.Current, LocalVariables))
579 foreach (KeyValuePair<string, IElement> P
in LocalVariables)
582 if (await SatisfiesConditionsAsync(
null, OtherConditions,
Variables))
586 for (j = 0; j < c; j++)
605 for (i = 0; i < c; i++)
606 Enumerators[i]?.Dispose();
630 return Vector.VectorElements;
633 if (Obj is
ISet Set2)
635 if (!Set2.Size.HasValue)
638 return Set2.ChildElements;
641 if (Obj is IEnumerable<IElement> Elements)
644 if (Obj is IEnumerable<object> Objects)
648 foreach (
object x
in Objects)
663 if (this.superSet is
null ^ S.superSet is
null)
666 if (!(this.superSet is
null) && !this.superSet.Equals(S.superSet))
670 this.pattern.Equals(S.pattern) &&
671 this.doubleColon.Equals(S.doubleColon) &&
679 int Result = this.pattern.GetHashCode();
681 if (!(this.superSet is
null))
682 Result ^= Result << 5 ^ this.superSet.GetHashCode();
686 Result ^= Result << 5 ^ this.doubleColon.GetHashCode();
694 if (!(this.finiteSet is
null))
697 StringBuilder sb =
new StringBuilder();
700 sb.Append(this.pattern.SubExpression);
702 if (!(this.superSet is
null))
705 sb.Append(this.superSet.ToString());
710 if (this.doubleColon)
715 if (!(this.setConditions is
null))
717 foreach (
ScriptNode Condition
in this.setConditions)
728 if (!(this.otherConditions is
null))
730 foreach (
ScriptNode Condition
in this.otherConditions)
743 return sb.ToString();
A chunked list is a linked list of chunks of objects of type T .
void Add(T Item)
Adds an item to the collection.
Base class for all types of elements.
Base class for all types of sets.
virtual ? int Size
Size of set, if finite and known, otherwise null is returned.
override ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Base class for script exceptions.
Script runtime exception.
Class managing a script expression.
static bool TryConvert(object Value, Type DesiredType, bool AcceptInformationLoss, out object Result)
Tries to convert an object Value to an object of type DesiredType .
static IElement Encapsulate(object Value)
Encapsulates an object.
Base class for all nodes in a parsed script tree.
int Length
Length of expression covered by node.
string SubExpression
Sub-expression defining the node.
virtual PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
static bool AreEqual(ScriptNode S1, ScriptNode S2)
Compares if two script nodes are equal.
override int GetHashCode()
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 ...
override bool Contains(IElement Element)
Checks if the set contains an element.
override? int Size
Size of set, if finite and known, otherwise null is returned.
override ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
override string ToString()
Represents an implicitly defined set
static async Task< IEnumerable< IElement > > CalculateElementsAsync(ScriptNode Pattern, IEnumerable< IElement > SuperSetElements, In[] SetConditions, ScriptNode[] OtherConditions, Variables Variables)
Calculates elements specified using implicit notation.
override? int Size
If the element represents a scalar value.
ImplicitSet(ScriptNode Pattern, ISet SuperSet, In[] SetConditions, ScriptNode[] OtherConditions, Variables Variables, bool DoubleColon)
Represents an implicitly defined set
static IEnumerable< IElement > GetSetMembers(IElement E)
Gets the elements of a (supposed) set.
static IEnumerable< IElement > CalculateElements(ScriptNode Pattern, IEnumerable< IElement > SuperSetElements, In[] SetConditions, ScriptNode[] OtherConditions, Variables Variables)
Calculates elements specified using implicit notation.
override ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
override bool Equals(object obj)
Compares the element to another. If elements are equal.
override int GetHashCode()
Calculates a hash code of the element. Hash code.
override string ToString()
override bool Contains(IElement Element)
Checks if the set contains an element.
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
virtual void Push()
Pushes the current set of variables to the stack. This state is restored by calling Pop....
virtual void Pop()
Pops a previously stored set of variables from the stack. Variables are stored on the stack by callin...
void CopyTo(Variables Variables)
Copies available variables to another variable collection.
Basic interface for all types of elements.
object AssociatedObjectValue
Associated object value.
Basic interface for vectors.
Basic interface for all types of sets.
PatternMatchResult
Status result of a pattern matching operation.