6using System.Threading.Tasks;
29 protected Dictionary<string, Variable>
variables =
new Dictionary<string, Variable>();
31 private CancellationTokenSource cancellation =
new CancellationTokenSource();
32 private Stack<Dictionary<string, Variable>> stack =
null;
33 private TextWriter consoleOut =
null;
35 private volatile bool active =
true;
62 if (this.variables.TryGetValue(Name, out
Variable))
66 if (!(this.contextVariables is
null))
86 if (this.variables.ContainsKey(Name))
90 if (!(this.contextVariables is
null))
104 public object this[
string Name]
109 return v.ValueObject;
116 this.
Add(Name, value);
132 lock (this.variables)
134 if (this.variables.TryGetValue(Name, out Result))
137 this.variables[Name] = Result =
new Variable(Name, Value);
151 public virtual bool Remove(
string VariableName)
155 lock (this.variables)
157 return this.variables.Remove(VariableName);
171 lock (this.variables)
173 this.variables.Clear();
188 if (this.stack is
null)
189 this.stack =
new Stack<Dictionary<string, Variable>>();
191 this.stack.Push(this.variables);
193 Dictionary<string, Variable> Clone =
new Dictionary<string, Variable>();
194 foreach (KeyValuePair<string, Variable> P
in this.variables)
195 Clone[P.Key] =
new Variable(P.Key, P.Value.ValueElement);
197 this.variables = Clone;
210 if (this.stack is
null)
213 this.variables = this.stack.Pop();
224 get => this.consoleOut;
225 set => this.consoleOut = value;
233 get => this.contextVariables;
234 set => this.contextVariables = value;
244 set => this.printer = value;
258 lock (this.variables)
261 this.variables.Values.CopyTo(
Variables, 0);
280 private class VariableEnumerator : IEnumerator<Variable>
282 private readonly
Variable[] variables;
283 private int pos = -1;
294 return this.variables[this.pos];
298 object IEnumerator.Current
302 return this.variables[this.pos];
306 public void Dispose()
310 public bool MoveNext()
312 return ++this.pos < this.variables.Length;
325 IEnumerator IEnumerable.GetEnumerator()
354 this.cancellation.Cancel();
366 this.cancellation =
new CancellationTokenSource();
383 public event EventHandlerAsync<PreviewEventArgs>
OnPreview;
392 if (!(this.OnPreview is
null))
395 if (this.contextVariables is
Variables v)
396 return v.HandlesPreview;
409 EventHandlerAsync<PreviewEventArgs> h = this.
OnPreview;
414 if (this.contextVariables is
Variables v)
426 EventHandlerAsync<StatusEventArgs> h = this.
OnStatus;
439 public event EventHandlerAsync<StatusEventArgs>
OnStatus =
null;
Exception thrown when a script has been aborted.
Base class for script exceptions.
Class managing a script expression.
Event arguments for preview events.
Event arguments for status events.
Contains information about a variable.
void SetValue(object Value)
Sets the value of the variable.
string Name
Name of variable.
TextWriter ConsoleOut
Console out interface. Can be used by functions and script to output data to the console.
virtual void Push()
Pushes the current set of variables to the stack. This state is restored by calling Pop....
virtual void Pop()
Pops a previously stored set of variables from the stack. Variables are stored on the stack by callin...
async Task Status(Expression Expression, string Result)
Reports current status of execution.
IContextVariables ContextVariables
Variables available during the current context.
EventHandlerAsync< StatusEventArgs > OnStatus
Event raised when a status message has been reported.
bool HandlesPreview
If previews are desired.
bool Aborted
If the script has been aborted.
bool HandlesStatus
If status messages are desired.
void CopyTo(Variables Variables)
Copies available variables to another variable collection.
void CancelAbort()
Allows new script to be evaluated using this collection of variables.
async Task Preview(Expression Expression, IElement Result)
Reports a preview of the final result.
virtual Variable Add(string Name, object Value)
Adds a variable to the collection.
virtual void Clear()
Removes all variables from the collection.
ValuePrinter Printer
Delegate that converts values to strings for (implicit) printing. Default is null,...
virtual bool ContainsVariable(string Name)
If the collection contains a variable with a given name.
Dictionary< string, Variable > variables
Internal set of variables.
EventHandlerAsync< PreviewEventArgs > OnPreview
Event raised when there is a new value to preview.
Variables(params Variable[] Variables)
Collection of variables.
Variable[] AvailableVariables
Returns an array of available variables.
CancellationToken CancellationToken
Cancellation token, that can be used to monitor for script abortion.
virtual bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
virtual bool Remove(string VariableName)
Removes a varaiable from the collection.
void Abort()
Aborts the execution of script using this collection of variables.
bool Active
If script is still active (i.e. has not been aborted).
IEnumerator< Variable > GetEnumerator()
Returns an enumerator that iterates through a collection.
Basic interface for all types of elements.
Variables available in a specific context.
bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
bool ContainsVariable(string Name)
If the collection contains a variable with a given name.
delegate Task< string > ValuePrinter(object Value, Variables Variables)
Converts a value to a printable string.