BadScript 2
Loading...
Searching...
No Matches
BadCompiler.cs
Go to the documentation of this file.
37
42
46public class BadCompiler
47{
51 public static readonly BadCompiler Instance = new BadCompiler();
52
56 public readonly bool AllowEval;
57
61 private readonly Dictionary<Type, IBadExpressionCompiler> m_Compilers = new Dictionary<Type, IBadExpressionCompiler>
62 {
63 {
65 },
66 {
68 },
69 {
71 },
72 {
74 },
75 {
77 },
78 {
80 },
81 {
83 },
84 {
86 },
87 {
89 },
90 {
92 },
93 {
95 },
96 {
98 },
99 {
101 },
102 {
104 },
105 {
107 },
108 {
110 },
111 {
113 },
114 {
116 },
117 {
119 },
120 {
122 },
123 {
125 },
126 {
128 },
129 {
131 },
132 {
134 },
135 {
137 },
138 {
140 },
141 {
143 },
144 {
146 },
147 {
149 },
150 {
152 },
153 {
155 },
156 {
158 },
159 {
161 },
162 {
164 },
165 {
167 },
168 {
170 },
171 {
173 },
174 {
176 },
177 {
179 },
180 {
182 },
183 {
185 },
186 {
188 },
189 {
191 },
192 {
194 },
195 {
197 },
198 {
200 },
201 {
203 },
204 {
206 },
207 {
209 },
210 {
212 },
213 {
215 },
216 {
218 },
219 {
221 },
222 {
224 },
225 {
227 },
228 {
230 },
231 {
233 },
234 {
236 },
237 {
239 },
240 {
242 },
243 {
245 },
246 {
248 },
249 {
251 },
252 {
254 },
255 {
257 },
258 {
260 },
261 {
263 },
264 {
266 },
267 {
269 },
270 {
272 },
273 {
275 },
276 {
278 },
279 {
281 },
282 {
284 },
285 {
287 },
288 };
289
294 public BadCompiler(bool allowEval = false)
295 {
296 AllowEval = allowEval;
297 }
298
308 public void Compile(BadExpressionCompileContext context, BadExpression expression)
309 {
310 Type t = expression.GetType();
311
312 if (m_Compilers.TryGetValue(t, out IBadExpressionCompiler compiler))
313 {
314 compiler.Compile(context, expression);
315 }
316 else if (!AllowEval)
317 {
318 throw new BadCompilerException("No Compiler for Expression Type " + expression.GetType().Name);
319 }
320 else
321 {
322 context.Emit(BadOpCode.Eval, expression.Position, expression);
323 }
324 }
325
332 public void Compile(BadExpressionCompileContext context, IEnumerable<BadExpression> expressions, bool clearStack = true)
333 {
334 foreach (BadExpression expression in expressions)
335 {
336 BadSourcePosition? position = null;
337
338 Compile(context, expression);
339
340 if (clearStack && position != null)
341 {
342 context.Emit(BadOpCode.ClearStack, position);
343 }
344 }
345 }
346
352 public static IEnumerable<BadInstruction> Compile(string src)
353 {
354 BadSourceParser parser = BadSourceParser.Create("<nofile>", src);
355
356 return Compile(parser.Parse());
357 }
358
359 public static IEnumerable<BadInstruction> Compile(IEnumerable<BadExpression> expressions)
360 {
362 Instance.Compile(ctx, expressions);
363 return ctx.GetInstructions();
364 }
365
366}
Describes a specific position inside a source file.
The Parser of the Language. It turns Source Code into an Expression Tree.
static BadSourceParser Create(string fileName, string source)
Creates a BadSourceParser Instance based on the source and filename provided.
static IEnumerable< BadExpression > Parse(string fileName, string source)
Parses a BadExpression from the Source Reader.
Implements the Array Access to set or get properties from an object. LEFT[RIGHT].
Implements the Reverse Array Access to set or get properties from an object. LEFT[^RIGHT].
Implements the Member Access to set or get properties from an object. LEFT.RIGHT.
Implements the Null Coalescing Assign Expression LEFT ??= RIGHT.
Implements the Null Coalescing Expression LEFT ?? RIGHT.
Implements the Ternary Expression LEFT ? TRUE_RET : FALSE_RET.
Implements an expression that Deletes an object pointed to by BadObjectReference.
Base Implementation for all Expressions used inside the Script.
BadSourcePosition Position
The source Position of the Expression.
Implements the Assign Expression LEFT = RIGHT.
Implements the binary ... operator. This operator is used to unpack the right side into the left side...
Implements the 'in' operator. The 'in' operator is used to check if a key is present in an instance o...
Implements the Range Expression START..END.
Implements the unary ... operator. This operator is used to unpack a table into the current execution...
Implements the Post Decrement Expression.
Implements the Post Increment Expression.
Implements the If Statement Expression.
Implements the Switch Statement Expression.
Implements the Try Catch Statement Expression.
Implements the Break Expression that is used to prematurely exit a loop.
Implements the Break Expression that is used to skip a loop iteraion.
Implements the Return expression that is used to exit the current function with an Optional Return Va...
Implements the Throw Expression that is used to raise errors inside the Script.
A Import Expression that is used to import a module from a specified path.
Gets thrown when a Compiler is not able to compile a specific BadExpression.
Implements the Compile for the BadVirtualMachine.
static IEnumerable< BadInstruction > Compile(string src)
Compiles the given source into a set of BadInstructions.
readonly Dictionary< Type, IBadExpressionCompiler > m_Compilers
The Dictionary of Compilers for the different BadExpression types.
void Compile(BadExpressionCompileContext context, BadExpression expression)
Compiles the given BadExpression into a set of BadInstructions.
static readonly BadCompiler Instance
The Default Compiler Instance.
static IEnumerable< BadInstruction > Compile(IEnumerable< BadExpression > expressions)
BadCompiler(bool allowEval=false)
Creates a new BadCompiler instance.
readonly bool AllowEval
Indicates whether or not the Compiler should allow Eval Instructions.
void Compile(BadExpressionCompileContext context, IEnumerable< BadExpression > expressions, bool clearStack=true)
Compiles the given BadExpressions into a set of BadInstructions.
Contains Shared Data Structures and Functionality.
Contains the Access Expressions for the BadScript2 Language.
Contains the Comparison Expressions for the BadScript2 Language.
Contains the Self-Assigning Logic Expressions for the BadScript2 Language.
Contains the Logic Expressions for the BadScript2 Language.
Contains the Self-Assigning Math Expressions for the BadScript2 Language.
Contains the Atomic Math Expressions for the BadScript2 Language.
Contains the Math Expressions for the BadScript2 Language.
Contains the Binary Expressions for the BadScript2 Language.
Contains the Locking Expressions for the BadScript2 Language.
Contains the Loop Expressions for the BadScript2 Language.
Contains the Block Expressions for the BadScript2 Language.
Contains the Constant Expressions for the BadScript2 Language.
Contains the Controlflow Expressions for the BadScript2 Language.
Contains the Function Expressions for the BadScript2 Language.
Contains the Type Expressions for the BadScript2 Language.
Contains the Variable Expressions for the BadScript2 Language.
Contains the Expressions for the BadScript2 Language.
Contains the Parser for the BadScript2 Language.
Contains the Compiler for the BadVirtualMachine.
BadOpCode
Defines the Operations that the BadVirtualMachine can execute.
Definition BadOpCode.cs:7