BadScript 2
Loading...
Searching...
No Matches
BadForEachExpressionCompiler.cs
Go to the documentation of this file.
8
13
17public class BadForEachExpressionCompiler : BadExpressionCompiler<BadForEachExpression>
18{
20 public override void Compile(BadExpressionCompileContext context, BadForEachExpression expression)
21 {
22 context.Compile(expression.Target);
23 context.Emit(BadOpCode.Dup, expression.Position);
24 context.Emit(BadOpCode.Push, expression.Position, (BadObject)"GetEnumerator");
25 context.Emit(BadOpCode.HasProperty, expression.Position);
26 context.Emit(BadOpCode.JumpRelativeIfFalse, expression.Position, 2);
27 context.Emit(BadOpCode.LoadMember, expression.Position, "GetEnumerator");
28 context.Emit(BadOpCode.Invoke, expression.Position, 0);
29 context.Emit(BadOpCode.CreateScope, expression.Position, "FOREACH_ENUMERATOR_SCOPE", BadObject.Null);
30 context.Emit(BadOpCode.DefVar, expression.Position, "~ENUMERATOR~", true);
31 context.Emit(BadOpCode.Swap, expression.Position);
32 context.Emit(BadOpCode.Assign, expression.Position);
33 List<BadExpression>? body = expression.Body.ToList();
34 body.Insert(
35 0,
38 expression.LoopVariable.Text,
39 expression.LoopVariable.SourcePosition,
40 null,
41 true
42 ),
45 new BadVariableExpression("~ENUMERATOR~", expression.Position),
46 "GetCurrent",
47 expression.Position,
48 new List<BadExpression>()
49 ),
50 Array.Empty<BadExpression>(),
51 expression.Position
52 ),
53 expression.Position
54 )
55 );
56 context.Compile(
60 new BadVariableExpression("~ENUMERATOR~", expression.LoopVariable.SourcePosition),
61 "MoveNext",
62 expression.LoopVariable.SourcePosition,
63 new List<BadExpression>()
64 ),
65 Array.Empty<BadExpression>(),
66 expression.Position
67 ),
68 body,
69 expression.Position
70 )
71 );
72 context.Emit(BadOpCode.DestroyScope, expression.Position);
73 }
74}
Implements the Member Access to set or get properties from an object. LEFT.RIGHT.
Base Implementation for all Expressions used inside the Script.
BadSourcePosition Position
The source Position of the Expression.
Implements the Assign Expression LEFT = RIGHT.
readonly BadWordToken LoopVariable
The Variable Name of the Current Loop iteration.
BadExpression Target
The Enumerable/Enumerator Expression of the Loop.
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, BadForEachExpression expression)
Contains the Access Expressions for the BadScript2 Language.
Contains the Binary Expressions for the BadScript2 Language.
Contains the Loop Expressions for the BadScript2 Language.
Contains the Function Expressions for the BadScript2 Language.
Contains the Variable 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