BadScript 2
Loading...
Searching...
No Matches
BadFunctionExpression.cs
Go to the documentation of this file.
1using System.Text;
2
12
14
15
20{
24 private readonly List<BadExpression> m_Body;
25
26
30 private readonly BadMetaData? m_MetaData;
31
35 private readonly List<BadFunctionParameter> m_Parameters;
36
51 BadWordToken? name,
52 List<BadFunctionParameter> parameter,
53 List<BadExpression> block,
54 BadSourcePosition position,
55 bool isConstant,
56 BadMetaData? metaData,
57 bool isSingleLine,
58 bool isStatic,
60 BadExpression? typeExpr = null) :
61 base(false, position)
62 {
63 Name = name;
64 m_Parameters = parameter;
65 m_Body = block;
66 TypeExpression = typeExpr;
67 IsConstantFunction = isConstant;
68 m_MetaData = metaData;
69 IsSingleLine = isSingleLine;
70 IsStatic = isStatic;
71 CompileLevel = compileLevel;
72 }
73
77 public bool IsSingleLine { get; }
78
82 public bool IsConstantFunction { get; }
83
87 public bool IsStatic { get; }
88
93
97 public IEnumerable<BadFunctionParameter> Parameters => m_Parameters;
98
102 public IEnumerable<BadExpression> Body => m_Body;
103
107 public BadWordToken? Name { get; private set; }
108
112 public BadFunctionCompileLevel CompileLevel { get; private set; }
113
114
116 public string? GetName()
117 {
118 return Name?.Text;
119 }
120
121 public void SetName(string name)
122 {
123 Name = name;
124 }
125
131 {
132 CompileLevel = level;
133 }
134
139 public void SetBody(IEnumerable<BadExpression> body)
140 {
141 m_Body.Clear();
142 m_Body.AddRange(body);
143 }
144
146 public override void Optimize()
147 {
148 for (int i = 0; i < m_Body.Count; i++)
149 {
151 }
152 }
153
158 public string GetHeader()
159 {
160 string level = CompileLevel switch
161 {
162 BadFunctionCompileLevel.Compiled => "compiled ",
163 BadFunctionCompileLevel.CompiledFast => "compiled fast",
164 _ => "",
165 };
166
167 return
168 $"{level}{BadStaticKeys.FUNCTION_KEY} {Name?.ToString() ?? "<anonymous>"}({string.Join(", ", Parameters.Cast<object>())})";
169 }
170
171
173 public override string ToString()
174 {
175 StringBuilder sb = new StringBuilder(GetHeader());
176 sb.AppendLine();
177 sb.AppendLine("{");
178
179 foreach (BadExpression expression in Body)
180 {
181 sb.AppendLine($"\t{expression}");
182 }
183
184 sb.AppendLine("}");
185
186 return sb.ToString();
187 }
188
190 public override IEnumerable<BadExpression> GetDescendants()
191 {
192 if (TypeExpression != null)
193 {
195 {
196 yield return typeExpr;
197 }
198 }
199
200 foreach (BadFunctionParameter parameter in m_Parameters)
201 {
202 if (parameter.TypeExpr == null)
203 {
204 continue;
205 }
206
207 foreach (BadExpression typeExpr in parameter.TypeExpr.GetDescendantsAndSelf())
208 {
209 yield return typeExpr;
210 }
211 }
212
213 foreach (BadExpression descendant in m_Body.SelectMany(expression => expression.GetDescendantsAndSelf()))
214 {
215 yield return descendant;
216 }
217 }
218
220 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
221 {
223
224 if (TypeExpression != null)
225 {
227
228 foreach (BadObject o in TypeExpression.Execute(context))
229 {
230 obj = o;
231 yield return o;
232 }
233
234 obj = obj.Dereference();
235
236 if (obj is not BadClassPrototype proto)
237 {
238 throw new BadRuntimeException(
239 $"Expected class prototype, but got {obj.GetType().Name}",
241 );
242 }
243
244 type = proto;
245 }
246
248 context.Scope,
249 Name,
250 m_Body,
251 m_Parameters.Select(x => x.Initialize(context)).ToArray(),
252 Position,
254 IsStatic,
256 type,
258 );
259
260 BadFunction fFinal = CompileLevel switch
261 {
262 BadFunctionCompileLevel.Compiled => BadCompilerApi.CompileFunction(f, true),
263 BadFunctionCompileLevel.CompiledFast => BadCompilerApi.CompileFunction(f, false),
264 _ => f,
265 };
266
267 if (Name != null)
268 {
269 var attributes = new List<BadObject>();
270 foreach (var o in ComputeAttributes(context, attributes))
271 {
272 yield return o;
273 }
274 context.Scope.DefineVariable(
275 Name.Text,
276 fFinal,
277 null,
278 new BadPropertyInfo(fFinal.GetPrototype()),
279 attributes.ToArray()
280 );
281 }
282 else
283 {
284 if (Attributes.Any())
285 {
286 throw new BadRuntimeException(
287 "Anonymous functions cannot have attributes",
289 );
290 }
291 }
292
293 yield return fFinal;
294 }
295}
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:929
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.