BadScript 2
Loading...
Searching...
No Matches
BadSwitchExpression.cs
Go to the documentation of this file.
9
14{
15
19 public BadExpression Value { get; }
23 private readonly Dictionary<BadExpression, BadExpression[]> m_Cases;
27 private List<BadExpression>? m_DefaultCase;
31 public IDictionary<BadExpression, BadExpression[]> Cases => m_Cases;
32
36 public IEnumerable<BadExpression>? DefaultCase => m_DefaultCase;
37
45 public BadSwitchExpression(BadSourcePosition position, BadExpression value, Dictionary<BadExpression, BadExpression[]> cases, List<BadExpression>? defaultCase) : base(false, position)
46 {
47 Value = value;
48 m_Cases = cases;
49 m_DefaultCase = defaultCase;
50 }
52 public override IEnumerable<BadExpression> GetDescendants()
53 {
54 yield return Value;
55 foreach (KeyValuePair<BadExpression, BadExpression[]> branch in m_Cases)
56 {
57 yield return branch.Key;
58 foreach (BadExpression valueExpr in branch.Value)
59 {
60 yield return valueExpr;
61 }
62 }
63
64 if (m_DefaultCase == null)
65 {
66 yield break;
67 }
68
69 foreach (BadExpression e in m_DefaultCase)
70 {
71 yield return e;
72 }
73 }
74
75 public override void Optimize()
76 {
77 KeyValuePair<BadExpression, BadExpression[]>[] branches = m_Cases.ToArray();
78 m_Cases.Clear();
79
80 foreach (KeyValuePair<BadExpression, BadExpression[]> branch in branches)
81 {
83 BadConstantFoldingOptimizer.Optimize(branch.Value).ToArray();
84 }
85
86 if (m_DefaultCase == null)
87 {
88 return;
89 }
90
91 for (int i = 0; i < m_DefaultCase.Count; i++)
92 {
94 }
95 }
96
101 public void SetDefaultCase(IEnumerable<BadExpression>? defaultCase)
102 {
103 if (defaultCase == null)
104 {
105 m_DefaultCase = null;
106 }
107 else if (m_DefaultCase != null)
108 {
109 m_DefaultCase.Clear();
110 m_DefaultCase.AddRange(defaultCase);
111 }
112 else
113 {
114 m_DefaultCase = new List<BadExpression>(defaultCase);
115 }
116 }
118 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
119 {
120 BadObject valueResult = BadObject.Null;
121 foreach (BadObject o in context.Execute(Value))
122 {
123 valueResult = o;
124 }
125 valueResult = valueResult.Dereference();
126
127 BadExecutionContext? switchContext = new BadExecutionContext(context.Scope.CreateChild("SwitchContext", context.Scope, null, BadScopeFlags.Breakable));
128 bool executeNextBlock = false;
129 foreach (KeyValuePair<BadExpression,BadExpression[]> branch in m_Cases)
130 {
131 if (!executeNextBlock)
132 {
133 BadObject keyResult = BadObject.Null;
134 foreach (BadObject o in switchContext.Execute(branch.Key))
135 {
136 keyResult = o;
137 }
138
139
140 keyResult = keyResult.Dereference();
141 BadObject? result = BadObject.Null;
142 foreach (BadObject o in BadEqualityExpression.EqualWithOverride(switchContext, valueResult, keyResult, Position))
143 {
144 result = o;
145 }
146
147
148 result = result.Dereference();
149 if (result is not IBadBoolean b)
150 {
151 throw new BadRuntimeException("Switch case result must be a boolean", Position);
152 }
153 executeNextBlock = b.Value;
154 }
155 if (executeNextBlock && branch.Value.Length > 0)
156 {
157 foreach (BadObject o in switchContext.Execute(branch.Value))
158 {
159 yield return o;
160 if(switchContext.Scope.IsBreak)
161 {
162 yield break;
163 }
164 }
165 throw BadRuntimeException.Create(switchContext.Scope,"Switch Case must break", Position);
166 }
167 }
168 if (m_DefaultCase != null)
169 {
170 foreach (BadObject o in switchContext.Execute(m_DefaultCase))
171 {
172 yield return o;
173 if(switchContext.Scope.IsBreak)
174 {
175 yield break;
176 }
177 }
178 throw BadRuntimeException.Create(switchContext.Scope,"Switch Case must break", Position);
179 }
180 }
181}
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.