BadScript 2
Loading...
Searching...
No Matches
BadIfExpressionCompiler.cs
Go to the documentation of this file.
4
6
10public class BadIfExpressionCompiler : BadExpressionCompiler<BadIfExpression>
11{
13 public override void Compile(BadExpressionCompileContext context, BadIfExpression expression)
14 {
15 List<int> endJumps = new List<int>();
16 foreach (KeyValuePair<BadExpression, BadExpression[]> branch in expression.ConditionalBranches)
17 {
18 context.Compile(branch.Key);
19 int endJump = context.EmitEmpty();
20 context.Emit(BadOpCode.CreateScope, expression.Position, "IfScope", BadObject.Null);
21 context.Compile(branch.Value);
22 context.Emit(BadOpCode.DestroyScope, expression.Position);
23 context.ResolveEmpty(endJump, BadOpCode.JumpRelativeIfFalse, expression.Position, context.InstructionCount - endJump);
24 endJumps.Add(context.EmitEmpty()); //Jump to the end of the if statement if the condition is true and the branch has been taken
25 }
26
27 if (expression.ElseBranch != null)
28 {
29 context.Emit(BadOpCode.CreateScope, expression.Position, "IfScope", BadObject.Null);
30 context.Compile(expression.ElseBranch);
31 context.Emit(BadOpCode.DestroyScope, expression.Position);
32 }
33
34 foreach (int endJump in endJumps)
35 {
36 int rel = context.InstructionCount - (endJump + 1);
37 if (rel != 0)
38 {
39 context.ResolveEmpty(endJump, BadOpCode.JumpRelative, expression.Position, rel);
40 }
41 else
42 {
43 context.ResolveEmpty(endJump, BadOpCode.Nop, expression.Position, "OPTIMIZED_JUMP_REMOVAL");
44 }
45 }
46 }
47}
Base Implementation for all Expressions used inside the Script.
Implements the If 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, BadIfExpression 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