BadScript 2
Loading...
Searching...
No Matches
BadTryCatchExpression.cs
Go to the documentation of this file.
6
8
13{
17 public readonly string ErrorName;
18
23
27 private readonly BadExpression[] m_Expressions;
28
33
42 BadExpression[] expressions,
43 BadExpression[] catchExpressions,
44 BadExpression[] finallyExpressions,
45 string errorName) : base(false, position)
46 {
47 m_Expressions = expressions;
48 m_CatchExpressions = catchExpressions;
49 ErrorName = errorName;
50 m_FinallyExpressions = finallyExpressions;
51 }
52
56 public IEnumerable<BadExpression> CatchExpressions => m_CatchExpressions;
57
61 public IEnumerable<BadExpression> TryExpressions => m_Expressions;
62
66 public IEnumerable<BadExpression> FinallyExpressions => m_FinallyExpressions;
67
69 public override void Optimize()
70 {
71 for (int i = 0; i < m_CatchExpressions.Length; i++)
72 {
74 }
75
76 for (int i = 0; i < m_Expressions.Length; i++)
77 {
79 }
80
81 for (int i = 0; i < m_FinallyExpressions.Length; i++)
82 {
84 }
85 }
86
88 public override IEnumerable<BadExpression> GetDescendants()
89 {
90 foreach (BadExpression expression in m_Expressions)
91 {
92 foreach (BadExpression e in expression.GetDescendantsAndSelf())
93 {
94 yield return e;
95 }
96 }
97
98 foreach (BadExpression expression in m_CatchExpressions)
99 {
100 foreach (BadExpression e in expression.GetDescendantsAndSelf())
101 {
102 yield return e;
103 }
104 }
105
106 foreach (BadExpression expression in m_FinallyExpressions)
107 {
108 foreach (BadExpression e in expression.GetDescendantsAndSelf())
109 {
110 yield return e;
111 }
112 }
113 }
114
116 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
117 {
118 if (m_Expressions.Length !=
119 0) //If the expressions are empty, then we cant possibly throw an error, nor do we need to catch one
120 {
121 IEnumerable<BadObject>? catchEnumerable = null;
122
123 using (BadExecutionContext tryContext =
124 new BadExecutionContext(context.Scope.CreateChild("TryBlock",
125 context.Scope,
126 null,
127 BadScopeFlags.CaptureThrow
128 )
129 ))
130 {
131 using IEnumerator<BadObject>? enumerator = tryContext.Execute(m_Expressions)
132 .GetEnumerator();
133
134 while (true)
135 {
136 BadObject o;
137
138 try
139 {
140 if (!enumerator.MoveNext())
141 {
142 break;
143 }
144
145 o = enumerator.Current ?? BadObject.Null;
146 }
147 catch (Exception e)
148 {
149 if (m_CatchExpressions.Length != 0)
150 {
151 using BadExecutionContext catchContext =
152 new BadExecutionContext(context.Scope.CreateChild("CatchBlock", context.Scope, null));
153
154 BadRuntimeError error;
155
156 if (e is BadRuntimeErrorException bre)
157 {
158 error = bre.Error;
159 }
160 else
161 {
162 error = BadRuntimeError.FromException(e, context.Scope.GetStackTrace());
163 }
164
165 catchContext.Scope.DefineVariable(ErrorName, error);
166
167 catchEnumerable = catchContext.Execute(m_CatchExpressions);
168 }
169
170 break;
171 }
172
173 yield return o;
174 }
175 }
176
177 if (catchEnumerable != null)
178 {
179 foreach (BadObject e in catchEnumerable)
180 {
181 yield return e;
182 }
183 }
184 }
185
186 if (m_FinallyExpressions.Length != 0)
187 {
188 using BadExecutionContext finallyContext =
189 new BadExecutionContext(context.Scope.CreateChild("FinallyBlock", context.Scope, null));
190
191 foreach (BadObject e in finallyContext.Execute(m_FinallyExpressions))
192 {
193 yield return e;
194 }
195 }
196 }
197}
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.
Implements the Try Catch Statement Expression.
readonly BadExpression[] m_CatchExpressions
The Catch Block.
IEnumerable< BadExpression > CatchExpressions
The Catch Block.
readonly BadExpression[] m_FinallyExpressions
The Finally Block.
override IEnumerable< BadObject > InnerExecute(BadExecutionContext context)
override void Optimize()
Uses the Constant Folding Optimizer to optimize the expression.
IEnumerable< BadExpression > TryExpressions
The Try Block.
readonly string ErrorName
The Variable name of the Exception inside the catch block.
IEnumerable< BadExpression > FinallyExpressions
The Finally Block.
BadTryCatchExpression(BadSourcePosition position, BadExpression[] expressions, BadExpression[] catchExpressions, BadExpression[] finallyExpressions, string errorName)
Constructor for the Try Catch Expression.
The Execution Context. Every execution of a script needs a context the script is running in....
IEnumerable< BadObject > Execute(IEnumerable< BadExpression > expressions)
Executes an enumeration of expressions.
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:597
string GetStackTrace()
Returns the Stack Trace of the Current scope.
Definition BadScope.cs:419
Gets thrown if the runtime encounters an error.
Implements the Error Object Type.
static BadRuntimeError FromException(Exception e, string? scriptStackTrace=null)
Creates a BadRuntimeError from an Exception.
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
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 Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.
BadScopeFlags
Defines Different Behaviours for the Current Scope.