BadScript 2
Loading...
Searching...
No Matches
BadWhileExpressionCompiler.cs
Go to the documentation of this file.
3
5
9public class BadWhileExpressionCompiler : BadExpressionCompiler<BadWhileExpression>
10{
12 public override void Compile(BadExpressionCompileContext context, BadWhileExpression expression)
13 {
14 int start = context.InstructionCount;
15 context.Compile(expression.Condition);
16 int endJump = context.EmitEmpty();
17 int loopScopeStart = context.InstructionCount;
18
19 //Break And continue pointers are relative to the "CreateScope" instruction
20 //If break instruction is encountered
21 // Jump to loopScopeStart + offset to after loop(the destroy scope instruction is omitted because the vm handles the scopes)
22 context.Emit(BadOpCode.CreateScope,
23 expression.Position,
24 "WhileScope",
26 BadScopeFlags.Breakable | BadScopeFlags.Continuable
27 );
28 int setBreakInstruction = context.EmitEmpty();
29 int setContinueInstruction = context.EmitEmpty();
30 context.Compile(expression.Body);
31 context.Emit(BadOpCode.DestroyScope, expression.Position);
32 //Jump back up to the loop condition
33 int continueJump = context.InstructionCount;
34 context.Emit(BadOpCode.JumpRelative, expression.Position, start - context.InstructionCount - 1);
35
36 //Set the end jump to the end of the loop
37 context.ResolveEmpty(endJump,
38 BadOpCode.JumpRelativeIfFalse,
39 expression.Position,
40 context.InstructionCount - endJump - 1
41 );
42
43 //address to the end of the loop(relative to the create scope instruction)
44 context.ResolveEmpty(setBreakInstruction,
45 BadOpCode.SetBreakPointer,
46 expression.Position,
47 context.InstructionCount - loopScopeStart - 1
48 );
49
50 //Address to start of the condition(relative to the start of the loop)
51 context.ResolveEmpty(setContinueInstruction,
52 BadOpCode.SetContinuePointer,
53 expression.Position,
54 continueJump - loopScopeStart - 1
55 );
56 }
57}
BadSourcePosition Position
The source Position of the 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, BadWhileExpression expression)
Contains the Loop 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)