Sawmill

Interface IRewritable<T>

A object is rewriterable if it knows how to access its immediate children.

Implementations should ensure that you always get the children you just set (rewritable.SetChildren(children).GetChildren() == children), and that successive sets overwrite the earlier operation (rewritable.SetChildren(children1).SetChildren(children2) == rewritable.SetChildren(children2)).

IRewriter<T>.

Declaration
public interface IRewritable<T>
    where T : IRewritable<T>
Type Parameters
Name Description

T

The type of the object implementing the interface

Methods

CountChildren()

Count the immediate children of the value. CountChildren(T)

Declaration
int CountChildren()
Returns
Type Description

Int32

The current instance's number of immediate children

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() counts the immediate children of the topmost (Add) node.

Assert.Equal(2, expr.CountChildren());

GetChildren(Span<T>)

Copy the immediate children of the value into childrenReceiver. GetChildren(Span<T>, T)

Declaration
void GetChildren(Span<T> childrenReceiver)
Parameters
Type Name Description

Span<T>

childrenReceiver

A Span<T> to copy the current instance's immediate children into. The Span<T>'s Length will be equal to the number returned by CountChildren().

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>) 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[expr.CountChildren()];
expr.GetChildren(array);
Assert.Equal(expected, array);

SetChildren(ReadOnlySpan<T>)

Set the immediate children of the currentInstance.

Callers should ensure that newChildren contains the same number of children as was returned by GetChildren(Span<T>).

SetChildren(ReadOnlySpan<T>, T)

Declaration
T SetChildren(ReadOnlySpan<T> newChildren)
Parameters
Type Name Description

ReadOnlySpan<T>

newChildren

The new children

Returns
Type Description

T

A copy of the current instance with updated children.

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>) replaces the immediate children of the topmost node.

Expr expected = new Add(
    new Lit(4),
    new Lit(5)
);
Assert.Equal(expected, expr.SetChildren(Children.Two(new Lit(4), new Lit(5))));