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)
).
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 SourceGetChildren(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 
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 handwritten 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 value
if none of the children have changed.
Declaration
T RewriteChildren(Func<T, T> transformer, T value)
Parameters
Type  Name  Description 

Func<T, T>  transformer  A transformation function to apply to each of 
T  value  The old value, whose immediate children should be transformed by 
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)
);
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).
Declaration
T SetChildren(Children<T> newChildren, T value)
Parameters
Type  Name  Description 

Children<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(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));