BadScript 2
Loading...
Searching...
No Matches
BadArrayAccessExpression.cs
Go to the documentation of this file.
7
12
18{
22 private readonly BadExpression[] m_Arguments;
23
32 BadExpression left,
33 BadExpression[] args,
34 BadSourcePosition position,
35 bool nullChecked = false) : base(
36 false,
37 position
38 )
39 {
40 Left = left;
41 m_Arguments = args;
42 NullChecked = nullChecked;
43 }
44
48 public int ArgumentCount => m_Arguments.Length;
49
53 public IEnumerable<BadExpression> Arguments => m_Arguments;
54
58 public BadExpression Left { get; private set; }
59
63 public bool NullChecked { get; }
64
66 public override IEnumerable<BadExpression> GetDescendants()
67 {
68 foreach (BadExpression expression in Left.GetDescendantsAndSelf())
69 {
70 yield return expression;
71 }
72
73 foreach (BadExpression expression in m_Arguments)
74 {
75 foreach (BadExpression descendant in expression.GetDescendantsAndSelf())
76 {
77 yield return descendant;
78 }
79 }
80 }
81
83 public override void Optimize()
84 {
86
87 for (int i = 0; i < m_Arguments.Length; i++)
88 {
90 }
91 }
92
102 public static IEnumerable<BadObject> Access(
103 BadExecutionContext? context,
104 BadObject left,
105 IEnumerable<BadObject> args,
106 BadSourcePosition position)
107 {
109 {
110 if (left.GetProperty(BadStaticKeys.ARRAY_ACCESS_OPERATOR_NAME, context?.Scope).Dereference() is not BadFunction func)
111 {
112 throw new BadRuntimeException("Array access operator is not a function", position);
113 }
114
116
117 foreach (BadObject o in func.Invoke(args.ToArray(), context!))
118 {
119 yield return o;
120
121 r = o;
122 }
123
124 yield return r;
125 }
126 else
127 {
128 throw new BadRuntimeException("Array access operator is not defined", position);
129 }
130 }
131
132
134 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
135 {
136 BadObject left = BadObject.Null;
137
138 foreach (BadObject o in Left.Execute(context))
139 {
140 left = o;
141
142 yield return o;
143 }
144
145 left = left.Dereference();
146
147 if (NullChecked && left == BadObject.Null)
148 {
149 yield return left;
150
151 yield break;
152 }
153
154 List<BadObject> args = new List<BadObject>();
155
156 foreach (BadExpression argExpr in m_Arguments)
157 {
158 BadObject argObj = BadObject.Null;
159
160 foreach (BadObject arg in argExpr.Execute(context))
161 {
162 argObj = arg;
163
164 yield return arg;
165 }
166
167 args.Add(argObj.Dereference());
168 }
169
170 foreach (BadObject o in Access(context, left, args.ToArray(), Position))
171 {
172 yield return o;
173 }
174 }
175}
Describes a specific position inside a source file.
Contains Static Data for the BadScript Language.
static BadExpression Optimize(BadExpression expr)
Optimizes the given expression.
Implements the Array Access to set or get properties from an object. LEFT[RIGHT].
IEnumerable< BadExpression > Arguments
The Arguments of the Array Access.
override void Optimize()
Uses the Constant Folding Optimizer to optimize the expression.
readonly BadExpression[] m_Arguments
Arguments of the array access.
BadArrayAccessExpression(BadExpression left, BadExpression[] args, BadSourcePosition position, bool nullChecked=false)
Constructor of the Array Access Expression.
static IEnumerable< BadObject > Access(BadExecutionContext? context, BadObject left, IEnumerable< BadObject > args, BadSourcePosition position)
Executes the Array Access Expression.
override IEnumerable< BadObject > InnerExecute(BadExecutionContext context)
bool NullChecked
Indicates if the expression will be null-checked by the runtime.
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.
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 Error Objects for the BadScript2 Language.
Contains Runtime Function Objects.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.