BadScript 2
Loading...
Searching...
No Matches
BadInvocationExpression.cs
Go to the documentation of this file.
9
11
16{
20 private readonly List<BadExpression> m_Arguments;
21
28 public BadInvocationExpression(BadExpression left, IEnumerable<BadExpression> args, BadSourcePosition position) : base(
29 false,
30 position
31 )
32 {
33 Left = left;
34 m_Arguments = args.ToList();
35 }
36
40 public int ArgumentCount => m_Arguments.Count;
41
45 public IEnumerable<BadExpression> Arguments => m_Arguments;
46
50 public BadExpression Left { get; }
51
56 public void SetArgs(IEnumerable<BadExpression> exprs)
57 {
58 m_Arguments.Clear();
59 m_Arguments.AddRange(exprs);
60 }
61
63 public override void Optimize()
64 {
65 for (int i = 0; i < m_Arguments.Count; i++)
66 {
68 }
69 }
70
72 public override IEnumerable<BadExpression> GetDescendants()
73 {
74 foreach (BadExpression left in Left.GetDescendantsAndSelf())
75 {
76 yield return left;
77 }
78
79 foreach (BadExpression arg in m_Arguments.SelectMany(argument => argument.GetDescendantsAndSelf()))
80 {
81 yield return arg;
82 }
83 }
84
91 public IEnumerable<BadObject> GetArgs(BadExecutionContext context, List<BadObject> args)
92 {
93 foreach (BadExpression argExpr in m_Arguments)
94 {
95 BadObject argObj = BadObject.Null;
96
97 foreach (BadObject arg in argExpr.Execute(context))
98 {
99 argObj = arg;
100
101
102 yield return arg;
103 }
104
105 args.Add(argObj.Dereference());
106 }
107 }
108
121 public static IEnumerable<BadObject> Invoke(
122 BadObject left,
123 IEnumerable<BadObject> args,
124 BadSourcePosition position,
125 BadExecutionContext context)
126 {
127 if (left is BadFunction func)
128 {
129 foreach (BadObject o in func.Invoke(args.ToArray(), context))
130 {
131 yield return o;
132 }
133 }
135 {
136 if (left.GetProperty(BadStaticKeys.INVOCATION_OPERATOR_NAME, context.Scope).Dereference() is not BadFunction invocationOp)
137 {
138 throw new BadRuntimeException("Function Invocation Operator is not a function", position);
139 }
140
142
143 foreach (BadObject o in invocationOp.Invoke(args.ToArray(), context))
144 {
145 yield return o;
146
147 r = o;
148 }
149
150 yield return r.Dereference();
151 }
152 else
153 {
154 throw new BadRuntimeException(
155 "Cannot invoke non-function object",
156 position
157 );
158 }
159 }
160
162 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
163 {
164 BadObject left = BadObject.Null;
165
166 foreach (BadObject o in Left.Execute(context))
167 {
168 left = o;
169
170 yield return o;
171 }
172
173
174 left = left.Dereference();
175
176 if (Left is IBadAccessExpression { NullChecked: true } && left.Equals(BadObject.Null))
177 {
178 yield return BadObject.Null;
179
180 yield break;
181 }
182
183 if (Left is BadVariableExpression vExpr && vExpr.Name == BadStaticKeys.BASE_KEY && context.Scope.FunctionObject?.Name?.Text == BadStaticKeys.CONSTRUCTOR_NAME)
184 {
185 //Invoke Base Constructor
186 left = left.GetProperty(BadStaticKeys.CONSTRUCTOR_NAME).Dereference();
187 }
188
189 List<BadObject> args = new List<BadObject>();
190
191 foreach (BadObject o in GetArgs(context, args))
192 {
193 yield return o;
194 }
195
196
197 foreach (BadObject? o in Invoke(left, args.ToArray(), Position, context))
198 {
199 yield return o;
200 }
201 }
202
204 public override string ToString()
205 {
206 return $"({Left}({string.Join(", ", m_Arguments.Select(x => x.ToString()))}))";
207 }
208}
Describes a specific position inside a source file.
Contains Static Data for the BadScript Language.
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.
BadSourcePosition Position
The source Position of the Expression.
IEnumerable< BadObject > Execute(BadExecutionContext context)
Evaluates the Expression within the current Execution Context.
readonly List< BadExpression > m_Arguments
The Invocation Arguments.
static IEnumerable< BadObject > Invoke(BadObject left, IEnumerable< BadObject > args, BadSourcePosition position, BadExecutionContext context)
Invokes a function.
override IEnumerable< BadObject > InnerExecute(BadExecutionContext context)
void SetArgs(IEnumerable< BadExpression > exprs)
Sets the arguments of the invocation.
IEnumerable< BadExpression > Arguments
The Arguments of the Invocation.
override void Optimize()
Uses the Constant Folding Optimizer to optimize the expression.
BadInvocationExpression(BadExpression left, IEnumerable< BadExpression > args, BadSourcePosition position)
Constructor of the Invocation Expression.
IEnumerable< BadObject > GetArgs(BadExecutionContext context, List< BadObject > args)
Returns the argument objects.
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
virtual BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.
Definition BadObject.cs:129
virtual bool HasProperty(string propName, BadScope? caller=null)
Returns true if the object contains a given property or there exists an extension for the current Ins...
Definition BadObject.cs:118
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Implements a function that can be called from the script.
Defines the interface for BadScript Access Expressions.
Contains Shared Data Structures and Functionality.
Contains the BadScript2 Constant Folding Optimizations.
Contains the Access Expressions for the BadScript2 Language.
Contains the Function Expressions for the BadScript2 Language.
Contains the Variable Expressions 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 Runtime Implementation.