Interface IRewriter<T>
A rewriter is an object which knows how to access the immediate children of a value of type T
.
Implementations should ensure that you always get the children you just set
(rewriter.GetChildren(rewriter.SetChildren(children, expr)) == children
),
and that successive sets overwrite the earlier operation
(rewriter.SetChildren(children2, rewriter.SetChildren(children1, expr)) == rewriter.SetChildren(children2, expr)
).
Declaration
public interface IRewriter<T>
Type Parameters
Name | Description |
---|---|
T |
The type for which the rewriter can get the immediate children |
Methods
CountChildren(T)
Count the immediate children of the value. CountChildren()
Declaration
int CountChildren(T value)
Parameters
Type | Name | Description |
---|---|---|
T |
value |
The value |
Returns
Type | Description |
---|---|
|
Examples
Given a representation of the expression (1+2)+3
,
Expr expr = new Add(
new Add(
new Lit(1),
new Lit(2)
),
new Lit(3)
);
CountChildren(T) counts the immediate children of the topmost (Add) node.
Assert.Equal(2, rewriter.CountChildren(expr));
GetChildren(Span<T>, T)
Copy the immediate children of the value into childrenReceiver
.
GetChildren(Span<T>)
Declaration
void GetChildren(Span<T> childrenReceiver, T value)
Parameters
Type | Name | Description |
---|---|---|
Span<T> |
childrenReceiver |
A Span<T> to copy |
T |
value |
The value |
Examples
Given a representation of the expression (1+2)+3
,
Expr expr = new Add(
new Add(
new Lit(1),
new Lit(2)
),
new Lit(3)
);
GetChildren(Span<T>, T) copies the immediate children of the topmost node into the span.
Expr[] expected = new[]
{
new Add(
new Lit(1),
new Lit(2)
),
new Lit(3)
};
var array = new Expr[rewriter.CountChildren(expr)];
rewriter.GetChildren(array, expr);
Assert.Equal(expected, array);
SetChildren(ReadOnlySpan<T>, T)
Set the immediate children of the value.
Callers should ensure that newChildren
contains the same number of children as was returned by
GetChildren(Span<T>, T).
Declaration
T SetChildren(ReadOnlySpan<T> newChildren, T value)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<T> |
newChildren |
The new children |
T |
value |
The old value, whose immediate children should be replaced |
Returns
Type | Description |
---|---|
T |
A copy of |
Examples
Given a representation of the expression (1+2)+3
,
Expr expr = new Add(
new Add(
new Lit(1),
new Lit(2)
),
new Lit(3)
);
SetChildren(ReadOnlySpan<T>, T) replaces the immediate children of the topmost node.
Expr expected = new Add(
new Lit(4),
new Lit(5)
);
Assert.Equal(expected, rewriter.SetChildren(Children.Two(new Lit(4), new Lit(5)), expr));