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,
15 expression.Position,
16 "TryScope",
18 BadScopeFlags.CaptureThrow
19 );
20 int setThrowInstruction = context.EmitEmpty();
21 context.Compile(expression.TryExpressions);
22 context.Emit(BadOpCode.DestroyScope, expression.Position);
23 int jumpToEnd = context.EmitEmpty();
24 int catchStart = context.InstructionCount;
25
26 if (expression.CatchExpressions.Any()) // If there are catch expressions, compile them
27 {
28 context.Emit(BadOpCode.CreateScope, expression.Position, "CatchScope", BadObject.Null);
29 context.Emit(BadOpCode.DefVar, expression.Position, expression.ErrorName, true);
30 context.Emit(BadOpCode.Swap, expression.Position);
31 context.Emit(BadOpCode.Assign, expression.Position);
32 context.Compile(expression.CatchExpressions);
33 context.Emit(BadOpCode.DestroyScope, expression.Position);
34 }
35 else
36 {
37 // If there are no catch expressions, we need to clean up the exception from the stack
38 context.Emit(BadOpCode.Pop, expression.Position);
39 }
40
41 context.ResolveEmpty(setThrowInstruction, BadOpCode.SetThrowPointer, expression.Position, catchStart - 1);
42
43 context.ResolveEmpty(jumpToEnd,
44 BadOpCode.JumpRelative,
45 expression.Position,
46 context.InstructionCount - catchStart
47 );
48
49 if (expression.FinallyExpressions.Any()) // If there are finally expressions, compile them
50 {
51 context.Emit(BadOpCode.CreateScope, expression.Position, "FinallyScope", BadObject.Null);
52 context.Compile(expression.FinallyExpressions);
53 context.Emit(BadOpCode.DestroyScope, expression.Position);
54 }
55 }
56}
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)