BadScript 2
Loading...
Searching...
No Matches
BadFunctionExpression.cs
Go to the documentation of this file.
1using System.Text;
2
12
14
19{
23 private readonly List<BadExpression> m_Body;
24
25
29 private readonly BadMetaData? m_MetaData;
30
34 private readonly List<BadFunctionParameter> m_Parameters;
35
50 List<BadFunctionParameter> parameter,
51 List<BadExpression> block,
52 BadSourcePosition position,
53 bool isConstant,
54 BadMetaData? metaData,
55 bool isSingleLine,
56 bool isStatic,
58 BadExpression? typeExpr = null) :
59 base(false, position)
60 {
61 Name = name;
62 m_Parameters = parameter;
63 m_Body = block;
64 TypeExpression = typeExpr;
65 IsConstantFunction = isConstant;
66 m_MetaData = metaData;
67 IsSingleLine = isSingleLine;
68 IsStatic = isStatic;
69 CompileLevel = compileLevel;
70 }
71
75 public bool IsSingleLine { get; }
76
80 public bool IsConstantFunction { get; }
81
85 public bool IsStatic { get; }
86
91
95 public IEnumerable<BadFunctionParameter> Parameters => m_Parameters;
96
100 public IEnumerable<BadExpression> Body => m_Body;
101
105 public BadWordToken? Name { get; private set; }
106
110 public BadFunctionCompileLevel CompileLevel { get; private set; }
111
112#region IBadNamedExpression Members
113
115 public string? GetName()
116 {
117 return Name?.Text;
118 }
119
120#endregion
121
122 public void SetName(string name)
123 {
124 Name = name;
125 }
126
132 {
133 CompileLevel = level;
134 }
135
140 public void SetBody(IEnumerable<BadExpression> body)
141 {
142 m_Body.Clear();
143 m_Body.AddRange(body);
144 }
145
147 public override void Optimize()
148 {
149 for (int i = 0; i < m_Body.Count; i++)
150 {
152 }
153 }
154
159 public string GetHeader()
160 {
161 string level = CompileLevel switch
162 {
163 BadFunctionCompileLevel.Compiled => "compiled ",
164 BadFunctionCompileLevel.CompiledFast => "compiled fast",
165 _ => "",
166 };
167
168 return
169 $"{level}{BadStaticKeys.FUNCTION_KEY} {Name?.ToString() ?? "<anonymous>"}({string.Join(", ", Parameters.Cast<object>())})";
170 }
171
172
174 public override string ToString()
175 {
176 StringBuilder sb = new StringBuilder(GetHeader());
177 sb.AppendLine();
178 sb.AppendLine("{");
179
180 foreach (BadExpression expression in Body)
181 {
182 sb.AppendLine($"\t{expression}");
183 }
184
185 sb.AppendLine("}");
186
187 return sb.ToString();
188 }
189
191 public override IEnumerable<BadExpression> GetDescendants()
192 {
193 if (TypeExpression != null)
194 {
196 {
197 yield return typeExpr;
198 }
199 }
200
201 foreach (BadFunctionParameter parameter in m_Parameters)
202 {
203 if (parameter.TypeExpr == null)
204 {
205 continue;
206 }
207
208 foreach (BadExpression typeExpr in parameter.TypeExpr.GetDescendantsAndSelf())
209 {
210 yield return typeExpr;
211 }
212 }
213
214 foreach (BadExpression descendant in m_Body.SelectMany(expression => expression.GetDescendantsAndSelf()))
215 {
216 yield return descendant;
217 }
218 }
219
221 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
222 {
224
225 if (TypeExpression != null)
226 {
228
229 foreach (BadObject o in TypeExpression.Execute(context))
230 {
231 obj = o;
232
233 yield return o;
234 }
235
236 obj = obj.Dereference(Position);
237
238 if (obj is not BadClassPrototype proto)
239 {
240 throw new BadRuntimeException($"Expected class prototype, but got {obj.GetType().Name}",
242 );
243 }
244
245 type = proto;
246 }
247
249 Name,
250 m_Body,
251 m_Parameters.Select(x => x.Initialize(context))
252 .ToArray(),
253 Position,
255 IsStatic,
257 type,
259 );
260
261 BadFunction fFinal = CompileLevel switch
262 {
263 BadFunctionCompileLevel.Compiled => BadCompilerApi.CompileFunction(f, true),
264 BadFunctionCompileLevel.CompiledFast => BadCompilerApi.CompileFunction(f, false),
265 _ => f,
266 };
267
268 if (Name != null)
269 {
270 List<BadObject>? attributes = new List<BadObject>();
271
272 foreach (BadObject? o in ComputeAttributes(context, attributes))
273 {
274 yield return o;
275 }
276
278 fFinal,
279 null,
280 new BadPropertyInfo(fFinal.GetPrototype()),
281 attributes.ToArray()
282 );
283 }
284 else
285 {
286 if (Attributes.Any())
287 {
288 throw new BadRuntimeException("Anonymous functions cannot have attributes",
290 );
291 }
292 }
293
294 yield return fFinal;
295 }
296}
Describes a specific position inside a source file.
static BadExpression Optimize(BadExpression expr)
Optimizes the given expression.
Implements a Meta Data container for an expression.
Base Implementation for all Expressions used inside the Script.
IEnumerable< BadObject > ComputeAttributes(BadExecutionContext ctx, List< BadObject > attributes)
IEnumerable< BadExpression > GetDescendantsAndSelf()
Returns all Descendants of the Expression and the Expression itself.
IEnumerable< BadExpression > Attributes
BadSourcePosition Position
The source Position of the Expression.
IEnumerable< BadObject > Execute(BadExecutionContext context)
Evaluates the Expression within the current Execution Context.
BadFunctionExpression(BadWordToken? name, List< BadFunctionParameter > parameter, List< BadExpression > block, BadSourcePosition position, bool isConstant, BadMetaData? metaData, bool isSingleLine, bool isStatic, BadFunctionCompileLevel compileLevel=BadFunctionCompileLevel.None, BadExpression? typeExpr=null)
Constructor of the Function Expression.
BadFunctionCompileLevel CompileLevel
The Compile Level of the Function.
IEnumerable< BadFunctionParameter > Parameters
The Function Parameters.
BadExpression? TypeExpression
The (optional) Type Expression that is used to type-check the return value.
override IEnumerable< BadObject > InnerExecute(BadExecutionContext context)
bool IsSingleLine
Indicates if the function is a single line function(e.g. a lambda expression)?
string? GetName()
Returns the Name of the Expression.The Name of the Expression
readonly? BadMetaData m_MetaData
The Meta data of the Function.
readonly List< BadFunctionParameter > m_Parameters
The Function parameters.
readonly List< BadExpression > m_Body
The Function Body.
void SetCompileLevel(BadFunctionCompileLevel level)
Sets the Compile Level of the Function.
override void Optimize()
Uses the Constant Folding Optimizer to optimize the expression.
void SetBody(IEnumerable< BadExpression > body)
Sets the Body of the Function.
bool IsConstantFunction
Indicates if this function can not be overwritten by another object.
IEnumerable< BadExpression > Body
The Function Body.
string Text
The Text Representation of the Token.
Definition BadToken.cs:27
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
void DefineVariable(string name, BadObject value, BadScope? caller=null, BadPropertyInfo? info=null, BadObject[]? attributes=null)
Defines a new Variable in the current scope.
Definition BadScope.cs:784
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
Stores Meta Information about a Property.
The Expression Function Implementation used if a function gets defined in the Source Code.
Implements a function that can be called from the script.
override BadClassPrototype GetPrototype()
BadExpression? TypeExpr
The Expression that returns the type of the parameter if evaluated.
The Any Prototype, Base type for all types.
static readonly BadAnyPrototype Instance
The Instance of the BadAnyPrototype.
Implements a Class Prototype for the BadScript Language.
static BadCompiledFunction CompileFunction(BadExpressionFunction func, bool useOverride)
Compiles a Function.
Gets inherited by all Expressions that have a Name(e.g. Variable Definitions, Function Definitions,...
Contains Shared Data Structures and Functionality.
Contains the BadScript2 Constant Folding Optimizations.
Contains the Function Expressions for the BadScript2 Language.
BadFunctionCompileLevel
The BadFunctionCompileLevel enum defines the different levels of compilation for a function.
Contains the Reader Tokens for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains Runtime Function Objects.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Compiler for the BadVirtualMachine.
Contains the Runtime Implementation.