BadScript 2
Loading...
Searching...
No Matches
BadTryCatchExpressionCompiler.cs
Go to the documentation of this file.
3
5
9public class BadTryCatchExpressionCompiler : BadExpressionCompiler<BadTryCatchExpression>
10{
12 public override void Compile(BadExpressionCompileContext context, BadTryCatchExpression expression)
13 {
14 context.Emit(BadOpCode.CreateScope, expression.Position, "TryScope", BadObject.Null, BadScopeFlags.CaptureThrow);
15 int setThrowInstruction = context.EmitEmpty();
16 context.Compile(expression.TryExpressions);
17 context.Emit(BadOpCode.DestroyScope, expression.Position);
18 int jumpToEnd = context.EmitEmpty();
19 int catchStart = context.InstructionCount;
20 if (expression.CatchExpressions.Any()) // If there are catch expressions, compile them
21 {
22 context.Emit(BadOpCode.CreateScope, expression.Position, "CatchScope", BadObject.Null);
23 context.Emit(BadOpCode.DefVar, expression.Position, expression.ErrorName, true);
24 context.Emit(BadOpCode.Swap, expression.Position);
25 context.Emit(BadOpCode.Assign, expression.Position);
26 context.Compile(expression.CatchExpressions);
27 context.Emit(BadOpCode.DestroyScope, expression.Position);
28 }
29 else
30 {
31 // If there are no catch expressions, we need to clean up the exception from the stack
32 context.Emit(BadOpCode.Pop, expression.Position);
33 }
34 context.ResolveEmpty(setThrowInstruction, BadOpCode.SetThrowPointer, expression.Position, catchStart - 1);
35 context.ResolveEmpty(jumpToEnd, BadOpCode.JumpRelative, expression.Position, context.InstructionCount - catchStart);
36 if(expression.FinallyExpressions.Any()) // If there are finally expressions, compile them
37 {
38 context.Emit(BadOpCode.CreateScope, expression.Position, "FinallyScope", BadObject.Null);
39 context.Compile(expression.FinallyExpressions);
40 context.Emit(BadOpCode.DestroyScope, expression.Position);
41 }
42
43 }
44}
BadSourcePosition Position
The source Position of the Expression.
Implements the Try Catch Statement Expression.
IEnumerable< BadExpression > CatchExpressions
The Catch Block.
IEnumerable< BadExpression > TryExpressions
The Try Block.
readonly string ErrorName
The Variable name of the Exception inside the catch block.
IEnumerable< BadExpression > FinallyExpressions
The Finally Block.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
override void Compile(BadExpressionCompileContext context, BadTryCatchExpression expression)
Contains the Block Expressions for the BadScript2 Language.
Contains the Runtime Objects.
Definition BadArray.cs:10
BadOpCode
Defines the Operations that the BadVirtualMachine can execute.
Definition BadOpCode.cs:7
BadScopeFlags
Defines Different Behaviours for the Current Scope.
void ResolveEmpty(int index, BadOpCode code, BadSourcePosition position, params object[] args)