BadScript 2
Loading...
Searching...
No Matches
BadArrayExtension.cs
Go to the documentation of this file.
11
16
21{
23 protected override void AddExtensions(BadInteropExtensionProvider provider)
24 {
25 provider.RegisterObject<BadArray>(
26 "Add",
28 "Add",
29 (_, o) => Add(a, o),
31 "elem"
32 )
33 );
34
35 provider.RegisterObject<BadArray>(
36 "Clear",
38 "Clear",
39 _ => Clear(a),
41 )
42 );
43
44 provider.RegisterObject<BadArray>(
45 "AddRange",
47 "AddRange",
48 (ctx, elems) =>
49 {
50 if (elems is BadArray arr)
51 {
52 a.InnerArray.AddRange(arr.InnerArray);
53 }
54 else if (BadNativeClassBuilder.Enumerable.IsSuperClassOf(elems.GetPrototype()))
55 {
56 // Get the Enumerator
57 BadObject[] enumerated = BadNativeClassHelper.ExecuteEnumerate(ctx, elems).ToArray();
58 a.InnerArray.AddRange(enumerated);
59 }
60 else
61 {
62 throw BadRuntimeException.Create(ctx.Scope, "Argument is not of type IEnumerable");
63 }
64
65 return BadObject.Null;
66 },
68 "elems"
69 )
70 );
71
72 provider.RegisterObject<BadArray>(
73 "Insert",
75 "Insert",
76 (_, i, o) => Insert(a, i, o),
78 "elem"
79 )
80 );
81
82 provider.RegisterObject<BadArray>(
83 "InsertRange",
85 "InsertRange",
86 (ctx, num, elems) =>
87 {
88 if (elems is BadArray arr)
89 {
90 a.InnerArray.InsertRange((int)num, arr.InnerArray);
91 }
92 else if (BadNativeClassBuilder.Enumerable.IsSuperClassOf(elems.GetPrototype()))
93 {
94 // Get the Enumerator
95 BadObject[] enumerated = BadNativeClassHelper.ExecuteEnumerate(ctx, elems).ToArray();
96 a.InnerArray.InsertRange((int)num, enumerated);
97 }
98 else
99 {
100 throw BadRuntimeException.Create(ctx.Scope, "Argument is not of type IEnumerable");
101 }
102
103 return BadObject.Null;
104 },
106 "index",
107 "elems"
108 )
109 );
110
111 provider.RegisterObject<BadArray>(
112 "Remove",
114 "Remove",
115 (_, o) => Remove(a, o),
117 "elem"
118 )
119 );
120
121 provider.RegisterObject<BadArray>(
122 "Contains",
124 "Contains",
125 (_, o) => Contains(a, o),
127 "elem"
128 )
129 );
130
131 provider.RegisterObject<BadArray>(
132 "RemoveAt",
134 "RemoveAt",
135 (_, o) => RemoveAt(a, o),
137 "index"
138 )
139 );
140
141 provider.RegisterObject<BadArray>(
142 "Get",
144 "Get",
145 (_, o) => Get(a, o),
147 "index"
148 )
149 );
150
151 provider.RegisterObject<BadArray>(
152 "Set",
154 "Set",
155 (_, i, v) => Set(a, i, v),
157 "index"
158 )
159 );
160
161 provider.RegisterObject<BadArray>(
162 "GetEnumerator",
164 "GetEnumerator",
165 _ => GetEnumerator(a),
167 )
168 );
169
170 provider.RegisterObject<BadArray>(
174 (c, i) => ArrayAccess(c, a, i),
176 "index"
177 )
178 );
179 provider.RegisterObject<BadArray>(
183 (_, i) => BadObjectReference.Make(
184 $"{a}[^{i}]",
185 () => Get(a, a.InnerArray.Count - i),
186 (v, _) => Set(a, a.InnerArray.Count - i, v)
187 ),
189 "index"
190 )
191 );
192
193 provider.RegisterObject<BadArray>("Length", a => BadObject.Wrap((decimal)a.InnerArray.Count));
194 }
195
196 private static BadObject ArrayAccess(BadExecutionContext context, BadArray array, BadObject enumerator)
197 {
198 BadSourcePosition position = BadSourcePosition.FromSource("ArrayAccess", 0, 1);
199 if (enumerator is IBadNumber num)
200 {
201 return BadObjectReference.Make($"{array}[{num.Value}]", () => Get(array, num.Value), (v, _) => Set(array, num.Value, v));
202 }
203
204 BadArray result = new BadArray();
205
206 if (enumerator.HasProperty("GetEnumerator", context.Scope) && enumerator.GetProperty("GetEnumerator", context.Scope).Dereference() is BadFunction getEnumerator)
207 {
208 foreach (BadObject e in getEnumerator.Invoke(Array.Empty<BadObject>(), context))
209 {
210 enumerator = e;
211 }
212
213 enumerator = enumerator.Dereference();
214 }
215 (BadFunction moveNext, BadFunction getCurrent) = BadForEachExpression.FindEnumerator(enumerator, context, position);
216
217
218 BadObject cond = BadObject.Null;
219
220 foreach (BadObject o in moveNext.Invoke(Array.Empty<BadObject>(), context))
221 {
222 cond = o;
223 }
224
225 IBadBoolean bRet = cond.Dereference() as IBadBoolean ??
226 throw new BadRuntimeException("While Condition is not a boolean", position);
227
228 while (bRet.Value)
229 {
230 using BadExecutionContext loopContext = new BadExecutionContext(
231 context.Scope.CreateChild(
232 "ForEachLoop",
233 context.Scope,
234 null,
235 BadScopeFlags.Breakable | BadScopeFlags.Continuable
236 )
237 );
238
239 BadObject current = BadObject.Null;
240
241 foreach (BadObject o in getCurrent.Invoke(Array.Empty<BadObject>(), loopContext))
242 {
243 current = o;
244 }
245
246
247
248 if (current is not IBadNumber n)
249 {
250 throw BadRuntimeException.Create(context.Scope, "Enumerator Current did not return a number", position);
251 }
252
253 result.InnerArray.Add(Get(array, n.Value));
254
255 foreach (BadObject o in moveNext.Invoke(Array.Empty<BadObject>(), loopContext))
256 {
257 cond = o;
258 }
259
260
261
262 bRet = cond.Dereference() as IBadBoolean ??
264 context.Scope,
265 "Enumerator MoveNext did not return a boolean",
266 position
267 );
268 }
269
270 return result;
271 }
272
278 private static BadObject GetEnumerator(BadArray array)
279 {
280 return new BadInteropEnumerator(array.InnerArray.GetEnumerator());
281 }
282
288 private static BadObject Clear(BadArray arg)
289 {
290 arg.InnerArray.Clear();
291
292 return BadObject.Null;
293 }
294
301 private static BadObject Add(BadArray arg, BadObject obj)
302 {
303 arg.InnerArray.Add(obj);
304
305 return BadObject.Null;
306 }
307
315 private static BadObject Insert(BadArray arg, decimal index, BadObject obj)
316 {
317 arg.InnerArray.Insert((int)index, obj);
318
319 return BadObject.Null;
320 }
321
328 private static BadObject Contains(BadArray arg, BadObject obj)
329 {
330 return arg.InnerArray.Contains(obj);
331 }
332
339 private static BadObject Remove(BadArray arg, BadObject obj)
340 {
341 return arg.InnerArray.Remove(obj);
342 }
343
350 private static BadObject RemoveAt(BadArray arg, decimal obj)
351 {
352 int index = (int)obj;
353 arg.InnerArray.RemoveAt(index);
354
355 return BadObject.Null;
356 }
357
364 private static BadObject Get(BadArray arg, decimal obj)
365 {
366 int index = (int)obj;
367
368 return arg.InnerArray[index];
369 }
370
378 private static BadObject Set(BadArray arg, decimal obj, BadObject value)
379 {
380 int index = (int)obj;
381 arg.InnerArray[index] = value;
382
383 return BadObject.Null;
384 }
385}
Describes a specific position inside a source file.
static BadSourcePosition FromSource(string source, int index, int length)
Creates a new Source Position.
Contains Static Data for the BadScript Language.
const string ARRAY_ACCESS_REVERSE_OPERATOR_NAME
static BadObject ArrayAccess(BadExecutionContext context, BadArray array, BadObject enumerator)
static BadObject Get(BadArray arg, decimal obj)
Returns a value from the array.
static BadObject Remove(BadArray arg, BadObject obj)
Removes an element from the array.
static BadObject Add(BadArray arg, BadObject obj)
Adds an element to the array.
static BadObject Set(BadArray arg, decimal obj, BadObject value)
Sets a value in the array.
override void AddExtensions(BadInteropExtensionProvider provider)
static BadObject RemoveAt(BadArray arg, decimal obj)
Removes an element at the given index.
static BadObject Contains(BadArray arg, BadObject obj)
Returns True if the array contains the given element.
static BadObject GetEnumerator(BadArray array)
Returns an enumerator of the array.
static BadObject Clear(BadArray arg)
Clears the array.
static BadObject Insert(BadArray arg, decimal index, BadObject obj)
Inserts an element at the given index.
static BadFunction BadFunction getCurrent FindEnumerator(BadObject target, BadExecutionContext context, BadSourcePosition position)
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
Implements a simple wrapper for C# IEnumerators to be used in BS2.
Public Extension API for the BS2 Runtime.
void RegisterObject(Type t, string propName, BadObject obj)
Registers the specified extension for the specified type.
Implements a Dynamic List/Array for the BadScript Language.
Definition BadArray.cs:17
List< BadObject > InnerArray
The Inner Array.
Definition BadArray.cs:45
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
virtual BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.
Definition BadObject.cs:129
virtual bool HasProperty(string propName, BadScope? caller=null)
Returns true if the object contains a given property or there exists an extension for the current Ins...
Definition BadObject.cs:118
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Implements the base functionality for a BadScript Reference.
static BadObjectReference Make(string refText, Func< BadObject > getter, Action< BadObject, BadPropertyInfo?>? setter=null, Action? delete=null)
Creates a new Reference Object.
Implements a function that can be called from the script.
The Any Prototype, Base type for all types.
static readonly BadAnyPrototype Instance
The Instance of the BadAnyPrototype.
Helper Class that Builds a Native Class from a Prototype.
static readonly BadInterfacePrototype Enumerable
The IEnumerable Interface Prototype.
static BadClassPrototype GetNative(string name)
Returns a Native Class Prototype for the given Native Type.
static readonly BadInterfacePrototype Enumerator
The IEnumerator Interface Prototype.
Helper Class for Creating Native Class Prototypes.
static IEnumerable< BadObject > ExecuteEnumerate(BadExecutionContext ctx, BadObject enumerable)
Executes the Enumerable.
Implements the Interface for Native Boolean.
Definition IBadBoolean.cs:7
Implements the Interface for Native Numbers.
Definition IBadNumber.cs:7
Contains Shared Data Structures and Functionality.
Contains Common Interop Extensions for the BadScript2 Runtime.
Contains the Loop Expressions for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains the Interop Function Classes for the BadScript2 Language.
Contains the Interop Abstractions and Implementations for the BadScript2 Language.
Contains Runtime Function Objects.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.
BadScopeFlags
Defines Different Behaviours for the Current Scope.