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) :
29 base(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 yield return arg;
102 }
103
104 args.Add(argObj.Dereference(argExpr.Position));
105 }
106 }
107
120 public static IEnumerable<BadObject> Invoke(BadObject left,
121 IEnumerable<BadObject> args,
122 BadSourcePosition position,
123 BadExecutionContext context)
124 {
125 if (left is BadFunction func)
126 {
127 foreach (BadObject o in func.Invoke(args.ToArray(), context))
128 {
129 yield return o;
130 }
131 }
133 {
135 .Dereference(position) is not BadFunction invocationOp)
136 {
137 throw new BadRuntimeException("Function Invocation Operator is not a function", position);
138 }
139
141
142 foreach (BadObject o in invocationOp.Invoke(args.ToArray(), context))
143 {
144 yield return o;
145
146 r = o;
147 }
148
149 yield return r.Dereference(position);
150 }
151 else
152 {
153 throw new BadRuntimeException("Cannot invoke non-function object",
154 position
155 );
156 }
157 }
158
160 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
161 {
162 BadObject left = BadObject.Null;
163
164 foreach (BadObject o in Left.Execute(context))
165 {
166 left = o;
167
168 yield return o;
169 }
170
171 left = left.Dereference(Position);
172
173 if (Left is IBadAccessExpression { NullChecked: true } && left.Equals(BadObject.Null))
174 {
175 yield return BadObject.Null;
176
177 yield break;
178 }
179
180 if (Left is BadVariableExpression vExpr &&
181 vExpr.Name == BadStaticKeys.BASE_KEY &&
182 context.Scope.FunctionObject?.Name?.Text == BadStaticKeys.CONSTRUCTOR_NAME)
183 {
184 //Invoke Base Constructor
185 left = left.GetProperty(BadStaticKeys.CONSTRUCTOR_NAME)
186 .Dereference(Position);
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 foreach (BadObject? o in Invoke(left, args.ToArray(), Position, context))
197 {
198 yield return o;
199 }
200 }
201
203 public override string ToString()
204 {
205 return $"({Left}({string.Join(", ", m_Arguments.Select(x => x.ToString()))}))";
206 }
207}
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:141
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:130
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.