BadScript 2
Loading...
Searching...
No Matches
BadVariableExpression.cs
Go to the documentation of this file.
6
8
13{
19 public BadVariableExpression(string name, BadSourcePosition position, params BadExpression[] genericParameters) :
20 base(false, position)
21 {
22 Name = name;
23 GenericParameters = genericParameters;
24 }
25
29 public string Name { get; }
30
31 public IReadOnlyList<BadExpression> GenericParameters { get; }
32
33#region IBadNamedExpression Members
34
36 public string? GetName()
37 {
38 return Name;
39 }
40
41#endregion
42
47 public override string ToString()
48 {
49 return Name;
50 }
51
53 public override IEnumerable<BadExpression> GetDescendants()
54 {
55 yield break;
56 }
57
59 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
60 {
61 if (!context.Scope.HasVariable(Name, context.Scope))
62 {
63 throw BadRuntimeException.Create(context.Scope, $"Variable '{Name}' is not defined", Position);
64 }
65
66 BadObjectReference obj = context.Scope.GetVariable(Name, context.Scope);
67
68 if (GenericParameters.Count != 0)
69 {
70 if (obj.Dereference(Position) is not IBadGenericObject genType)
71 {
72 throw BadRuntimeException.Create(context.Scope, "Type is not generic", Position);
73 }
74
75 BadObject[] genParams = new BadObject[GenericParameters.Count];
76
77 for (int i = 0; i < GenericParameters.Count; i++)
78 {
79 foreach (BadObject? o in GenericParameters[i]
80 .Execute(context))
81 {
82 genParams[i] = o;
83 }
84
85 genParams[i] = genParams[i]
86 .Dereference(Position);
87 }
88
89 yield return genType.CreateGeneric(genParams);
90 }
91 else
92 {
93 yield return obj;
94 }
95 }
96}
Describes a specific position inside a source file.
Base Implementation for all Expressions used inside the Script.
BadSourcePosition Position
The source Position of the Expression.
IEnumerable< BadObject > Execute(BadExecutionContext context)
Evaluates the Expression within the current Execution Context.
string? GetName()
Returns the Name of the Expression.The Name of the Expression
BadVariableExpression(string name, BadSourcePosition position, params BadExpression[] genericParameters)
Constructor of the Variable Expression.
override IEnumerable< BadObject > InnerExecute(BadExecutionContext context)
override string ToString()
Returns the String representation of the Variable Expression.
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
BadObjectReference GetVariable(string name, BadScope caller)
Returns the variable reference of the specified variable.
Definition BadScope.cs:878
bool HasVariable(string name, BadScope caller)
returns true if the specified variable is defined in the current scope or any parent scope
Definition BadScope.cs:936
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
Implements the base functionality for a BadScript Reference.
Gets inherited by all Expressions that have a Name(e.g. Variable Definitions, Function Definitions,...
Contains Shared Data Structures and Functionality.
Contains the Variable Expressions for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.