BadScript 2
Loading...
Searching...
No Matches
BadIfExpression.cs
Go to the documentation of this file.
7
12
17{
21 private readonly Dictionary<BadExpression, BadExpression[]> m_ConditionalBranches;
22
26 private List<BadExpression>? m_ElseBranch;
27
35 Dictionary<BadExpression, BadExpression[]> branches,
36 IEnumerable<BadExpression>? elseBranch,
37 BadSourcePosition position) : base(false, position)
38 {
39 m_ConditionalBranches = branches;
40 m_ElseBranch = elseBranch?.ToList();
41 }
42
46 public IDictionary<BadExpression, BadExpression[]> ConditionalBranches => m_ConditionalBranches;
47
51 public IEnumerable<BadExpression>? ElseBranch => m_ElseBranch;
52
57 public void SetElseBranch(IEnumerable<BadExpression>? branch)
58 {
59 if (branch == null)
60 {
61 m_ElseBranch = null;
62 }
63 else if (m_ElseBranch != null)
64 {
65 m_ElseBranch.Clear();
66 m_ElseBranch.AddRange(branch);
67 }
68 else
69 {
70 m_ElseBranch = new List<BadExpression>(branch);
71 }
72 }
73
75 public override void Optimize()
76 {
77 KeyValuePair<BadExpression, BadExpression[]>[] branches = m_ConditionalBranches.ToArray();
79
80 foreach (KeyValuePair<BadExpression, BadExpression[]> branch in branches)
81 {
83 BadConstantFoldingOptimizer.Optimize(branch.Value).ToArray();
84 }
85
86 if (m_ElseBranch == null)
87 {
88 return;
89 }
90
91 for (int i = 0; i < m_ElseBranch.Count; i++)
92 {
94 }
95 }
96
98 public override IEnumerable<BadExpression> GetDescendants()
99 {
100 foreach (KeyValuePair<BadExpression, BadExpression[]> branch in m_ConditionalBranches)
101 {
102 foreach (BadExpression keyExpr in branch.Key.GetDescendantsAndSelf())
103 {
104 yield return keyExpr;
105 }
106
107 foreach (BadExpression valueExpr in branch.Value)
108 {
109 foreach (BadExpression expr in valueExpr.GetDescendantsAndSelf())
110 {
111 yield return expr;
112 }
113 }
114 }
115
116 if (m_ElseBranch == null)
117 {
118 yield break;
119 }
120
121 foreach (BadExpression e in m_ElseBranch.SelectMany(expr => expr.GetDescendantsAndSelf()))
122 {
123 yield return e;
124 }
125 }
126
128 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
129 {
130 foreach (KeyValuePair<BadExpression, BadExpression[]> keyValuePair in m_ConditionalBranches)
131 {
132 BadObject conditionResult = BadObject.Null;
133
134 foreach (BadObject o in keyValuePair.Key.Execute(context))
135 {
136 conditionResult = o;
137
138 yield return o;
139 }
140
141 conditionResult = conditionResult.Dereference();
142
143
144
145 if (conditionResult is not IBadBoolean cBool)
146 {
147 throw new BadRuntimeException("Condition must be a boolean", Position);
148 }
149
150 if (!cBool.Value)
151 {
152 continue;
153 }
154
155 if (keyValuePair.Value.Length == 0)
156 {
157 yield break;
158 }
159
160 using BadExecutionContext branchContext = new BadExecutionContext(
161 context.Scope.CreateChild(
162 "IfBranch",
163 context.Scope,
164 null
165 )
166 );
167
168 foreach (BadObject o in branchContext.Execute(keyValuePair.Value))
169 {
170 yield return o;
171 }
172
173 yield break;
174 }
175
176 if (m_ElseBranch is null)
177 {
178 yield break;
179 }
180
181 using BadExecutionContext elseContext = new BadExecutionContext(
182 context.Scope.CreateChild(
183 "IfBranch",
184 context.Scope,
185 null
186 )
187 );
188
189 foreach (BadObject o in elseContext.Execute(m_ElseBranch))
190 {
191 yield return o;
192 }
193 }
194}
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.
Implements the If Statement Expression.
readonly Dictionary< BadExpression, BadExpression[]> m_ConditionalBranches
The Conditional Branches.
IEnumerable< BadExpression >? ElseBranch
The (optional) Else Branch.
void SetElseBranch(IEnumerable< BadExpression >? branch)
Sets the Else Branch.
IDictionary< BadExpression, BadExpression[]> ConditionalBranches
The Conditional Branches.
override void Optimize()
Uses the Constant Folding Optimizer to optimize the expression.
BadIfExpression(Dictionary< BadExpression, BadExpression[]> branches, IEnumerable< BadExpression >? elseBranch, BadSourcePosition position)
Constructor of the If Expression.
override IEnumerable< BadExpression > GetDescendants()
override IEnumerable< BadObject > InnerExecute(BadExecutionContext context)
List< BadExpression >? m_ElseBranch
The (optional) Else Branch.
The Execution Context. Every execution of a script needs a context the script is running in....
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 Block 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.