BadScript 2
Loading...
Searching...
No Matches
BadLinqGenericExtensions.cs
Go to the documentation of this file.
1using System.Collections;
2
8
10
14public static class BadLinqGenericExtensions
15{
26 private static TOut InnerSelect<T, TOut>(string varName, BadExpression query, T o)
27 {
29
30 if (o is BadObject bo)
31 {
32 ctx.Scope.DefineVariable(varName, bo);
33 }
34 else
35 {
36 ctx.Scope.DefineVariable(varName, BadObject.CanWrap(o) ? BadObject.Wrap(o) : new BadReflectedObject(o!));
37 }
38
40
41 foreach (BadObject o1 in query.Execute(ctx))
42 {
43 r = o1;
44 }
45
46 r = r.Dereference();
47
48 if (r is TOut t)
49 {
50 return t;
51 }
52
53 if (r.CanUnwrap())
54 {
55 return r.Unwrap<TOut>();
56 }
57
58 if (r is BadReflectedObject ro && ro.Type.IsAssignableFrom(typeof(TOut)))
59 {
60 return (TOut)ro.Instance;
61 }
62
63 throw new Exception(
64 $"Error in LINQ Where: {varName} => {query} : {r.ToSafeString()} is not a {typeof(TOut).Name}"
65 );
66 }
67
68
80 public static T FirstOrDefault<T>(this IEnumerable<T> enumerable, string predicate)
81 {
82 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
83 BadExpression query = BadLinqCommon.Parse(queryStr).First();
84
85 return enumerable.FirstOrDefault(o => BadLinqCommon.InnerWhere(varName, query, o));
86 }
87
99 public static T First<T>(this IEnumerable<T> enumerable, string predicate)
100 {
101 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
102 BadExpression query = BadLinqCommon.Parse(queryStr).First();
103
104 return enumerable.First(o => BadLinqCommon.InnerWhere(varName, query, o));
105 }
106
118 public static T LastOrDefault<T>(this IEnumerable<T> enumerable, string predicate)
119 {
120 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
121 BadExpression query = BadLinqCommon.Parse(queryStr).First();
122
123 return enumerable.LastOrDefault(o => BadLinqCommon.InnerWhere(varName, query, o));
124 }
125
137 public static T Last<T>(this IEnumerable<T> enumerable, string predicate)
138 {
139 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
140 BadExpression query = BadLinqCommon.Parse(queryStr).First();
141
142 return enumerable.Last(o => BadLinqCommon.InnerWhere(varName, query, o));
143 }
144
153 public static IEnumerable<TOut> Select<T, TOut>(this IEnumerable<T> enumerable, string predicate)
154 {
155 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
156 BadExpression query = BadLinqCommon.Parse(queryStr).First();
157
158 return enumerable.Select(o => InnerSelect<T, TOut>(varName, query, o));
159 }
160
170 public static IEnumerable<TOut> SelectMany<T, TOut>(this IEnumerable<T> enumerable, string predicate)
171 {
172 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
173 BadExpression query = BadLinqCommon.Parse(queryStr).First();
174
175 foreach (T o in enumerable)
176 {
177 IEnumerable e = InnerSelect<T, IEnumerable>(varName, query, o);
178
179 foreach (object o1 in e)
180 {
181 if (o1 is not TOut to)
182 {
183 throw new Exception(
184 $"Error in LINQ SelectMany: {varName} => {query} : {o1} is not a {typeof(TOut).Name}"
185 );
186 }
187
188 yield return to;
189 }
190 }
191 }
192
200 public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> enumerable, string predicate)
201 {
202 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
203 BadExpression query = BadLinqCommon.Parse(queryStr).First();
204
205 return enumerable.OrderBy(o => InnerSelect<T, IComparable>(varName, query, o));
206 }
207
215 public static IEnumerable<T> OrderByDescending<T>(this IEnumerable<T> enumerable, string predicate)
216 {
217 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
218 BadExpression query = BadLinqCommon.Parse(queryStr).First();
219
220 return enumerable.OrderByDescending(o => InnerSelect<T, IComparable>(varName, query, o));
221 }
222
230 public static IEnumerable<T> SkipWhile<T>(this IEnumerable<T> enumerable, string predicate)
231 {
232 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
233 BadExpression query = BadLinqCommon.Parse(queryStr).First();
234
235 return enumerable.SkipWhile(o => BadLinqCommon.InnerWhere(varName, query, o));
236 }
237
245 public static IEnumerable<T> TakeWhile<T>(this IEnumerable<T> enumerable, string predicate)
246 {
247 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
248 BadExpression query = BadLinqCommon.Parse(queryStr).First();
249
250 return enumerable.TakeWhile(o => BadLinqCommon.InnerWhere(varName, query, o));
251 }
252
260 public static bool All<T>(this IEnumerable<T> enumerable, string predicate)
261 {
262 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
263 BadExpression query = BadLinqCommon.Parse(queryStr).First();
264
265 return enumerable.All(o => BadLinqCommon.InnerWhere(varName, query, o));
266 }
267
275 public static bool Any<T>(this IEnumerable<T> enumerable, string predicate)
276 {
277 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
278 BadExpression query = BadLinqCommon.Parse(queryStr).First();
279
280 return enumerable.Any(o => BadLinqCommon.InnerWhere(varName, query, o));
281 }
282
290 public static IEnumerable<T> Where<T>(this IEnumerable<T> enumerable, string predicate)
291 {
292 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
293 BadExpression query = BadLinqCommon.Parse(queryStr).First();
294
295 return enumerable.Where(o => BadLinqCommon.InnerWhere(varName, query, o));
296 }
297}
Base Implementation for all Expressions used inside the Script.
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
BadExecutionContext Build()
Builds a new BadExecutionContext with the options provided in this Options Instance.
void DefineVariable(string name, BadObject value, BadScope? caller=null, BadPropertyInfo? info=null, BadObject[]? attributes=null)
Defines a new Variable in the current scope.
Definition BadScope.cs:929
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
static bool CanWrap(object? o)
Returns true if the given object cam be wrapped.
Definition BadObject.cs:51
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Implements Common Functionality to parse Linq Queries in BadScript2.
static string string query ParsePredicate(string query)
static IEnumerable< BadExpression > Parse(string src)
Parses the given source into an IEnumerable of BadExpressions.
static bool InnerWhere(string varName, BadExpression query, object? o)
The Where Lamda Function for the Linq Extensions.
static readonly BadExecutionContextOptions PredicateContextOptions
The Predicate Context Options for the Linq Extensions.
static IEnumerable< TOut > SelectMany< T, TOut >(this IEnumerable< T > enumerable, string predicate)
Selects a property from the given enumerables elements and flattens the result.
static TOut InnerSelect< T, TOut >(string varName, BadExpression query, T o)
Select Lambda Function.
static bool Any< T >(this IEnumerable< T > enumerable, string predicate)
Returns true if any element in the enumerable matches the given predicate.
static IEnumerable< T > OrderByDescending< T >(this IEnumerable< T > enumerable, string predicate)
Orders the given enumerable by the given predicate in descending order.
static T Last< T >(this IEnumerable< T > enumerable, string predicate)
Returns the last element in the enumerable that matches the given predicate or throws an exception if...
static T First< T >(this IEnumerable< T > enumerable, string predicate)
Returns the first element in the enumerable that matches the given predicate or throws an exception i...
static IEnumerable< T > OrderBy< T >(this IEnumerable< T > enumerable, string predicate)
Orders the given enumerable by the given predicate.
static IEnumerable< TOut > Select< T, TOut >(this IEnumerable< T > enumerable, string predicate)
Selects a property from the given enumerables elements.
static IEnumerable< T > TakeWhile< T >(this IEnumerable< T > enumerable, string predicate)
Takes the elements while the given predicate is true.
static T FirstOrDefault< T >(this IEnumerable< T > enumerable, string predicate)
Returns the first element in the enumerable that matches the given predicate or the default value if ...
static IEnumerable< T > SkipWhile< T >(this IEnumerable< T > enumerable, string predicate)
Skips the elements while the given predicate is true.
static T LastOrDefault< T >(this IEnumerable< T > enumerable, string predicate)
Returns the last element in the enumerable that matches the given predicate or the default value if n...
static IEnumerable< T > Where< T >(this IEnumerable< T > enumerable, string predicate)
Filters the given enumerable by the given predicate.
static bool All< T >(this IEnumerable< T > enumerable, string predicate)
Returns true if all elements in the enumerable match the given predicate.
Contains the Expressions for the BadScript2 Language.
Contains the Classes for Reflection Objects.
Contains the Interop Abstractions and Implementations for the BadScript2 Language.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.
Contains the Linq Implementation and Extensions.