BadScript 2
Loading...
Searching...
No Matches
BadUsingExpression.cs
Go to the documentation of this file.
7
9
14{
18 private readonly BadExpression[] m_Expressions;
19
23 public readonly string Name;
24
29
36 public BadUsingExpression(string name, BadExpression[] expressions, BadSourcePosition position, BadExpression definition) : base(false, position)
37 {
38 Name = name;
39 m_Expressions = expressions;
40 m_Definition = definition;
41 }
42
46 public IEnumerable<BadExpression> Expressions => m_Expressions;
47
52
54 public override void Optimize()
55 {
57 for (int i = 0; i < m_Expressions.Length; i++)
58 {
60 }
61 }
62
64 public override IEnumerable<BadExpression> GetDescendants()
65 {
66 return m_Expressions.SelectMany(expr => expr.GetDescendantsAndSelf());
67 }
68
70 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
71 {
72 using BadExecutionContext usingContext = new BadExecutionContext(
73 context.Scope.CreateChild("UsingBlock", context.Scope, null)
74 );
75
76
77 foreach (BadObject o in usingContext.Execute(m_Definition))
78 {
79 yield return o;
80 }
81
82 if (m_Expressions.Length != 0)
83 {
84 foreach (BadObject o in usingContext.Execute(m_Expressions))
85 {
86 yield return o;
87 }
88 }
89
90 // ReSharper disable once AccessToDisposedClosure
91 usingContext.Scope.AddFinalizer(() => Finalize(usingContext, Name, Position));
92
93 yield return BadObject.Null;
94 }
95
103 public static void Finalize(BadExecutionContext usingContext, string name, BadSourcePosition position)
104 {
105 BadObject obj = usingContext.Scope.GetVariable(name).Dereference();
106
107 if (!obj.HasProperty("Dispose"))
108 {
109 throw BadRuntimeException.Create(usingContext.Scope, "Object does not implement IDisposable", position);
110 }
111
112 BadObject disposeFunc = obj.GetProperty("Dispose", usingContext.Scope).Dereference();
113 foreach (BadObject? o in BadInvocationExpression.Invoke(disposeFunc, Array.Empty<BadObject>(), position, usingContext))
114 {
115 //Do Nothing
116 }
117 }
118}
Describes a specific position inside a source file.
static BadExpression Optimize(BadExpression expr)
Optimizes the given expression.
Base Implementation for all Expressions used inside the Script.
BadSourcePosition Position
The source Position of the Expression.
BadUsingExpression(string name, BadExpression[] expressions, BadSourcePosition position, BadExpression definition)
Creates a new Using Expression.
BadExpression Definition
The definition of the object.
IEnumerable< BadExpression > Expressions
Creates a new Using Expression.
override IEnumerable< BadExpression > GetDescendants()
override void Optimize()
Uses the Constant Folding Optimizer to optimize the expression.
static void Finalize(BadExecutionContext usingContext, string name, BadSourcePosition position)
The Finalizer method of the Using Expression.
override IEnumerable< BadObject > InnerExecute(BadExecutionContext context)
readonly string Name
The name of the variable that holds the object.
BadExpression m_Definition
The definition of the object.
readonly BadExpression[] m_Expressions
The Expressions inside the Using Block.
static IEnumerable< BadObject > Invoke(BadObject left, IEnumerable< BadObject > args, BadSourcePosition position, BadExecutionContext context)
Invokes a function.
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
BadScope CreateChild(string name, BadScope? caller, bool? useVisibility, BadScopeFlags flags=BadScopeFlags.RootScope)
Creates a subscope of the current scope.
Definition BadScope.cs:792
BadObjectReference GetVariable(string name, BadScope caller)
Returns the variable reference of the specified variable.
Definition BadScope.cs:1018
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:129
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:118
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Contains Shared Data Structures and Functionality.
Contains the BadScript2 Constant Folding Optimizations.
Contains the Block Expressions for the BadScript2 Language.
Contains the Function 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.