BadScript 2
Loading...
Searching...
No Matches
BadVariableDefinitionExpression.cs
Go to the documentation of this file.
7
9
11{
12
13 public BadWordToken Name { get; }
14 public bool IsReadOnly { get; }
18
20 BadWordToken name,
21 BadSourcePosition position,
22 BadExpression getExpression,
23 BadExpression? typeExpression = null,
24 BadExpression? setExpression = null,
25 bool isReadOnly = false) : base(false, position)
26 {
27 Name = name;
28 IsReadOnly = isReadOnly;
29 TypeExpression = typeExpression;
30 GetExpression = getExpression;
31 SetExpression = setExpression;
32 }
33
34 public override IEnumerable<BadExpression> GetDescendants()
35 {
36 yield return GetExpression;
37 if (TypeExpression != null)
38 {
39 yield return TypeExpression;
40 }
41 if (SetExpression != null)
42 {
43 yield return SetExpression;
44 }
45 }
46 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
47 {
48 if (context.Scope.ClassObject == null)
49 {
50 throw BadRuntimeException.Create(context.Scope, "Can only define properties in class scope", Position);
51 }
53
54 if (TypeExpression != null)
55 {
57
58 foreach (BadObject o in TypeExpression.Execute(context))
59 {
60 obj = o;
61
62 yield return o;
63 }
64
65 obj = obj.Dereference();
66
67 if (obj is not BadClassPrototype proto)
68 {
69 throw new BadRuntimeException("Type expression must be a class prototype", Position);
70 }
71
72 type = proto;
73 }
74
75 List<BadObject> attributes = new List<BadObject>();
76 foreach (var o in ComputeAttributes(context, attributes))
77 {
78 yield return o;
79 }
80
81 context.Scope.DefineProperty(Name.Text, type, GetExpression, SetExpression, context, attributes.ToArray());
82 }
83
84}
89{
98 string name,
99 BadSourcePosition position,
100 BadExpression? typeExpression = null,
101 bool isReadOnly = false) : base(
102 name,
103 position,
104 Array.Empty<BadExpression>()
105 )
106 {
107 IsReadOnly = isReadOnly;
108 TypeExpression = typeExpression;
109 }
110
114 public bool IsReadOnly { get; }
115
120
125 public override string ToString()
126 {
127 return IsReadOnly ? $"{BadStaticKeys.CONSTANT_DEFINITION_KEY} {Name}" : $"{BadStaticKeys.VARIABLE_DEFINITION_KEY} {Name}";
128 }
129
131 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
132 {
134
135 if (TypeExpression != null)
136 {
138
139 foreach (BadObject o in TypeExpression.Execute(context))
140 {
141 obj = o;
142
143 yield return o;
144 }
145
146 obj = obj.Dereference();
147
148 if (obj is not BadClassPrototype proto)
149 {
150 throw BadRuntimeException.Create(context.Scope,"Type expression must be a class prototype", Position);
151 }
152
153 type = proto;
154 }
155
156 if (type == BadVoidPrototype.Instance)
157 {
158 throw BadRuntimeException.Create(context.Scope, "Cannot declare a variable of type 'void'", Position);
159 }
160
161 if (context.Scope.HasLocal(Name, context.Scope, false))
162 {
163 throw BadRuntimeException.Create(context.Scope, $"Variable '{Name}' is already defined", Position);
164 }
165 List<BadObject> attributes = new List<BadObject>();
166 foreach (var o in ComputeAttributes(context, attributes))
167 {
168 yield return o;
169 }
170 context.Scope.DefineVariable(Name, BadObject.Null, null, new BadPropertyInfo(type, IsReadOnly), attributes.ToArray());
171
172 foreach (BadObject o in base.InnerExecute(context))
173 {
174 yield return o;
175 }
176 }
177
179 public override IEnumerable<BadExpression> GetDescendants()
180 {
181 if (TypeExpression != null)
182 {
183 foreach (BadExpression typeExpression in TypeExpression.GetDescendantsAndSelf())
184 {
185 yield return typeExpression;
186 }
187 }
188
189 foreach (BadExpression descendant in base.GetDescendants())
190 {
191 yield return descendant;
192 }
193 }
194}
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:373
bool HasLocal(string name, BadScope caller, bool useExtensions=true)
returns true if the specified variable is defined in the current scope
Definition BadScope.cs:1061
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.