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[] args,
33 BadSourcePosition position,
34 bool nullChecked = false) : base(false,
35 position
36 )
37 {
38 Left = left;
39 m_Arguments = args;
40 NullChecked = nullChecked;
41 }
42
46 public int ArgumentCount => m_Arguments.Length;
47
51 public IEnumerable<BadExpression> Arguments => m_Arguments;
52
56 public BadExpression Left { get; private set; }
57
58#region IBadAccessExpression Members
59
63 public bool NullChecked { get; }
64
65#endregion
66
68 public override IEnumerable<BadExpression> GetDescendants()
69 {
70 foreach (BadExpression expression in Left.GetDescendantsAndSelf())
71 {
72 yield return expression;
73 }
74
75 foreach (BadExpression expression in m_Arguments)
76 {
77 foreach (BadExpression descendant in expression.GetDescendantsAndSelf())
78 {
79 yield return descendant;
80 }
81 }
82 }
83
85 public override void Optimize()
86 {
88
89 for (int i = 0; i < m_Arguments.Length; i++)
90 {
92 }
93 }
94
104 public static IEnumerable<BadObject> Access(BadExecutionContext? context,
105 BadObject left,
106 IEnumerable<BadObject> args,
107 BadSourcePosition position)
108 {
110 {
112 .Dereference(position) is not BadFunction func)
113 {
114 throw new BadRuntimeException("Array access operator is not a function", position);
115 }
116
118
119 foreach (BadObject o in func.Invoke(args.ToArray(), context!))
120 {
121 yield return o;
122
123 r = o;
124 }
125
126 yield return r;
127 }
128 else
129 {
130 throw new BadRuntimeException("Array access operator is not defined", position);
131 }
132 }
133
134
136 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
137 {
138 BadObject left = BadObject.Null;
139
140 foreach (BadObject o in Left.Execute(context))
141 {
142 left = o;
143
144 yield return o;
145 }
146
147 left = left.Dereference(Position);
148
149 if (NullChecked && left == BadObject.Null)
150 {
151 yield return left;
152
153 yield break;
154 }
155
156 List<BadObject> args = new List<BadObject>();
157
158 foreach (BadExpression argExpr in m_Arguments)
159 {
160 BadObject argObj = BadObject.Null;
161
162 foreach (BadObject arg in argExpr.Execute(context))
163 {
164 argObj = arg;
165
166 yield return arg;
167 }
168
169 args.Add(argObj.Dereference(Position));
170 }
171
172 foreach (BadObject o in Access(context, left, args.ToArray(), Position))
173 {
174 yield return o;
175 }
176 }
177}
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: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 Error Objects for the BadScript2 Language.
Contains Runtime Function Objects.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.