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 //Break And continue pointers are relative to the "CreateScope" instruction
19 //If break instruction is encountered
20 // Jump to loopScopeStart + offset to after loop(the destroy scope instruction is omitted because the vm handles the scopes)
21 context.Emit(
22 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, BadOpCode.JumpRelativeIfFalse, expression.Position, context.InstructionCount - endJump - 1);
38 //address to the end of the loop(relative to the create scope instruction)
39 context.ResolveEmpty(setBreakInstruction, BadOpCode.SetBreakPointer, expression.Position, context.InstructionCount - loopScopeStart - 1);
40 //Address to start of the condition(relative to the start of the loop)
41 context.ResolveEmpty(setContinueInstruction, BadOpCode.SetContinuePointer, expression.Position, continueJump - loopScopeStart - 1);
42 }
43}
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)