BadScript 2
Loading...
Searching...
No Matches
BadExpressionFunction.cs
Go to the documentation of this file.
6
11
16{
20 private readonly List<BadExpression> m_Body;
21
25 public readonly BadScope ParentScope;
26
30 public readonly BadSourcePosition Position;
31
35 private string? m_FuncString;
36
50 public BadExpressionFunction(BadScope parentScope,
51 BadWordToken? name,
52 List<BadExpression> expressions,
53 BadFunctionParameter[] parameters,
54 BadSourcePosition position,
55 bool isConstant,
56 bool isStatic,
57 BadMetaData? metaData,
58 BadClassPrototype returnType,
59 bool isSingleLine) : base(name,
60 isConstant,
61 isStatic,
62 returnType,
63 isSingleLine,
64 parameters
65 )
66 {
67 m_Body = expressions;
68 Position = position;
69 ParentScope = parentScope;
70 MetaData = metaData ?? BadMetaData.Empty;
71 }
72
76 public IEnumerable<BadExpression> Body => m_Body;
77
78
80 public override BadMetaData MetaData { get; }
81
83 public override BadFunction BindParentScope(BadScope scope)
84 {
85 return new BadExpressionFunction(scope,
86 Name,
87 m_Body,
95 );
96 }
97
99 protected override IEnumerable<BadObject> InvokeBlock(BadObject[] args, BadExecutionContext caller)
100 {
102 caller.Scope,
103 null,
104 BadScopeFlags.Returnable |
105 BadScopeFlags.AllowThrow |
106 BadScopeFlags.CaptureThrow
107 )
108 );
109
110 ctx.Scope.FunctionObject = this;
111
112 ApplyParameters(ctx, args, Position);
113
114 if (m_Body.Count == 0)
115 {
116 yield return Null;
117
118 yield break;
119 }
120
121 foreach (BadObject o in ctx.Execute(Body))
122 {
123 yield return o;
124 }
125
126 if (ctx.Scope.ReturnValue != null)
127 {
128 yield return ctx.Scope.ReturnValue;
129 }
130 else
131 {
132 yield return Null;
133 }
134 }
135
137 public override string ToString()
138 {
139 if (m_FuncString == null)
140 {
141 m_FuncString = base.ToString() + " at " + Position.GetPositionInfo();
142 }
143
144 return m_FuncString;
145 }
146}
Describes a specific position inside a source file.
string GetPositionInfo()
Returns position info. Format: file://[FileName] : Line [Line].
Implements a Meta Data container for an expression.
static readonly BadMetaData Empty
An empty Meta Data object.
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
Implements the Scope for the Script Engine.
Definition BadScope.cs:16
BadScope CreateChild(string name, BadScope? caller, bool? useVisibility, BadScopeFlags flags=BadScopeFlags.RootScope)
Creates a subscope of the current scope.
Definition BadScope.cs:597
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
The Expression Function Implementation used if a function gets defined in the Source Code.
readonly BadSourcePosition Position
The Source Position of the Function.
BadExpressionFunction(BadScope parentScope, BadWordToken? name, List< BadExpression > expressions, BadFunctionParameter[] parameters, BadSourcePosition position, bool isConstant, bool isStatic, BadMetaData? metaData, BadClassPrototype returnType, bool isSingleLine)
Creates a new Expression Function.
readonly List< BadExpression > m_Body
The Function Body.
override BadFunction BindParentScope(BadScope scope)
Returns an instance that is bound to the given scope.Function Instance
readonly BadScope ParentScope
The Scope the function is defined in.
override IEnumerable< BadObject > InvokeBlock(BadObject[] args, BadExecutionContext caller)
IEnumerable< BadExpression > Body
Enumeration of all expressions in the function body.
Implements a function that can be called from the script.
BadFunctionParameter[] Parameters
The Function Parameters.
bool IsSingleLine
Indicates if the function is a single line function(without block) Is used to determine the behaviour...
BadWordToken? Name
(optional) Name of the Function
static void ApplyParameters(string funcStr, BadFunctionParameter[] parameters, BadExecutionContext context, BadObject[] args, BadSourcePosition? position=null)
Applies the function arguments to the context of the function.
bool IsConstant
Indicates if the function has no side effects and the result can be cached.
bool IsStatic
Indicates if the Function is static.
BadClassPrototype ReturnType
The Return Type of the Function.
Implements a Class Prototype for the BadScript Language.
Contains Shared Data Structures and Functionality.
Contains the Expressions for the BadScript2 Language.
Contains the Parser for the BadScript2 Language.
Contains the Reader Tokens for the BadScript2 Language.
Contains Runtime Function Objects.
BadScopeFlags
Defines Different Behaviours for the Current Scope.