-
-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathCycleStatement.cs
More file actions
42 lines (31 loc) · 1.3 KB
/
CycleStatement.cs
File metadata and controls
42 lines (31 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using Fluid.Values;
using System.Text.Encodings.Web;
namespace Fluid.Ast
{
public sealed class CycleStatement : Statement
{
public IReadOnlyList<Expression> Values;
public Expression Group { get; }
public CycleStatement(Expression group, IReadOnlyList<Expression> values)
{
Group = group;
Values = values;
}
public override async ValueTask<Completion> WriteToAsync(TextWriter writer, TextEncoder encoder, TemplateContext context)
{
context.IncrementSteps();
var groupValue = Group == null ? "$cycle_" : "$cycle_" + (await Group.EvaluateAsync(context)).ToStringValue();
var currentValue = context.GetValue(groupValue);
if (currentValue.IsNil())
{
currentValue = NumberValue.Zero;
}
var index = (uint)currentValue.ToNumberValue() % Values.Count;
var value = await Values[(int)index].EvaluateAsync(context);
context.SetValue(groupValue, NumberValue.Create(index + 1));
await value.WriteToAsync(writer, encoder, context.CultureInfo);
return Completion.Normal;
}
protected internal override Statement Accept(AstVisitor visitor) => visitor.VisitCycleStatement(this);
}
}