BadScript 2
Loading...
Searching...
No Matches
BadSwitchExpression.cs
Go to the documentation of this file.
8
10
15{
19 private readonly Dictionary<BadExpression, BadExpression[]> m_Cases;
20
24 private List<BadExpression>? m_DefaultCase;
25
34 BadExpression value,
35 Dictionary<BadExpression, BadExpression[]> cases,
36 List<BadExpression>? defaultCase) : base(false, position)
37 {
38 Value = value;
39 m_Cases = cases;
40 m_DefaultCase = defaultCase;
41 }
42
46 public BadExpression Value { get; }
47
51 public IDictionary<BadExpression, BadExpression[]> Cases => m_Cases;
52
56 public IEnumerable<BadExpression>? DefaultCase => m_DefaultCase;
57
59 public override IEnumerable<BadExpression> GetDescendants()
60 {
61 yield return Value;
62
63 foreach (KeyValuePair<BadExpression, BadExpression[]> branch in m_Cases)
64 {
65 yield return branch.Key;
66
67 foreach (BadExpression valueExpr in branch.Value)
68 {
69 yield return valueExpr;
70 }
71 }
72
73 if (m_DefaultCase == null)
74 {
75 yield break;
76 }
77
78 foreach (BadExpression e in m_DefaultCase)
79 {
80 yield return e;
81 }
82 }
83
84 public override void Optimize()
85 {
86 KeyValuePair<BadExpression, BadExpression[]>[] branches = m_Cases.ToArray();
87 m_Cases.Clear();
88
89 foreach (KeyValuePair<BadExpression, BadExpression[]> branch in branches)
90 {
93 .ToArray();
94 }
95
96 if (m_DefaultCase == null)
97 {
98 return;
99 }
100
101 for (int i = 0; i < m_DefaultCase.Count; i++)
102 {
104 }
105 }
106
111 public void SetDefaultCase(IEnumerable<BadExpression>? defaultCase)
112 {
113 if (defaultCase == null)
114 {
115 m_DefaultCase = null;
116 }
117 else if (m_DefaultCase != null)
118 {
119 m_DefaultCase.Clear();
120 m_DefaultCase.AddRange(defaultCase);
121 }
122 else
123 {
124 m_DefaultCase = new List<BadExpression>(defaultCase);
125 }
126 }
127
129 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
130 {
131 BadObject valueResult = BadObject.Null;
132
133 foreach (BadObject o in context.Execute(Value))
134 {
135 valueResult = o;
136 }
137
138 valueResult = valueResult.Dereference(Position);
139
140 BadExecutionContext? switchContext =
141 new BadExecutionContext(context.Scope.CreateChild("SwitchContext",
142 context.Scope,
143 null,
144 BadScopeFlags.Breakable
145 )
146 );
147 bool executeNextBlock = false;
148
149 foreach (KeyValuePair<BadExpression, BadExpression[]> branch in m_Cases)
150 {
151 if (!executeNextBlock)
152 {
153 BadObject keyResult = BadObject.Null;
154
155 foreach (BadObject o in switchContext.Execute(branch.Key))
156 {
157 keyResult = o;
158 }
159
160 keyResult = keyResult.Dereference(Position);
161 BadObject? result = BadObject.Null;
162
163 foreach (BadObject o in BadEqualityExpression.EqualWithOverride(switchContext,
164 valueResult,
165 keyResult,
167 ))
168 {
169 result = o;
170 }
171
172 result = result.Dereference(Position);
173
174 if (result is not IBadBoolean b)
175 {
176 throw new BadRuntimeException("Switch case result must be a boolean", Position);
177 }
178
179 executeNextBlock = b.Value;
180 }
181
182 if (executeNextBlock && branch.Value.Length > 0)
183 {
184 foreach (BadObject o in switchContext.Execute(branch.Value))
185 {
186 yield return o;
187
188 if (switchContext.Scope.IsBreak)
189 {
190 yield break;
191 }
192 }
193
194 throw BadRuntimeException.Create(switchContext.Scope, "Switch Case must break", Position);
195 }
196 }
197
198 if (m_DefaultCase != null)
199 {
200 foreach (BadObject o in switchContext.Execute(m_DefaultCase))
201 {
202 yield return o;
203
204 if (switchContext.Scope.IsBreak)
205 {
206 yield break;
207 }
208 }
209
210 throw BadRuntimeException.Create(switchContext.Scope, "Switch Case must break", Position);
211 }
212 }
213}
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.
BadSourcePosition Position
The source Position of the Expression.
static IEnumerable< BadObject > EqualWithOverride(BadExecutionContext? caller, BadObject left, BadObject right, BadSourcePosition position)
Executes the operator override for the given operator name.
Implements the Switch Statement Expression.
override void Optimize()
Uses the Constant Folding Optimizer to optimize the expression.
readonly Dictionary< BadExpression, BadExpression[]> m_Cases
The Cases.
IDictionary< BadExpression, BadExpression[]> Cases
The Cases.
void SetDefaultCase(IEnumerable< BadExpression >? defaultCase)
Sets the Default Case.
override IEnumerable< BadObject > InnerExecute(BadExecutionContext context)
BadSwitchExpression(BadSourcePosition position, BadExpression value, Dictionary< BadExpression, BadExpression[]> cases, List< BadExpression >? defaultCase)
Constructs a new BadSwitchExpression.
override IEnumerable< BadExpression > GetDescendants()
IEnumerable< BadExpression >? DefaultCase
The (optional) Default Case.
List< BadExpression >? m_DefaultCase
The (optional) Default Case.
The Execution Context. Every execution of a script needs a context the script is running in....
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
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 Comparison Expressions for the BadScript2 Language.
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.
BadScopeFlags
Defines Different Behaviours for the Current Scope.