BadScript 2
Loading...
Searching...
No Matches
BadVariableDefinitionExpression.cs
Go to the documentation of this file.
7
9
11{
13 BadSourcePosition position,
14 BadExpression getExpression,
15 BadExpression? typeExpression = null,
16 BadExpression? setExpression = null,
17 bool isReadOnly = false) : base(false, position)
18 {
19 Name = name;
20 IsReadOnly = isReadOnly;
21 TypeExpression = typeExpression;
22 GetExpression = getExpression;
23 SetExpression = setExpression;
24 }
25
26 public BadWordToken Name { get; }
27
28 public bool IsReadOnly { get; }
29
31
33
35
36 public override IEnumerable<BadExpression> GetDescendants()
37 {
38 yield return GetExpression;
39
40 if (TypeExpression != null)
41 {
42 yield return TypeExpression;
43 }
44
45 if (SetExpression != null)
46 {
47 yield return SetExpression;
48 }
49 }
50
51 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
52 {
53 if (context.Scope.ClassObject == null)
54 {
55 throw BadRuntimeException.Create(context.Scope, "Can only define properties in class scope", Position);
56 }
57
59
60 if (TypeExpression != null)
61 {
63
64 foreach (BadObject o in TypeExpression.Execute(context))
65 {
66 obj = o;
67
68 yield return o;
69 }
70
71 obj = obj.Dereference(Position);
72
73 if (obj is not BadClassPrototype proto)
74 {
75 throw new BadRuntimeException("Type expression must be a class prototype", Position);
76 }
77
78 type = proto;
79 }
80
81 List<BadObject> attributes = new List<BadObject>();
82
83 foreach (BadObject? o in ComputeAttributes(context, attributes))
84 {
85 yield return o;
86 }
87
88 context.Scope.DefineProperty(Name.Text, type, GetExpression, SetExpression, context, attributes.ToArray());
89 }
90}
91
96{
105 BadSourcePosition position,
106 BadExpression? typeExpression = null,
107 bool isReadOnly = false) : base(name,
108 position,
109 Array.Empty<BadExpression>()
110 )
111 {
112 IsReadOnly = isReadOnly;
113 TypeExpression = typeExpression;
114 }
115
119 public bool IsReadOnly { get; }
120
125
130 public override string ToString()
131 {
132 return IsReadOnly
133 ? $"{BadStaticKeys.CONSTANT_DEFINITION_KEY} {Name}"
134 : $"{BadStaticKeys.VARIABLE_DEFINITION_KEY} {Name}";
135 }
136
138 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
139 {
141
142 if (TypeExpression != null)
143 {
145
146 foreach (BadObject o in TypeExpression.Execute(context))
147 {
148 obj = o;
149
150 yield return o;
151 }
152
153 obj = obj.Dereference(Position);
154
155 if (obj is not BadClassPrototype proto)
156 {
157 throw BadRuntimeException.Create(context.Scope, "Type expression must be a class prototype", Position);
158 }
159
160 type = proto;
161 }
162
163 if (type == BadVoidPrototype.Instance)
164 {
165 throw BadRuntimeException.Create(context.Scope, "Cannot declare a variable of type 'void'", Position);
166 }
167
168 if (context.Scope.HasLocal(Name, context.Scope, false))
169 {
170 throw BadRuntimeException.Create(context.Scope, $"Variable '{Name}' is already defined", Position);
171 }
172
173 List<BadObject> attributes = new List<BadObject>();
174
175 foreach (BadObject? o in ComputeAttributes(context, attributes))
176 {
177 yield return o;
178 }
179
180 context.Scope.DefineVariable(Name,
182 null,
183 new BadPropertyInfo(type, IsReadOnly),
184 attributes.ToArray()
185 );
186
187 foreach (BadObject o in base.InnerExecute(context))
188 {
189 yield return o;
190 }
191 }
192
194 public override IEnumerable<BadExpression> GetDescendants()
195 {
196 if (TypeExpression != null)
197 {
198 foreach (BadExpression typeExpression in TypeExpression.GetDescendantsAndSelf())
199 {
200 yield return typeExpression;
201 }
202 }
203
204 foreach (BadExpression descendant in base.GetDescendants())
205 {
206 yield return descendant;
207 }
208 }
209}
Describes a specific position inside a source file.
Base Implementation for all Expressions used inside the Script.
IEnumerable< BadObject > ComputeAttributes(BadExecutionContext ctx, List< BadObject > attributes)
IEnumerable< BadExpression > GetDescendantsAndSelf()
Returns all Descendants of the Expression and the Expression itself.
BadSourcePosition Position
The source Position of the Expression.
IEnumerable< BadExpression > GetDescendants()
Returns all Descendants of the Expression.
IEnumerable< BadObject > Execute(BadExecutionContext context)
Evaluates the Expression within the current Execution Context.
override IEnumerable< BadObject > InnerExecute(BadExecutionContext context)
BadPropertyDefinitionExpression(BadWordToken name, BadSourcePosition position, BadExpression getExpression, BadExpression? typeExpression=null, BadExpression? setExpression=null, bool isReadOnly=false)
override IEnumerable< BadObject > InnerExecute(BadExecutionContext context)
bool IsReadOnly
Indicates if the Variable will be declared as Read-Only.
BadVariableDefinitionExpression(string name, BadSourcePosition position, BadExpression? typeExpression=null, bool isReadOnly=false)
Constructor of the Variable Definition Expression.
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....
BadScope Scope
The Root Scope of the Context.
BadClass? ClassObject
The Class Object of the Scope.
Definition BadScope.cs:141
bool HasLocal(string name, BadScope caller, bool useExtensions=true)
returns true if the specified variable is defined in the current scope
Definition BadScope.cs:923
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Stores Meta Information about a Property.
The Any Prototype, Base type for all types.
static readonly BadAnyPrototype Instance
The Instance of the BadAnyPrototype.
Implements a Class Prototype for the BadScript Language.
The Void Prototype, can be assigned to nothing, can not be inherited from, can not be instantiated....
static BadVoidPrototype Instance
The Instance of the BadVoidPrototype.
Contains Shared Data Structures and Functionality.
Contains the Variable 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.