BadScript 2
Loading...
Searching...
No Matches
BadSwitchExpressionCompiler.cs
Go to the documentation of this file.
5
6public class BadSwitchExpressionCompiler: BadExpressionCompiler<BadSwitchExpression>
7{
8 public override void Compile(BadExpressionCompileContext context, BadSwitchExpression expression)
9 {
10 //1. Compile Value
11 context.Compile(expression.Value);
12 //2. Create a scope for the switch statement
13 int switchScopeStart = context.InstructionCount;
14 context.Emit(BadOpCode.CreateScope, expression.Position, "SwitchScope", BadObject.Null, BadScopeFlags.Breakable);
15 int setBreakInstruction = context.EmitEmpty();
16 //3. Compile the cases
17 // a. Compile the case value
18 // b. Compare the case value with the switch value
19 // c. If the case value is equal to the switch value, jump to the next case body
20 // d. If the case value is not equal to the switch value, jump to the next case
21 // e. If no case value is equal to the switch value, jump to the end of the switch statement
22 List<int> caseBodyJumps = new List<int>(); //List of jumps to the next case body
23 List<int> caseEndJumps = new List<int>(); //List of jumps to the end of the switch statement(after the default case)
24 foreach (KeyValuePair<BadExpression, BadExpression[]> @case in expression.Cases)
25 {
26 context.Emit(BadOpCode.Dup, expression.Position); //Duplicate the switch value
27 context.Compile(@case.Key); //compile the case value
28 context.Emit(BadOpCode.Equals, expression.Position);
29 if (@case.Value.Length > 0)
30 {
31 int endJump = context.EmitEmpty(); //Jump to the next case if the case value is not equal to the switch value
32
33 //We have a case body,
34 //Resolve all caseBodyJumps to the start of the case body
35 foreach (int caseBodyJump in caseBodyJumps)
36 {
37 context.ResolveEmpty(caseBodyJump, BadOpCode.JumpRelativeIfTrue, expression.Position, context.InstructionCount - caseBodyJump - 1);
38 }
39 caseBodyJumps.Clear();
40 context.Compile(@case.Value, false);
41
42 //Resolve the endJump to the end of the case body
43 context.ResolveEmpty(endJump, BadOpCode.JumpRelativeIfFalse, expression.Position, context.InstructionCount - endJump);
44 caseEndJumps.Add(context.EmitEmpty());
45 }
46 else
47 {
48 //We dont have a case body
49 //If the case value is equal to the switch value, jump to the next case body
50 caseBodyJumps.Add(context.EmitEmpty());
51 }
52 }
53 //Resolve all caseBodyJumps to the start of the default case
54 foreach (int caseBodyJump in caseBodyJumps)
55 {
56 context.ResolveEmpty(caseBodyJump, BadOpCode.JumpRelativeIfTrue, expression.Position, context.InstructionCount - caseBodyJump - 1);
57 }
58 caseBodyJumps.Clear();
59 if (expression.DefaultCase != null)
60 {
61 context.Compile(expression.DefaultCase, false);
62 }
63 //Resolve all caseEndJumps to the end of the switch statement
64 foreach (int caseEndJump in caseEndJumps)
65 {
66 context.ResolveEmpty(caseEndJump, BadOpCode.JumpRelative, expression.Position, context.InstructionCount - caseEndJump - 1);
67 }
68 context.ResolveEmpty(setBreakInstruction, BadOpCode.SetBreakPointer, expression.Position, context.InstructionCount - switchScopeStart);
69 context.Emit(BadOpCode.DestroyScope, expression.Position);
70 }
71}
Base Implementation for all Expressions used inside the Script.
BadSourcePosition Position
The source Position of the Expression.
Implements the Switch Statement Expression.
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, BadSwitchExpression expression)
Contains the Block Expressions for the BadScript2 Language.
Contains the 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.