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
34 public BadIfExpression(Dictionary<BadExpression, BadExpression[]> branches,
35 IEnumerable<BadExpression>? elseBranch,
36 BadSourcePosition position) : base(false, position)
37 {
38 m_ConditionalBranches = branches;
39 m_ElseBranch = elseBranch?.ToList();
40 }
41
45 public IDictionary<BadExpression, BadExpression[]> ConditionalBranches => m_ConditionalBranches;
46
50 public IEnumerable<BadExpression>? ElseBranch => m_ElseBranch;
51
56 public void SetElseBranch(IEnumerable<BadExpression>? branch)
57 {
58 if (branch == null)
59 {
60 m_ElseBranch = null;
61 }
62 else if (m_ElseBranch != null)
63 {
64 m_ElseBranch.Clear();
65 m_ElseBranch.AddRange(branch);
66 }
67 else
68 {
69 m_ElseBranch = new List<BadExpression>(branch);
70 }
71 }
72
74 public override void Optimize()
75 {
76 KeyValuePair<BadExpression, BadExpression[]>[] branches = m_ConditionalBranches.ToArray();
78
79 foreach (KeyValuePair<BadExpression, BadExpression[]> branch in branches)
80 {
83 .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(Position);
142
143 if (conditionResult is not IBadBoolean cBool)
144 {
145 throw new BadRuntimeException("Condition must be a boolean", Position);
146 }
147
148 if (!cBool.Value)
149 {
150 continue;
151 }
152
153 if (keyValuePair.Value.Length == 0)
154 {
155 yield break;
156 }
157
158 using BadExecutionContext branchContext = new BadExecutionContext(context.Scope.CreateChild("IfBranch",
159 context.Scope,
160 null
161 )
162 );
163
164 foreach (BadObject o in branchContext.Execute(keyValuePair.Value))
165 {
166 yield return o;
167 }
168
169 yield break;
170 }
171
172 if (m_ElseBranch is null)
173 {
174 yield break;
175 }
176
177 using BadExecutionContext elseContext = new BadExecutionContext(context.Scope.CreateChild("IfBranch",
178 context.Scope,
179 null
180 )
181 );
182
183 foreach (BadObject o in elseContext.Execute(m_ElseBranch))
184 {
185 yield return o;
186 }
187 }
188}
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.