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(query.Position);
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
64 Exception($"Error in LINQ Where: {varName} => {query} : {r.ToSafeString()} is not a {typeof(TOut).Name}");
65 }
66
67
79 public static T FirstOrDefault<T>(this IEnumerable<T> enumerable, string predicate)
80 {
81 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
82
83 BadExpression query = BadLinqCommon.Parse(queryStr)
84 .First();
85
86 return enumerable.FirstOrDefault(o => BadLinqCommon.InnerWhere(varName, query, o));
87 }
88
100 public static T First<T>(this IEnumerable<T> enumerable, string predicate)
101 {
102 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
103
104 BadExpression query = BadLinqCommon.Parse(queryStr)
105 .First();
106
107 return enumerable.First(o => BadLinqCommon.InnerWhere(varName, query, o));
108 }
109
121 public static T LastOrDefault<T>(this IEnumerable<T> enumerable, string predicate)
122 {
123 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
124
125 BadExpression query = BadLinqCommon.Parse(queryStr)
126 .First();
127
128 return enumerable.LastOrDefault(o => BadLinqCommon.InnerWhere(varName, query, o));
129 }
130
142 public static T Last<T>(this IEnumerable<T> enumerable, string predicate)
143 {
144 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
145
146 BadExpression query = BadLinqCommon.Parse(queryStr)
147 .First();
148
149 return enumerable.Last(o => BadLinqCommon.InnerWhere(varName, query, o));
150 }
151
160 public static IEnumerable<TOut> Select<T, TOut>(this IEnumerable<T> enumerable, string predicate)
161 {
162 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
163
164 BadExpression query = BadLinqCommon.Parse(queryStr)
165 .First();
166
167 return enumerable.Select(o => InnerSelect<T, TOut>(varName, query, o));
168 }
169
179 public static IEnumerable<TOut> SelectMany<T, TOut>(this IEnumerable<T> enumerable, string predicate)
180 {
181 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
182
183 BadExpression query = BadLinqCommon.Parse(queryStr)
184 .First();
185
186 foreach (T o in enumerable)
187 {
188 IEnumerable e = InnerSelect<T, IEnumerable>(varName, query, o);
189
190 foreach (object o1 in e)
191 {
192 if (o1 is not TOut to)
193 {
194 throw new
195 Exception($"Error in LINQ SelectMany: {varName} => {query} : {o1} is not a {typeof(TOut).Name}"
196 );
197 }
198
199 yield return to;
200 }
201 }
202 }
203
211 public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> enumerable, string predicate)
212 {
213 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
214
215 BadExpression query = BadLinqCommon.Parse(queryStr)
216 .First();
217
218 return enumerable.OrderBy(o => InnerSelect<T, IComparable>(varName, query, o));
219 }
220
228 public static IEnumerable<T> OrderByDescending<T>(this IEnumerable<T> enumerable, string predicate)
229 {
230 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
231
232 BadExpression query = BadLinqCommon.Parse(queryStr)
233 .First();
234
235 return enumerable.OrderByDescending(o => InnerSelect<T, IComparable>(varName, query, o));
236 }
237
245 public static IEnumerable<T> SkipWhile<T>(this IEnumerable<T> enumerable, string predicate)
246 {
247 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
248
249 BadExpression query = BadLinqCommon.Parse(queryStr)
250 .First();
251
252 return enumerable.SkipWhile(o => BadLinqCommon.InnerWhere(varName, query, o));
253 }
254
262 public static IEnumerable<T> TakeWhile<T>(this IEnumerable<T> enumerable, string predicate)
263 {
264 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
265
266 BadExpression query = BadLinqCommon.Parse(queryStr)
267 .First();
268
269 return enumerable.TakeWhile(o => BadLinqCommon.InnerWhere(varName, query, o));
270 }
271
279 public static bool All<T>(this IEnumerable<T> enumerable, string predicate)
280 {
281 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
282
283 BadExpression query = BadLinqCommon.Parse(queryStr)
284 .First();
285
286 return enumerable.All(o => BadLinqCommon.InnerWhere(varName, query, o));
287 }
288
296 public static bool Any<T>(this IEnumerable<T> enumerable, string predicate)
297 {
298 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
299
300 BadExpression query = BadLinqCommon.Parse(queryStr)
301 .First();
302
303 return enumerable.Any(o => BadLinqCommon.InnerWhere(varName, query, o));
304 }
305
313 public static IEnumerable<T> Where<T>(this IEnumerable<T> enumerable, string predicate)
314 {
315 (string varName, string queryStr) = BadLinqCommon.ParsePredicate(predicate);
316
317 BadExpression query = BadLinqCommon.Parse(queryStr)
318 .First();
319
320 return enumerable.Where(o => BadLinqCommon.InnerWhere(varName, query, o));
321 }
322}
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:784
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.