BadScript 2
Loading...
Searching...
No Matches
BadNativeClassHelper.cs
Go to the documentation of this file.
6
8
12public static class BadNativeClassHelper
13{
17 private static readonly Dictionary<string, Func<BadObject[], BadObject>> s_NativeConstructors =
18 new Dictionary<string, Func<BadObject[], BadObject>>
19 {
20 {
21 "string", a =>
22 {
23 if (a.Length != 1)
24 {
25 throw new BadRuntimeException("string constructor takes exactly one argument");
26 }
27
28 return BadObject.Wrap(a[0].ToString());
29 }
30 },
31 {
32 "bool", a =>
33 {
34 if (a.Length != 1)
35 {
36 throw new BadRuntimeException("boolean constructor takes exactly one argument");
37 }
38
39 if (a[0] is BadBoolean boolVal)
40 {
41 return boolVal;
42 }
43
44 throw new BadRuntimeException("boolean constructor takes a boolean");
45 }
46 },
47 {
48 "num", a =>
49 {
50 if (a.Length != 1)
51 {
52 throw new BadRuntimeException("number constructor takes exactly one argument");
53 }
54
55 if (a[0] is BadNumber num)
56 {
57 return num;
58 }
59
60 throw new BadRuntimeException("number constructor takes a number");
61 }
62 },
63 {
64 "Function", a =>
65 {
66 if (a.Length != 1)
67 {
68 throw new BadRuntimeException("function constructor takes exactly one argument");
69 }
70
71 if (a[0] is BadFunction func)
72 {
73 return func;
74 }
75
76 throw new BadRuntimeException("function constructor takes a function");
77 }
78 },
79 {
80 "Array", a =>
81 {
82 if (a.Length != 1)
83 {
84 throw new BadRuntimeException("array constructor takes exactly one argument");
85 }
86
87 if (a[0] is BadArray arr)
88 {
89 return arr;
90 }
91
92 throw new BadRuntimeException("array constructor takes an array");
93 }
94 },
95 {
96 "Table", a =>
97 {
98 if (a.Length != 1)
99 {
100 throw new BadRuntimeException("table constructor takes exactly one argument");
101 }
102
103 if (a[0] is BadTable table)
104 {
105 return table;
106 }
107
108 throw new BadRuntimeException("table constructor takes a table");
109 }
110 },
111 };
112
120 public static IEnumerable<BadObject> ExecuteEnumerator(BadExecutionContext ctx, BadObject enumerator)
121 {
122 BadObject moveNext = enumerator.GetProperty("MoveNext").Dereference();
123 BadObject getCurrent = enumerator.GetProperty("GetCurrent").Dereference();
124 BadSourcePosition runtimePos = BadSourcePosition.Create("<runtime>", "", 0, 0);
125 BadObject? result = BadObject.False;
126 foreach (BadObject o in BadInvocationExpression.Invoke(moveNext, Array.Empty<BadObject>(), runtimePos, ctx))
127 {
128 result = o;
129 }
130
131 while (result.Dereference() == BadObject.True)
132 {
133 BadObject? obj = null;
134 foreach (BadObject o in BadInvocationExpression.Invoke(getCurrent, Array.Empty<BadObject>(), runtimePos, ctx))
135 {
136 obj = o;
137 }
138
139 if (obj == null)
140 {
141 throw new BadRuntimeException("Enumerator.GetCurrent() returned NULL");
142 }
143
144 yield return obj.Dereference();
145
146 foreach (BadObject o in BadInvocationExpression.Invoke(moveNext, Array.Empty<BadObject>(), runtimePos, ctx))
147 {
148 result = o;
149 }
150 }
151 }
152
160 public static IEnumerable<BadObject> ExecuteEnumerate(BadExecutionContext ctx, BadObject enumerable)
161 {
162 BadObject enumerator = enumerable.GetProperty("GetEnumerator").Dereference();
163 BadObject result = BadObject.Null;
164 BadSourcePosition runtimePos = BadSourcePosition.Create("<runtime>", "", 0, 0);
165 foreach (BadObject o in BadInvocationExpression.Invoke(enumerator, Array.Empty<BadObject>(), runtimePos, ctx))
166 {
167 result = o;
168 }
169
170 if (result == BadObject.Null)
171 {
172 throw BadRuntimeException.Create(ctx.Scope, "GetEnumerator() returned NULL");
173 }
174
175 return ExecuteEnumerator(ctx, result);
176 }
177
183 public static Func<BadObject[], BadObject> GetConstructor(string name)
184 {
185 return s_NativeConstructors[name];
186 }
187}
Describes a specific position inside a source file.
static BadSourcePosition Create(string fileName, string source, int index, int length)
Creates a new Source Position.
static IEnumerable< BadObject > Invoke(BadObject left, IEnumerable< BadObject > args, BadSourcePosition position, BadExecutionContext context)
Invokes a function.
The Execution Context. Every execution of a script needs a context the script is running in....
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
Implements a Dynamic List/Array for the BadScript Language.
Definition BadArray.cs:17
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
static readonly BadObject True
The True Value for the BadScript Language.
Definition BadObject.cs:33
static readonly BadObject False
The False Value for the BadScript Language.
Definition BadObject.cs:38
virtual BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.
Definition BadObject.cs:129
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Implements a Table Structure for the BadScript Language.
Definition BadTable.cs:14
Implements a function that can be called from the script.
Helper Class for Creating Native Class Prototypes.
static IEnumerable< BadObject > ExecuteEnumerator(BadExecutionContext ctx, BadObject enumerator)
Executes the Enumerator.
static Func< BadObject[], BadObject > GetConstructor(string name)
Returns a Constructor for the given Native Type.
static IEnumerable< BadObject > ExecuteEnumerate(BadExecutionContext ctx, BadObject enumerable)
Executes the Enumerable.
static readonly Dictionary< string, Func< BadObject[], BadObject > > s_NativeConstructors
The Constructors for the Native Types.
Contains Shared Data Structures and Functionality.
Contains the Function Expressions for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains Runtime Function Objects.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6