Show / Hide Table of Contents

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)).

IRewritable<T>.

Namespace: Sawmill
Assembly: Sawmill.dll
Syntax
public interface IRewriter<T>
Type Parameters
Name Description
T

The type for which the rewriter can get the immediate children

Methods

| Improve this Doc View Source

GetChildren(T)

Get the immediate children of the value. GetChildren()

Declaration
Children<T> GetChildren(T value)
Parameters
Type Name Description
T value

The value

Returns
Type Description
Children<T>

The immediate children of 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(T) returns the immediate children of the topmost node.

Expr[] expected = new[]
    {
        new Add(
            new Lit(1),
            new Lit(2)
        ),
        new Lit(3)
    };
Assert.Equal(expected, rewriter.GetChildren(expr));
| Improve this Doc View Source

RewriteChildren(Func<T, T>, T)

Update the immediate children of the value by applying a transformation function to each one.

Implementations of IRewriter<T> can use DefaultRewriteChildren<T>(IRewriter<T>, Func<T, T>, T), or you can write your own.

NB: A hand-written implementation will not usually be faster than DefaultRewriteChildren<T>(IRewriter<T>, Func<T, T>, T). If your type has a fixed number of children, and that number is greater than two, you may see some performance improvements from implementing this method yourself. Be careful not to rebuild oldValue if none of the children have changed.

RewriteChildren(Func<T, T>)

Declaration
T RewriteChildren(Func<T, T> transformer, T oldValue)
Parameters
Type Name Description
System.Func<T, T> transformer

A transformation function to apply to each of oldValue's immediate children.

T oldValue

The old value, whose immediate children should be transformed by transformer.

Returns
Type Description
T

A copy of oldValue 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)
);

RewriteChildren(Func<T, T>, T) only affects the immediate children of the topmost node.

Expr expected = new Add(
    transformer(new Add(
        new Lit(1),
        new Lit(2)
    )),
    transformer(new Lit(3))
);
Assert.Equal(expected, rewriter.RewriteChildren(transformer, expr));
| Improve this Doc View Source

SetChildren(Children<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(T).

SetChildren(Children<T>)

Declaration
T SetChildren(Children<T> newChildren, T oldValue)
Parameters
Type Name Description
Children<T> newChildren

The new children

T oldValue

The old value, whose immediate children should be replaced

Returns
Type Description
T

A copy of oldValue 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(Children<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));

Extension Methods

Rewriter.ChildrenInContext<T>(IRewriter<T>, T)
Rewriter.Cursor<T>(IRewriter<T>, T)
Rewriter.DefaultRewriteChildren<T>(IRewriter<T>, Func<T, T>, T)
Rewriter.DescendantsAndSelf<T>(IRewriter<T>, T)
Rewriter.DescendantsAndSelfInContext<T>(IRewriter<T>, T)
Rewriter.Fold<T, U>(IRewriter<T>, Func<T, Children<U>, U>, T)
Rewriter.Rewrite<T>(IRewriter<T>, Func<T, T>, T)
Rewriter.SelfAndDescendants<T>(IRewriter<T>, T)
Rewriter.SelfAndDescendantsBreadthFirst<T>(IRewriter<T>, T)
Rewriter.SelfAndDescendantsInContext<T>(IRewriter<T>, T)
Rewriter.SelfAndDescendantsInContextBreadthFirst<T>(IRewriter<T>, T)
Rewriter.ZipFold<T, U>(IRewriter<T>, Func<T[], IEnumerable<U>, U>, T[])
  • Improve this Doc
  • View Source
Back to top Copyright © 2015-2017 Microsoft
Generated by DocFX