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 varDef,
30 BadExpression condition,
31 BadExpression varIncrement,
32 IEnumerable<BadExpression> body,
33 BadSourcePosition position) : base(false, position)
34 {
35 VarDef = varDef;
36 Condition = condition;
37 VarIncrement = varIncrement;
38 m_Body = body.ToList();
39 }
40
44 public IEnumerable<BadExpression> Body => m_Body;
45
49 public BadExpression Condition { get; private set; }
50
54 public BadExpression VarDef { get; private set; }
55
59 public BadExpression VarIncrement { get; private set; }
60
65 public void SetBody(IEnumerable<BadExpression> body)
66 {
67 m_Body.Clear();
68 m_Body.AddRange(body);
69 }
70
83
85 public override IEnumerable<BadExpression> GetDescendants()
86 {
87 foreach (BadExpression? vDef in VarDef.GetDescendantsAndSelf())
88 {
89 yield return vDef;
90 }
91
93 {
94 yield return vInc;
95 }
96
98 {
99 yield return cond;
100 }
101
102 foreach (BadExpression descendant in m_Body.SelectMany(expression => expression.GetDescendantsAndSelf()))
103 {
104 yield return descendant;
105 }
106 }
107
109 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
110 {
111 using BadExecutionContext loopCtx =
112 new BadExecutionContext(context.Scope.CreateChild("ForLoop", context.Scope, null));
113
114 foreach (BadObject o in VarDef.Execute(loopCtx))
115 {
116 yield return o;
117 }
118
119
120 BadObject cond = BadObject.Null;
121
122 foreach (BadObject o in Condition.Execute(loopCtx))
123 {
124 cond = o;
125
126 yield return o;
127 }
128
129
130 IBadBoolean bRet = cond.Dereference() as IBadBoolean ??
131 throw new BadRuntimeException("While Condition is not a boolean", Position);
132
133 while (bRet.Value)
134 {
135 using BadExecutionContext loopContext = new BadExecutionContext(
136 loopCtx.Scope.CreateChild(
137 "InnerForLoop",
138 loopCtx.Scope,
139 null,
140 BadScopeFlags.Breakable | BadScopeFlags.Continuable
141 )
142 );
143
144 if (m_Body.Count != 0)
145 {
146 foreach (BadObject o in loopContext.Execute(m_Body))
147 {
148 yield return o;
149 }
150
151 if (loopContext.Scope.IsBreak || loopContext.Scope.ReturnValue != null)
152 {
153 break;
154 }
155 }
156
157 foreach (BadObject o in VarIncrement.Execute(loopContext))
158 {
159 yield return o;
160 }
161
162 foreach (BadObject o in Condition.Execute(loopContext))
163 {
164 cond = o;
165
166 yield return o;
167 }
168
169
170 bRet = cond.Dereference() as IBadBoolean ??
171 throw new BadRuntimeException("While Condition is not a boolean", Position);
172 }
173
174 yield return BadObject.Null;
175 }
176}
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:792
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.