BadScript 2
Loading...
Searching...
No Matches
BadForExpression.cs
Go to the documentation of this file.
7
9
14{
18 private readonly List<BadExpression> m_Body;
19
29 BadExpression condition,
30 BadExpression varIncrement,
31 IEnumerable<BadExpression> body,
32 BadSourcePosition position) : base(false, position)
33 {
34 VarDef = varDef;
35 Condition = condition;
36 VarIncrement = varIncrement;
37 m_Body = body.ToList();
38 }
39
43 public IEnumerable<BadExpression> Body => m_Body;
44
48 public BadExpression Condition { get; private set; }
49
53 public BadExpression VarDef { get; private set; }
54
58 public BadExpression VarIncrement { get; private set; }
59
64 public void SetBody(IEnumerable<BadExpression> body)
65 {
66 m_Body.Clear();
67 m_Body.AddRange(body);
68 }
69
82
84 public override IEnumerable<BadExpression> GetDescendants()
85 {
86 foreach (BadExpression? vDef in VarDef.GetDescendantsAndSelf())
87 {
88 yield return vDef;
89 }
90
92 {
93 yield return vInc;
94 }
95
97 {
98 yield return cond;
99 }
100
101 foreach (BadExpression descendant in m_Body.SelectMany(expression => expression.GetDescendantsAndSelf()))
102 {
103 yield return descendant;
104 }
105 }
106
108 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
109 {
110 using BadExecutionContext loopCtx =
111 new BadExecutionContext(context.Scope.CreateChild("ForLoop", context.Scope, null));
112
113 foreach (BadObject o in VarDef.Execute(loopCtx))
114 {
115 yield return o;
116 }
117
118 BadObject cond = BadObject.Null;
119
120 foreach (BadObject o in Condition.Execute(loopCtx))
121 {
122 cond = o;
123
124 yield return o;
125 }
126
127 IBadBoolean bRet = cond.Dereference(Position) as IBadBoolean ??
128 throw new BadRuntimeException("While Condition is not a boolean", Position);
129
130 while (bRet.Value)
131 {
132 using BadExecutionContext loopContext = new BadExecutionContext(loopCtx.Scope.CreateChild("InnerForLoop",
133 loopCtx.Scope,
134 null,
135 BadScopeFlags.Breakable |
136 BadScopeFlags.Continuable
137 )
138 );
139
140 if (m_Body.Count != 0)
141 {
142 foreach (BadObject o in loopContext.Execute(m_Body))
143 {
144 yield return o;
145 }
146
147 if (loopContext.Scope.IsBreak || loopContext.Scope.ReturnValue != null)
148 {
149 break;
150 }
151 }
152
153 foreach (BadObject o in VarIncrement.Execute(loopContext))
154 {
155 yield return o;
156 }
157
158 foreach (BadObject o in Condition.Execute(loopContext))
159 {
160 cond = o;
161
162 yield return o;
163 }
164
165 bRet = cond.Dereference(Position) as IBadBoolean ??
166 throw new BadRuntimeException("While Condition is not a boolean", Position);
167 }
168
169 yield return BadObject.Null;
170 }
171}
Describes a specific position inside a source file.
static BadExpression Optimize(BadExpression expr)
Optimizes the given expression.
Base Implementation for all Expressions used inside the Script.
IEnumerable< BadExpression > GetDescendantsAndSelf()
Returns all Descendants of the Expression and the Expression itself.
BadSourcePosition Position
The source Position of the Expression.
IEnumerable< BadObject > Execute(BadExecutionContext context)
Evaluates the Expression within the current Execution Context.
void SetBody(IEnumerable< BadExpression > body)
Sets the Body of the Loop.
override IEnumerable< BadExpression > GetDescendants()
override void Optimize()
Uses the Constant Folding Optimizer to optimize the expression.
override IEnumerable< BadObject > InnerExecute(BadExecutionContext context)
BadForExpression(BadExpression varDef, BadExpression condition, BadExpression varIncrement, IEnumerable< BadExpression > body, BadSourcePosition position)
Constructor of the For Expression.
BadExpression VarDef
The Variable Definition part of the for loop.
BadExpression Condition
The Exit Condition of the For Loop.
BadExpression VarIncrement
The Variable Modifier Expression of the For Loop.
readonly List< BadExpression > m_Body
Loop Body.
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
BadScope CreateChild(string name, BadScope? caller, bool? useVisibility, BadScopeFlags flags=BadScopeFlags.RootScope)
Creates a subscope of the current scope.
Definition BadScope.cs:597
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
Implements the Interface for Native Boolean.
Definition IBadBoolean.cs:7
Contains Shared Data Structures and Functionality.
Contains the BadScript2 Constant Folding Optimizations.
Contains the Loop Expressions for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.
BadScopeFlags
Defines Different Behaviours for the Current Scope.