Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DecCounter.cs
1using System.Threading.Tasks;
6
8{
13 {
22 : base(new ScriptNode[] { Key }, argumentTypes1Scalar, Start, Length, Expression)
23 {
24 }
25
35 : base(new ScriptNode[] { Key, Amount }, argumentTypes2Scalar, Start, Length, Expression)
36 {
37 }
38
42 public override string FunctionName => nameof(DecCounter);
43
47 public override string[] DefaultArgumentNames => new string[] { "Key", "Amount" };
48
53 public override bool IsAsynchronous => true;
54
62 {
63 return this.EvaluateAsync(Arguments, Variables).Result;
64 }
65
72 public override async Task<IElement> EvaluateAsync(IElement[] Arguments, Variables Variables)
73 {
74 long Count;
75
76 if (!(Arguments[0].AssociatedObjectValue is string Key))
77 throw new ScriptRuntimeException("Key must be a string.", this);
78
79 if (Arguments.Length == 2)
80 {
81 double N = Expression.ToDouble(Arguments[1].AssociatedObjectValue);
82 long Amount = (long)N;
83
84 if (Amount != N)
85 throw new ScriptRuntimeException("Amounts must be integers.", this);
86
87 Count = await Runtime.Counters.RuntimeCounters.DecrementCounter(Key, Amount);
88 }
89 else
90 {
91 Count = await Runtime.Counters.RuntimeCounters.DecrementCounter(Key);
92 }
93
94 return new DoubleNumber(Count);
95 }
96 }
97}
Class managing a script expression.
Definition: Expression.cs:41
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:5110
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes2Scalar
Two scalar parameters.
static readonly ArgumentType[] argumentTypes1Scalar
One scalar parameter.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
DecCounter(ScriptNode Key, int Start, int Length, Expression Expression)
Decrements a counter
Definition: DecCounter.cs:21
override string[] DefaultArgumentNames
Default Argument names
Definition: DecCounter.cs:47
DecCounter(ScriptNode Key, ScriptNode Amount, int Start, int Length, Expression Expression)
Decrements a counter
Definition: DecCounter.cs:34
override async Task< IElement > EvaluateAsync(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: DecCounter.cs:72
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: DecCounter.cs:53
override string FunctionName
Name of the function
Definition: DecCounter.cs:42
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: DecCounter.cs:61
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:21