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 BadSourcePosition position,
43 BadExpression[] expressions,
44 BadExpression[] catchExpressions,
45 BadExpression[] finallyExpressions,
46 string errorName) : base(false, position)
47 {
48 m_Expressions = expressions;
49 m_CatchExpressions = catchExpressions;
50 ErrorName = errorName;
51 m_FinallyExpressions = finallyExpressions;
52 }
53
57 public IEnumerable<BadExpression> CatchExpressions => m_CatchExpressions;
58
62 public IEnumerable<BadExpression> TryExpressions => m_Expressions;
63
67 public IEnumerable<BadExpression> FinallyExpressions => m_FinallyExpressions;
68
70 public override void Optimize()
71 {
72 for (int i = 0; i < m_CatchExpressions.Length; i++)
73 {
75 }
76
77 for (int i = 0; i < m_Expressions.Length; i++)
78 {
80 }
81
82 for (int i = 0; i < m_FinallyExpressions.Length; i++)
83 {
85 }
86 }
87
89 public override IEnumerable<BadExpression> GetDescendants()
90 {
91 foreach (BadExpression expression in m_Expressions)
92 {
93 foreach (BadExpression e in expression.GetDescendantsAndSelf())
94 {
95 yield return e;
96 }
97 }
98
99 foreach (BadExpression expression in m_CatchExpressions)
100 {
101 foreach (BadExpression e in expression.GetDescendantsAndSelf())
102 {
103 yield return e;
104 }
105 }
106
107 foreach (BadExpression expression in m_FinallyExpressions)
108 {
109 foreach (BadExpression e in expression.GetDescendantsAndSelf())
110 {
111 yield return e;
112 }
113 }
114 }
115
117 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
118 {
119 if (m_Expressions.Length != 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 using (BadExecutionContext tryContext = new BadExecutionContext(
123 context.Scope.CreateChild("TryBlock", context.Scope, null, BadScopeFlags.CaptureThrow)
124 ))
125 {
126 using IEnumerator<BadObject>? enumerator = tryContext.Execute(m_Expressions).GetEnumerator();
127 while (true)
128 {
129 BadObject o;
130
131 try {
132 if (!enumerator.MoveNext())
133 break;
134 o = enumerator.Current ?? BadObject.Null;
135 }
136 catch (Exception e)
137 {
138 if(m_CatchExpressions.Length != 0)
139 {
140 using BadExecutionContext catchContext = new BadExecutionContext(
141 context.Scope.CreateChild("CatchBlock", context.Scope, null)
142 );
143
144
145 BadRuntimeError error;
146 if (e is BadRuntimeErrorException bre) error = bre.Error;
147 else error = BadRuntimeError.FromException(e, context.Scope.GetStackTrace());
148 catchContext.Scope.DefineVariable(ErrorName, error);
149
150 catchEnumerable = catchContext.Execute(m_CatchExpressions);
151 }
152 break;
153 }
154 yield return o;
155 }
156 }
157
158 if (catchEnumerable != null)
159 {
160 foreach (BadObject e in catchEnumerable)
161 {
162 yield return e;
163 }
164 }
165 }
166
167 if (m_FinallyExpressions.Length != 0)
168 {
169 using BadExecutionContext finallyContext = new BadExecutionContext(
170 context.Scope.CreateChild("FinallyBlock", context.Scope, null)
171 );
172
173 foreach (BadObject e in finallyContext.Execute(m_FinallyExpressions))
174 {
175 yield return e;
176 }
177 }
178 }
179}
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:792
string GetStackTrace()
Returns the Stack Trace of the Current scope.
Definition BadScope.cs:633
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.