2using System.Collections.Generic;
4using System.Threading.Tasks;
19 private readonly
ISet superSet;
21 private readonly
In[] setConditions;
22 private readonly
Variables variables =
null;
24 private readonly
bool doubleColon;
38 this.pattern = Pattern;
39 this.superSet = SuperSet;
40 this.doubleColon = DoubleColon;
41 this.setConditions = SetConditions;
42 this.otherConditions = OtherConditions;
55 if (!(this.finiteSet is
null))
59 if (!(this.superSet is
null) && !this.superSet.Contains(
Element))
62 Dictionary<string, IElement>
Variables =
new Dictionary<string, IElement>();
67 this.variables.Push();
71 foreach (KeyValuePair<string, IElement> P
in Variables)
72 this.variables[P.Key] = P.Value;
74 return SatisfiesConditions(this.setConditions, this.otherConditions, this.variables);
93 if (!(SetConditions is
null))
95 foreach (
ScriptNode Condition
in SetConditions)
97 if (!SatisfiesCondition(Condition,
Variables))
102 if (!(OtherConditions is
null))
104 foreach (
ScriptNode Condition
in OtherConditions)
106 if (!SatisfiesCondition(Condition,
Variables))
116 if (!(SetConditions is
null))
118 foreach (
ScriptNode Condition
in SetConditions)
120 if (!await SatisfiesConditionAsync(Condition,
Variables))
125 if (!(OtherConditions is
null))
127 foreach (
ScriptNode Condition
in OtherConditions)
129 if (!await SatisfiesConditionAsync(Condition,
Variables))
168 if (!(this.finiteSet is
null))
171 if (!this.CalcSubset())
185 if (!(this.finiteSet is
null))
186 return this.finiteSet.
Size;
188 if (!this.CalcSubset())
191 return this.finiteSet.
Size;
195 private bool CalcSubset()
197 IEnumerable<IElement> Elements;
199 if (!(this.superSet is
null) && this.superSet.Size.HasValue)
200 Elements =
CalculateElements(this.pattern, this.superSet.ChildElements,
this.setConditions,
this.otherConditions,
this.variables);
202 Elements =
CalculateElements(this.pattern,
null, this.setConditions, this.otherConditions, this.variables);
204 if (Elements is
null)
208 this.finiteSet =
new FiniteSet(Elements);
225 if (!(SuperSetElements is
null))
227 Dictionary<string, IElement> LocalVariables =
new Dictionary<string, IElement>();
228 LinkedList<IElement> Items =
new LinkedList<IElement>();
235 LocalVariables.Clear();
239 foreach (KeyValuePair<string, IElement> P
in LocalVariables)
244 if (!SatisfiesConditions(SetConditions, OtherConditions,
Variables))
272 else if (SuperSetElements is
null && !(SetConditions is
null))
274 int i, c = SetConditions.
Length;
275 IEnumerator<IElement>[] Enumerators =
new IEnumerator<IElement>[c];
276 string[][] AffectedVariables =
new string[c][];
278 for (i = 0; i < c; i++)
284 Enumerators[i] = Members.GetEnumerator();
285 if (!Enumerators[i].MoveNext())
292 LinkedList<IElement> Items =
new LinkedList<IElement>();
293 Dictionary<string, IElement> LocalVariables =
new Dictionary<string, IElement>();
294 IEnumerator<IElement> e;
295 bool Collision =
false;
302 LocalVariables.Clear();
305 for (i = 0; i <= j; i++)
307 if (!(AffectedVariables[i] is
null))
309 foreach (
string s
in AffectedVariables[i])
310 LocalVariables.Remove(s);
317 for (i = 0; Match && (i <= j || (Collision && i < c)); i++)
321 if (AffectedVariables[i] is
null)
323 Dictionary<string, IElement> v =
new Dictionary<string, IElement>();
325 switch (SetConditions[i].LeftOperand.
PatternMatch(e.Current, v))
328 string[] v2 =
new string[v.Count];
329 v.Keys.CopyTo(v2, 0);
330 AffectedVariables[i] = v2;
332 foreach (KeyValuePair<string, IElement> P
in v)
334 if (LocalVariables.TryGetValue(P.Key, out
IElement E))
338 if (!e.Current.Equals(E))
342 LocalVariables[P.Key] = P.Value;
357 switch (SetConditions[i].LeftOperand.
PatternMatch(e.Current, LocalVariables))
375 foreach (KeyValuePair<string, IElement> P
in LocalVariables)
378 if (SatisfiesConditions(
null, OtherConditions,
Variables))
382 for (j = 0; j < c; j++)
418 if (!(SuperSetElements is
null))
420 Dictionary<string, IElement> LocalVariables =
new Dictionary<string, IElement>();
421 LinkedList<IElement> Items =
new LinkedList<IElement>();
428 LocalVariables.Clear();
432 foreach (KeyValuePair<string, IElement> P
in LocalVariables)
437 if (!await SatisfiesConditionsAsync(SetConditions, OtherConditions,
Variables))
465 else if (SuperSetElements is
null && !(SetConditions is
null))
467 int i, c = SetConditions.
Length;
468 IEnumerator<IElement>[] Enumerators =
new IEnumerator<IElement>[c];
469 string[][] AffectedVariables =
new string[c][];
471 for (i = 0; i < c; i++)
477 Enumerators[i] = Members.GetEnumerator();
478 if (!Enumerators[i].MoveNext())
485 LinkedList<IElement> Items =
new LinkedList<IElement>();
486 Dictionary<string, IElement> LocalVariables =
new Dictionary<string, IElement>();
487 IEnumerator<IElement> e;
488 bool Collision =
false;
495 LocalVariables.Clear();
498 for (i = 0; i <= j; i++)
500 if (!(AffectedVariables[i] is
null))
502 foreach (
string s
in AffectedVariables[i])
503 LocalVariables.Remove(s);
510 for (i = 0; Match && (i <= j || (Collision && i < c)); i++)
514 if (AffectedVariables[i] is
null)
516 Dictionary<string, IElement> v =
new Dictionary<string, IElement>();
518 switch (SetConditions[i].LeftOperand.
PatternMatch(e.Current, v))
521 string[] v2 =
new string[v.Count];
522 v.Keys.CopyTo(v2, 0);
523 AffectedVariables[i] = v2;
525 foreach (KeyValuePair<string, IElement> P
in v)
527 if (LocalVariables.TryGetValue(P.Key, out
IElement E))
531 if (!e.Current.Equals(E))
535 LocalVariables[P.Key] = P.Value;
550 switch (SetConditions[i].LeftOperand.
PatternMatch(e.Current, LocalVariables))
568 foreach (KeyValuePair<string, IElement> P
in LocalVariables)
571 if (await SatisfiesConditionsAsync(
null, OtherConditions,
Variables))
575 for (j = 0; j < c; j++)
615 return Vector.VectorElements;
618 if (Obj is
ISet Set2)
620 if (!Set2.Size.HasValue)
623 return Set2.ChildElements;
626 if (Obj is IEnumerable<IElement> Elements)
629 if (Obj is IEnumerable<object> Objects)
631 LinkedList<IElement> List =
new LinkedList<IElement>();
633 foreach (
object x
in Objects)
648 if (this.superSet is
null ^ S.superSet is
null)
651 if (!(this.superSet is
null) && !this.superSet.Equals(S.superSet))
655 this.pattern.Equals(S.pattern) &&
656 this.doubleColon.Equals(S.doubleColon) &&
664 int Result = this.pattern.GetHashCode();
666 if (!(this.superSet is
null))
667 Result ^= Result << 5 ^ this.superSet.GetHashCode();
671 Result ^= Result << 5 ^ this.doubleColon.GetHashCode();
679 if (!(this.finiteSet is
null))
682 StringBuilder sb =
new StringBuilder();
685 sb.Append(this.pattern.SubExpression);
687 if (!(this.superSet is
null))
690 sb.Append(this.superSet.ToString());
695 if (this.doubleColon)
700 if (!(this.setConditions is
null))
702 foreach (
ScriptNode Condition
in this.setConditions)
713 if (!(this.otherConditions is
null))
715 foreach (
ScriptNode Condition
in this.otherConditions)
728 return sb.ToString();
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 IElement Encapsulate(object Value)
Encapsulates an object.
static bool TryConvert(object Value, Type DesiredType, out object Result)
Tries to convert an object Value to an object of type DesiredType .
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.