BadScript 2
Loading...
Searching...
No Matches
BadMemberAccessExpression.cs
Go to the documentation of this file.
8
10
16{
28 BadWordToken right,
29 BadSourcePosition position,
30 List<BadExpression> genericArguments,
31 bool nullChecked = false) : base(false,
32 position
33 )
34 {
35 Left = left;
36 Right = right;
37 GenericArguments = genericArguments;
38 NullChecked = nullChecked;
39 }
40
41 public IReadOnlyList<BadExpression> GenericArguments { get; }
42
46 public BadExpression Left { get; private set; }
47
51 public BadWordToken Right { get; }
52
53#region IBadAccessExpression Members
54
58 public bool NullChecked { get; }
59
60#endregion
61
62 public override IEnumerable<BadExpression> GetDescendants()
63 {
65 }
66
67 public override void Optimize()
68 {
70 }
71
72 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
73 {
75
76 foreach (BadObject o in Left.Execute(context))
77 {
78 left = o;
79
80 yield return o;
81 }
82
83 left = left.Dereference(Position);
84
85 if (NullChecked && !left.HasProperty(Right.Text, context.Scope))
86 {
87 yield return BadObject.Null;
88 }
89 else
90 {
91 BadObject ret = left.GetProperty(Right.Text, context.Scope);
92
93 if (GenericArguments.Count != 0)
94 {
95 ret = ret.Dereference(Position);
96
97 if (ret is not IBadGenericObject genType)
98 {
99 throw BadRuntimeException.Create(context.Scope,
100 $"Object {ret} does not support generic arguments",
102 );
103 }
104
105 BadObject[] genParams = new BadObject[GenericArguments.Count];
106
107 for (int i = 0; i < GenericArguments.Count; i++)
108 {
109 foreach (BadObject? o in GenericArguments[i]
110 .Execute(context))
111 {
112 genParams[i] = o;
113 }
114
115 genParams[i] = genParams[i]
116 .Dereference(Position);
117 }
118
119 yield return genType.CreateGeneric(genParams);
120 }
121 else
122 {
123 yield return ret;
124 }
125 }
126 }
127
128
129 public override string ToString()
130 {
131 return $"({Left}{(NullChecked ? "?" : "")}.{Right})";
132 }
133}
Describes a specific position inside a source file.
static BadExpression Optimize(BadExpression expr)
Optimizes the given expression.
Implements the Member Access to set or get properties from an object. LEFT.RIGHT.
override IEnumerable< BadObject > InnerExecute(BadExecutionContext context)
override void Optimize()
Uses the Constant Folding Optimizer to optimize the expression.
bool NullChecked
Property that indicates if the result of the left side of the expression should be null-checked.
BadMemberAccessExpression(BadExpression left, BadWordToken right, BadSourcePosition position, List< BadExpression > genericArguments, bool nullChecked=false)
Constructor for the Member Access 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.
string Text
The Text Representation of the Token.
Definition BadToken.cs:27
The Execution Context. Every execution of a script needs a context the script is running in....
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
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
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
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 Reader Tokens for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.