BadScript 2
Loading...
Searching...
No Matches
BadSwitchExpressionCompiler.cs
Go to the documentation of this file.
4
6
7public class BadSwitchExpressionCompiler : BadExpressionCompiler<BadSwitchExpression>
8{
9 public override void Compile(BadExpressionCompileContext context, BadSwitchExpression expression)
10 {
11 //1. Compile Value
12 context.Compile(expression.Value);
13 //2. Create a scope for the switch statement
14 int switchScopeStart = context.InstructionCount;
15
16 context.Emit(BadOpCode.CreateScope,
17 expression.Position,
18 "SwitchScope",
20 BadScopeFlags.Breakable
21 );
22 int setBreakInstruction = context.EmitEmpty();
23 //3. Compile the cases
24 // a. Compile the case value
25 // b. Compare the case value with the switch value
26 // c. If the case value is equal to the switch value, jump to the next case body
27 // d. If the case value is not equal to the switch value, jump to the next case
28 // e. If no case value is equal to the switch value, jump to the end of the switch statement
29 List<int> caseBodyJumps = new List<int>(); //List of jumps to the next case body
30
31 List<int>
32 caseEndJumps = new List<int>(); //List of jumps to the end of the switch statement(after the default case)
33
34 foreach (KeyValuePair<BadExpression, BadExpression[]> @case in expression.Cases)
35 {
36 context.Emit(BadOpCode.Dup, expression.Position); //Duplicate the switch value
37 context.Compile(@case.Key); //compile the case value
38 context.Emit(BadOpCode.Equals, expression.Position);
39
40 if (@case.Value.Length > 0)
41 {
42 int endJump =
43 context.EmitEmpty(); //Jump to the next case if the case value is not equal to the switch value
44
45 //We have a case body,
46 //Resolve all caseBodyJumps to the start of the case body
47 foreach (int caseBodyJump in caseBodyJumps)
48 {
49 context.ResolveEmpty(caseBodyJump,
50 BadOpCode.JumpRelativeIfTrue,
51 expression.Position,
52 context.InstructionCount - caseBodyJump - 1
53 );
54 }
55
56 caseBodyJumps.Clear();
57 context.Compile(@case.Value, false);
58
59 //Resolve the endJump to the end of the case body
60 context.ResolveEmpty(endJump,
61 BadOpCode.JumpRelativeIfFalse,
62 expression.Position,
63 context.InstructionCount - endJump
64 );
65 caseEndJumps.Add(context.EmitEmpty());
66 }
67 else
68 {
69 //We dont have a case body
70 //If the case value is equal to the switch value, jump to the next case body
71 caseBodyJumps.Add(context.EmitEmpty());
72 }
73 }
74
75 //Resolve all caseBodyJumps to the start of the default case
76 foreach (int caseBodyJump in caseBodyJumps)
77 {
78 context.ResolveEmpty(caseBodyJump,
79 BadOpCode.JumpRelativeIfTrue,
80 expression.Position,
81 context.InstructionCount - caseBodyJump - 1
82 );
83 }
84
85 caseBodyJumps.Clear();
86
87 if (expression.DefaultCase != null)
88 {
89 context.Compile(expression.DefaultCase, false);
90 }
91
92 //Resolve all caseEndJumps to the end of the switch statement
93 foreach (int caseEndJump in caseEndJumps)
94 {
95 context.ResolveEmpty(caseEndJump,
96 BadOpCode.JumpRelative,
97 expression.Position,
98 context.InstructionCount - caseEndJump - 1
99 );
100 }
101
102 context.ResolveEmpty(setBreakInstruction,
103 BadOpCode.SetBreakPointer,
104 expression.Position,
105 context.InstructionCount - switchScopeStart
106 );
107 context.Emit(BadOpCode.DestroyScope, expression.Position);
108 }
109}
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.